diff --git a/api/units/all-features.txt b/api/units/all-features.txt index 5d5fdeab7..8eb819b1f 100644 --- a/api/units/all-features.txt +++ b/api/units/all-features.txt @@ -1069,6 +1069,7 @@ pub fn bitcoin_units::fee_rate::FeeRate::sub_assign(&mut self, rhs: &bitcoin_uni pub fn bitcoin_units::fee_rate::FeeRate::sub_assign(&mut self, rhs: bitcoin_units::fee_rate::FeeRate) pub fn bitcoin_units::fee_rate::FeeRate::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator pub fn bitcoin_units::fee_rate::FeeRate::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator +pub fn bitcoin_units::fee_rate::FeeRate::to_fee(self, weight: bitcoin_units::weight::Weight) -> core::option::Option pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: &str) -> core::result::Result pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: alloc::boxed::Box) -> core::result::Result pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: alloc::string::String) -> core::result::Result diff --git a/api/units/alloc-only.txt b/api/units/alloc-only.txt index 8aaeaeb7d..37c97385c 100644 --- a/api/units/alloc-only.txt +++ b/api/units/alloc-only.txt @@ -950,6 +950,7 @@ pub fn bitcoin_units::fee_rate::FeeRate::sub_assign(&mut self, rhs: &bitcoin_uni pub fn bitcoin_units::fee_rate::FeeRate::sub_assign(&mut self, rhs: bitcoin_units::fee_rate::FeeRate) pub fn bitcoin_units::fee_rate::FeeRate::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator pub fn bitcoin_units::fee_rate::FeeRate::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator +pub fn bitcoin_units::fee_rate::FeeRate::to_fee(self, weight: bitcoin_units::weight::Weight) -> core::option::Option pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: &str) -> core::result::Result pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: alloc::boxed::Box) -> core::result::Result pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: alloc::string::String) -> core::result::Result diff --git a/api/units/no-features.txt b/api/units/no-features.txt index 19a530501..1464a2aa3 100644 --- a/api/units/no-features.txt +++ b/api/units/no-features.txt @@ -916,6 +916,7 @@ pub fn bitcoin_units::fee_rate::FeeRate::sub_assign(&mut self, rhs: &bitcoin_uni pub fn bitcoin_units::fee_rate::FeeRate::sub_assign(&mut self, rhs: bitcoin_units::fee_rate::FeeRate) pub fn bitcoin_units::fee_rate::FeeRate::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator pub fn bitcoin_units::fee_rate::FeeRate::sum(iter: I) -> Self where I: core::iter::traits::iterator::Iterator +pub fn bitcoin_units::fee_rate::FeeRate::to_fee(self, weight: bitcoin_units::weight::Weight) -> core::option::Option pub fn bitcoin_units::fee_rate::FeeRate::try_from(s: &str) -> core::result::Result pub fn bitcoin_units::locktime::absolute::ConversionError::clone(&self) -> bitcoin_units::locktime::absolute::ConversionError pub fn bitcoin_units::locktime::absolute::ConversionError::eq(&self, other: &bitcoin_units::locktime::absolute::ConversionError) -> bool diff --git a/bitcoin/src/blockdata/mod.rs b/bitcoin/src/blockdata/mod.rs index a8d0af44b..e9e803fc8 100644 --- a/bitcoin/src/blockdata/mod.rs +++ b/bitcoin/src/blockdata/mod.rs @@ -43,7 +43,7 @@ pub mod fee_rate { let rate = FeeRate::from_sat_per_vb(1).expect("1 sat/byte is valid"); - assert_eq!(rate.fee_vb(tx.vsize().to_u64()), rate.fee_wu(tx.weight())); + assert_eq!(rate.fee_vb(tx.vsize().to_u64()), rate.to_fee(tx.weight())); } } } diff --git a/units/src/fee.rs b/units/src/fee.rs index f53a15c89..23327cede 100644 --- a/units/src/fee.rs +++ b/units/src/fee.rs @@ -118,6 +118,14 @@ impl FeeRate { /// /// This is equivalent to `Self::checked_mul_by_weight()`. #[must_use] + pub fn to_fee(self, weight: Weight) -> Option { self.checked_mul_by_weight(weight) } + + /// Calculates the fee by multiplying this fee rate by weight, in weight units, returning [`None`] + /// if an overflow occurred. + /// + /// This is equivalent to `Self::checked_mul_by_weight()`. + #[must_use] + #[deprecated(since = "TBD", note = "use `to_fee()` instead")] pub fn fee_wu(self, weight: Weight) -> Option { self.checked_mul_by_weight(weight) } /// Calculates the fee by multiplying this fee rate by weight, in virtual bytes, returning [`None`] @@ -127,7 +135,7 @@ impl FeeRate { /// `Self::fee_wu(weight)`. #[must_use] pub fn fee_vb(self, vb: u64) -> Option { - Weight::from_vb(vb).and_then(|w| self.fee_wu(w)) + Weight::from_vb(vb).and_then(|w| self.to_fee(w)) } /// Checked weight multiplication. @@ -218,12 +226,12 @@ mod tests { #[test] fn fee_wu() { - let fee_overflow = FeeRate::from_sat_per_kwu(10).fee_wu(Weight::MAX); + let fee_overflow = FeeRate::from_sat_per_kwu(10).to_fee(Weight::MAX); assert!(fee_overflow.is_none()); let fee_rate = FeeRate::from_sat_per_vb(2).unwrap(); let weight = Weight::from_vb(3).unwrap(); - assert_eq!(fee_rate.fee_wu(weight).unwrap(), Amount::from_sat_unchecked(6)); + assert_eq!(fee_rate.to_fee(weight).unwrap(), Amount::from_sat_unchecked(6)); } #[test]