From 282fcfce0adccbd931843b4e766195a6df1812ed Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Wed, 24 Apr 2024 09:44:01 -0400 Subject: [PATCH] refactor: move feerate type to bitcoin module --- bdk-ffi/src/bdk.udl | 28 ++++++++++++++-------------- bdk-ffi/src/bitcoin.rs | 32 +++++++++++++++++++++++++++++++- bdk-ffi/src/lib.rs | 2 +- bdk-ffi/src/types.rs | 32 -------------------------------- bdk-ffi/src/wallet.rs | 6 ++---- 5 files changed, 48 insertions(+), 52 deletions(-) diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index 27219964..42473bc3 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -222,20 +222,6 @@ dictionary CanonicalTx { // bdk crate - wallet module // ------------------------------------------------------------------------ -interface FeeRate { - [Name=from_sat_per_vb, Throws=FeeRateError] - constructor(u64 sat_per_vb); - - [Name=from_sat_per_kwu] - constructor(u64 sat_per_kwu); - - u64 to_sat_per_vb_ceil(); - - u64 to_sat_per_vb_floor(); - - u64 to_sat_per_kwu(); -}; - enum ChangeSpendPolicy { "ChangeAllowed", "OnlyChange", @@ -523,3 +509,17 @@ dictionary OutPoint { string txid; u32 vout; }; + +interface FeeRate { + [Name=from_sat_per_vb, Throws=FeeRateError] + constructor(u64 sat_per_vb); + + [Name=from_sat_per_kwu] + constructor(u64 sat_per_kwu); + + u64 to_sat_per_vb_ceil(); + + u64 to_sat_per_vb_floor(); + + u64 to_sat_per_kwu(); +}; diff --git a/bdk-ffi/src/bitcoin.rs b/bdk-ffi/src/bitcoin.rs index 6189f92f..74171013 100644 --- a/bdk-ffi/src/bitcoin.rs +++ b/bdk-ffi/src/bitcoin.rs @@ -1,4 +1,4 @@ -use crate::error::{AddressError, PsbtParseError, TransactionError}; +use crate::error::{AddressError, FeeRateError, PsbtParseError, TransactionError}; use bdk::bitcoin::address::{NetworkChecked, NetworkUnchecked}; use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf; @@ -7,6 +7,7 @@ use bdk::bitcoin::consensus::encode::serialize; use bdk::bitcoin::consensus::Decodable; use bdk::bitcoin::psbt::ExtractTxError; use bdk::bitcoin::Address as BdkAddress; +use bdk::bitcoin::FeeRate as BdkFeeRate; use bdk::bitcoin::Network; use bdk::bitcoin::OutPoint as BdkOutPoint; use bdk::bitcoin::Psbt as BdkPsbt; @@ -304,6 +305,35 @@ impl From<&BdkTxOut> for TxOut { } } +#[derive(Clone, Debug)] +pub struct FeeRate(pub BdkFeeRate); + +impl FeeRate { + pub fn from_sat_per_vb(sat_per_vb: u64) -> Result { + let fee_rate: Option = BdkFeeRate::from_sat_per_vb(sat_per_vb); + match fee_rate { + Some(fee_rate) => Ok(FeeRate(fee_rate)), + None => Err(FeeRateError::ArithmeticOverflow), + } + } + + pub fn from_sat_per_kwu(sat_per_kwu: u64) -> Self { + FeeRate(BdkFeeRate::from_sat_per_kwu(sat_per_kwu)) + } + + pub fn to_sat_per_vb_ceil(&self) -> u64 { + self.0.to_sat_per_vb_ceil() + } + + pub fn to_sat_per_vb_floor(&self) -> u64 { + self.0.to_sat_per_vb_floor() + } + + pub fn to_sat_per_kwu(&self) -> u64 { + self.0.to_sat_per_kwu() + } +} + #[cfg(test)] mod tests { use crate::bitcoin::Address; diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index 84400890..a54863ae 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -7,6 +7,7 @@ mod types; mod wallet; use crate::bitcoin::Address; +use crate::bitcoin::FeeRate; use crate::bitcoin::OutPoint; use crate::bitcoin::Psbt; use crate::bitcoin::Script; @@ -39,7 +40,6 @@ use crate::types::AddressInfo; use crate::types::Balance; use crate::types::CanonicalTx; use crate::types::ChainPosition; -use crate::types::FeeRate; use crate::types::LocalOutput; use crate::types::ScriptAmount; use crate::wallet::BumpFeeTxBuilder; diff --git a/bdk-ffi/src/types.rs b/bdk-ffi/src/types.rs index f891a18a..04751638 100644 --- a/bdk-ffi/src/types.rs +++ b/bdk-ffi/src/types.rs @@ -1,8 +1,5 @@ -use crate::error::FeeRateError; - use crate::bitcoin::{Address, OutPoint, Script, Transaction, TxOut}; -use bdk::bitcoin::FeeRate as BdkFeeRate; use bdk::chain::tx_graph::CanonicalTx as BdkCanonicalTx; use bdk::chain::{ChainPosition as BdkChainPosition, ConfirmationTimeHeightAnchor}; use bdk::wallet::AddressIndex as BdkAddressIndex; @@ -45,35 +42,6 @@ impl From, ConfirmationTimeHei } } -#[derive(Clone, Debug)] -pub struct FeeRate(pub BdkFeeRate); - -impl FeeRate { - pub fn from_sat_per_vb(sat_per_vb: u64) -> Result { - let fee_rate: Option = BdkFeeRate::from_sat_per_vb(sat_per_vb); - match fee_rate { - Some(fee_rate) => Ok(FeeRate(fee_rate)), - None => Err(FeeRateError::ArithmeticOverflow), - } - } - - pub fn from_sat_per_kwu(sat_per_kwu: u64) -> Self { - FeeRate(BdkFeeRate::from_sat_per_kwu(sat_per_kwu)) - } - - pub fn to_sat_per_vb_ceil(&self) -> u64 { - self.0.to_sat_per_vb_ceil() - } - - pub fn to_sat_per_vb_floor(&self) -> u64 { - self.0.to_sat_per_vb_floor() - } - - pub fn to_sat_per_kwu(&self) -> u64 { - self.0.to_sat_per_kwu() - } -} - pub struct ScriptAmount { pub script: Arc