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.
This commit is contained in:
jrakibi 2025-01-30 03:36:10 +03:00
parent 98db7bca74
commit 134c146748
1 changed files with 15 additions and 0 deletions

View File

@ -191,6 +191,21 @@ impl ops::Div<FeeRate> 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<Amount> {
fee_rate.checked_mul_by_weight(self)
}
}
#[cfg(test)]
mod tests {
use super::*;