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 c47aed1
Show file tree
Hide file tree
Showing 4 changed files with 57 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
40 changes: 40 additions & 0 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use bdk::bitcoin::OutPoint as BdkOutPoint;
use bdk::chain::tx_graph::CalculateFeeError;

use std::fmt;

#[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
@@ -1,5 +1,6 @@
mod bitcoin;
mod descriptor;
mod error;
mod esplora;
mod keys;
mod types;
Expand All @@ -13,6 +14,7 @@ use crate::bitcoin::Script;
use crate::bitcoin::Transaction;
use crate::bitcoin::TxOut;
use crate::descriptor::Descriptor;
use crate::error::BdkCalculateFeeError;
use crate::esplora::EsploraClient;
use crate::keys::DerivationPath;
use crate::keys::DescriptorPublicKey;
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 c47aed1

Please sign in to comment.