Skip to content

Commit

Permalink
refactor: move feerate type to bitcoin module
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Apr 25, 2024
1 parent 4dd4e91 commit 282fcfc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 52 deletions.
28 changes: 14 additions & 14 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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();
};
32 changes: 31 additions & 1 deletion bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Self, FeeRateError> {
let fee_rate: Option<BdkFeeRate> = 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;
Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 0 additions & 32 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -45,35 +42,6 @@ impl From<BdkCanonicalTx<'_, Arc<bdk::bitcoin::Transaction>, ConfirmationTimeHei
}
}

#[derive(Clone, Debug)]
pub struct FeeRate(pub BdkFeeRate);

impl FeeRate {
pub fn from_sat_per_vb(sat_per_vb: u64) -> Result<Self, FeeRateError> {
let fee_rate: Option<BdkFeeRate> = 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<Script>,
pub amount: u64,
Expand Down
6 changes: 2 additions & 4 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::bitcoin::{OutPoint, Psbt, Script, Transaction};
use crate::bitcoin::{FeeRate, OutPoint, Psbt, Script, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{
Alpha3Error, CalculateFeeError, CannotConnectError, PersistenceError, SignerError,
TxidParseError, WalletCreationError,
};
use crate::types::{
AddressIndex, AddressInfo, Balance, CanonicalTx, FeeRate, LocalOutput, ScriptAmount,
};
use crate::types::{AddressIndex, AddressInfo, Balance, CanonicalTx, LocalOutput, ScriptAmount};

use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf;
use bdk::bitcoin::Network;
Expand Down

0 comments on commit 282fcfc

Please sign in to comment.