Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TxBuilder APIs #611

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,8 @@ interface TxBuilder {

TxBuilder add_data(sequence<u8> data);

TxBuilder current_height(u32 height);

[Throws=CreateTxError]
Psbt finish([ByRef] Wallet wallet);
};
Expand All @@ -721,6 +723,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()
})
}

Comment on lines +216 to +222
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewing commit by commit, this looks good, I wrote a test for it and it passed ✅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You sir are building good tests. Do you want to maybe add them as a commit to this PR? I'd merge that for sure.

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