1- use std:: { str:: FromStr , thread :: sleep , time:: Duration } ;
1+ use std:: { str:: FromStr , time:: Duration } ;
22
33use anyhow:: Result ;
44use bdk:: {
@@ -11,22 +11,26 @@ use bdk::{
1111 template:: P2Wpkh ,
1212 SyncOptions , Wallet ,
1313} ;
14+ use romeo:: { config:: Config , stacks_client:: StacksClient } ;
1415use sbtc_cli:: commands:: {
1516 broadcast:: { broadcast_tx, BroadcastArgs } ,
1617 deposit:: { build_deposit_tx, DepositArgs } ,
1718} ;
19+ use stacks_core:: address:: StacksAddress ;
20+ use tokio:: time:: sleep;
1821
1922use super :: {
2023 bitcoin_client:: {
21- bitcoin_url, client_new, electrs_url, mine_blocks,
24+ bitcoin_url, client_new, electrs_url, mine_blocks, sbtc_balance ,
2225 wait_for_tx_confirmation,
2326 } ,
2427 KeyType :: * ,
2528 WALLETS ,
2629} ;
2730
28- #[ test]
29- fn broadcast_deposit ( ) -> Result < ( ) > {
31+ #[ tokio:: test]
32+ /// preceeds withdrawal
33+ async fn broadcast_deposit ( ) -> Result < ( ) > {
3034 let b_client = client_new ( bitcoin_url ( ) . as_str ( ) , "devnet" , "devnet" ) ;
3135
3236 b_client
@@ -46,8 +50,6 @@ fn broadcast_deposit() -> Result<()> {
4650
4751 let electrum_url = electrs_url ( ) ;
4852
49- // suboptimal, replace once we have better events.
50- // replace with b_client balance?
5153 {
5254 let blockchain =
5355 ElectrumBlockchain :: from_config ( & ElectrumBlockchainConfig {
@@ -76,34 +78,74 @@ fn broadcast_deposit() -> Result<()> {
7678 if balance. confirmed != 0 {
7779 break ;
7880 }
79- sleep ( Duration :: from_millis ( 1_000 ) ) ;
81+ sleep ( Duration :: from_millis ( 1_000 ) ) . await ;
8082 }
8183 }
8284
8385 let amount = 10_000 ;
86+ let deployer_stacks_address = WALLETS [ 0 ] [ Stacks ] . address ;
87+ let recipient_stacks_address = WALLETS [ 1 ] [ Stacks ] . address ;
8488
8589 let tx = {
8690 let args = DepositArgs {
8791 node_url : electrum_url. clone ( ) ,
8892 wif : WALLETS [ 1 ] [ P2wpkh ] . wif . into ( ) ,
8993 network : bdk:: bitcoin:: Network :: Regtest ,
90- recipient : WALLETS [ 1 ] [ Stacks ] . address . into ( ) ,
94+ recipient : recipient_stacks_address . into ( ) ,
9195 amount,
9296 sbtc_wallet : WALLETS [ 0 ] [ P2tr ] . address . into ( ) ,
9397 } ;
9498
9599 build_deposit_tx ( & args) . unwrap ( )
96100 } ;
97101
98- broadcast_tx ( & BroadcastArgs {
99- node_url : electrum_url,
100- tx : hex:: encode ( tx. serialize ( ) ) ,
101- } )
102- . unwrap ( ) ;
102+ let config = Config :: from_path ( "config.json" ) . unwrap ( ) ;
103103
104- let txid = tx. txid ( ) ;
104+ // make sure config urls match devenv.
105+ let stacks_client =
106+ StacksClient :: new ( config. clone ( ) , reqwest:: Client :: new ( ) ) ;
105107
106- wait_for_tx_confirmation ( & b_client, & txid, 1 ) ;
108+ let deployer_address =
109+ StacksAddress :: transmute_stacks_address ( deployer_stacks_address) ;
110+ let recipient_address =
111+ StacksAddress :: transmute_stacks_address ( recipient_stacks_address) ;
112+
113+ // prior balance
114+ assert_eq ! (
115+ sbtc_balance(
116+ & stacks_client,
117+ deployer_address,
118+ recipient_address,
119+ config. contract_name. clone( )
120+ )
121+ . await ,
122+ 0
123+ ) ;
124+
125+ // Sign, send and wait for confirmation.
126+ {
127+ broadcast_tx ( & BroadcastArgs {
128+ node_url : electrum_url,
129+ tx : hex:: encode ( tx. serialize ( ) ) ,
130+ } )
131+ . unwrap ( ) ;
132+
133+ let txid = tx. txid ( ) ;
134+
135+ wait_for_tx_confirmation ( & b_client, & txid, 1 ) . await ;
136+ }
137+
138+ // assert on new sbtc token balance
139+ while sbtc_balance (
140+ & stacks_client,
141+ deployer_address,
142+ recipient_address,
143+ config. contract_name . clone ( ) ,
144+ )
145+ . await != amount as u128
146+ {
147+ sleep ( Duration :: from_secs ( 2 ) ) . await
148+ }
107149
108150 Ok ( ( ) )
109151}
0 commit comments