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

Commit a0d3aa0

Browse files
committed
deposit integration test
1 parent 35ea437 commit a0d3aa0

File tree

8 files changed

+89
-39
lines changed

8 files changed

+89
-39
lines changed

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/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ rs_merkle.workspace = true
2626

2727
[dev-dependencies]
2828
reqwest = { workspace = true, features = ["json", "blocking"] }
29+
sbtc-cli = { path = "../sbtc-cli" }

romeo/tests/bitcoin_client_integration.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@ use reqwest::blocking::Client;
44
use serde_json::json;
55
use url::Url;
66

7+
pub fn project_name() -> String {
8+
env::var("PROJECT_NAME").unwrap()
9+
}
10+
711
pub fn bitcoin_url() -> Url {
8-
let base = env::var("PROJECT_NAME").unwrap();
9-
Url::parse(&format!("http://{base}-bitcoin-1")).unwrap()
12+
let base = project_name();
13+
Url::parse(&format!("http://{base}-bitcoin-1:18443")).unwrap()
14+
}
15+
16+
pub fn electrs_url() -> Url {
17+
let base = project_name();
18+
Url::parse(&format!("http://{base}-electrs-1:60401")).unwrap()
1019
}
1120

1221
pub fn generate_blocks(blocks: u64, ctx: Client) {
13-
let endpoint = {
14-
let mut endpoint = bitcoin_url();
15-
endpoint.set_port(Some(18443)).unwrap();
16-
endpoint
17-
};
22+
let endpoint = bitcoin_url();
1823
let user = "devnet";
1924
let password = "devnet";
2025
let body = json!({

romeo/tests/deposit_integration.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use anyhow::Result;
2+
use bdk::bitcoin::psbt::serialize::Serialize;
3+
use sbtc_cli::commands::{
4+
broadcast::{broadcast_tx, BroadcastArgs},
5+
deposit::{build_deposit_tx, DepositArgs},
6+
utils,
7+
};
8+
use serde_json::to_string_pretty;
9+
10+
mod bitcoin_client_integration;
11+
use bitcoin_client_integration::electrs_url;
12+
13+
const DEPLOYER_P2TR_ADDRESS: &str =
14+
"bcrt1pte5zmd7qzj4hdu45lh9mmdm0nwq3z35pwnxmzkwld6y0a8g83nnqhj6vc0";
15+
const ALICE_P2WPKH_WIF: &str =
16+
"cNcXK2r8bNdWJQymtAW8tGS7QHNtFFvG5CdXqhhT752u29WspXRM";
17+
const STX_ADDRESS: &str = "ST2ST2H80NP5C9SPR4ENJ1Z9CDM9PKAJVPYWPQZ50";
18+
19+
#[test]
20+
fn deposit() -> Result<()> {
21+
let electrum_url = electrs_url();
22+
let amount = 10_000;
23+
24+
let args = DepositArgs {
25+
node_url: electrum_url.clone(),
26+
wif: ALICE_P2WPKH_WIF.into(),
27+
network: bdk::bitcoin::Network::Regtest,
28+
recipient: STX_ADDRESS.into(),
29+
amount,
30+
dkg_wallet: DEPLOYER_P2TR_ADDRESS.into(),
31+
};
32+
33+
let tx = build_deposit_tx(&args)?;
34+
35+
broadcast_tx(&BroadcastArgs {
36+
node_url: electrum_url,
37+
tx: to_string_pretty(&utils::TransactionData {
38+
id: tx.txid().to_string(),
39+
hex: hex::encode(tx.serialize()),
40+
})?,
41+
})?;
42+
43+
Ok(())
44+
}

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::address::StacksAddress;
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
/// Dkg wallet address
4543
#[clap(short, long)]
46-
dkg_wallet: String,
44+
pub dkg_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 =
@@ -72,21 +70,12 @@ pub fn build_deposit_tx(deposit: &DepositArgs) -> anyhow::Result<()> {
7270
StacksAddress::try_from(deposit.recipient.as_str())?;
7371
let dkg_address = BitcoinAddress::from_str(&deposit.dkg_wallet)?;
7472

75-
let tx = build_deposit_transaction(
73+
build_deposit_transaction(
7674
wallet,
7775
recipient_address.into(),
7876
dkg_address,
7977
deposit.amount,
8078
deposit.network,
81-
)?;
82-
83-
serde_json::to_writer_pretty(
84-
stdout(),
85-
&utils::TransactionData {
86-
id: tx.txid().to_string(),
87-
hex: hex::encode(tx.serialize()),
88-
},
89-
)?;
90-
91-
Ok(())
79+
)
80+
.map_err(|e| e.into())
9281
}

sbtc-cli/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod commands;

sbtc-cli/src/main.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
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};
10-
11-
use crate::commands::{
12+
use sbtc_cli::commands::{
1213
broadcast::{broadcast_tx, BroadcastArgs},
1314
deposit::{build_deposit_tx, DepositArgs},
1415
generate::{generate, GenerateArgs},
16+
utils,
1517
withdraw::{build_withdrawal_tx, WithdrawalArgs},
1618
};
1719

18-
mod commands;
19-
2020
#[derive(Parser)]
2121
struct Cli {
2222
#[command(subcommand)]
@@ -35,7 +35,17 @@ fn main() -> Result<(), anyhow::Error> {
3535
let args = Cli::parse();
3636

3737
match args.command {
38-
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+
}),
3949
Command::Withdraw(withdrawal_args) => {
4050
build_withdrawal_tx(&withdrawal_args)
4151
}

0 commit comments

Comments
 (0)