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,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
0 commit comments