From 86bbd79064551b348a3e23566caa7e50f06a5304 Mon Sep 17 00:00:00 2001 From: Leonardo Lima Date: Fri, 26 Apr 2024 11:41:17 -0300 Subject: [PATCH] feat: use `Amount` on `TxBuilder::add_recipient` --- crates/bdk/src/wallet/coin_selection.rs | 2 +- crates/bdk/src/wallet/mod.rs | 8 +- crates/bdk/src/wallet/tx_builder.rs | 28 ++-- crates/bdk/tests/psbt.rs | 10 +- crates/bdk/tests/wallet.rs | 142 +++++++++--------- crates/chain/src/keychain.rs | 13 +- crates/chain/src/tx_graph.rs | 11 +- example-crates/wallet_electrum/src/main.rs | 5 +- .../wallet_esplora_async/src/main.rs | 5 +- .../wallet_esplora_blocking/src/main.rs | 5 +- 10 files changed, 115 insertions(+), 114 deletions(-) diff --git a/crates/bdk/src/wallet/coin_selection.rs b/crates/bdk/src/wallet/coin_selection.rs index f1897677bf..6be3cb9713 100644 --- a/crates/bdk/src/wallet/coin_selection.rs +++ b/crates/bdk/src/wallet/coin_selection.rs @@ -92,7 +92,7 @@ //! .unwrap(); //! let psbt = { //! let mut builder = wallet.build_tx().coin_selection(AlwaysSpendEverything); -//! builder.add_recipient(to_address.script_pubkey(), 50_000); +//! builder.add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000)); //! builder.finish()? //! }; //! diff --git a/crates/bdk/src/wallet/mod.rs b/crates/bdk/src/wallet/mod.rs index ff996fc68c..6f071c327e 100644 --- a/crates/bdk/src/wallet/mod.rs +++ b/crates/bdk/src/wallet/mod.rs @@ -950,7 +950,7 @@ impl Wallet { /// [`insert_txout`]: Self::insert_txout pub fn calculate_fee_rate(&self, tx: &Transaction) -> Result { self.calculate_fee(tx) - .map(|fee| bitcoin::Amount::from_sat(fee) / tx.weight()) + .map(|fee| Amount::from_sat(fee) / tx.weight()) } /// Compute the `tx`'s sent and received amounts (in satoshis). @@ -1197,7 +1197,7 @@ impl Wallet { /// let psbt = { /// let mut builder = wallet.build_tx(); /// builder - /// .add_recipient(to_address.script_pubkey(), 50_000); + /// .add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000)); /// builder.finish()? /// }; /// @@ -1579,7 +1579,7 @@ impl Wallet { /// let mut psbt = { /// let mut builder = wallet.build_tx(); /// builder - /// .add_recipient(to_address.script_pubkey(), 50_000) + /// .add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000)) /// .enable_rbf(); /// builder.finish()? /// }; @@ -1752,7 +1752,7 @@ impl Wallet { /// # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked(); /// let mut psbt = { /// let mut builder = wallet.build_tx(); - /// builder.add_recipient(to_address.script_pubkey(), 50_000); + /// builder.add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000)); /// builder.finish()? /// }; /// let finalized = wallet.sign(&mut psbt, SignOptions::default())?; diff --git a/crates/bdk/src/wallet/tx_builder.rs b/crates/bdk/src/wallet/tx_builder.rs index bf6d404a2d..5c3e70d54e 100644 --- a/crates/bdk/src/wallet/tx_builder.rs +++ b/crates/bdk/src/wallet/tx_builder.rs @@ -29,7 +29,7 @@ //! //! tx_builder //! // Create a transaction with one output to `to_address` of 50_000 satoshi -//! .add_recipient(to_address.script_pubkey(), 50_000) +//! .add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000)) //! // With a custom fee rate of 5.0 satoshi/vbyte //! .fee_rate(FeeRate::from_sat_per_vb(5).expect("valid feerate")) //! // Only spend non-change outputs @@ -47,7 +47,7 @@ use core::marker::PhantomData; use bitcoin::psbt::{self, Psbt}; use bitcoin::script::PushBytes; -use bitcoin::{absolute, FeeRate, OutPoint, ScriptBuf, Sequence, Transaction, Txid}; +use bitcoin::{absolute, Amount, FeeRate, OutPoint, ScriptBuf, Sequence, Transaction, Txid}; use super::coin_selection::{CoinSelectionAlgorithm, DefaultCoinSelectionAlgorithm}; use super::{CreateTxError, Wallet}; @@ -94,8 +94,8 @@ impl TxBuilderContext for BumpFee {} /// let mut builder = wallet.build_tx(); /// builder /// .ordering(TxOrdering::Untouched) -/// .add_recipient(addr1.script_pubkey(), 50_000) -/// .add_recipient(addr2.script_pubkey(), 50_000); +/// .add_recipient(addr1.script_pubkey(), Amount::from_sat(50_000)) +/// .add_recipient(addr2.script_pubkey(), Amount::from_sat(50_000)); /// builder.finish()? /// }; /// @@ -104,7 +104,7 @@ impl TxBuilderContext for BumpFee {} /// let mut builder = wallet.build_tx(); /// builder.ordering(TxOrdering::Untouched); /// for addr in &[addr1, addr2] { -/// builder.add_recipient(addr.script_pubkey(), 50_000); +/// builder.add_recipient(addr.script_pubkey(), Amount::from_sat(50_000)); /// } /// builder.finish()? /// }; @@ -274,7 +274,7 @@ impl<'a, Cs, Ctx> TxBuilder<'a, Cs, Ctx> { /// /// let builder = wallet /// .build_tx() - /// .add_recipient(to_address.script_pubkey(), 50_000) + /// .add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000)) /// .policy_path(path, KeychainKind::External); /// /// # Ok::<(), anyhow::Error>(()) @@ -713,22 +713,26 @@ impl std::error::Error for AllowShrinkingError {} impl<'a, Cs: CoinSelectionAlgorithm> TxBuilder<'a, Cs, CreateTx> { /// Replace the recipients already added with a new list - pub fn set_recipients(&mut self, recipients: Vec<(ScriptBuf, u64)>) -> &mut Self { - self.params.recipients = recipients; + pub fn set_recipients(&mut self, recipients: Vec<(ScriptBuf, Amount)>) -> &mut Self { + self.params.recipients = recipients + .into_iter() + .map(|(script, amount)| (script, amount.to_sat())) + .collect(); 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)); + pub fn add_recipient(&mut self, script_pubkey: ScriptBuf, amount: Amount) -> &mut Self { + self.params + .recipients + .push((script_pubkey, amount.to_sat())); self } /// Add data as an output, using OP_RETURN pub fn add_data>(&mut self, data: &T) -> &mut Self { let script = ScriptBuf::new_op_return(data); - self.add_recipient(script, 0u64); + self.add_recipient(script, Amount::ZERO); self } diff --git a/crates/bdk/tests/psbt.rs b/crates/bdk/tests/psbt.rs index 1a81f6e807..820d2d1d10 100644 --- a/crates/bdk/tests/psbt.rs +++ b/crates/bdk/tests/psbt.rs @@ -1,4 +1,4 @@ -use bdk::bitcoin::{FeeRate, Psbt, TxIn}; +use bdk::bitcoin::{Amount, FeeRate, Psbt, TxIn}; use bdk::{psbt, KeychainKind, SignOptions}; use core::str::FromStr; mod common; @@ -14,7 +14,7 @@ fn test_psbt_malformed_psbt_input_legacy() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); let send_to = wallet.peek_address(KeychainKind::External, 0); let mut builder = wallet.build_tx(); - builder.add_recipient(send_to.script_pubkey(), 10_000); + builder.add_recipient(send_to.script_pubkey(), Amount::from_sat(10_000)); let mut psbt = builder.finish().unwrap(); psbt.inputs.push(psbt_bip.inputs[0].clone()); let options = SignOptions { @@ -31,7 +31,7 @@ fn test_psbt_malformed_psbt_input_segwit() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); let send_to = wallet.peek_address(KeychainKind::External, 0); let mut builder = wallet.build_tx(); - builder.add_recipient(send_to.script_pubkey(), 10_000); + builder.add_recipient(send_to.script_pubkey(), Amount::from_sat(10_000)); let mut psbt = builder.finish().unwrap(); psbt.inputs.push(psbt_bip.inputs[1].clone()); let options = SignOptions { @@ -47,7 +47,7 @@ fn test_psbt_malformed_tx_input() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); let send_to = wallet.peek_address(KeychainKind::External, 0); let mut builder = wallet.build_tx(); - builder.add_recipient(send_to.script_pubkey(), 10_000); + builder.add_recipient(send_to.script_pubkey(), Amount::from_sat(10_000)); let mut psbt = builder.finish().unwrap(); psbt.unsigned_tx.input.push(TxIn::default()); let options = SignOptions { @@ -63,7 +63,7 @@ fn test_psbt_sign_with_finalized() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); let send_to = wallet.peek_address(KeychainKind::External, 0); let mut builder = wallet.build_tx(); - builder.add_recipient(send_to.script_pubkey(), 10_000); + builder.add_recipient(send_to.script_pubkey(), Amount::from_sat(10_000)); let mut psbt = builder.finish().unwrap(); // add a finalized input diff --git a/crates/bdk/tests/wallet.rs b/crates/bdk/tests/wallet.rs index 5d701c4201..dd42c4765d 100644 --- a/crates/bdk/tests/wallet.rs +++ b/crates/bdk/tests/wallet.rs @@ -200,7 +200,7 @@ fn test_get_funded_wallet_balance() { // The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000 // to a foreign address and one returning 50_000 back to the wallet as change. The remaining 1000 // sats are the transaction fee. - assert_eq!(wallet.get_balance().confirmed.to_sat(), 50_000); + assert_eq!(wallet.get_balance().confirmed, Amount::from_sat(50_000)); } #[test] @@ -347,7 +347,7 @@ fn test_create_tx_manually_selected_empty_utxos() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .manually_selected_only(); builder.finish().unwrap(); } @@ -358,7 +358,7 @@ fn test_create_tx_version_0() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .version(0); assert!(matches!(builder.finish(), Err(CreateTxError::Version0))); } @@ -369,7 +369,7 @@ fn test_create_tx_version_1_csv() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .version(1); assert!(matches!(builder.finish(), Err(CreateTxError::Version1Csv))); } @@ -380,7 +380,7 @@ fn test_create_tx_custom_version() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .version(42); let psbt = builder.finish().unwrap(); @@ -393,7 +393,7 @@ fn test_create_tx_default_locktime_is_last_sync_height() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); // Since we never synced the wallet we don't have a last_sync_height @@ -406,7 +406,7 @@ fn test_create_tx_fee_sniping_locktime_last_sync() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); @@ -422,7 +422,7 @@ fn test_create_tx_default_locktime_cltv() { let (mut wallet, _) = get_funded_wallet(get_test_single_sig_cltv()); let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); assert_eq!(psbt.unsigned_tx.lock_time.to_consensus_u32(), 100_000); @@ -434,7 +434,7 @@ fn test_create_tx_custom_locktime() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .current_height(630_001) .nlocktime(absolute::LockTime::from_height(630_000).unwrap()); let psbt = builder.finish().unwrap(); @@ -451,7 +451,7 @@ fn test_create_tx_custom_locktime_compatible_with_cltv() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .nlocktime(absolute::LockTime::from_height(630_000).unwrap()); let psbt = builder.finish().unwrap(); @@ -464,7 +464,7 @@ fn test_create_tx_custom_locktime_incompatible_with_cltv() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .nlocktime(absolute::LockTime::from_height(50000).unwrap()); assert!(matches!(builder.finish(), Err(CreateTxError::LockTime { requested, required }) @@ -476,7 +476,7 @@ fn test_create_tx_no_rbf_csv() { let (mut wallet, _) = get_funded_wallet(get_test_single_sig_csv()); let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); assert_eq!(psbt.unsigned_tx.input[0].sequence, Sequence(6)); @@ -488,7 +488,7 @@ fn test_create_tx_with_default_rbf_csv() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .enable_rbf(); let psbt = builder.finish().unwrap(); // When CSV is enabled it takes precedence over the rbf value (unless forced by the user). @@ -502,7 +502,7 @@ fn test_create_tx_with_custom_rbf_csv() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .enable_rbf_with_sequence(Sequence(3)); assert!(matches!(builder.finish(), Err(CreateTxError::RbfSequenceCsv { rbf, csv }) @@ -514,7 +514,7 @@ fn test_create_tx_no_rbf_cltv() { let (mut wallet, _) = get_funded_wallet(get_test_single_sig_cltv()); let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); assert_eq!(psbt.unsigned_tx.input[0].sequence, Sequence(0xFFFFFFFE)); @@ -526,7 +526,7 @@ fn test_create_tx_invalid_rbf_sequence() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .enable_rbf_with_sequence(Sequence(0xFFFFFFFE)); assert!(matches!(builder.finish(), Err(CreateTxError::RbfSequence))); } @@ -537,7 +537,7 @@ fn test_create_tx_custom_rbf_sequence() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .enable_rbf_with_sequence(Sequence(0xDEADBEEF)); let psbt = builder.finish().unwrap(); @@ -549,7 +549,7 @@ fn test_create_tx_default_sequence() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); assert_eq!(psbt.unsigned_tx.input[0].sequence, Sequence(0xFFFFFFFE)); @@ -561,7 +561,7 @@ fn test_create_tx_change_policy_no_internal() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .do_not_spend_change(); assert!(matches!( builder.finish(), @@ -604,7 +604,7 @@ fn test_create_tx_drain_wallet_and_drain_to_and_with_recipient() { let drain_addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 20_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(20_000)) .drain_to(drain_addr.script_pubkey()) .drain_wallet(); let psbt = builder.finish().unwrap(); @@ -659,7 +659,7 @@ fn test_create_tx_default_fee_rate() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); let fee = check_fee!(wallet, psbt); @@ -672,7 +672,7 @@ fn test_create_tx_custom_fee_rate() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .fee_rate(FeeRate::from_sat_per_vb_unchecked(5)); let psbt = builder.finish().unwrap(); let fee = check_fee!(wallet, psbt); @@ -741,7 +741,7 @@ fn test_create_tx_add_change() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .ordering(TxOrdering::Untouched); let psbt = builder.finish().unwrap(); let fee = check_fee!(wallet, psbt); @@ -759,7 +759,7 @@ fn test_create_tx_skip_change_dust() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 49_800); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(49_800)); let psbt = builder.finish().unwrap(); let fee = check_fee!(wallet, psbt); @@ -788,8 +788,8 @@ fn test_create_tx_ordering_respected() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 30_000) - .add_recipient(addr.script_pubkey(), 10_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(30_000)) + .add_recipient(addr.script_pubkey(), Amount::from_sat(10_000)) .ordering(bdk::wallet::tx_builder::TxOrdering::Bip69Lexicographic); let psbt = builder.finish().unwrap(); let fee = check_fee!(wallet, psbt); @@ -808,7 +808,7 @@ fn test_create_tx_default_sighash() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 30_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(30_000)); let psbt = builder.finish().unwrap(); assert_eq!(psbt.inputs[0].sighash_type, None); @@ -820,7 +820,7 @@ fn test_create_tx_custom_sighash() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 30_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(30_000)) .sighash(EcdsaSighashType::Single.into()); let psbt = builder.finish().unwrap(); @@ -1019,7 +1019,7 @@ fn test_create_tx_add_utxo() { .assume_checked(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 30_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(30_000)) .add_utxo(OutPoint { txid: small_output_tx.txid(), vout: 0, @@ -1070,7 +1070,7 @@ fn test_create_tx_manually_selected_insufficient() { .assume_checked(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 30_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(30_000)) .add_utxo(OutPoint { txid: small_output_tx.txid(), vout: 0, @@ -1089,7 +1089,7 @@ fn test_create_tx_policy_path_required() { .unwrap() .assume_checked(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 30_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(10_000)); builder.finish().unwrap(); } @@ -1124,7 +1124,7 @@ fn test_create_tx_policy_path_no_csv() { .assume_checked(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 30_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(30_000)) .policy_path(path, KeychainKind::External); let psbt = builder.finish().unwrap(); @@ -1145,7 +1145,7 @@ fn test_create_tx_policy_path_use_csv() { .assume_checked(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 30_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(30_000)) .policy_path(path, KeychainKind::External); let psbt = builder.finish().unwrap(); @@ -1166,7 +1166,7 @@ fn test_create_tx_policy_path_ignored_subtree_with_csv() { .assume_checked(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 30_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(30_000)) .policy_path(path, KeychainKind::External); let psbt = builder.finish().unwrap(); @@ -1182,7 +1182,7 @@ fn test_create_tx_global_xpubs_with_origin() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .add_global_xpubs(); let psbt = builder.finish().unwrap(); @@ -1216,7 +1216,7 @@ fn test_add_foreign_utxo() { let mut builder = wallet1.build_tx(); builder - .add_recipient(addr.script_pubkey(), 60_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(60_000)) .only_witness_utxo() .add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction) .unwrap(); @@ -1292,7 +1292,7 @@ fn test_calculate_fee_with_missing_foreign_utxo() { let mut builder = wallet1.build_tx(); builder - .add_recipient(addr.script_pubkey(), 60_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(60_000)) .only_witness_utxo() .add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction) .unwrap(); @@ -1376,7 +1376,7 @@ fn test_add_foreign_utxo_only_witness_utxo() { .unwrap(); let mut builder = wallet1.build_tx(); - builder.add_recipient(addr.script_pubkey(), 60_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000)); { let mut builder = builder.clone(); @@ -1445,7 +1445,7 @@ fn test_create_tx_global_xpubs_origin_missing() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .add_global_xpubs(); builder.finish().unwrap(); } @@ -1459,7 +1459,7 @@ fn test_create_tx_global_xpubs_master_without_origin() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .add_global_xpubs(); let psbt = builder.finish().unwrap(); @@ -1479,7 +1479,7 @@ fn test_bump_fee_irreplaceable_tx() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); let tx = psbt.extract_tx().expect("failed to extract tx"); @@ -1496,7 +1496,7 @@ fn test_bump_fee_confirmed_tx() { let (mut wallet, _) = get_funded_wallet(get_test_wpkh()); let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); let tx = psbt.extract_tx().expect("failed to extract tx"); @@ -1521,7 +1521,7 @@ fn test_bump_fee_low_fee_rate() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .enable_rbf(); let psbt = builder.finish().unwrap(); let feerate = psbt.fee_rate().unwrap(); @@ -1555,7 +1555,7 @@ fn test_bump_fee_low_abs() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .enable_rbf(); let psbt = builder.finish().unwrap(); @@ -1578,7 +1578,7 @@ fn test_bump_fee_zero_abs() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .enable_rbf(); let psbt = builder.finish().unwrap(); @@ -1601,7 +1601,7 @@ fn test_bump_fee_reduce_change() { .assume_checked(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .enable_rbf(); let psbt = builder.finish().unwrap(); let original_sent_received = @@ -1933,7 +1933,7 @@ fn test_bump_fee_add_input() { .assume_checked(); let mut builder = wallet.build_tx().coin_selection(LargestFirstCoinSelection); builder - .add_recipient(addr.script_pubkey(), 45_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(45_000)) .enable_rbf(); let psbt = builder.finish().unwrap(); let tx = psbt.extract_tx().expect("failed to extract tx"); @@ -1990,7 +1990,7 @@ fn test_bump_fee_absolute_add_input() { .assume_checked(); let mut builder = wallet.build_tx().coin_selection(LargestFirstCoinSelection); builder - .add_recipient(addr.script_pubkey(), 45_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(45_000)) .enable_rbf(); let psbt = builder.finish().unwrap(); let tx = psbt.extract_tx().expect("failed to extract tx"); @@ -2118,7 +2118,7 @@ fn test_bump_fee_add_input_change_dust() { .assume_checked(); let mut builder = wallet.build_tx().coin_selection(LargestFirstCoinSelection); builder - .add_recipient(addr.script_pubkey(), 45_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(45_000)) .enable_rbf(); let psbt = builder.finish().unwrap(); let original_sent_received = @@ -2195,7 +2195,7 @@ fn test_bump_fee_force_add_input() { .assume_checked(); let mut builder = wallet.build_tx().coin_selection(LargestFirstCoinSelection); builder - .add_recipient(addr.script_pubkey(), 45_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(45_000)) .enable_rbf(); let psbt = builder.finish().unwrap(); let mut tx = psbt.extract_tx().expect("failed to extract tx"); @@ -2261,7 +2261,7 @@ fn test_bump_fee_absolute_force_add_input() { .assume_checked(); let mut builder = wallet.build_tx().coin_selection(LargestFirstCoinSelection); builder - .add_recipient(addr.script_pubkey(), 45_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(45_000)) .enable_rbf(); let psbt = builder.finish().unwrap(); let mut tx = psbt.extract_tx().expect("failed to extract tx"); @@ -2411,7 +2411,7 @@ fn test_fee_amount_negative_drain_val() { let mut builder = wallet.build_tx(); builder - .add_recipient(send_to.script_pubkey(), 8630) + .add_recipient(send_to.script_pubkey(), Amount::from_sat(8630)) .add_utxo(incoming_op) .unwrap() .enable_rbf() @@ -2525,7 +2525,7 @@ fn test_include_output_redeem_witness_script() { .assume_checked(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 45_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(45_000)) .include_output_redeem_witness_script(); let psbt = builder.finish().unwrap(); @@ -2544,7 +2544,7 @@ fn test_signing_only_one_of_multiple_inputs() { .assume_checked(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 45_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(45_000)) .include_output_redeem_witness_script(); let mut psbt = builder.finish().unwrap(); @@ -2889,7 +2889,7 @@ fn test_sending_to_bip350_bech32m_address() { .unwrap() .assume_checked(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 45_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(45_000)); builder.finish().unwrap(); } @@ -3022,7 +3022,7 @@ fn test_taproot_psbt_populate_tap_key_origins() { let addr = wallet.reveal_next_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let psbt = builder.finish().unwrap(); assert_eq!( @@ -3062,7 +3062,7 @@ fn test_taproot_psbt_populate_tap_key_origins_repeated_key() { let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), 25_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)) .policy_path(path, KeychainKind::External); let psbt = builder.finish().unwrap(); @@ -3246,7 +3246,7 @@ fn test_taproot_foreign_utxo() { let mut builder = wallet1.build_tx(); builder - .add_recipient(addr.script_pubkey(), 60_000) + .add_recipient(addr.script_pubkey(), Amount::from_sat(60_000)) .add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction) .unwrap(); let psbt = builder.finish().unwrap(); @@ -3274,7 +3274,7 @@ fn test_spend_from_wallet(mut wallet: Wallet) { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let mut psbt = builder.finish().unwrap(); assert!( @@ -3298,7 +3298,7 @@ fn test_taproot_no_key_spend() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let mut psbt = builder.finish().unwrap(); assert!( @@ -3333,7 +3333,7 @@ fn test_taproot_script_spend_sign_all_leaves() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let mut psbt = builder.finish().unwrap(); assert!( @@ -3364,7 +3364,7 @@ fn test_taproot_script_spend_sign_include_some_leaves() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let mut psbt = builder.finish().unwrap(); let mut script_leaves: Vec<_> = psbt.inputs[0] .tap_scripts @@ -3404,7 +3404,7 @@ fn test_taproot_script_spend_sign_exclude_some_leaves() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let mut psbt = builder.finish().unwrap(); let mut script_leaves: Vec<_> = psbt.inputs[0] .tap_scripts @@ -3442,7 +3442,7 @@ fn test_taproot_script_spend_sign_no_leaves() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let mut psbt = builder.finish().unwrap(); wallet @@ -3465,7 +3465,7 @@ fn test_taproot_sign_derive_index_from_psbt() { let addr = wallet.next_unused_address(KeychainKind::External).unwrap(); let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 25_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(25_000)); let mut psbt = builder.finish().unwrap(); // re-create the wallet with an empty db @@ -3625,7 +3625,7 @@ fn test_spend_coinbase() { .assume_checked(); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), balance.immature.to_sat() / 2) + .add_recipient(addr.script_pubkey(), balance.immature / 2) .current_height(confirmation_height); assert!(matches!( builder.finish(), @@ -3640,7 +3640,7 @@ fn test_spend_coinbase() { // Still unspendable... let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), balance.immature.to_sat() / 2) + .add_recipient(addr.script_pubkey(), balance.immature / 2) .current_height(not_yet_mature_time); assert_matches!( builder.finish(), @@ -3670,7 +3670,7 @@ fn test_spend_coinbase() { ); let mut builder = wallet.build_tx(); builder - .add_recipient(addr.script_pubkey(), balance.confirmed.to_sat() / 2) + .add_recipient(addr.script_pubkey(), balance.confirmed / 2) .current_height(maturity_time); builder.finish().unwrap(); } @@ -3683,7 +3683,7 @@ fn test_allow_dust_limit() { let mut builder = wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 0); + builder.add_recipient(addr.script_pubkey(), Amount::ZERO); assert_matches!( builder.finish(), @@ -3694,7 +3694,7 @@ fn test_allow_dust_limit() { builder .allow_dust(true) - .add_recipient(addr.script_pubkey(), 0); + .add_recipient(addr.script_pubkey(), Amount::ZERO); assert!(builder.finish().is_ok()); } @@ -3822,7 +3822,7 @@ fn test_tx_cancellation() { .unwrap() .assume_checked(); let mut builder = $wallet.build_tx(); - builder.add_recipient(addr.script_pubkey(), 10_000); + builder.add_recipient(addr.script_pubkey(), Amount::from_sat(10_000)); let psbt = builder.finish().unwrap(); diff --git a/crates/chain/src/keychain.rs b/crates/chain/src/keychain.rs index fa3dbf7451..240d944ef2 100644 --- a/crates/chain/src/keychain.rs +++ b/crates/chain/src/keychain.rs @@ -14,6 +14,7 @@ use crate::{collections::BTreeMap, Append}; #[cfg(feature = "miniscript")] mod txout_index; +use bitcoin::Amount; #[cfg(feature = "miniscript")] pub use txout_index::*; @@ -90,13 +91,13 @@ impl AsRef> for ChangeSet { )] pub struct Balance { /// All coinbase outputs not yet matured - pub immature: bitcoin::Amount, + pub immature: Amount, /// Unconfirmed UTXOs generated by a wallet tx - pub trusted_pending: bitcoin::Amount, + pub trusted_pending: Amount, /// Unconfirmed UTXOs received from an external wallet - pub untrusted_pending: bitcoin::Amount, + pub untrusted_pending: Amount, /// Confirmed and immediately spendable balance - pub confirmed: bitcoin::Amount, + pub confirmed: Amount, } impl Balance { @@ -104,12 +105,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) -> bitcoin::Amount { + pub fn trusted_spendable(&self) -> Amount { self.confirmed + self.trusted_pending } /// Get the whole balance visible to the wallet. - pub fn total(&self) -> bitcoin::Amount { + pub fn total(&self) -> 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 a3a9e1beba..cf51485547 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -95,7 +95,7 @@ use crate::{ use alloc::collections::vec_deque::VecDeque; use alloc::sync::Arc; use alloc::vec::Vec; -use bitcoin::{OutPoint, Script, Transaction, TxOut, Txid}; +use bitcoin::{Amount, OutPoint, Script, Transaction, TxOut, Txid}; use core::fmt::{self, Formatter}; use core::{ convert::Infallible, @@ -1155,15 +1155,14 @@ impl TxGraph { outpoints: impl IntoIterator, mut trust_predicate: impl FnMut(&OI, &Script) -> bool, ) -> Result { - 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; + let mut immature = Amount::ZERO; + let mut trusted_pending = Amount::ZERO; + let mut untrusted_pending = Amount::ZERO; + let mut confirmed = 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) { diff --git a/example-crates/wallet_electrum/src/main.rs b/example-crates/wallet_electrum/src/main.rs index c71592f592..e2c5fd9fdc 100644 --- a/example-crates/wallet_electrum/src/main.rs +++ b/example-crates/wallet_electrum/src/main.rs @@ -1,5 +1,5 @@ const DB_MAGIC: &str = "bdk_wallet_electrum_example"; -const SEND_AMOUNT: u64 = 5000; +const SEND_AMOUNT: Amount = Amount::from_sat(5000); const STOP_GAP: usize = 50; const BATCH_SIZE: usize = 5; @@ -81,8 +81,7 @@ fn main() -> Result<(), anyhow::Error> { let balance = wallet.get_balance(); println!("Wallet balance after syncing: {} sats", balance.total()); - // TODO: (@leonardo) Should we format here, or update on constant and TxBuilder::add_recipient() instead ? - if balance.total() < Amount::from_sat(SEND_AMOUNT) { + if balance.total() < 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 42dd6ae374..7664ec32e7 100644 --- a/example-crates/wallet_esplora_async/src/main.rs +++ b/example-crates/wallet_esplora_async/src/main.rs @@ -8,7 +8,7 @@ use bdk_esplora::{esplora_client, EsploraAsyncExt}; use bdk_file_store::Store; const DB_MAGIC: &str = "bdk_wallet_esplora_async_example"; -const SEND_AMOUNT: u64 = 5000; +const SEND_AMOUNT: Amount = Amount::from_sat(5000); const STOP_GAP: usize = 50; const PARALLEL_REQUESTS: usize = 5; @@ -81,8 +81,7 @@ async fn main() -> Result<(), anyhow::Error> { let balance = wallet.get_balance(); println!("Wallet balance after syncing: {} sats", balance.total()); - // TODO: (@leonardo) Should we format here, or update on constant and TxBuilder::add_recipient() instead ? - if balance.total() < Amount::from_sat(SEND_AMOUNT) { + if balance.total() < 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 95acf5de53..4d713156aa 100644 --- a/example-crates/wallet_esplora_blocking/src/main.rs +++ b/example-crates/wallet_esplora_blocking/src/main.rs @@ -1,5 +1,5 @@ const DB_MAGIC: &str = "bdk_wallet_esplora_example"; -const SEND_AMOUNT: u64 = 1000; +const SEND_AMOUNT: Amount = Amount::from_sat(1000); const STOP_GAP: usize = 5; const PARALLEL_REQUESTS: usize = 1; @@ -57,8 +57,7 @@ fn main() -> Result<(), anyhow::Error> { let balance = wallet.get_balance(); println!("Wallet balance after syncing: {} sats", balance.total()); - // TODO: (@leonardo) Should we format here, or update on constant and TxBuilder::add_recipient() instead ? - if balance.total() < Amount::from_sat(SEND_AMOUNT) { + if balance.total() < SEND_AMOUNT { println!( "Please send at least {} sats to the receiving address", SEND_AMOUNT