Skip to content

Commit e152d68

Browse files
feat: expose FeeRate::fee_vb and FeeRate::fee_wu methods
1 parent 055e343 commit e152d68

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

bdk-ffi/src/bitcoin.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use bdk_wallet::bitcoin::Transaction as BdkTransaction;
2626
use bdk_wallet::bitcoin::TxIn as BdkTxIn;
2727
use bdk_wallet::bitcoin::TxOut as BdkTxOut;
2828
use bdk_wallet::bitcoin::Txid as BitcoinTxid;
29+
use bdk_wallet::bitcoin::Weight;
2930
use bdk_wallet::bitcoin::Wtxid as BitcoinWtxid;
3031
use bdk_wallet::miniscript::psbt::PsbtExt;
3132
use bdk_wallet::serde_json;
@@ -156,6 +157,30 @@ impl FeeRate {
156157
pub fn to_sat_per_kwu(&self) -> u64 {
157158
self.0.to_sat_per_kwu()
158159
}
160+
161+
/// Calculates fee in satoshis by multiplying this fee rate by weight, in virtual bytes, returning `None` if overflow occurred.
162+
///
163+
/// This is equivalent to converting vb to weight using Weight::from_vb and then calling Self::fee_wu(weight).
164+
pub fn fee_vb(&self, vb: u64) -> Option<Arc<Amount>> {
165+
let rust_amount: BdkAmount = self.0.fee_vb(vb)?;
166+
let amount: Amount = rust_amount.into();
167+
Some(Arc::new(amount))
168+
169+
// The whole code above should be replaceable by the following line:
170+
// self.0.fee_vb(vb).map(Arc::new(Amount::from))
171+
// But in practice you get uniffi compilation errors on it. Not sure what is going on with it,
172+
// but the code we use works just as well.
173+
}
174+
175+
/// Calculates fee by multiplying this fee rate by weight, in weight units, returning `None` if overflow occurred.
176+
//
177+
// This is equivalent to Self::checked_mul_by_weight().
178+
pub fn fee_wu(&self, wu: u64) -> Option<Arc<Amount>> {
179+
let weight: Weight = Weight::from_wu(wu);
180+
let rust_amount: BdkAmount = self.0.fee_wu(weight)?;
181+
let amount: Amount = rust_amount.into();
182+
Some(Arc::new(amount))
183+
}
159184
}
160185

161186
impl Display for FeeRate {

0 commit comments

Comments
 (0)