Skip to content

Commit

Permalink
feat: add calculate_fee on wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Dec 17, 2023
1 parent cdec63e commit 7610800
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ enum BdkError {
"Psbt",
};

[Error]
enum BdkCalculateFeeError {
"MissingTxOut",
"NegativeFee",
};

enum ChangeSpendPolicy {
"ChangeAllowed",
"OnlyChange",
Expand Down Expand Up @@ -111,6 +117,9 @@ interface Wallet {
SentAndReceivedValues sent_and_received([ByRef] Transaction tx);

sequence<Transaction> transactions();

[Throws=BdkCalculateFeeError]
u64 calculate_fee([ByRef] Transaction tx);
};

interface Update {};
Expand Down
39 changes: 39 additions & 0 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::fmt;
use bdk::chain::tx_graph::CalculateFeeError;
use bdk::bitcoin::OutPoint as BdkOutPoint;

#[derive(Debug)]
pub enum BdkCalculateFeeError {
MissingTxOut { out_points: Vec<BdkOutPoint> },
NegativeFee { fee: i64 },
}

impl fmt::Display for BdkCalculateFeeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
BdkCalculateFeeError::MissingTxOut { out_points } =>
write!(f, "Missing transaction output: {:?}", out_points),
BdkCalculateFeeError::NegativeFee { fee } =>
write!(f, "Negative fee value: {}", fee),
}
}
}

impl From<CalculateFeeError> for BdkCalculateFeeError {
fn from(error: CalculateFeeError) -> Self {
match error {
CalculateFeeError::MissingTxOut(out_points) => BdkCalculateFeeError::MissingTxOut {
out_points: out_points
.into_iter()
.map(|out_point| BdkOutPoint {
txid: out_point.txid,
vout: out_point.vout,
})
.collect(),
},
CalculateFeeError::NegativeFee(fee) => BdkCalculateFeeError::NegativeFee { fee },
}
}
}

impl std::error::Error for BdkCalculateFeeError {}
2 changes: 2 additions & 0 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod esplora;
mod keys;
mod types;
mod wallet;
mod error;

use crate::bitcoin::Address;
use crate::bitcoin::Network;
Expand All @@ -24,6 +25,7 @@ use crate::types::Balance;
use crate::types::LocalUtxo;
use crate::types::ScriptAmount;
use crate::wallet::BumpFeeTxBuilder;
use crate::error::BdkCalculateFeeError;
use crate::wallet::SentAndReceivedValues;
use crate::wallet::TxBuilder;
use crate::wallet::Update;
Expand Down
6 changes: 6 additions & 0 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Transaction};
use crate::descriptor::Descriptor;
use crate::error::BdkCalculateFeeError;
use crate::types::Balance;
use crate::types::ScriptAmount;
use crate::Script;
Expand Down Expand Up @@ -98,6 +99,11 @@ impl Wallet {
.map(|tx| Arc::new(tx.tx_node.tx.clone().into()))
.collect()
}

pub fn calculate_fee(&self, tx: &Transaction) -> Result<u64, BdkCalculateFeeError> {
let fee_result = self.get_wallet().calculate_fee(&tx.clone().into());
fee_result.map_err(|err| err.into())
}
}

pub struct SentAndReceivedValues {
Expand Down

0 comments on commit 7610800

Please sign in to comment.