Skip to content

Commit 053fd4f

Browse files
temp 1: add coin selection to tx_builder
1 parent 5ef6134 commit 053fd4f

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

bdk-ffi/src/tx_builder.rs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::bitcoin::{Amount, FeeRate, OutPoint, Psbt, Script, Txid};
22
use crate::error::CreateTxError;
3-
use crate::types::{LockTime, ScriptAmount};
3+
use crate::types::{CoinSelectionAlgorithm, LockTime, ScriptAmount};
44
use crate::wallet::Wallet;
55

66
use bdk_wallet::bitcoin::absolute::LockTime as BdkLockTime;
@@ -9,7 +9,7 @@ use bdk_wallet::bitcoin::script::PushBytesBuf;
99
use bdk_wallet::bitcoin::Psbt as BdkPsbt;
1010
use bdk_wallet::bitcoin::ScriptBuf as BdkScriptBuf;
1111
use bdk_wallet::bitcoin::{OutPoint as BdkOutPoint, Sequence};
12-
use bdk_wallet::KeychainKind;
12+
use bdk_wallet::{coin_selection, KeychainKind};
1313

1414
use std::collections::BTreeMap;
1515
use std::collections::HashMap;
@@ -40,6 +40,7 @@ pub struct TxBuilder {
4040
locktime: Option<LockTime>,
4141
allow_dust: bool,
4242
version: Option<i32>,
43+
coin_selection: Option<CoinSelectionAlgorithm>,
4344
}
4445

4546
#[uniffi::export]
@@ -65,6 +66,7 @@ impl TxBuilder {
6566
locktime: None,
6667
allow_dust: false,
6768
version: None,
69+
coin_selection: None,
6870
}
6971
}
7072

@@ -333,6 +335,14 @@ impl TxBuilder {
333335
})
334336
}
335337

338+
/// Choose the coin selection algorithm
339+
pub fn coin_selection(&self, coin_selection: CoinSelectionAlgorithm) -> Arc<Self> {
340+
Arc::new(TxBuilder {
341+
coin_selection: Some(coin_selection),
342+
..self.clone()
343+
})
344+
}
345+
336346
/// Finish building the transaction.
337347
///
338348
/// Uses the thread-local random number generator (rng).
@@ -344,7 +354,54 @@ impl TxBuilder {
344354
pub fn finish(&self, wallet: &Arc<Wallet>) -> Result<Arc<Psbt>, CreateTxError> {
345355
// TODO: I had to change the wallet here to be mutable. Why is that now required with the 1.0 API?
346356
let mut wallet = wallet.get_wallet();
347-
let mut tx_builder = wallet.build_tx();
357+
let psbt = if let Some(coin_selection) = &self.coin_selection {
358+
match coin_selection {
359+
CoinSelectionAlgorithm::BranchAndBoundCoinSelection => {
360+
let mut tx_builder = wallet.build_tx().coin_selection(
361+
coin_selection::BranchAndBoundCoinSelection::<
362+
coin_selection::SingleRandomDraw,
363+
>::default(),
364+
);
365+
self.apply_config(&mut tx_builder)?;
366+
tx_builder.finish().map_err(CreateTxError::from)?
367+
}
368+
CoinSelectionAlgorithm::SingleRandomDraw => {
369+
let mut tx_builder = wallet
370+
.build_tx()
371+
.coin_selection(coin_selection::SingleRandomDraw);
372+
self.apply_config(&mut tx_builder)?;
373+
tx_builder.finish().map_err(CreateTxError::from)?
374+
}
375+
CoinSelectionAlgorithm::OldestFirstCoinSelection => {
376+
let mut tx_builder = wallet
377+
.build_tx()
378+
.coin_selection(coin_selection::OldestFirstCoinSelection);
379+
self.apply_config(&mut tx_builder)?;
380+
tx_builder.finish().map_err(CreateTxError::from)?
381+
}
382+
CoinSelectionAlgorithm::LargestFirstCoinSelection => {
383+
let mut tx_builder = wallet
384+
.build_tx()
385+
.coin_selection(coin_selection::LargestFirstCoinSelection);
386+
self.apply_config(&mut tx_builder)?;
387+
tx_builder.finish().map_err(CreateTxError::from)?
388+
}
389+
}
390+
} else {
391+
let mut tx_builder = wallet.build_tx();
392+
self.apply_config(&mut tx_builder)?;
393+
tx_builder.finish().map_err(CreateTxError::from)?
394+
};
395+
396+
Ok(Arc::new(psbt.into()))
397+
}
398+
}
399+
400+
impl TxBuilder {
401+
fn apply_config<C>(
402+
&self,
403+
tx_builder: &mut bdk_wallet::TxBuilder<'_, C>,
404+
) -> Result<(), CreateTxError> {
348405
if self.add_global_xpubs {
349406
tx_builder.add_global_xpubs();
350407
}
@@ -401,10 +458,7 @@ impl TxBuilder {
401458
if let Some(version) = self.version {
402459
tx_builder.version(version);
403460
}
404-
405-
let psbt = tx_builder.finish().map_err(CreateTxError::from)?;
406-
407-
Ok(Arc::new(psbt.into()))
461+
Ok(())
408462
}
409463
}
410464

bdk-ffi/src/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,3 +1175,11 @@ impl From<bdk_wallet::TxDetails> for TxDetails {
11751175
}
11761176
}
11771177
}
1178+
1179+
#[derive(uniffi::Enum, Clone)]
1180+
pub enum CoinSelectionAlgorithm {
1181+
BranchAndBoundCoinSelection,
1182+
SingleRandomDraw,
1183+
OldestFirstCoinSelection,
1184+
LargestFirstCoinSelection,
1185+
}

0 commit comments

Comments
 (0)