From 134c1467489189ab821a5255ce7f122993028687 Mon Sep 17 00:00:00 2001 From: jrakibi Date: Thu, 30 Jan 2025 03:36:10 +0300 Subject: [PATCH] Add Weight::checked_mul_by_fee_rate method Mirror FeeRate::checked_mul_by_weight functionality by adding a symmetrical method to Weight. This allows users to calculate fees starting from either Weight or FeeRate with consistent behavior. --- units/src/fee.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/units/src/fee.rs b/units/src/fee.rs index 833399279..f53a15c89 100644 --- a/units/src/fee.rs +++ b/units/src/fee.rs @@ -191,6 +191,21 @@ impl ops::Div for Amount { fn div(self, rhs: FeeRate) -> Self::Output { self.checked_div_by_fee_rate_floor(rhs).unwrap() } } +impl Weight { + /// Checked fee rate multiplication. + /// + /// Computes the absolute fee amount for a given [`FeeRate`] at this weight. + /// When the resulting fee is a non-integer amount, the amount is rounded up, + /// ensuring that the transaction fee is enough instead of falling short if + /// rounded down. + /// + /// [`None`] is returned if an overflow occurred. + #[must_use] + pub const fn checked_mul_by_fee_rate(self, fee_rate: FeeRate) -> Option { + fee_rate.checked_mul_by_weight(self) + } +} + #[cfg(test)] mod tests { use super::*;