This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
generated from stacks-network/.github
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
130 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
pub mod bitcoin_client; | ||
pub mod deposit; | ||
pub mod stacks_client; | ||
pub mod withdrawal; | ||
|
||
use std::ops::Index; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use std::{thread::sleep, time::Duration}; | ||
|
||
use bdk::{ | ||
bitcoin::{psbt::serialize::Serialize, PrivateKey}, | ||
blockchain::{ | ||
ConfigurableBlockchain, ElectrumBlockchain, ElectrumBlockchainConfig, | ||
}, | ||
database::MemoryDatabase, | ||
template::P2Wpkh, | ||
SyncOptions, Wallet, | ||
}; | ||
use reqwest::blocking::Client; | ||
use sbtc_cli::commands::{ | ||
broadcast::{broadcast_tx, BroadcastArgs}, | ||
withdraw::{build_withdrawal_tx, WithdrawalArgs}, | ||
}; | ||
|
||
use super::{ | ||
bitcoin_client::{electrs_url, generate_blocks}, | ||
KeyType::*, | ||
WALLETS, | ||
}; | ||
|
||
#[test] | ||
fn broadcast_withdrawal() { | ||
let client = Client::new(); | ||
{ | ||
generate_blocks(1, &client, WALLETS[0][P2wpkh].address); | ||
generate_blocks(1, &client, WALLETS[1][P2wpkh].address); | ||
// pads blocks to get rewards. | ||
generate_blocks(100, &client, WALLETS[0][P2wpkh].address); | ||
}; | ||
|
||
let electrum_url = electrs_url(); | ||
|
||
// suboptimal, replace once we have better events. | ||
{ | ||
let blockchain = | ||
ElectrumBlockchain::from_config(&ElectrumBlockchainConfig { | ||
url: electrum_url.clone().into(), | ||
socks5: None, | ||
retry: 3, | ||
timeout: Some(10), | ||
stop_gap: 10, | ||
validate_domain: false, | ||
}) | ||
.unwrap(); | ||
|
||
let private_key = PrivateKey::from_wif(WALLETS[0][P2wpkh].wif).unwrap(); | ||
|
||
let wallet = Wallet::new( | ||
P2Wpkh(private_key), | ||
Some(P2Wpkh(private_key)), | ||
bdk::bitcoin::Network::Regtest, | ||
MemoryDatabase::default(), | ||
) | ||
.unwrap(); | ||
|
||
loop { | ||
wallet.sync(&blockchain, SyncOptions::default()).unwrap(); | ||
let balance = wallet.get_balance().unwrap(); | ||
if balance.confirmed != 0 { | ||
break; | ||
} | ||
sleep(Duration::from_millis(1_000)); | ||
} | ||
} | ||
|
||
// amount random [1000,2000) | ||
// fee random [1000,2000) | ||
let args = WithdrawalArgs { | ||
node_url: electrs_url(), | ||
network: bdk::bitcoin::Network::Regtest, | ||
wif: WALLETS[1][P2wpkh].wif.into(), | ||
drawee_wif: WALLETS[1][Stacks].wif.into(), | ||
payee_address: WALLETS[1][P2wpkh].address.into(), | ||
amount: 2000, | ||
fulfillment_fee: 2000, | ||
sbtc_wallet: WALLETS[0][P2tr].address.into(), | ||
}; | ||
|
||
let tx = build_withdrawal_tx(&args).unwrap(); | ||
|
||
broadcast_tx(&BroadcastArgs { | ||
node_url: electrum_url, | ||
tx: hex::encode(tx.serialize()), | ||
}) | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters