Skip to content

Commit

Permalink
feat: expose TxBuilder::current_height
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Nov 21, 2024
1 parent fa4a609 commit 1d8b95d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,8 @@ interface TxBuilder {

TxBuilder add_data(sequence<u8> data);

TxBuilder current_height(u32 height);

[Throws=CreateTxError]
Psbt finish([ByRef] Wallet wallet);
};
Expand All @@ -714,6 +716,8 @@ interface BumpFeeTxBuilder {

BumpFeeTxBuilder set_exact_sequence(u32 nsequence);

BumpFeeTxBuilder current_height(u32 height);

[Throws=CreateTxError]
Psbt finish([ByRef] Wallet wallet);
};
Expand Down
28 changes: 26 additions & 2 deletions bdk-ffi/src/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ use crate::error::CreateTxError;
use crate::types::ScriptAmount;
use crate::wallet::Wallet;

use bdk_wallet::KeychainKind;
use bitcoin_ffi::{Amount, FeeRate, Script};

use bdk_wallet::bitcoin::amount::Amount as BdkAmount;
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, Sequence, Txid};
use bdk_wallet::ChangeSpendPolicy;
use bdk_wallet::KeychainKind;

use std::collections::BTreeMap;
use std::collections::HashMap;
use bdk_wallet::bitcoin::script::PushBytesBuf;
use std::collections::HashSet;
use std::convert::TryFrom;
use std::str::FromStr;
Expand All @@ -36,6 +36,7 @@ pub struct TxBuilder {
pub(crate) drain_to: Option<BdkScriptBuf>,
pub(crate) sequence: Option<u32>,
pub(crate) data: Vec<u8>,
pub(crate) current_height: Option<u32>,
}

impl TxBuilder {
Expand All @@ -55,6 +56,7 @@ impl TxBuilder {
drain_to: None,
sequence: None,
data: Vec::new(),
current_height: None,
}
}

Expand Down Expand Up @@ -204,6 +206,13 @@ impl TxBuilder {
})
}

pub(crate) fn current_height(&self, height: u32) -> Arc<Self> {
Arc::new(TxBuilder {
current_height: Some(height),
..self.clone()
})
}

pub(crate) 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();
Expand Down Expand Up @@ -252,6 +261,9 @@ impl TxBuilder {
let push_bytes = PushBytesBuf::try_from(self.data.clone())?;
tx_builder.add_data(&push_bytes);
}
if let Some(height) = self.current_height {
tx_builder.current_height(height);
}

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

Expand All @@ -264,6 +276,7 @@ pub(crate) struct BumpFeeTxBuilder {
pub(crate) txid: String,
pub(crate) fee_rate: Arc<FeeRate>,
pub(crate) sequence: Option<u32>,
pub(crate) current_height: Option<u32>,
}

impl BumpFeeTxBuilder {
Expand All @@ -272,6 +285,7 @@ impl BumpFeeTxBuilder {
txid,
fee_rate,
sequence: None,
current_height: None,
}
}

Expand All @@ -282,6 +296,13 @@ impl BumpFeeTxBuilder {
})
}

pub(crate) fn current_height(&self, height: u32) -> Arc<Self> {
Arc::new(BumpFeeTxBuilder {
current_height: Some(height),
..self.clone()
})
}

pub(crate) fn finish(&self, wallet: &Arc<Wallet>) -> Result<Arc<Psbt>, CreateTxError> {
let txid = Txid::from_str(self.txid.as_str()).map_err(|_| CreateTxError::UnknownUtxo {
outpoint: self.txid.clone(),
Expand All @@ -292,6 +313,9 @@ impl BumpFeeTxBuilder {
if let Some(sequence) = self.sequence {
tx_builder.set_exact_sequence(Sequence(sequence));
}
if let Some(height) = self.current_height {
tx_builder.current_height(height);
}

let psbt: BdkPsbt = tx_builder.finish()?;

Expand Down

0 comments on commit 1d8b95d

Please sign in to comment.