Simplified driver code a bit

This commit is contained in:
Dave Hrycyszyn
2024-07-24 18:14:08 +01:00
parent 479abcbba2
commit 09c10b8d45
2 changed files with 16 additions and 12 deletions

View File

@@ -1,4 +1,4 @@
use std::{collections::BTreeSet, fs, io::Write}; use std::fs;
use bdk::keys::bip39::Mnemonic; use bdk::keys::bip39::Mnemonic;
use bdk_esplora::{ use bdk_esplora::{
@@ -6,7 +6,7 @@ use bdk_esplora::{
EsploraAsyncExt, EsploraAsyncExt,
}; };
use bdk_wallet::{ use bdk_wallet::{
bitcoin::{Address, Amount, Network, Script}, bitcoin::{Address, Amount, Network},
chain::ConfirmationTimeHeightAnchor, chain::ConfirmationTimeHeightAnchor,
keys::{DerivableKey, ExtendedKey}, keys::{DerivableKey, ExtendedKey},
wallet::AddressInfo, wallet::AddressInfo,

View File

@@ -13,29 +13,33 @@ pub(crate) async fn simple_transfer() -> Result<(), anyhow::Error> {
let _next_address = dave.next_unused_address()?; let _next_address = dave.next_unused_address()?;
let _ = dave.balance(); let _ = dave.balance();
dave.sync().await?; dave.sync().await?;
let dave_balance = dave.balance(); let _ = dave.balance();
let sammy_address = sammy.next_unused_address()?.address;
let _sammy = sammy.balance(); let _sammy = sammy.balance();
sammy.sync().await?; sammy.sync().await?;
let _sammy = sammy.balance(); let _ = sammy.balance();
let send_amount = Amount::from_sat(500); let send_amount = Amount::from_sat(500);
ensure_enough_sats(dave, send_amount);
if dave_balance.total() < send_amount { let sammy_address = sammy.next_unused_address()?.address;
let tx = dave.build_and_sign_send_tx(sammy_address, send_amount)?;
dave.broadcast(&tx).await?;
Ok(())
}
/// Exit if the wallet does not have enough sats to send.
fn ensure_enough_sats(wallet: EsploraWallet, send_amount: bitcoin::Amount) -> _ {
if wallet.balance().total() < send_amount {
tracing::error!( tracing::error!(
"Please send at least {} sats to the receiving address. Exiting.", "Please send at least {} sats to the receiving address. Exiting.",
send_amount send_amount
); );
std::process::exit(0); std::process::exit(0);
} }
let tx = dave.build_and_sign_send_tx(sammy_address, send_amount)?;
dave.broadcast(&tx).await?;
Ok(())
} }
pub(crate) async fn htlc() -> anyhow::Result<()> { pub(crate) async fn htlc() -> anyhow::Result<()> {