1
1
use crate :: bitcoin:: { Amount , FeeRate , OutPoint , Psbt , Script , Txid } ;
2
2
use crate :: error:: CreateTxError ;
3
- use crate :: types:: { LockTime , ScriptAmount } ;
3
+ use crate :: types:: { CoinSelectionAlgorithm , LockTime , ScriptAmount } ;
4
4
use crate :: wallet:: Wallet ;
5
5
6
6
use bdk_wallet:: bitcoin:: absolute:: LockTime as BdkLockTime ;
@@ -9,7 +9,7 @@ use bdk_wallet::bitcoin::script::PushBytesBuf;
9
9
use bdk_wallet:: bitcoin:: Psbt as BdkPsbt ;
10
10
use bdk_wallet:: bitcoin:: ScriptBuf as BdkScriptBuf ;
11
11
use bdk_wallet:: bitcoin:: { OutPoint as BdkOutPoint , Sequence } ;
12
- use bdk_wallet:: KeychainKind ;
12
+ use bdk_wallet:: { coin_selection , KeychainKind } ;
13
13
14
14
use std:: collections:: BTreeMap ;
15
15
use std:: collections:: HashMap ;
@@ -40,6 +40,7 @@ pub struct TxBuilder {
40
40
locktime : Option < LockTime > ,
41
41
allow_dust : bool ,
42
42
version : Option < i32 > ,
43
+ coin_selection : Option < CoinSelectionAlgorithm > ,
43
44
}
44
45
45
46
#[ uniffi:: export]
@@ -65,6 +66,7 @@ impl TxBuilder {
65
66
locktime : None ,
66
67
allow_dust : false ,
67
68
version : None ,
69
+ coin_selection : None ,
68
70
}
69
71
}
70
72
@@ -333,6 +335,14 @@ impl TxBuilder {
333
335
} )
334
336
}
335
337
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
+
336
346
/// Finish building the transaction.
337
347
///
338
348
/// Uses the thread-local random number generator (rng).
@@ -344,7 +354,42 @@ impl TxBuilder {
344
354
pub fn finish ( & self , wallet : & Arc < Wallet > ) -> Result < Arc < Psbt > , CreateTxError > {
345
355
// TODO: I had to change the wallet here to be mutable. Why is that now required with the 1.0 API?
346
356
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 > {
348
393
if self . add_global_xpubs {
349
394
tx_builder. add_global_xpubs ( ) ;
350
395
}
@@ -401,10 +446,7 @@ impl TxBuilder {
401
446
if let Some ( version) = self . version {
402
447
tx_builder. version ( version) ;
403
448
}
404
-
405
- let psbt = tx_builder. finish ( ) . map_err ( CreateTxError :: from) ?;
406
-
407
- Ok ( Arc :: new ( psbt. into ( ) ) )
449
+ Ok ( ( ) )
408
450
}
409
451
}
410
452
0 commit comments