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
This commit is contained in:
Shobit Beltangdy 2023-01-02 23:05:00 -08:00
parent 5f7d8a9077
commit f01e0e30f3
No known key found for this signature in database
GPG Key ID: ECDDED18A4204604
4 changed files with 22 additions and 1 deletions

View File

@ -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 {}

View File

@ -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)
}

View File

@ -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();

View File

@ -305,6 +305,12 @@ impl Blockchain {
self.get_blockchain().broadcast(&tx)
}
fn estimate_fee(&self, target: u64) -> Result<Arc<FeeRate>, BdkError> {
let result: Result<FeeRate, bdk::Error> =
self.get_blockchain().estimate_fee(target as usize);
result.map(Arc::new)
}
fn get_height(&self) -> Result<u32, BdkError> {
self.get_blockchain().get_height()
}