use std::str::FromStr; 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(()) }