@@ -4,7 +4,7 @@ use alloc::string::ToString;
44use alloc:: sync:: Arc ;
55use core:: str:: FromStr ;
66
7- use bdk_chain:: { tx_graph , BlockId , ConfirmationBlockTime } ;
7+ use bdk_chain:: { BlockId , ConfirmationBlockTime , TxUpdate } ;
88use bitcoin:: {
99 absolute, hashes:: Hash , transaction, Address , Amount , BlockHash , FeeRate , Network , OutPoint ,
1010 Transaction , TxIn , TxOut , Txid ,
@@ -312,43 +312,45 @@ pub fn insert_checkpoint(wallet: &mut Wallet, block: BlockId) {
312312 . unwrap ( ) ;
313313}
314314
315- /// Insert transaction
315+ /// Inserts a transaction into the local view, assuming it is currently present in the mempool.
316+ ///
317+ /// This can be used, for example, to track a transaction immediately after it is broadcast.
316318pub fn insert_tx ( wallet : & mut Wallet , tx : Transaction ) {
319+ let txid = tx. compute_txid ( ) ;
320+ let seen_at = std:: time:: UNIX_EPOCH . elapsed ( ) . unwrap ( ) . as_secs ( ) ;
321+ let mut tx_update = TxUpdate :: default ( ) ;
322+ tx_update. txs = vec ! [ Arc :: new( tx) ] ;
323+ tx_update. seen_ats = [ ( txid, seen_at) ] . into ( ) ;
317324 wallet
318325 . apply_update ( Update {
319- tx_update : bdk_chain:: TxUpdate {
320- txs : vec ! [ Arc :: new( tx) ] ,
321- ..Default :: default ( )
322- } ,
326+ tx_update,
323327 ..Default :: default ( )
324328 } )
325- . unwrap ( ) ;
329+ . expect ( "failed to apply update" ) ;
326330}
327331
328332/// Simulates confirming a tx with `txid` by applying an update to the wallet containing
329333/// the given `anchor`. Note: to be considered confirmed the anchor block must exist in
330334/// the current active chain.
331335pub fn insert_anchor ( wallet : & mut Wallet , txid : Txid , anchor : ConfirmationBlockTime ) {
336+ let mut tx_update = TxUpdate :: default ( ) ;
337+ tx_update. anchors = [ ( anchor, txid) ] . into ( ) ;
332338 wallet
333339 . apply_update ( Update {
334- tx_update : tx_graph:: TxUpdate {
335- anchors : [ ( anchor, txid) ] . into ( ) ,
336- ..Default :: default ( )
337- } ,
340+ tx_update,
338341 ..Default :: default ( )
339342 } )
340- . unwrap ( ) ;
343+ . expect ( "failed to apply update" ) ;
341344}
342345
343346/// Marks the given `txid` seen as unconfirmed at `seen_at`
344347pub fn insert_seen_at ( wallet : & mut Wallet , txid : Txid , seen_at : u64 ) {
348+ let mut tx_update = TxUpdate :: default ( ) ;
349+ tx_update. seen_ats = [ ( txid, seen_at) ] . into ( ) ;
345350 wallet
346- . apply_update ( crate :: Update {
347- tx_update : tx_graph:: TxUpdate {
348- seen_ats : [ ( txid, seen_at) ] . into_iter ( ) . collect ( ) ,
349- ..Default :: default ( )
350- } ,
351+ . apply_update ( Update {
352+ tx_update,
351353 ..Default :: default ( )
352354 } )
353- . unwrap ( ) ;
355+ . expect ( "failed to apply update" ) ;
354356}
0 commit comments