Skip to content

Commit 22aa534

Browse files
committed
feat: use Amount on TxBuilder::add_recipient
1 parent d5c0e72 commit 22aa534

File tree

9 files changed

+102
-102
lines changed

9 files changed

+102
-102
lines changed

crates/bdk/src/wallet/coin_selection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
//! .unwrap();
9393
//! let psbt = {
9494
//! let mut builder = wallet.build_tx().coin_selection(AlwaysSpendEverything);
95-
//! builder.add_recipient(to_address.script_pubkey(), 50_000);
95+
//! builder.add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000));
9696
//! builder.finish()?
9797
//! };
9898
//!

crates/bdk/src/wallet/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ impl Wallet {
11971197
/// let psbt = {
11981198
/// let mut builder = wallet.build_tx();
11991199
/// builder
1200-
/// .add_recipient(to_address.script_pubkey(), 50_000);
1200+
/// .add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000));
12011201
/// builder.finish()?
12021202
/// };
12031203
///
@@ -1579,7 +1579,7 @@ impl Wallet {
15791579
/// let mut psbt = {
15801580
/// let mut builder = wallet.build_tx();
15811581
/// builder
1582-
/// .add_recipient(to_address.script_pubkey(), 50_000)
1582+
/// .add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000))
15831583
/// .enable_rbf();
15841584
/// builder.finish()?
15851585
/// };
@@ -1752,7 +1752,7 @@ impl Wallet {
17521752
/// # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
17531753
/// let mut psbt = {
17541754
/// let mut builder = wallet.build_tx();
1755-
/// builder.add_recipient(to_address.script_pubkey(), 50_000);
1755+
/// builder.add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000));
17561756
/// builder.finish()?
17571757
/// };
17581758
/// let finalized = wallet.sign(&mut psbt, SignOptions::default())?;

crates/bdk/src/wallet/tx_builder.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
//!
3030
//! tx_builder
3131
//! // Create a transaction with one output to `to_address` of 50_000 satoshi
32-
//! .add_recipient(to_address.script_pubkey(), 50_000)
32+
//! .add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000))
3333
//! // With a custom fee rate of 5.0 satoshi/vbyte
3434
//! .fee_rate(FeeRate::from_sat_per_vb(5).expect("valid feerate"))
3535
//! // Only spend non-change outputs
@@ -47,7 +47,7 @@ use core::marker::PhantomData;
4747

4848
use bitcoin::psbt::{self, Psbt};
4949
use bitcoin::script::PushBytes;
50-
use bitcoin::{absolute, FeeRate, OutPoint, ScriptBuf, Sequence, Transaction, Txid};
50+
use bitcoin::{absolute, Amount, FeeRate, OutPoint, ScriptBuf, Sequence, Transaction, Txid};
5151

5252
use super::coin_selection::{CoinSelectionAlgorithm, DefaultCoinSelectionAlgorithm};
5353
use super::{CreateTxError, Wallet};
@@ -94,8 +94,8 @@ impl TxBuilderContext for BumpFee {}
9494
/// let mut builder = wallet.build_tx();
9595
/// builder
9696
/// .ordering(TxOrdering::Untouched)
97-
/// .add_recipient(addr1.script_pubkey(), 50_000)
98-
/// .add_recipient(addr2.script_pubkey(), 50_000);
97+
/// .add_recipient(addr1.script_pubkey(), Amount::from_sat(50_000))
98+
/// .add_recipient(addr2.script_pubkey(), Amount::from_sat(50_000));
9999
/// builder.finish()?
100100
/// };
101101
///
@@ -104,7 +104,7 @@ impl TxBuilderContext for BumpFee {}
104104
/// let mut builder = wallet.build_tx();
105105
/// builder.ordering(TxOrdering::Untouched);
106106
/// for addr in &[addr1, addr2] {
107-
/// builder.add_recipient(addr.script_pubkey(), 50_000);
107+
/// builder.add_recipient(addr.script_pubkey(), Amount::from_sat(50_000));
108108
/// }
109109
/// builder.finish()?
110110
/// };
@@ -274,7 +274,7 @@ impl<'a, Cs, Ctx> TxBuilder<'a, Cs, Ctx> {
274274
///
275275
/// let builder = wallet
276276
/// .build_tx()
277-
/// .add_recipient(to_address.script_pubkey(), 50_000)
277+
/// .add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000))
278278
/// .policy_path(path, KeychainKind::External);
279279
///
280280
/// # Ok::<(), anyhow::Error>(())
@@ -713,22 +713,26 @@ impl std::error::Error for AllowShrinkingError {}
713713

714714
impl<'a, Cs: CoinSelectionAlgorithm> TxBuilder<'a, Cs, CreateTx> {
715715
/// Replace the recipients already added with a new list
716-
pub fn set_recipients(&mut self, recipients: Vec<(ScriptBuf, u64)>) -> &mut Self {
717-
self.params.recipients = recipients;
716+
pub fn set_recipients(&mut self, recipients: Vec<(ScriptBuf, Amount)>) -> &mut Self {
717+
self.params.recipients = recipients
718+
.into_iter()
719+
.map(|(script, amount)| (script, amount.to_sat()))
720+
.collect();
718721
self
719722
}
720723

721-
// TODO: (@leonardo) Should this expect/use `bitcoin::Amount` instead ? Would it be a huge breaking change ?
722724
/// Add a recipient to the internal list
723-
pub fn add_recipient(&mut self, script_pubkey: ScriptBuf, amount: u64) -> &mut Self {
724-
self.params.recipients.push((script_pubkey, amount));
725+
pub fn add_recipient(&mut self, script_pubkey: ScriptBuf, amount: Amount) -> &mut Self {
726+
self.params
727+
.recipients
728+
.push((script_pubkey, amount.to_sat()));
725729
self
726730
}
727731

728732
/// Add data as an output, using OP_RETURN
729733
pub fn add_data<T: AsRef<PushBytes>>(&mut self, data: &T) -> &mut Self {
730734
let script = ScriptBuf::new_op_return(data);
731-
self.add_recipient(script, 0u64);
735+
self.add_recipient(script, Amount::ZERO);
732736
self
733737
}
734738

crates/bdk/tests/psbt.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bdk::bitcoin::{FeeRate, Psbt, TxIn};
1+
use bdk::bitcoin::{Amount, FeeRate, Psbt, TxIn};
22
use bdk::{psbt, KeychainKind, SignOptions};
33
use core::str::FromStr;
44
mod common;
@@ -14,7 +14,7 @@ fn test_psbt_malformed_psbt_input_legacy() {
1414
let (mut wallet, _) = get_funded_wallet(get_test_wpkh());
1515
let send_to = wallet.peek_address(KeychainKind::External, 0);
1616
let mut builder = wallet.build_tx();
17-
builder.add_recipient(send_to.script_pubkey(), 10_000);
17+
builder.add_recipient(send_to.script_pubkey(), Amount::from_sat(10_000));
1818
let mut psbt = builder.finish().unwrap();
1919
psbt.inputs.push(psbt_bip.inputs[0].clone());
2020
let options = SignOptions {
@@ -31,7 +31,7 @@ fn test_psbt_malformed_psbt_input_segwit() {
3131
let (mut wallet, _) = get_funded_wallet(get_test_wpkh());
3232
let send_to = wallet.peek_address(KeychainKind::External, 0);
3333
let mut builder = wallet.build_tx();
34-
builder.add_recipient(send_to.script_pubkey(), 10_000);
34+
builder.add_recipient(send_to.script_pubkey(), Amount::from_sat(10_000));
3535
let mut psbt = builder.finish().unwrap();
3636
psbt.inputs.push(psbt_bip.inputs[1].clone());
3737
let options = SignOptions {
@@ -47,7 +47,7 @@ fn test_psbt_malformed_tx_input() {
4747
let (mut wallet, _) = get_funded_wallet(get_test_wpkh());
4848
let send_to = wallet.peek_address(KeychainKind::External, 0);
4949
let mut builder = wallet.build_tx();
50-
builder.add_recipient(send_to.script_pubkey(), 10_000);
50+
builder.add_recipient(send_to.script_pubkey(), Amount::from_sat(10_000));
5151
let mut psbt = builder.finish().unwrap();
5252
psbt.unsigned_tx.input.push(TxIn::default());
5353
let options = SignOptions {
@@ -63,7 +63,7 @@ fn test_psbt_sign_with_finalized() {
6363
let (mut wallet, _) = get_funded_wallet(get_test_wpkh());
6464
let send_to = wallet.peek_address(KeychainKind::External, 0);
6565
let mut builder = wallet.build_tx();
66-
builder.add_recipient(send_to.script_pubkey(), 10_000);
66+
builder.add_recipient(send_to.script_pubkey(), Amount::from_sat(10_000));
6767
let mut psbt = builder.finish().unwrap();
6868

6969
// add a finalized input

0 commit comments

Comments
 (0)