From f01e0e30f3dea71af192885a0e7c4691afd81c63 Mon Sep 17 00:00:00 2001 From: Shobit Beltangdy Date: Mon, 2 Jan 2023 23:05:00 -0800 Subject: [PATCH] expose estimate_fee api bdk::blockchain::Blockchain contains an `estimate_fee` api. This commit adds the bindings for estimate_fee. This will fix https://github.com/bitcoindevkit/bdk-ffi/issues/287 Tested this by adding a unit test in lib.rs. Also generated the python code and verified that it is able to invoke estimate_fee and get the fee rate --- .../kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt | 3 +++ .../src/test/kotlin/org/bitcoindevkit/Samples.kt | 11 ++++++++++- bdk-ffi/src/bdk.udl | 3 +++ bdk-ffi/src/lib.rs | 6 ++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt b/api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt index 6fa4ad1..4401ffc 100644 --- a/api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt +++ b/api-docs/kotlin/src/main/kotlin/org/bitcoindevkit/bdk.kt @@ -250,6 +250,9 @@ class Blockchain( /** Broadcast a transaction. */ fun broadcast(psbt: PartiallySignedBitcoinTransaction) {} + /** Estimate the fee rate required to confirm a transaction in a given target of blocks. */ + fun estimateFee(target: ULong): FeeRate {} + /** Get the current height of the blockchain. */ fun getHeight(): UInt {} diff --git a/api-docs/kotlin/src/test/kotlin/org/bitcoindevkit/Samples.kt b/api-docs/kotlin/src/test/kotlin/org/bitcoindevkit/Samples.kt index 8fe7dfe..92dd0d7 100644 --- a/api-docs/kotlin/src/test/kotlin/org/bitcoindevkit/Samples.kt +++ b/api-docs/kotlin/src/test/kotlin/org/bitcoindevkit/Samples.kt @@ -107,7 +107,16 @@ fun blockchainSample() { val blockchain: Blockchain = Blockchain(blockchainConfig) - blockchain.broadcast(signedPsbt) + val feeRate: FeeRate = blockchain.estimateFee(3u) + + val (psbt, txDetails) = TxBuilder() + .addRecipient(faucetAddress.scriptPubkey(), 1000uL) + .feeRate(feeRate.asSatPerVb()) + .finish(wallet) + + wallet.sign(psbt) + + blockchain.broadcast(psbt) } diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index 7ca27f1..25f65eb 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -164,6 +164,9 @@ interface Blockchain { [Throws=BdkError] void broadcast([ByRef] PartiallySignedTransaction psbt); + [Throws=BdkError] + FeeRate estimate_fee(u64 target); + [Throws=BdkError] u32 get_height(); diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index 12f86f4..6ffb6a4 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -305,6 +305,12 @@ impl Blockchain { self.get_blockchain().broadcast(&tx) } + fn estimate_fee(&self, target: u64) -> Result, BdkError> { + let result: Result = + self.get_blockchain().estimate_fee(target as usize); + result.map(Arc::new) + } + fn get_height(&self) -> Result { self.get_blockchain().get_height() }