11use crate :: bitcoin:: { Amount , FeeRate , OutPoint , Psbt , Script , Txid } ;
22use crate :: error:: CreateTxError ;
3- use crate :: types:: { LockTime , ScriptAmount } ;
3+ use crate :: types:: { CoinSelectionAlgorithm , LockTime , ScriptAmount } ;
44use crate :: wallet:: Wallet ;
55
66use bdk_wallet:: bitcoin:: absolute:: LockTime as BdkLockTime ;
@@ -9,7 +9,7 @@ use bdk_wallet::bitcoin::script::PushBytesBuf;
99use bdk_wallet:: bitcoin:: Psbt as BdkPsbt ;
1010use bdk_wallet:: bitcoin:: ScriptBuf as BdkScriptBuf ;
1111use bdk_wallet:: bitcoin:: { OutPoint as BdkOutPoint , Sequence } ;
12- use bdk_wallet:: KeychainKind ;
12+ use bdk_wallet:: { coin_selection , KeychainKind } ;
1313
1414use std:: collections:: BTreeMap ;
1515use 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,42 @@ 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 ( coin_selection:: BranchAndBoundCoinSelection :: < coin_selection:: SingleRandomDraw > :: default ( ) ) ;
361+ self . apply_config ( & mut tx_builder) ?;
362+ tx_builder. finish ( ) . map_err ( CreateTxError :: from) ?
363+ } ,
364+ CoinSelectionAlgorithm :: SingleRandomDraw => {
365+ let mut tx_builder = wallet. build_tx ( ) . coin_selection ( coin_selection:: SingleRandomDraw ) ;
366+ self . apply_config ( & mut tx_builder) ?;
367+ tx_builder. finish ( ) . map_err ( CreateTxError :: from) ?
368+ } ,
369+ CoinSelectionAlgorithm :: OldestFirstCoinSelection => {
370+ let mut tx_builder = wallet. build_tx ( ) . coin_selection ( coin_selection:: OldestFirstCoinSelection ) ;
371+ self . apply_config ( & mut tx_builder) ?;
372+ tx_builder. finish ( ) . map_err ( CreateTxError :: from) ?
373+ } ,
374+ CoinSelectionAlgorithm :: LargestFirstCoinSelection => {
375+ let mut tx_builder = wallet. build_tx ( ) . coin_selection ( coin_selection:: LargestFirstCoinSelection ) ;
376+ self . apply_config ( & mut tx_builder) ?;
377+ tx_builder. finish ( ) . map_err ( CreateTxError :: from) ?
378+ } ,
379+ }
380+ } else {
381+ let mut tx_builder = wallet. build_tx ( ) ;
382+ self . apply_config ( & mut tx_builder) ?;
383+ tx_builder. finish ( ) . map_err ( CreateTxError :: from) ?
384+ } ;
385+
386+ Ok ( Arc :: new ( psbt. into ( ) ) )
387+ }
388+
389+ }
390+
391+ impl TxBuilder {
392+ fn apply_config < C > ( & self , tx_builder : & mut bdk_wallet:: TxBuilder < ' _ , C > ) -> Result < ( ) , CreateTxError > {
348393 if self . add_global_xpubs {
349394 tx_builder. add_global_xpubs ( ) ;
350395 }
@@ -401,10 +446,7 @@ impl TxBuilder {
401446 if let Some ( version) = self . version {
402447 tx_builder. version ( version) ;
403448 }
404-
405- let psbt = tx_builder. finish ( ) . map_err ( CreateTxError :: from) ?;
406-
407- Ok ( Arc :: new ( psbt. into ( ) ) )
449+ Ok ( ( ) )
408450 }
409451}
410452
0 commit comments