More refactoring to make the driver do the work

This commit is contained in:
Dave Hrycyszyn
2024-06-25 14:43:57 +01:00
parent c6242e99f7
commit 2474f5186d
2 changed files with 47 additions and 46 deletions

View File

@@ -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(())
}