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,25 @@ 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 } ,
24- KeyType :: * ,
27+ KeyType :: { self , * } ,
2528 WALLETS ,
2629} ;
2730
28- #[ test]
29- fn broadcast_deposit ( ) -> Result < ( ) > {
31+ #[ tokio :: test]
32+ async fn broadcast_deposit ( ) -> Result < ( ) > {
3033 let b_client = client_new ( bitcoin_url ( ) . as_str ( ) , "devnet" , "devnet" ) ;
3134
3235 b_client
@@ -46,8 +49,6 @@ fn broadcast_deposit() -> Result<()> {
4649
4750 let electrum_url = electrs_url ( ) ;
4851
49- // suboptimal, replace once we have better events.
50- // replace with b_client balance?
5152 {
5253 let blockchain =
5354 ElectrumBlockchain :: from_config ( & ElectrumBlockchainConfig {
@@ -76,34 +77,74 @@ fn broadcast_deposit() -> Result<()> {
7677 if balance. confirmed != 0 {
7778 break ;
7879 }
79- sleep ( Duration :: from_millis ( 1_000 ) ) ;
80+ sleep ( Duration :: from_millis ( 1_000 ) ) . await ;
8081 }
8182 }
8283
8384 let amount = 10_000 ;
85+ let deployer_stacks_address = WALLETS [ 0 ] [ KeyType :: Stacks ] . address ;
86+ let recipient_stacks_address = WALLETS [ 1 ] [ KeyType :: Stacks ] . address ;
8487
8588 let tx = {
8689 let args = DepositArgs {
8790 node_url : electrum_url. clone ( ) ,
8891 wif : WALLETS [ 1 ] [ P2wpkh ] . wif . into ( ) ,
8992 network : bdk:: bitcoin:: Network :: Regtest ,
90- recipient : WALLETS [ 1 ] [ Stacks ] . address . into ( ) ,
93+ recipient : recipient_stacks_address . into ( ) ,
9194 amount,
9295 sbtc_wallet : WALLETS [ 0 ] [ P2tr ] . address . into ( ) ,
9396 } ;
9497
9598 build_deposit_tx ( & args) . unwrap ( )
9699 } ;
97100
98- broadcast_tx ( & BroadcastArgs {
99- node_url : electrum_url,
100- tx : hex:: encode ( tx. serialize ( ) ) ,
101- } )
102- . unwrap ( ) ;
101+ let config = Config :: from_path ( "config.json" ) . unwrap ( ) ;
103102
104- let txid = tx. txid ( ) ;
103+ // make sure config urls match devenv.
104+ let stacks_client =
105+ StacksClient :: new ( config. clone ( ) , reqwest:: Client :: new ( ) ) ;
105106
106- wait_for_tx_confirmation ( & b_client, & txid, 1 ) ;
107+ let deployer_address =
108+ StacksAddress :: transmute_stacks_address ( deployer_stacks_address) ;
109+ let recipient_address =
110+ StacksAddress :: transmute_stacks_address ( recipient_stacks_address) ;
111+
112+ // prior balance
113+ assert_eq ! (
114+ sbtc_balance(
115+ & stacks_client,
116+ deployer_address,
117+ recipient_address,
118+ config. contract_name. clone( )
119+ )
120+ . await ,
121+ 0
122+ ) ;
123+
124+ // Sign, send and wait for confirmation.
125+ {
126+ broadcast_tx ( & BroadcastArgs {
127+ node_url : electrum_url,
128+ tx : hex:: encode ( tx. serialize ( ) ) ,
129+ } )
130+ . unwrap ( ) ;
131+
132+ let txid = tx. txid ( ) ;
133+
134+ wait_for_tx_confirmation ( & b_client, & txid, 1 ) ;
135+ }
136+
137+ // assert on new sbtc token balance
138+ while sbtc_balance (
139+ & stacks_client,
140+ deployer_address,
141+ recipient_address,
142+ config. contract_name . clone ( ) ,
143+ )
144+ . await != amount as u128
145+ {
146+ sleep ( Duration :: from_secs ( 2 ) ) . await
147+ }
107148
108149 Ok ( ( ) )
109150}
0 commit comments