Skip to content

Commit 8d01bd4

Browse files
committed
feat: add calculate_fee on wallet
1 parent cdec63e commit 8d01bd4

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

bdk-ffi/src/bdk.udl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ enum BdkError {
8282
"Psbt",
8383
};
8484

85+
[Error]
86+
enum BdkCalculateFeeError {
87+
"MissingTxOut",
88+
"NegativeFee",
89+
};
90+
8591
enum ChangeSpendPolicy {
8692
"ChangeAllowed",
8793
"OnlyChange",
@@ -111,6 +117,9 @@ interface Wallet {
111117
SentAndReceivedValues sent_and_received([ByRef] Transaction tx);
112118

113119
sequence<Transaction> transactions();
120+
121+
[Throws=BdkCalculateFeeError]
122+
u64 calculate_fee([ByRef] Transaction tx);
114123
};
115124

116125
interface Update {};

bdk-ffi/src/error.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::fmt;
2+
use bdk::chain::tx_graph::CalculateFeeError;
3+
use bdk::bitcoin::OutPoint as BdkOutPoint;
4+
5+
#[derive(Debug)]
6+
pub enum BdkCalculateFeeError {
7+
MissingTxOut { out_points: Vec<BdkOutPoint> },
8+
NegativeFee { fee: i64 },
9+
}
10+
11+
impl fmt::Display for BdkCalculateFeeError {
12+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13+
match self {
14+
BdkCalculateFeeError::MissingTxOut { out_points } =>
15+
write!(f, "Missing transaction output: {:?}", out_points),
16+
BdkCalculateFeeError::NegativeFee { fee } =>
17+
write!(f, "Negative fee value: {}", fee),
18+
}
19+
}
20+
}
21+
22+
impl From<CalculateFeeError> for BdkCalculateFeeError {
23+
fn from(error: CalculateFeeError) -> Self {
24+
match error {
25+
CalculateFeeError::MissingTxOut(out_points) => BdkCalculateFeeError::MissingTxOut {
26+
out_points: out_points
27+
.into_iter()
28+
.map(|out_point| BdkOutPoint {
29+
txid: out_point.txid,
30+
vout: out_point.vout,
31+
})
32+
.collect(),
33+
},
34+
CalculateFeeError::NegativeFee(fee) => BdkCalculateFeeError::NegativeFee { fee },
35+
}
36+
}
37+
}
38+
39+
impl std::error::Error for BdkCalculateFeeError {}

bdk-ffi/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod esplora;
44
mod keys;
55
mod types;
66
mod wallet;
7+
mod error;
78

89
use crate::bitcoin::Address;
910
use crate::bitcoin::Network;
@@ -24,6 +25,7 @@ use crate::types::Balance;
2425
use crate::types::LocalUtxo;
2526
use crate::types::ScriptAmount;
2627
use crate::wallet::BumpFeeTxBuilder;
28+
use crate::error::BdkCalculateFeeError;
2729
use crate::wallet::SentAndReceivedValues;
2830
use crate::wallet::TxBuilder;
2931
use crate::wallet::Update;

bdk-ffi/src/wallet.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Transaction};
22
use crate::descriptor::Descriptor;
3+
use crate::error::BdkCalculateFeeError;
34
use crate::types::Balance;
45
use crate::types::ScriptAmount;
56
use crate::Script;
@@ -98,6 +99,11 @@ impl Wallet {
9899
.map(|tx| Arc::new(tx.tx_node.tx.clone().into()))
99100
.collect()
100101
}
102+
103+
pub fn calculate_fee(&self, tx: &Transaction) -> Result<u64, BdkCalculateFeeError> {
104+
let fee_result = self.get_wallet().calculate_fee(&tx.clone().into());
105+
fee_result.map_err(|err| err.into())
106+
}
101107
}
102108

103109
pub struct SentAndReceivedValues {

0 commit comments

Comments
 (0)