Skip to content

Commit

Permalink
feat: add get_tx_status to esplora
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Feb 7, 2025
1 parent 2ce3b82 commit 6fd9162
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,10 @@ interface EsploraClient {
/// Get the [`BlockHash`] of a specific block height
[Throws=EsploraError]
string get_block_hash(u32 block_height);

/// Get the status of a [`Transaction`] given its [`Txid`].
[Throws=EsploraError]
TxStatus get_tx_status(string txid);
};

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -1226,6 +1230,13 @@ dictionary KeychainAndIndex {
u32 index;
};

dictionary TxStatus {
boolean confirmed;
u32? block_height;
string? block_hash;
u64? block_time;
};

// ------------------------------------------------------------------------
// bdk_wallet crate - bitcoin re-exports
// ------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions bdk-ffi/src/esplora.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::bitcoin::Transaction;
use crate::error::EsploraError;
use crate::types::TxStatus;
use crate::types::Update;
use crate::types::{FullScanRequest, SyncRequest};

Expand Down Expand Up @@ -98,10 +99,18 @@ impl EsploraClient {
self.0.get_fee_estimates().map_err(EsploraError::from)
}


pub fn get_block_hash(&self, block_height: u32) -> Result<String, EsploraError> {
self.0
.get_block_hash(block_height)
.map(|hash| hash.to_string())
.map_err(EsploraError::from)
}
pub fn get_tx_status(&self, txid: String) -> Result<TxStatus, EsploraError> {
let txid = Txid::from_str(&txid)?;
self.0
.get_tx_status(&txid)
.map(TxStatus::from)
.map_err(EsploraError::from)
}
}
1 change: 1 addition & 0 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ use crate::types::SignOptions;
use crate::types::SyncRequest;
use crate::types::SyncRequestBuilder;
use crate::types::SyncScriptInspector;
use crate::types::TxStatus;
use crate::types::Update;
use crate::wallet::Wallet;

Expand Down
20 changes: 20 additions & 0 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use std::collections::HashMap;
use std::convert::TryFrom;
use std::sync::{Arc, Mutex};

use bdk_esplora::esplora_client::api::TxStatus as BdkTxStatus;

#[derive(Debug)]
pub enum ChainPosition {
Confirmed {
Expand Down Expand Up @@ -546,3 +548,21 @@ impl From<SignOptions> for BdkSignOptions {
}
}
}

pub struct TxStatus {
pub confirmed: bool,
pub block_height: Option<u32>,
pub block_hash: Option<String>,
pub block_time: Option<u64>,
}

impl From<BdkTxStatus> for TxStatus {
fn from(status: BdkTxStatus) -> Self {
TxStatus {
confirmed: status.confirmed,
block_height: status.block_height,
block_hash: status.block_hash.map(|h| h.to_string()),
block_time: status.block_time,
}
}
}

0 comments on commit 6fd9162

Please sign in to comment.