Skip to content

Commit

Permalink
feat: use FeeRate type in TxBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Jan 19, 2024
1 parent 7319aea commit a86649e
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LiveTxBuilderTest {
val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.TESTNET)
val psbt: PartiallySignedTransaction = TxBuilder()
.addRecipient(recipient.scriptPubkey(), 4200uL)
.feeRate(2.0f)
.feeRate(FeeRate.fromSatPerVb(2.0f))
.finish(wallet)

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

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

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

println(psbt.serialize())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.runner.RunWith
import kotlin.test.assertEquals

@RunWith(AndroidJUnit4::class)
class OfflineDescriptorTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.bitcoindevkit
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import kotlin.test.assertFalse
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.runner.RunWith

Expand Down
9 changes: 8 additions & 1 deletion bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ dictionary TxOut {
// ------------------------------------------------------------------------

interface FeeRate {
[Name=from_sat_per_vb]
constructor(f32 sat_per_vb);

[Name=from_sat_per_kwu]
constructor(f32 sat_per_kwu);

f32 as_sat_per_vb();

f32 sat_per_kwu();
};

Expand Down Expand Up @@ -132,7 +139,7 @@ interface TxBuilder {

TxBuilder manually_selected_only();

TxBuilder fee_rate(float sat_per_vbyte);
TxBuilder fee_rate([ByRef] FeeRate fee_rate);

TxBuilder fee_absolute(u64 fee);

Expand Down
9 changes: 9 additions & 0 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ use bdk::FeeRate as BdkFeeRate;

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_kwu(sat_per_kwu: f32) -> Self {
FeeRate(BdkFeeRate::from_sat_per_kwu(sat_per_kwu))
}

pub fn as_sat_per_vb(&self) -> f32 {
self.0.as_sat_per_vb()
}
Expand Down
10 changes: 5 additions & 5 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ pub struct TxBuilder {
pub(crate) unspendable: HashSet<OutPoint>,
pub(crate) change_policy: ChangeSpendPolicy,
pub(crate) manually_selected_only: bool,
pub(crate) fee_rate: Option<f32>,
pub(crate) fee_rate: Option<FeeRate>,
pub(crate) fee_absolute: Option<u64>,
pub(crate) drain_wallet: bool,
pub(crate) drain_to: Option<BdkScriptBuf>,
Expand Down Expand Up @@ -412,9 +412,9 @@ impl TxBuilder {
})
}

pub(crate) fn fee_rate(&self, sat_per_vb: f32) -> Arc<Self> {
pub(crate) fn fee_rate(&self, fee_rate: &FeeRate) -> Arc<Self> {
Arc::new(TxBuilder {
fee_rate: Some(sat_per_vb),
fee_rate: Some(fee_rate.clone()),
..self.clone()
})
}
Expand Down Expand Up @@ -486,8 +486,8 @@ impl TxBuilder {
if self.manually_selected_only {
tx_builder.manually_selected_only();
}
if let Some(sat_per_vb) = self.fee_rate {
tx_builder.fee_rate(BdkFeeRate::from_sat_per_vb(sat_per_vb));
if let Some(fee_rate) = &self.fee_rate {
tx_builder.fee_rate(fee_rate.0);
}
if let Some(fee_amount) = self.fee_absolute {
tx_builder.fee_absolute(fee_amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LiveTxBuilderTest {
val recipient: Address = Address("tb1qrnfslnrve9uncz9pzpvf83k3ukz22ljgees989", Network.TESTNET)
val psbt: PartiallySignedTransaction = TxBuilder()
.addRecipient(recipient.scriptPubkey(), 4200uL)
.feeRate(2.0f)
.feeRate(FeeRate.fromSatPerVb(2.0f))//.feeRate(2.0f)
.finish(wallet)

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

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

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

println(psbt.serialize())
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 @@ -25,7 +25,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(satPerVbyte: 2.0)
.feeRate(feeRate: FeeRate.fromSatPerVb(satPerVb: 2.0))
.finish(wallet: wallet)

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

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

print(psbt.serialize())
Expand Down

0 comments on commit a86649e

Please sign in to comment.