Sending between multiple addresses works nicely

This commit is contained in:
Dave Hrycyszyn
2024-06-25 15:18:30 +01:00
parent a6105cf2bf
commit 6b1aa2b4ca
2 changed files with 43 additions and 15 deletions

View File

@@ -22,9 +22,10 @@ const PARALLEL_REQUESTS: usize = 5;
/// A wallet that uses the Esplora client to interact with the Bitcoin network.
pub struct EsploraWallet {
wallet: Wallet,
db: Store<KeychainKind, ConfirmationTimeHeightAnchor>,
client: AsyncClient,
db: Store<KeychainKind, ConfirmationTimeHeightAnchor>,
name: String,
wallet: Wallet,
}
impl EsploraWallet {
@@ -107,7 +108,10 @@ impl EsploraWallet {
if let Some(changeset) = self.wallet.take_staged() {
self.db.write(&changeset)?;
}
println!("Generated Address: {}", address);
println!(
"Generated address: https://mutinynet.com/address/{}",
address
);
Ok(address)
}
@@ -121,6 +125,11 @@ impl EsploraWallet {
&self,
tx: &bitcoin::Transaction,
) -> Result<(), esplora_client::Error> {
println!(
"{} broadcasting tx https://mutinynet.com/tx/{}",
self.name,
tx.compute_txid()
);
self.client.broadcast(tx).await
}
}
@@ -166,7 +175,12 @@ pub(crate) fn create_wallet(name: &str, network: Network) -> anyhow::Result<Espl
.build_async()
.expect("couldn't build esplora client");
let esplora = EsploraWallet { wallet, db, client };
let esplora = EsploraWallet {
name: name.to_string(),
wallet,
db,
client,
};
Ok(esplora)
}

View File

@@ -12,22 +12,38 @@ use crate::bitcoin::clients;
/// with 30 second block times.
pub(crate) async fn run() -> Result<(), anyhow::Error> {
let mut dave = clients::esplora::create_wallet("dave", Network::Signet)?;
let mut sammy = clients::esplora::create_wallet("sammy", Network::Signet)?;
let _next_address = dave.next_unused_address()?;
let balance = dave.balance();
println!("Wallet balance before syncing: {} sats", balance.total());
let dave_balance = dave.balance();
println!(
"Dave wallet balance before syncing: {} sats",
dave_balance.total()
);
dave.sync().await?;
let balance = dave.balance();
println!("Wallet balance after syncing: {} sats", balance.total());
let dave_balance = dave.balance();
println!("Wallet balance after syncing: {} sats", dave_balance);
let faucet_address = Address::from_str("mkHS9ne12qx9pS9VojpwU5xtRd4T7X7ZUt")?
.require_network(Network::Signet)?;
let send_amount = Amount::from_sat(5000);
let sammy_address = sammy.next_unused_address()?.address;
println!("Sammy's address: {}", sammy_address);
if balance.total() < send_amount {
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
@@ -35,10 +51,8 @@ pub(crate) async fn run() -> Result<(), anyhow::Error> {
std::process::exit(0);
}
let tx = dave.build_send_tx(faucet_address, send_amount)?;
let tx = dave.build_send_tx(sammy_address, send_amount)?;
dave.broadcast(&tx).await?;
println!("Tx broadcasted! Txid: {}", tx.compute_txid());
Ok(())
}