Skip to content

Commit c47aed1

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

File tree

4 files changed

+57
-0
lines changed

4 files changed

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

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)