@@ -4,7 +4,7 @@ use alloc::string::ToString;
4
4
use alloc:: sync:: Arc ;
5
5
use core:: str:: FromStr ;
6
6
7
- use bdk_chain:: { tx_graph , BlockId , ConfirmationBlockTime } ;
7
+ use bdk_chain:: { BlockId , ConfirmationBlockTime , TxUpdate } ;
8
8
use bitcoin:: {
9
9
absolute, hashes:: Hash , transaction, Address , Amount , BlockHash , FeeRate , Network , OutPoint ,
10
10
Transaction , TxIn , TxOut , Txid ,
@@ -312,43 +312,45 @@ pub fn insert_checkpoint(wallet: &mut Wallet, block: BlockId) {
312
312
. unwrap ( ) ;
313
313
}
314
314
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.
316
318
pub 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 ( ) ;
317
324
wallet
318
325
. apply_update ( Update {
319
- tx_update : bdk_chain:: TxUpdate {
320
- txs : vec ! [ Arc :: new( tx) ] ,
321
- ..Default :: default ( )
322
- } ,
326
+ tx_update,
323
327
..Default :: default ( )
324
328
} )
325
- . unwrap ( ) ;
329
+ . expect ( "failed to apply update" ) ;
326
330
}
327
331
328
332
/// Simulates confirming a tx with `txid` by applying an update to the wallet containing
329
333
/// the given `anchor`. Note: to be considered confirmed the anchor block must exist in
330
334
/// the current active chain.
331
335
pub fn insert_anchor ( wallet : & mut Wallet , txid : Txid , anchor : ConfirmationBlockTime ) {
336
+ let mut tx_update = TxUpdate :: default ( ) ;
337
+ tx_update. anchors = [ ( anchor, txid) ] . into ( ) ;
332
338
wallet
333
339
. apply_update ( Update {
334
- tx_update : tx_graph:: TxUpdate {
335
- anchors : [ ( anchor, txid) ] . into ( ) ,
336
- ..Default :: default ( )
337
- } ,
340
+ tx_update,
338
341
..Default :: default ( )
339
342
} )
340
- . unwrap ( ) ;
343
+ . expect ( "failed to apply update" ) ;
341
344
}
342
345
343
346
/// Marks the given `txid` seen as unconfirmed at `seen_at`
344
347
pub 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 ( ) ;
345
350
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,
351
353
..Default :: default ( )
352
354
} )
353
- . unwrap ( ) ;
355
+ . expect ( "failed to apply update" ) ;
354
356
}
0 commit comments