Skip to content

Commit

Permalink
feat: add get_tx_info to esplora
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Feb 7, 2025
1 parent 6fd9162 commit 623525a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
14 changes: 14 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,10 @@ interface EsploraClient {
/// Get the status of a [`Transaction`] given its [`Txid`].
[Throws=EsploraError]
TxStatus get_tx_status(string txid);

/// Get transaction info given it's [`Txid`].
[Throws=EsploraError]
Tx? get_tx_info(string txid);
};

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -1237,6 +1241,16 @@ dictionary TxStatus {
u64? block_time;
};

dictionary Tx {
string txid;
i32 version;
u32 locktime;
u64 size;
u64 weight;
u64 fee;
TxStatus status;
};

// ------------------------------------------------------------------------
// bdk_wallet crate - bitcoin re-exports
// ------------------------------------------------------------------------
Expand Down
10 changes: 9 additions & 1 deletion 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::Tx;
use crate::types::TxStatus;
use crate::types::Update;
use crate::types::{FullScanRequest, SyncRequest};
Expand Down Expand Up @@ -99,7 +100,6 @@ 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)
Expand All @@ -113,4 +113,12 @@ impl EsploraClient {
.map(TxStatus::from)
.map_err(EsploraError::from)
}

pub fn get_tx_info(&self, txid: String) -> Result<Option<Tx>, EsploraError> {
let txid = Txid::from_str(&txid)?;
self.0
.get_tx_info(&txid)
.map(|tx| tx.map(Tx::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::Tx;
use crate::types::TxStatus;
use crate::types::Update;
use crate::wallet::Wallet;
Expand Down
27 changes: 27 additions & 0 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use std::collections::HashMap;
use std::convert::TryFrom;
use std::sync::{Arc, Mutex};

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

#[derive(Debug)]
Expand Down Expand Up @@ -549,6 +550,7 @@ impl From<SignOptions> for BdkSignOptions {
}
}

#[derive(Debug)]
pub struct TxStatus {
pub confirmed: bool,
pub block_height: Option<u32>,
Expand All @@ -566,3 +568,28 @@ impl From<BdkTxStatus> for TxStatus {
}
}
}

#[derive(Debug)]
pub struct Tx {
pub txid: String,
pub version: i32,
pub locktime: u32,
pub size: u64,
pub weight: u64,
pub fee: u64,
pub status: TxStatus,
}

impl From<BdkTx> for Tx {
fn from(tx: BdkTx) -> Self {
Self {
txid: tx.txid.to_string(),
version: tx.version,
locktime: tx.locktime,
size: tx.size as u64,
weight: tx.weight,
fee: tx.fee,
status: tx.status.into(),
}
}
}

0 comments on commit 623525a

Please sign in to comment.