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,54 @@ 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 (
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 > {
348
405
if self . add_global_xpubs {
349
406
tx_builder. add_global_xpubs ( ) ;
350
407
}
@@ -401,10 +458,7 @@ impl TxBuilder {
401
458
if let Some ( version) = self . version {
402
459
tx_builder. version ( version) ;
403
460
}
404
-
405
- let psbt = tx_builder. finish ( ) . map_err ( CreateTxError :: from) ?;
406
-
407
- Ok ( Arc :: new ( psbt. into ( ) ) )
461
+ Ok ( ( ) )
408
462
}
409
463
}
410
464
0 commit comments