Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cont_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
persist-credentials: false

- name: "Set default toolchain"
run: rustup default nightly
run: rustup default 1.84.1

- name: "Set profile"
run: rustup set profile minimal
Expand Down
68 changes: 61 additions & 7 deletions bdk-ffi/src/tx_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::bitcoin::{Amount, FeeRate, OutPoint, Psbt, Script, Txid};
use crate::error::CreateTxError;
use crate::types::{LockTime, ScriptAmount};
use crate::types::{CoinSelectionAlgorithm, LockTime, ScriptAmount};
use crate::wallet::Wallet;

use bdk_wallet::bitcoin::absolute::LockTime as BdkLockTime;
Expand All @@ -9,7 +9,7 @@ use bdk_wallet::bitcoin::script::PushBytesBuf;
use bdk_wallet::bitcoin::Psbt as BdkPsbt;
use bdk_wallet::bitcoin::ScriptBuf as BdkScriptBuf;
use bdk_wallet::bitcoin::{OutPoint as BdkOutPoint, Sequence};
use bdk_wallet::KeychainKind;
use bdk_wallet::{coin_selection, KeychainKind};

use std::collections::BTreeMap;
use std::collections::HashMap;
Expand Down Expand Up @@ -40,6 +40,7 @@ pub struct TxBuilder {
locktime: Option<LockTime>,
allow_dust: bool,
version: Option<i32>,
coin_selection: Option<CoinSelectionAlgorithm>,
}

#[uniffi::export]
Expand All @@ -65,6 +66,7 @@ impl TxBuilder {
locktime: None,
allow_dust: false,
version: None,
coin_selection: None,
}
}

Expand Down Expand Up @@ -333,6 +335,14 @@ impl TxBuilder {
})
}

/// Choose the coin selection algorithm
pub fn coin_selection(&self, coin_selection: CoinSelectionAlgorithm) -> Arc<Self> {
Arc::new(TxBuilder {
coin_selection: Some(coin_selection),
..self.clone()
})
}

/// Finish building the transaction.
///
/// Uses the thread-local random number generator (rng).
Expand All @@ -344,7 +354,54 @@ impl TxBuilder {
pub fn finish(&self, wallet: &Arc<Wallet>) -> Result<Arc<Psbt>, CreateTxError> {
// TODO: I had to change the wallet here to be mutable. Why is that now required with the 1.0 API?
let mut wallet = wallet.get_wallet();
let mut tx_builder = wallet.build_tx();
let psbt = if let Some(coin_selection) = &self.coin_selection {
match coin_selection {
CoinSelectionAlgorithm::BranchAndBoundCoinSelection => {
let mut tx_builder = wallet.build_tx().coin_selection(
coin_selection::BranchAndBoundCoinSelection::<
coin_selection::SingleRandomDraw,
>::default(),
);
self.apply_config(&mut tx_builder)?;
tx_builder.finish().map_err(CreateTxError::from)?
}
CoinSelectionAlgorithm::SingleRandomDraw => {
let mut tx_builder = wallet
.build_tx()
.coin_selection(coin_selection::SingleRandomDraw);
self.apply_config(&mut tx_builder)?;
tx_builder.finish().map_err(CreateTxError::from)?
}
CoinSelectionAlgorithm::OldestFirstCoinSelection => {
let mut tx_builder = wallet
.build_tx()
.coin_selection(coin_selection::OldestFirstCoinSelection);
self.apply_config(&mut tx_builder)?;
tx_builder.finish().map_err(CreateTxError::from)?
}
CoinSelectionAlgorithm::LargestFirstCoinSelection => {
let mut tx_builder = wallet
.build_tx()
.coin_selection(coin_selection::LargestFirstCoinSelection);
self.apply_config(&mut tx_builder)?;
tx_builder.finish().map_err(CreateTxError::from)?
}
}
} else {
let mut tx_builder = wallet.build_tx();
self.apply_config(&mut tx_builder)?;
tx_builder.finish().map_err(CreateTxError::from)?
};

Ok(Arc::new(psbt.into()))
}
}

impl TxBuilder {
fn apply_config<C>(
&self,
tx_builder: &mut bdk_wallet::TxBuilder<'_, C>,
) -> Result<(), CreateTxError> {
if self.add_global_xpubs {
tx_builder.add_global_xpubs();
}
Expand Down Expand Up @@ -401,10 +458,7 @@ impl TxBuilder {
if let Some(version) = self.version {
tx_builder.version(version);
}

let psbt = tx_builder.finish().map_err(CreateTxError::from)?;

Ok(Arc::new(psbt.into()))
Ok(())
}
}

Expand Down
8 changes: 8 additions & 0 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,3 +1175,11 @@ impl From<bdk_wallet::TxDetails> for TxDetails {
}
}
}

#[derive(uniffi::Enum, Clone)]
pub enum CoinSelectionAlgorithm {
BranchAndBoundCoinSelection,
SingleRandomDraw,
OldestFirstCoinSelection,
LargestFirstCoinSelection,
}
Loading