Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit 3e75d96

Browse files
committed
init deposit test
1 parent eea7543 commit 3e75d96

6 files changed

Lines changed: 140 additions & 29 deletions

File tree

devenv/integration/bin/test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ project_name() {
99
echo "$filter"
1010
}
1111

12-
filters=("package(romeo)")
12+
filters=("test(deposit)")
1313
filter_union=""
1414
ids=()
1515
projects=()

romeo/tests/bitcoin_client_integration.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ pub fn electrs_url() -> Url {
1818
Url::parse(&format!("tcp://{base}-electrs-1:60401")).unwrap()
1919
}
2020

21-
pub fn generate_blocks(blocks: u64, ctx: &Client, address: &str) {
21+
pub fn generate_blocks(
22+
blocks: u64,
23+
ctx: &Client,
24+
address: &str,
25+
) -> Vec<String> {
2226
let endpoint = bitcoin_url();
2327
let user = "devnet";
2428
let password = "devnet";
@@ -41,6 +45,7 @@ pub fn generate_blocks(blocks: u64, ctx: &Client, address: &str) {
4145
.unwrap();
4246

4347
assert_eq!(response_json["error"], serde_json::Value::Null);
48+
serde_json::from_value(response_json["result"].clone()).expect("block_ids")
4449
}
4550

4651
#[test]

romeo/tests/deposit_integration.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use std::{thread::sleep, time::Duration};
2+
3+
use anyhow::Result;
4+
use bdk::bitcoin::psbt::serialize::Serialize;
5+
use reqwest::blocking::Client;
6+
use sbtc_cli::commands::{
7+
broadcast::{broadcast_tx, BroadcastArgs},
8+
deposit::{build_deposit_tx, DepositArgs},
9+
};
10+
11+
mod bitcoin_client_integration;
12+
13+
use bitcoin_client_integration::{electrs_url, generate_blocks};
14+
15+
const WALLET_0_P2TR_ADDRESS: &str =
16+
"bcrt1pte5zmd7qzj4hdu45lh9mmdm0nwq3z35pwnxmzkwld6y0a8g83nnqhj6vc0";
17+
const WALLET_0_P2WPKH_ADDRESS: &str =
18+
"bcrt1q3tj2fr9scwmcw3rq5m6jslva65f2rqjxfrjz47";
19+
const WALLET_0_P2WPKH_WIF: &str =
20+
"cTyHitzs3VRnNxrpwxo3fXTTe569wHNUs57tQM7Z1FrzUDNB5mqm";
21+
const WALLET_1_P2WPKH_WIF: &str =
22+
"cNcXK2r8bNdWJQymtAW8tGS7QHNtFFvG5CdXqhhT752u29WspXRM";
23+
const WALLET_1_STX_ADDRESS: &str = "ST2ST2H80NP5C9SPR4ENJ1Z9CDM9PKAJVPYWPQZ50";
24+
const WALLET_1_P2WPKH_ADDRESS: &str =
25+
"bcrt1q3zl64vadtuh3vnsuhdgv6pm93n82ye8q6cr4ch";
26+
27+
use bdk::{
28+
bitcoin::PrivateKey,
29+
blockchain::{
30+
ConfigurableBlockchain, ElectrumBlockchain, ElectrumBlockchainConfig,
31+
},
32+
database::MemoryDatabase,
33+
template::P2Wpkh,
34+
SyncOptions, Wallet,
35+
};
36+
37+
#[test]
38+
fn broadcast_deposit() -> Result<()> {
39+
let client = Client::new();
40+
{
41+
generate_blocks(1, &client, WALLET_0_P2WPKH_ADDRESS);
42+
generate_blocks(1, &client, WALLET_1_P2WPKH_ADDRESS);
43+
// pads blocks to get rewards.
44+
generate_blocks(200, &client, WALLET_0_P2WPKH_ADDRESS);
45+
};
46+
47+
let electrum_url = electrs_url();
48+
49+
// suboptimal, replace once we have better events.
50+
{
51+
let blockchain =
52+
ElectrumBlockchain::from_config(&ElectrumBlockchainConfig {
53+
url: electrum_url.clone().into(),
54+
socks5: None,
55+
retry: 3,
56+
timeout: Some(10),
57+
stop_gap: 10,
58+
validate_domain: false,
59+
})
60+
.unwrap();
61+
62+
let private_key = PrivateKey::from_wif(WALLET_0_P2WPKH_WIF)?;
63+
64+
let wallet = Wallet::new(
65+
P2Wpkh(private_key),
66+
Some(P2Wpkh(private_key)),
67+
bdk::bitcoin::Network::Regtest,
68+
MemoryDatabase::default(),
69+
)
70+
.unwrap();
71+
72+
loop {
73+
wallet.sync(&blockchain, SyncOptions::default()).unwrap();
74+
let balance = wallet.get_balance().unwrap();
75+
if balance.confirmed != 0 {
76+
break;
77+
}
78+
sleep(Duration::from_millis(1_000));
79+
}
80+
}
81+
82+
let amount = 10_000;
83+
84+
let tx = {
85+
let args = DepositArgs {
86+
node_url: electrum_url.clone(),
87+
wif: WALLET_1_P2WPKH_WIF.into(),
88+
network: bdk::bitcoin::Network::Regtest,
89+
recipient: WALLET_1_STX_ADDRESS.into(),
90+
amount,
91+
sbtc_wallet: WALLET_0_P2TR_ADDRESS.into(),
92+
};
93+
94+
build_deposit_tx(&args).unwrap()
95+
};
96+
97+
broadcast_tx(&BroadcastArgs {
98+
node_url: electrum_url,
99+
tx: hex::encode(tx.serialize()),
100+
})
101+
.unwrap();
102+
103+
Ok(())
104+
}

sbtc-cli/src/commands/broadcast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use url::Url;
1010
#[derive(Parser, Debug, Clone)]
1111
pub struct BroadcastArgs {
1212
/// Where to broadcast the transaction
13-
node_url: Url,
13+
pub node_url: Url,
1414

1515
/// The transaction to broadcast
16-
tx: String,
16+
pub tx: String,
1717
}
1818

1919
pub fn broadcast_tx(broadcast: &BroadcastArgs) -> anyhow::Result<()> {

sbtc-cli/src/commands/deposit.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::{io::stdout, str::FromStr};
1+
use std::str::FromStr;
22

33
use bdk::{
44
bitcoin::{
5-
psbt::serialize::Serialize, Address as BitcoinAddress,
6-
Network as BitcoinNetwork, PrivateKey,
5+
Address as BitcoinAddress, Network as BitcoinNetwork, PrivateKey,
6+
Transaction,
77
},
88
blockchain::{
99
ConfigurableBlockchain, ElectrumBlockchain, ElectrumBlockchainConfig,
@@ -17,36 +17,34 @@ use sbtc_core::operations::op_return::deposit::build_deposit_transaction;
1717
use stacks_core::utils::PrincipalData;
1818
use url::Url;
1919

20-
use crate::commands::utils;
21-
2220
#[derive(Parser, Debug, Clone)]
2321
pub struct DepositArgs {
2422
/// Where to broadcast the transaction
2523
#[clap(short('u'), long)]
26-
node_url: Url,
24+
pub node_url: Url,
2725

2826
/// Bitcoin WIF of the P2wPKH address
2927
#[clap(short, long)]
30-
wif: String,
28+
pub wif: String,
3129

3230
/// Bitcoin network where the deposit will be broadcasted to
3331
#[clap(short, long)]
34-
network: BitcoinNetwork,
32+
pub network: BitcoinNetwork,
3533

3634
/// Stacks address that will receive sBTC
3735
#[clap(short, long)]
38-
recipient: String,
36+
pub recipient: String,
3937

4038
/// The amount of sats to send
4139
#[clap(short, long)]
42-
amount: u64,
40+
pub amount: u64,
4341

4442
/// Bitcoin address of the sbtc wallet
4543
#[clap(short, long)]
46-
sbtc_wallet: String,
44+
pub sbtc_wallet: String,
4745
}
4846

49-
pub fn build_deposit_tx(deposit: &DepositArgs) -> anyhow::Result<()> {
47+
pub fn build_deposit_tx(deposit: &DepositArgs) -> anyhow::Result<Transaction> {
5048
let private_key = PrivateKey::from_wif(&deposit.wif)?;
5149

5250
let blockchain =
@@ -71,21 +69,12 @@ pub fn build_deposit_tx(deposit: &DepositArgs) -> anyhow::Result<()> {
7169
let stx_recipient = PrincipalData::try_from(deposit.recipient.to_string())?;
7270
let sbtc_wallet_address = BitcoinAddress::from_str(&deposit.sbtc_wallet)?;
7371

74-
let tx = build_deposit_transaction(
72+
build_deposit_transaction(
7573
wallet,
7674
stx_recipient,
7775
sbtc_wallet_address,
7876
deposit.amount,
7977
deposit.network,
80-
)?;
81-
82-
serde_json::to_writer_pretty(
83-
stdout(),
84-
&utils::TransactionData {
85-
id: tx.txid().to_string(),
86-
hex: hex::encode(tx.serialize()),
87-
},
88-
)?;
89-
90-
Ok(())
78+
)
79+
.map_err(|e| e.into())
9180
}

sbtc-cli/src/main.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
//!
66
//! It also allows you to generate credentials needed to generate transactions
77
//! and interact with the Bitcoin and Stacks networks.
8+
use std::io::stdout;
89

10+
use bdk::bitcoin::psbt::serialize::Serialize;
911
use clap::{Parser, Subcommand};
1012
use sbtc_cli::commands::{
1113
broadcast::{broadcast_tx, BroadcastArgs},
1214
deposit::{build_deposit_tx, DepositArgs},
1315
generate::{generate, GenerateArgs},
16+
utils,
1417
withdraw::{build_withdrawal_tx, WithdrawalArgs},
1518
};
1619

@@ -32,7 +35,17 @@ fn main() -> Result<(), anyhow::Error> {
3235
let args = Cli::parse();
3336

3437
match args.command {
35-
Command::Deposit(deposit_args) => build_deposit_tx(&deposit_args),
38+
Command::Deposit(deposit_args) => build_deposit_tx(&deposit_args)
39+
.and_then(|t| {
40+
serde_json::to_writer_pretty(
41+
stdout(),
42+
&utils::TransactionData {
43+
id: t.txid().to_string(),
44+
hex: hex::encode(t.serialize()),
45+
},
46+
)?;
47+
Ok(())
48+
}),
3649
Command::Withdraw(withdrawal_args) => {
3750
build_withdrawal_tx(&withdrawal_args)
3851
}

0 commit comments

Comments
 (0)