Skip to content

Commit

Permalink
chore: bump bdk to alpha 8 and use bitcoin::FeeRate
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Apr 5, 2024
1 parent e774c94 commit aa03558
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LiveTxBuilderTest {
val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.TESTNET)
val psbt: PartiallySignedTransaction = TxBuilder()
.addRecipient(recipient.scriptPubkey(), 4200uL)
.feeRate(FeeRate.fromSatPerVb(2.0f))
.feeRate(FeeRate.fromSatPerVb(2uL))
.finish(wallet)

println(psbt.serialize())
Expand Down Expand Up @@ -63,7 +63,7 @@ class LiveTxBuilderTest {

val psbt: PartiallySignedTransaction = TxBuilder()
.setRecipients(allRecipients)
.feeRate(FeeRate.fromSatPerVb(4.0f))
.feeRate(FeeRate.fromSatPerVb(4uL))
.changePolicy(ChangeSpendPolicy.CHANGE_FORBIDDEN)
.enableRbf()
.finish(wallet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class LiveWalletTest {
println("Tx fee is: ${txFee}")

val feeRate: FeeRate = wallet.calculateFeeRate(tx)
println("Tx fee rate is: ${feeRate.asSatPerVb()} sat/vB")
println("Tx fee rate is: ${feeRate.toSatPerVbCeil()} sat/vB")

esploraClient.broadcast(tx)
}
Expand Down
14 changes: 7 additions & 7 deletions bdk-ffi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions bdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bdk-ffi"
version = "1.0.0-alpha.7"
version = "1.0.0-alpha.8"
homepage = "https://bitcoindevkit.org"
repository = "https://github.com/bitcoindevkit/bdk"
edition = "2018"
Expand All @@ -18,10 +18,10 @@ path = "uniffi-bindgen.rs"
default = ["uniffi/cli"]

[dependencies]
bdk = { version = "1.0.0-alpha.7", features = ["all-keys", "keys-bip39"] }
bdk_esplora = { version = "0.9.0", default-features = false, features = ["std", "blocking"] }
# bdk_esplora = { version = "0.9.0", default-features = false, features = ["std", "blocking", "async-https-rustls"] }
bdk_file_store = { version = "0.7.0" }
bdk = { version = "1.0.0-alpha.8", features = ["all-keys", "keys-bip39"] }
bdk_esplora = { version = "0.10.0", default-features = false, features = ["std", "blocking"] }
# bdk_esplora = { version = "0.10.0", default-features = false, features = ["std", "blocking", "async-https-rustls"] }
bdk_file_store = { version = "0.8.0" }

uniffi = { version = "=0.26.1" }
thiserror = "1.0.58"
Expand Down
19 changes: 13 additions & 6 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ interface EsploraError {
HeaderHashNotFound();
};

[Error]
enum FeeRateError {
"ArithmeticOverflow"
};

// ------------------------------------------------------------------------
// bdk crate - types module
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -100,15 +105,17 @@ dictionary TxOut {
// ------------------------------------------------------------------------

interface FeeRate {
[Name=from_sat_per_vb]
constructor(f32 sat_per_vb);
[Name=from_sat_per_vb, Throws=FeeRateError]
constructor(u64 sat_per_vb);

[Name=from_sat_per_kwu]
constructor(f32 sat_per_kwu);
constructor(u64 sat_per_kwu);

u64 to_sat_per_vb_ceil();

f32 as_sat_per_vb();
u64 to_sat_per_vb_floor();

f32 sat_per_kwu();
u64 to_sat_per_kwu();
};

enum ChangeSpendPolicy {
Expand Down Expand Up @@ -189,7 +196,7 @@ interface TxBuilder {
};

interface BumpFeeTxBuilder {
constructor(string txid, f32 fee_rate);
constructor(string txid, FeeRate fee_rate);

BumpFeeTxBuilder allow_shrinking(Script script_pubkey);

Expand Down
6 changes: 6 additions & 0 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ pub enum EsploraError {
HeaderHashNotFound,
}

#[derive(Debug, thiserror::Error)]
pub enum FeeRateError {
#[error("arithmetic overflow on feerate")]
ArithmeticOverflow,
}

impl From<BdkFileError> for WalletCreationError {
fn from(error: BdkFileError) -> Self {
match error {
Expand Down
1 change: 1 addition & 0 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::descriptor::Descriptor;
use crate::error::Alpha3Error;
use crate::error::CalculateFeeError;
use crate::error::EsploraError;
use crate::error::FeeRateError;
use crate::esplora::EsploraClient;
use crate::keys::DerivationPath;
use crate::keys::DescriptorPublicKey;
Expand Down
26 changes: 17 additions & 9 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,39 @@ use bdk::wallet::AddressIndex as BdkAddressIndex;
use bdk::wallet::AddressInfo as BdkAddressInfo;
use bdk::wallet::Balance as BdkBalance;
use bdk::KeychainKind;

use bdk::LocalOutput as BdkLocalOutput;

use bdk::FeeRate as BdkFeeRate;
use bdk::bitcoin::FeeRate as BdkFeeRate;

use crate::error::FeeRateError;
use std::sync::Arc;

#[derive(Clone, Debug)]
pub struct FeeRate(pub BdkFeeRate);

impl FeeRate {
pub fn from_sat_per_vb(sat_per_vb: f32) -> Self {
FeeRate(BdkFeeRate::from_sat_per_vb(sat_per_vb))
pub fn from_sat_per_vb(sat_per_vb: u64) -> Result<Self, FeeRateError> {
let fee_rate: Option<BdkFeeRate> = BdkFeeRate::from_sat_per_vb(sat_per_vb);
match fee_rate {
Some(fee_rate) => Ok(FeeRate(fee_rate)),
None => Err(FeeRateError::ArithmeticOverflow),
}
}

pub fn from_sat_per_kwu(sat_per_kwu: f32) -> Self {
pub fn from_sat_per_kwu(sat_per_kwu: u64) -> Self {
FeeRate(BdkFeeRate::from_sat_per_kwu(sat_per_kwu))
}

pub fn as_sat_per_vb(&self) -> f32 {
self.0.as_sat_per_vb()
pub fn to_sat_per_vb_ceil(&self) -> u64 {
self.0.to_sat_per_vb_ceil()
}

pub fn to_sat_per_vb_floor(&self) -> u64 {
self.0.to_sat_per_vb_floor()
}

pub fn sat_per_kwu(&self) -> f32 {
self.0.sat_per_kwu()
pub fn to_sat_per_kwu(&self) -> u64 {
self.0.to_sat_per_kwu()
}
}

Expand Down
8 changes: 4 additions & 4 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use bdk::bitcoin::Network;
use bdk::bitcoin::{OutPoint as BdkOutPoint, Sequence, Txid};
use bdk::wallet::tx_builder::ChangeSpendPolicy;
use bdk::wallet::{ChangeSet, Update as BdkUpdate};
use bdk::SignOptions;
use bdk::Wallet as BdkWallet;
use bdk::{FeeRate as BdkFeeRate, SignOptions};
use bdk_file_store::Store;

use std::collections::HashSet;
Expand Down Expand Up @@ -532,13 +532,13 @@ impl TxBuilder {
#[derive(Clone)]
pub(crate) struct BumpFeeTxBuilder {
pub(crate) txid: String,
pub(crate) fee_rate: f32,
pub(crate) fee_rate: Arc<FeeRate>,
pub(crate) allow_shrinking: Option<Arc<Script>>,
pub(crate) rbf: Option<RbfValue>,
}

impl BumpFeeTxBuilder {
pub(crate) fn new(txid: String, fee_rate: f32) -> Self {
pub(crate) fn new(txid: String, fee_rate: Arc<FeeRate>) -> Self {
Self {
txid,
fee_rate,
Expand Down Expand Up @@ -575,7 +575,7 @@ impl BumpFeeTxBuilder {
let txid = Txid::from_str(self.txid.as_str()).map_err(|_| Alpha3Error::Generic)?;
let mut wallet = wallet.get_wallet();
let mut tx_builder = wallet.build_fee_bump(txid)?;
tx_builder.fee_rate(BdkFeeRate::from_sat_per_vb(self.fee_rate));
tx_builder.fee_rate(self.fee_rate.0);
if let Some(allow_shrinking) = &self.allow_shrinking {
tx_builder.allow_shrinking(allow_shrinking.0.clone())?;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LiveTxBuilderTest {
val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.TESTNET)
val psbt: PartiallySignedTransaction = TxBuilder()
.addRecipient(recipient.scriptPubkey(), 4200uL)
.feeRate(FeeRate.fromSatPerVb(2.0f))
.feeRate(FeeRate.fromSatPerVb(2uL))
.finish(wallet)

println(psbt.serialize())
Expand Down Expand Up @@ -62,7 +62,7 @@ class LiveTxBuilderTest {

val psbt: PartiallySignedTransaction = TxBuilder()
.setRecipients(allRecipients)
.feeRate(FeeRate.fromSatPerVb(4.0f))
.feeRate(FeeRate.fromSatPerVb(4uL))
.changePolicy(ChangeSpendPolicy.CHANGE_FORBIDDEN)
.enableRbf()
.finish(wallet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class LiveWalletTest {

val psbt: PartiallySignedTransaction = TxBuilder()
.addRecipient(recipient.scriptPubkey(), 4200uL)
.feeRate(FeeRate.fromSatPerVb(2.0f))
.feeRate(FeeRate.fromSatPerVb(2uL))
.finish(wallet)

println(psbt.serialize())
Expand All @@ -77,7 +77,7 @@ class LiveWalletTest {
println("Tx fee is: ${txFee}")

val feeRate: FeeRate = wallet.calculateFeeRate(tx)
println("Tx fee rate is: ${feeRate.asSatPerVb()} sat/vB")
println("Tx fee rate is: ${feeRate.toSatPerVbCeil()} sat/vB")

esploraClient.broadcast(tx)
}
Expand Down
4 changes: 2 additions & 2 deletions bdk-python/tests/test_live_tx_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_tx_builder(self):
network = bdk.Network.TESTNET
)

psbt = bdk.TxBuilder().add_recipient(script=recipient.script_pubkey(), amount=4200).fee_rate(fee_rate=bdk.FeeRate.from_sat_per_vb(2.0)).finish(wallet)
psbt = bdk.TxBuilder().add_recipient(script=recipient.script_pubkey(), amount=4200).fee_rate(fee_rate=bdk.FeeRate.from_sat_per_vb(2)).finish(wallet)

self.assertTrue(psbt.serialize().startswith("cHNi"), "The PSBT should start with cHNi")

Expand Down Expand Up @@ -76,7 +76,7 @@ def complex_tx_builder(self):
bdk.ScriptAmount(recipient2.script_pubkey, 4200)
)

psbt: bdk.PartiallySignedTransaction = bdk.TxBuilder().set_recipients(all_recipients).fee_rate(fee_rate=bdk.FeeRate.from_sat_per_vb(2.0)).enable_rbf().finish(wallet)
psbt: bdk.PartiallySignedTransaction = bdk.TxBuilder().set_recipients(all_recipients).fee_rate(fee_rate=bdk.FeeRate.from_sat_per_vb(2)).enable_rbf().finish(wallet)
wallet.sign(psbt)

self.assertTrue(psbt.serialize().startswith("cHNi"), "The PSBT should start with cHNi")
Expand Down
4 changes: 2 additions & 2 deletions bdk-python/tests/test_live_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_broadcast_transaction(self):
network = bdk.Network.TESTNET
)

psbt = bdk.TxBuilder().add_recipient(script=recipient.script_pubkey(), amount=4200).fee_rate(fee_rate=bdk.FeeRate.from_sat_per_vb(2.0)).finish(wallet)
psbt = bdk.TxBuilder().add_recipient(script=recipient.script_pubkey(), amount=4200).fee_rate(fee_rate=bdk.FeeRate.from_sat_per_vb(2)).finish(wallet)
# print(psbt.serialize())
self.assertTrue(psbt.serialize().startswith("cHNi"), "The PSBT should start with cHNi")

Expand All @@ -75,7 +75,7 @@ def test_broadcast_transaction(self):
fee = wallet.calculate_fee(tx)
print(f"Transaction Fee: {fee}")
fee_rate = wallet.calculate_fee_rate(tx)
print(f"Transaction Fee Rate: {fee_rate.as_sat_per_vb()} sat/vB")
print(f"Transaction Fee Rate: {fee_rate.to_sat_per_vb_ceil()} sat/vB")

esploraClient.broadcast(tx)

Expand Down
4 changes: 2 additions & 2 deletions bdk-swift/Tests/BitcoinDevKitTests/LiveTxBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class LiveTxBuilderTests: XCTestCase {
let recipient: Address = try Address(address: "tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", network: .testnet)
let psbt: PartiallySignedTransaction = try TxBuilder()
.addRecipient(script: recipient.scriptPubkey(), amount: 4200)
.feeRate(feeRate: FeeRate.fromSatPerVb(satPerVb: 2.0))
.feeRate(feeRate: FeeRate.fromSatPerVb(satPerVb: 2))
.finish(wallet: wallet)

print(psbt.serialize())
Expand Down Expand Up @@ -88,7 +88,7 @@ final class LiveTxBuilderTests: XCTestCase {

let psbt: PartiallySignedTransaction = try TxBuilder()
.setRecipients(recipients: allRecipients)
.feeRate(feeRate: FeeRate.fromSatPerVb(satPerVb: 4.0))
.feeRate(feeRate: FeeRate.fromSatPerVb(satPerVb: 4))
.changePolicy(changePolicy: ChangeSpendPolicy.changeForbidden)
.enableRbf()
.finish(wallet: wallet)
Expand Down
4 changes: 2 additions & 2 deletions bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ final class LiveWalletTests: XCTestCase {
let psbt: PartiallySignedTransaction = try
TxBuilder()
.addRecipient(script: recipient.scriptPubkey(), amount: 4200)
.feeRate(feeRate: FeeRate.fromSatPerVb(satPerVb: 2.0))
.feeRate(feeRate: FeeRate.fromSatPerVb(satPerVb: 2))
.finish(wallet: wallet)

print(psbt.serialize())
Expand All @@ -95,7 +95,7 @@ final class LiveWalletTests: XCTestCase {
let fee: UInt64 = try wallet.calculateFee(tx: tx)
print("Transaction Fee: \(fee)")
let feeRate: FeeRate = try wallet.calculateFeeRate(tx: tx)
print("Transaction Fee Rate: \(feeRate.asSatPerVb()) sat/vB")
print("Transaction Fee Rate: \(feeRate.toSatPerVbCeil()) sat/vB")

try esploraClient.broadcast(transaction: tx)
}
Expand Down

0 comments on commit aa03558

Please sign in to comment.