@@ -7,35 +7,36 @@ use bdk_wallet::{
77 KeychainKind , SignOptions , Wallet ,
88} ;
99use std:: { collections:: BTreeSet , io:: Write } ;
10+ use tokio:: time:: { sleep, Duration } ;
1011
1112const SEND_AMOUNT : Amount = Amount :: from_sat ( 5000 ) ;
1213const STOP_GAP : usize = 5 ;
1314const PARALLEL_REQUESTS : usize = 5 ;
1415
1516const DB_PATH : & str = "bdk-example-esplora-async.sqlite" ;
16- const NETWORK : Network = Network :: Signet ;
17+ const NETWORK : Network = Network :: Testnet4 ;
1718const EXTERNAL_DESC : & str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)" ;
1819const INTERNAL_DESC : & str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)" ;
19- const ESPLORA_URL : & str = "http ://signet.bitcoindevkit.net " ;
20+ const ESPLORA_URL : & str = "https ://mempool.space/testnet4/api " ;
2021
2122#[ tokio:: main]
2223async fn main ( ) -> Result < ( ) , anyhow:: Error > {
23- let mut conn = Connection :: open ( DB_PATH ) ?;
24+ let mut db = Connection :: open ( DB_PATH ) ?;
2425 let wallet_opt = Wallet :: load ( )
2526 . descriptor ( KeychainKind :: External , Some ( EXTERNAL_DESC ) )
2627 . descriptor ( KeychainKind :: Internal , Some ( INTERNAL_DESC ) )
2728 . extract_keys ( )
2829 . check_network ( NETWORK )
29- . load_wallet ( & mut conn ) ?;
30+ . load_wallet ( & mut db ) ?;
3031 let mut wallet = match wallet_opt {
3132 Some ( wallet) => wallet,
3233 None => Wallet :: create ( EXTERNAL_DESC , INTERNAL_DESC )
3334 . network ( NETWORK )
34- . create_wallet ( & mut conn ) ?,
35+ . create_wallet ( & mut db ) ?,
3536 } ;
3637
3738 let address = wallet. next_unused_address ( KeychainKind :: External ) ;
38- wallet. persist ( & mut conn ) ?;
39+ wallet. persist ( & mut db ) ?;
3940 println ! ( "Next unused address: ({}) {address}" , address. index) ;
4041
4142 let balance = wallet. balance ( ) ;
@@ -63,7 +64,7 @@ async fn main() -> Result<(), anyhow::Error> {
6364 . await ?;
6465
6566 wallet. apply_update ( update) ?;
66- wallet. persist ( & mut conn ) ?;
67+ wallet. persist ( & mut db ) ?;
6768 println ! ( ) ;
6869
6970 let balance = wallet. balance ( ) ;
@@ -78,8 +79,11 @@ async fn main() -> Result<(), anyhow::Error> {
7879 println ! ( "Please send at least {SEND_AMOUNT} to the receiving address" ) ;
7980 std:: process:: exit ( 0 ) ;
8081 }
82+
83+ let target_fee_rate = FeeRate :: from_sat_per_vb ( 1 ) . unwrap ( ) ;
8184 let mut tx_builder = wallet. build_tx ( ) ;
8285 tx_builder. add_recipient ( address. script_pubkey ( ) , SEND_AMOUNT ) ;
86+ tx_builder. fee_rate ( target_fee_rate) ;
8387
8488 let mut psbt = tx_builder. finish ( ) ?;
8589 let finalized = wallet. sign ( & mut psbt, SignOptions :: default ( ) ) ?;
@@ -89,7 +93,7 @@ async fn main() -> Result<(), anyhow::Error> {
8993 let tx = psbt. extract_tx ( ) ?;
9094 client. broadcast ( & tx) . await ?;
9195 let txid = tx. compute_txid ( ) ;
92- println ! ( "Tx broadcasted! Txid: {txid}" ) ;
96+ println ! ( "Tx broadcasted! Txid: https://mempool.space/testnet4/tx/ {txid}" ) ;
9397
9498 println ! ( "Partial Sync..." ) ;
9599 print ! ( "SCANNING: " ) ;
@@ -109,9 +113,10 @@ async fn main() -> Result<(), anyhow::Error> {
109113 let sync_update = client. sync ( sync_request, PARALLEL_REQUESTS ) . await ?;
110114 println ! ( ) ;
111115 wallet. apply_update ( sync_update) ?;
112- wallet. persist ( & mut conn ) ?;
116+ wallet. persist ( & mut db ) ?;
113117
114- let feerate = FeeRate :: from_sat_per_kwu ( tx_feerate. to_sat_per_kwu ( ) + 250 ) ;
118+ // bump fee rate for tx by at least 1 sat per vbyte
119+ let feerate = FeeRate :: from_sat_per_vb ( tx_feerate. to_sat_per_vb_ceil ( ) + 1 ) . unwrap ( ) ;
115120 let mut builder = wallet. build_fee_bump ( txid) . expect ( "failed to bump tx" ) ;
116121 builder. fee_rate ( feerate) ;
117122 let mut bumped_psbt = builder. finish ( ) . unwrap ( ) ;
@@ -133,8 +138,14 @@ async fn main() -> Result<(), anyhow::Error> {
133138 new_fee > original_fee,
134139 "New fee ({new_fee}) should be higher than original ({original_fee})" ,
135140 ) ;
141+
142+ // wait for first transaction to make it into the mempool and be indexed on mempool.space
143+ sleep ( Duration :: from_secs ( 10 ) ) . await ;
136144 client. broadcast ( & bumped_tx) . await ?;
137- println ! ( "Broadcasted bumped tx. Txid: {}" , bumped_tx. compute_txid( ) ) ;
145+ println ! (
146+ "Broadcasted bumped tx. Txid: https://mempool.space/testnet4/tx/{}" ,
147+ bumped_tx. compute_txid( )
148+ ) ;
138149
139150 println ! ( "syncing after broadcasting bumped tx..." ) ;
140151 print ! ( "SCANNING: " ) ;
@@ -155,26 +166,17 @@ async fn main() -> Result<(), anyhow::Error> {
155166
156167 let mut evicted_txs = Vec :: new ( ) ;
157168
158- let last_seen = wallet
159- . tx_graph ( )
160- . full_txs ( )
161- . find ( |full_tx| full_tx. txid == txid)
162- . map_or ( 0 , |full_tx| full_tx. last_seen . unwrap_or ( 0 ) ) ;
163- if !evicted_txs
164- . iter ( )
165- . any ( |( evicted_txid, _) | evicted_txid == & txid)
166- {
167- evicted_txs. push ( ( txid, last_seen) ) ;
169+ for ( txid, last_seen) in & sync_update. tx_update . evicted_ats {
170+ evicted_txs. push ( ( * txid, * last_seen) ) ;
168171 }
169172
173+ wallet. apply_update ( sync_update) ?;
174+
170175 if !evicted_txs. is_empty ( ) {
171- let evicted_count = evicted_txs. len ( ) ;
172- wallet. apply_evicted_txs ( evicted_txs) ;
173- println ! ( "Applied {evicted_count} evicted transactions" ) ;
176+ println ! ( "Applied {} evicted transactions" , evicted_txs. len( ) ) ;
174177 }
175178
176- wallet. apply_update ( sync_update) ?;
177- wallet. persist ( & mut conn) ?;
179+ wallet. persist ( & mut db) ?;
178180
179181 let balance_after_sync = wallet. balance ( ) ;
180182 println ! ( "Wallet balance after sync: {}" , balance_after_sync. total( ) ) ;
0 commit comments