@@ -76,6 +76,9 @@ use crate::wallet::{
76
76
} ;
77
77
78
78
// re-exports
79
+ #[ cfg( feature = "std" ) ]
80
+ use crate :: wallet:: utils:: TxDetails ;
81
+
79
82
pub use bdk_chain:: Balance ;
80
83
pub use changeset:: ChangeSet ;
81
84
pub use params:: * ;
@@ -821,6 +824,45 @@ impl Wallet {
821
824
. map ( |( ( k, i) , full_txo) | new_local_utxo ( k, i, full_txo) )
822
825
}
823
826
827
+ /// Build a [`TxDetails`] struct for a given transaction.
828
+ ///
829
+ /// If the transaction with txid [`Txid`] cannot be found in the wallet's transaction graph,
830
+ /// None is returned.
831
+ // #[cfg(feature = "std")]
832
+ pub fn get_tx_details ( & self , txid : Txid ) -> Option < TxDetails > {
833
+ let tx_graph = self . indexed_graph . graph ( ) ;
834
+ let tx_index = & self . indexed_graph . index ;
835
+
836
+ // It should be impossible to have more than 1 right? In which case I can use the `find()`
837
+ // method on the collection instead of collecting into a vector.
838
+
839
+ // This relies on the impossibility of having 2 transactions with the same Txid in the list
840
+ // canonical transactions but let me know if this is wrong!
841
+ let optional_tx: Option < CanonicalTx < Arc < Transaction > , ConfirmationBlockTime > > = tx_graph
842
+ . list_canonical_txs ( & self . chain , self . chain . tip ( ) . block_id ( ) )
843
+ . filter ( |c_tx| tx_index. is_tx_relevant ( & c_tx. tx_node . tx ) )
844
+ . find ( |tx| tx. tx_node . compute_txid ( ) == txid) ;
845
+
846
+ let tx = optional_tx?;
847
+
848
+ let ( sent, received) = self . sent_and_received ( & tx. tx_node . tx ) ;
849
+ let fee = self . calculate_fee ( & tx. tx_node . tx ) . unwrap ( ) ;
850
+ let fee_rate = self . calculate_fee_rate ( & tx. tx_node . tx ) . unwrap ( ) ;
851
+ let chain_position = tx. chain_position ;
852
+
853
+ let tx_details: TxDetails = TxDetails {
854
+ txid,
855
+ received,
856
+ sent,
857
+ fee,
858
+ fee_rate,
859
+ chain_position,
860
+ tx : tx. tx_node . tx ,
861
+ } ;
862
+
863
+ Some ( tx_details)
864
+ }
865
+
824
866
/// List all relevant outputs (includes both spent and unspent, confirmed and unconfirmed).
825
867
///
826
868
/// To list only unspent outputs (UTXOs), use [`Wallet::list_unspent`] instead.
0 commit comments