-
Notifications
You must be signed in to change notification settings - Fork 69
New stuff on the FeeRate type
#859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thunderbiscuit
merged 3 commits into
bitcoindevkit:master
from
thunderbiscuit:feature/display
Nov 13, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ use bdk_wallet::bitcoin::Transaction as BdkTransaction; | |
| use bdk_wallet::bitcoin::TxIn as BdkTxIn; | ||
| use bdk_wallet::bitcoin::TxOut as BdkTxOut; | ||
| use bdk_wallet::bitcoin::Txid as BitcoinTxid; | ||
| use bdk_wallet::bitcoin::Weight; | ||
| use bdk_wallet::bitcoin::Wtxid as BitcoinWtxid; | ||
| use bdk_wallet::miniscript::psbt::PsbtExt; | ||
| use bdk_wallet::serde_json; | ||
|
|
@@ -125,10 +126,12 @@ impl HashableOutPoint { | |
| /// This is an integer type representing fee rate in sat/kwu. It provides protection against mixing | ||
| /// up the types as well as basic formatting features. | ||
| #[derive(Clone, Debug, uniffi::Object)] | ||
| #[uniffi::export(Display)] | ||
| pub struct FeeRate(pub(crate) BdkFeeRate); | ||
|
|
||
| #[uniffi::export] | ||
| impl FeeRate { | ||
| /// Constructs `FeeRate` from satoshis per virtual bytes. | ||
| #[uniffi::constructor] | ||
| pub fn from_sat_per_vb(sat_vb: u64) -> Result<Self, FeeRateError> { | ||
| let fee_rate: Option<BdkFeeRate> = BdkFeeRate::from_sat_per_vb(sat_vb); | ||
|
|
@@ -138,22 +141,56 @@ impl FeeRate { | |
| } | ||
| } | ||
|
|
||
| /// Constructs `FeeRate` from satoshis per 1000 weight units. | ||
| #[uniffi::constructor] | ||
| pub fn from_sat_per_kwu(sat_kwu: u64) -> Self { | ||
| FeeRate(BdkFeeRate::from_sat_per_kwu(sat_kwu)) | ||
| } | ||
|
|
||
| /// Converts to sat/vB rounding up. | ||
| pub fn to_sat_per_vb_ceil(&self) -> u64 { | ||
| self.0.to_sat_per_vb_ceil() | ||
| } | ||
|
|
||
| /// Converts to sat/vB rounding down. | ||
| pub fn to_sat_per_vb_floor(&self) -> u64 { | ||
| self.0.to_sat_per_vb_floor() | ||
| } | ||
|
|
||
| /// Returns raw fee rate. | ||
| pub fn to_sat_per_kwu(&self) -> u64 { | ||
| self.0.to_sat_per_kwu() | ||
| } | ||
|
|
||
| /// Calculates fee in satoshis by multiplying this fee rate by weight, in virtual bytes, returning `None` if overflow occurred. | ||
| /// | ||
| /// This is equivalent to converting vb to weight using Weight::from_vb and then calling Self::fee_wu(weight). | ||
| pub fn fee_vb(&self, vb: u64) -> Option<Arc<Amount>> { | ||
| let rust_amount: BdkAmount = self.0.fee_vb(vb)?; | ||
| let amount: Amount = rust_amount.into(); | ||
| Some(Arc::new(amount)) | ||
|
|
||
| // The whole code above should be replaceable by the following line: | ||
| // self.0.fee_vb(vb).map(Arc::new(Amount::from)) | ||
| // But in practice you get uniffi compilation errors on it. Not sure what is going on with it, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how weird, good comment for the future when hopefully it resolves itself |
||
| // but the code we use works just as well. | ||
| } | ||
|
|
||
| /// Calculates fee by multiplying this fee rate by weight, in weight units, returning `None` if overflow occurred. | ||
| /// | ||
| /// This is equivalent to Self::checked_mul_by_weight(). | ||
| pub fn fee_wu(&self, wu: u64) -> Option<Arc<Amount>> { | ||
| let weight: Weight = Weight::from_wu(wu); | ||
| let rust_amount: BdkAmount = self.0.fee_wu(weight)?; | ||
| let amount: Amount = rust_amount.into(); | ||
| Some(Arc::new(amount)) | ||
| } | ||
| } | ||
|
|
||
| impl Display for FeeRate { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
| write!(f, "{:#}", self.0) | ||
| } | ||
| } | ||
|
|
||
| impl_from_core_type!(BdkFeeRate, FeeRate); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice