diff --git a/side-node/src/bitcoin/clients/esplora.rs b/side-node/src/bitcoin/clients/esplora.rs index 8b8ae82..cce3e88 100644 --- a/side-node/src/bitcoin/clients/esplora.rs +++ b/side-node/src/bitcoin/clients/esplora.rs @@ -14,54 +14,17 @@ use bdk_sqlite::{rusqlite::Connection, Store}; use crate::utils; -const SEND_AMOUNT: Amount = Amount::from_sat(5000); const STOP_GAP: usize = 50; const PARALLEL_REQUESTS: usize = 5; -/// Demonstrates the use of bdk with the Esplora client. -/// -/// This is more complex than the bare `bdk` crate, but the esplora client works. -/// -/// Also, it very nadily works with the mutinynet.com esplora server, which is configured -/// with 30 second block times. -pub async fn run() -> Result<(), anyhow::Error> { - let (mut wallet, mut db) = create_wallet("dave")?; - - let _next_address = next_unused_address(&mut wallet, &mut db)?; - - let balance = wallet.balance(); - println!("Wallet balance before syncing: {} sats", balance.total()); - - let client = sync(&mut wallet, &mut db).await?; - - let balance = wallet.balance(); - println!("Wallet balance after syncing: {} sats", balance.total()); - - if balance.total() < SEND_AMOUNT { - println!( - "Please send at least {} sats to the receiving address", - SEND_AMOUNT - ); - std::process::exit(0); - } - - let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")? - .require_network(Network::Signet)?; - - let tx = build_send_tx(wallet, faucet_address)?; - client.broadcast(&tx).await?; - println!("Tx broadcasted! Txid: {}", tx.compute_txid()); - - Ok(()) -} - -fn build_send_tx( +pub(crate) fn build_send_tx( mut wallet: Wallet, faucet_address: Address, + amount: Amount, ) -> Result { let mut tx_builder = wallet.build_tx(); tx_builder - .add_recipient(faucet_address.script_pubkey(), SEND_AMOUNT) + .add_recipient(faucet_address.script_pubkey(), amount) .enable_rbf(); let mut psbt = tx_builder.finish()?; let finalized = wallet.sign(&mut psbt, SignOptions::default())?; @@ -70,7 +33,7 @@ fn build_send_tx( Ok(tx) } -async fn sync( +pub(crate) async fn sync( wallet: &mut Wallet, db: &mut Store, ) -> Result { @@ -122,7 +85,7 @@ async fn sync( Ok(client) } -fn next_unused_address( +pub(crate) fn next_unused_address( wallet: &mut Wallet, db: &mut Store, ) -> Result { @@ -134,7 +97,7 @@ fn next_unused_address( Ok(address) } -fn create_wallet( +pub(crate) fn create_wallet( name: &str, ) -> anyhow::Result<(Wallet, Store)> { let keys_dir = utils::home(name); diff --git a/side-node/src/bitcoin/driver.rs b/side-node/src/bitcoin/driver.rs index d11c9f0..46ce4fd 100644 --- a/side-node/src/bitcoin/driver.rs +++ b/side-node/src/bitcoin/driver.rs @@ -1,5 +1,43 @@ -use crate::bitcoin; +use std::str::FromStr; -pub(crate) async fn run() { - bitcoin::clients::esplora::run().await.unwrap(); +use bdk_wallet::bitcoin::{Address, Amount, Network}; + +use crate::bitcoin::clients; + +/// Demonstrates the use of bdk with the Esplora client. +/// +/// This is more complex than the bare `bdk` crate, but the esplora client works. +/// +/// Also, it very handily works with the mutinynet.com esplora server, which is configured +/// with 30 second block times. +pub(crate) async fn run() -> Result<(), anyhow::Error> { + let (mut wallet, mut db) = clients::esplora::create_wallet("dave")?; + + let _next_address = clients::esplora::next_unused_address(&mut wallet, &mut db)?; + + let balance = wallet.balance(); + println!("Wallet balance before syncing: {} sats", balance.total()); + + let client = clients::esplora::sync(&mut wallet, &mut db).await?; + + let balance = wallet.balance(); + println!("Wallet balance after syncing: {} sats", balance.total()); + + let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")? + .require_network(Network::Signet)?; + let send_amount = Amount::from_sat(5000); + + if balance.total() < send_amount { + println!( + "Please send at least {} sats to the receiving address", + send_amount + ); + std::process::exit(0); + } + + let tx = clients::esplora::build_send_tx(wallet, faucet_address, send_amount)?; + client.broadcast(&tx).await?; + println!("Tx broadcasted! Txid: {}", tx.compute_txid()); + + Ok(()) }