diff --git a/crates/bdk/src/wallet/tx_builder.rs b/crates/bdk/src/wallet/tx_builder.rs index e7df86678d..dd3f52cbee 100644 --- a/crates/bdk/src/wallet/tx_builder.rs +++ b/crates/bdk/src/wallet/tx_builder.rs @@ -718,6 +718,7 @@ impl<'a, Cs: CoinSelectionAlgorithm> TxBuilder<'a, Cs, CreateTx> { self } + // TODO: (@leonardo) Should this expect/use `bitcoin::Amount` instead ? Would it be a huge breaking change ? /// Add a recipient to the internal list pub fn add_recipient(&mut self, script_pubkey: ScriptBuf, amount: u64) -> &mut Self { self.params.recipients.push((script_pubkey, amount)); diff --git a/crates/chain/src/keychain.rs b/crates/chain/src/keychain.rs index c20d1f6cd9..fa3dbf7451 100644 --- a/crates/chain/src/keychain.rs +++ b/crates/chain/src/keychain.rs @@ -90,13 +90,13 @@ impl AsRef> for ChangeSet { )] pub struct Balance { /// All coinbase outputs not yet matured - pub immature: u64, + pub immature: bitcoin::Amount, /// Unconfirmed UTXOs generated by a wallet tx - pub trusted_pending: u64, + pub trusted_pending: bitcoin::Amount, /// Unconfirmed UTXOs received from an external wallet - pub untrusted_pending: u64, + pub untrusted_pending: bitcoin::Amount, /// Confirmed and immediately spendable balance - pub confirmed: u64, + pub confirmed: bitcoin::Amount, } impl Balance { @@ -104,12 +104,12 @@ impl Balance { /// /// This is the balance you can spend right now that shouldn't get cancelled via another party /// double spending it. - pub fn trusted_spendable(&self) -> u64 { + pub fn trusted_spendable(&self) -> bitcoin::Amount { self.confirmed + self.trusted_pending } /// Get the whole balance visible to the wallet. - pub fn total(&self) -> u64 { + pub fn total(&self) -> bitcoin::Amount { self.confirmed + self.trusted_pending + self.untrusted_pending + self.immature } } diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index f6144e7a25..a3a9e1beba 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -1155,27 +1155,28 @@ impl TxGraph { outpoints: impl IntoIterator, mut trust_predicate: impl FnMut(&OI, &Script) -> bool, ) -> Result { - let mut immature = 0; - let mut trusted_pending = 0; - let mut untrusted_pending = 0; - let mut confirmed = 0; + let mut immature = bitcoin::Amount::ZERO; + let mut trusted_pending = bitcoin::Amount::ZERO; + let mut untrusted_pending = bitcoin::Amount::ZERO; + let mut confirmed = bitcoin::Amount::ZERO; for res in self.try_filter_chain_unspents(chain, chain_tip, outpoints) { let (spk_i, txout) = res?; + // TODO: (@leonardo) Should these operations use `bitcoin::Amount::checked_add()` instead ? match &txout.chain_position { ChainPosition::Confirmed(_) => { if txout.is_confirmed_and_spendable(chain_tip.height) { - confirmed += txout.txout.value.to_sat(); + confirmed += txout.txout.value; } else if !txout.is_mature(chain_tip.height) { - immature += txout.txout.value.to_sat(); + immature += txout.txout.value; } } ChainPosition::Unconfirmed(_) => { if trust_predicate(&spk_i, &txout.txout.script_pubkey) { - trusted_pending += txout.txout.value.to_sat(); + trusted_pending += txout.txout.value; } else { - untrusted_pending += txout.txout.value.to_sat(); + untrusted_pending += txout.txout.value; } } } diff --git a/example-crates/example_cli/src/lib.rs b/example-crates/example_cli/src/lib.rs index e7c4efece2..8a57e1f53e 100644 --- a/example-crates/example_cli/src/lib.rs +++ b/example-crates/example_cli/src/lib.rs @@ -505,7 +505,7 @@ where let chain = &*chain.lock().unwrap(); fn print_balances<'a>( title_str: &'a str, - items: impl IntoIterator, + items: impl IntoIterator, ) { println!("{}:", title_str); for (name, amount) in items.into_iter() { diff --git a/example-crates/wallet_electrum/src/main.rs b/example-crates/wallet_electrum/src/main.rs index 4af9e71dec..c71592f592 100644 --- a/example-crates/wallet_electrum/src/main.rs +++ b/example-crates/wallet_electrum/src/main.rs @@ -6,7 +6,7 @@ const BATCH_SIZE: usize = 5; use std::io::Write; use std::str::FromStr; -use bdk::bitcoin::Address; +use bdk::bitcoin::{Address, Amount}; use bdk::wallet::Update; use bdk::{bitcoin::Network, Wallet}; use bdk::{KeychainKind, SignOptions}; @@ -81,7 +81,8 @@ fn main() -> Result<(), anyhow::Error> { let balance = wallet.get_balance(); println!("Wallet balance after syncing: {} sats", balance.total()); - if balance.total() < SEND_AMOUNT { + // TODO: (@leonardo) Should we format here, or update on constant and TxBuilder::add_recipient() instead ? + if balance.total() < Amount::from_sat(SEND_AMOUNT) { println!( "Please send at least {} sats to the receiving address", SEND_AMOUNT diff --git a/example-crates/wallet_esplora_async/src/main.rs b/example-crates/wallet_esplora_async/src/main.rs index c37b6e6650..bf8b9b76bb 100644 --- a/example-crates/wallet_esplora_async/src/main.rs +++ b/example-crates/wallet_esplora_async/src/main.rs @@ -1,7 +1,7 @@ use std::{io::Write, str::FromStr}; use bdk::{ - bitcoin::{Address, Network}, + bitcoin::{Address, Amount, Network}, wallet::Update, KeychainKind, SignOptions, Wallet, }; @@ -72,7 +72,8 @@ async fn main() -> Result<(), anyhow::Error> { let balance = wallet.get_balance(); println!("Wallet balance after syncing: {} sats", balance.total()); - if balance.total() < SEND_AMOUNT { + // TODO: (@leonardo) Should we format here, or update on constant and TxBuilder::add_recipient() instead ? + if balance.total() < Amount::from_sat(SEND_AMOUNT) { println!( "Please send at least {} sats to the receiving address", SEND_AMOUNT diff --git a/example-crates/wallet_esplora_blocking/src/main.rs b/example-crates/wallet_esplora_blocking/src/main.rs index 979e272f56..5f5a428349 100644 --- a/example-crates/wallet_esplora_blocking/src/main.rs +++ b/example-crates/wallet_esplora_blocking/src/main.rs @@ -6,7 +6,7 @@ const PARALLEL_REQUESTS: usize = 1; use std::{io::Write, str::FromStr}; use bdk::{ - bitcoin::{Address, Network}, + bitcoin::{Address, Amount, Network}, wallet::Update, KeychainKind, SignOptions, Wallet, }; @@ -72,7 +72,8 @@ fn main() -> Result<(), anyhow::Error> { let balance = wallet.get_balance(); println!("Wallet balance after syncing: {} sats", balance.total()); - if balance.total() < SEND_AMOUNT { + // TODO: (@leonardo) Should we format here, or update on constant and TxBuilder::add_recipient() instead ? + if balance.total() < Amount::from_sat(SEND_AMOUNT) { println!( "Please send at least {} sats to the receiving address", SEND_AMOUNT