Skip to content

Commit 8c45a12

Browse files
committed
feat: Add exclude_unconfirmed to tx_builder
1 parent 58c425b commit 8c45a12

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

bdk-ffi/src/tx_builder.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ pub struct TxBuilder {
4040
locktime: Option<LockTime>,
4141
allow_dust: bool,
4242
version: Option<i32>,
43+
exclude_unconfirmed: bool,
44+
exclude_below_confirmations: Option<u32>,
4345
}
4446

4547
#[allow(clippy::new_without_default)]
@@ -66,6 +68,8 @@ impl TxBuilder {
6668
locktime: None,
6769
allow_dust: false,
6870
version: None,
71+
exclude_unconfirmed: false,
72+
exclude_below_confirmations: None,
6973
}
7074
}
7175

@@ -81,6 +85,34 @@ impl TxBuilder {
8185
})
8286
}
8387

88+
/// Exclude outpoints whose enclosing transaction is unconfirmed.
89+
/// This is a shorthand for exclude_below_confirmations(1).
90+
pub fn exclude_unconfirmed(&self) -> Arc<Self> {
91+
Arc::new(TxBuilder {
92+
exclude_unconfirmed: true,
93+
..self.clone()
94+
})
95+
}
96+
97+
/// Excludes any outpoints whose enclosing transaction has fewer than `min_confirms`
98+
/// confirmations.
99+
///
100+
/// `min_confirms` is the minimum number of confirmations a transaction must have in order for
101+
/// its outpoints to remain spendable.
102+
/// - Passing `0` will include all transactions (no filtering).
103+
/// - Passing `1` will exclude all unconfirmed transactions (equivalent to
104+
/// `exclude_unconfirmed`).
105+
/// - Passing `6` will only allow outpoints from transactions with at least 6 confirmations.
106+
///
107+
/// If you chain this with other filtering methods, the final set of unspendable outpoints will
108+
/// be the union of all filters.
109+
pub fn exclude_below_confirmations(&self, min_confirms: u32) -> Arc<Self> {
110+
Arc::new(TxBuilder {
111+
exclude_below_confirmations: Some(min_confirms),
112+
..self.clone()
113+
})
114+
}
115+
84116
/// Add a recipient to the internal list of recipients.
85117
pub fn add_recipient(&self, script: &Script, amount: Arc<Amount>) -> Arc<Self> {
86118
let mut recipients: Vec<(BdkScriptBuf, BdkAmount)> = self.recipients.clone();
@@ -402,7 +434,12 @@ impl TxBuilder {
402434
if let Some(version) = self.version {
403435
tx_builder.version(version);
404436
}
405-
437+
if self.exclude_unconfirmed {
438+
tx_builder.exclude_unconfirmed();
439+
}
440+
if let Some(min_confirms) = self.exclude_below_confirmations {
441+
tx_builder.exclude_below_confirmations(min_confirms);
442+
}
406443
let psbt = tx_builder.finish().map_err(CreateTxError::from)?;
407444

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

0 commit comments

Comments
 (0)