Files
bft-crdt-experiment/side-node/src/bitcoin/driver.rs

59 lines
1.6 KiB
Rust
Raw Normal View History

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> {
2024-06-25 15:05:33 +01:00
let mut dave = clients::esplora::create_wallet("dave", Network::Signet)?;
let mut sammy = clients::esplora::create_wallet("sammy", Network::Signet)?;
2024-06-25 14:58:53 +01:00
let _next_address = dave.next_unused_address()?;
let dave_balance = dave.balance();
println!(
"Dave wallet balance before syncing: {} sats",
dave_balance.total()
);
2024-06-25 14:58:53 +01:00
dave.sync().await?;
let dave_balance = dave.balance();
println!("Wallet balance after syncing: {} sats", dave_balance);
let sammy_address = sammy.next_unused_address()?.address;
println!("Sammy's address: {}", sammy_address);
let sammy_balance = sammy.balance();
println!(
"Sammy wallet balance before syncing: {} sats",
sammy_balance
);
sammy.sync().await?;
let sammy_balance = sammy.balance();
println!("Sammy wallet balance after syncing: {} sats", sammy_balance);
let send_amount = Amount::from_sat(500);
if dave_balance.total() < send_amount {
println!(
"Please send at least {} sats to the receiving address",
send_amount
);
std::process::exit(0);
}
let tx = dave.build_send_tx(sammy_address, send_amount)?;
2024-06-25 14:58:53 +01:00
dave.broadcast(&tx).await?;
Ok(())
}