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