diff --git a/units/src/fee_rate/mod.rs b/units/src/fee_rate/mod.rs index f0db11cf0..75c516ea5 100644 --- a/units/src/fee_rate/mod.rs +++ b/units/src/fee_rate/mod.rs @@ -11,6 +11,10 @@ use core::ops; #[cfg(feature = "arbitrary")] use arbitrary::{Arbitrary, Unstructured}; +use NumOpResult as R; + +use crate::{Amount,MathOp, NumOpError as E, NumOpResult}; + mod encapsulate { /// Fee rate. /// @@ -62,7 +66,16 @@ impl FeeRate { } } - /// Constructs a new [`FeeRate`] from satoshis per virtual bytes, + /// Constructs a new [`FeeRate`] from amount per 1000 weight units. + pub const fn from_per_kwu(rate: Amount) -> NumOpResult { + // No `map()` in const context. + match rate.checked_mul(4_000) { + Some(per_mvb) => R::Valid(FeeRate::from_sat_per_mvb(per_mvb.to_sat())), + None => R::Error(E::while_doing(MathOp::Mul)), + } + } + + /// Constructs a new [`FeeRate`] from satoshis per virtual byte, /// returning `None` if overflow occurred. pub const fn from_sat_per_vb(sat_vb: u64) -> Option { // No `map()` in const context. @@ -72,6 +85,15 @@ impl FeeRate { } } + /// Constructs a new [`FeeRate`] from amount per virtual byte. + pub const fn from_per_vb(rate: Amount) -> NumOpResult { + // No `map()` in const context. + match rate.checked_mul(1_000_000) { + Some(per_mvb) => R::Valid(FeeRate::from_sat_per_mvb(per_mvb.to_sat())), + None => R::Error(E::while_doing(MathOp::Mul)), + } + } + /// Constructs a new [`FeeRate`] from satoshis per virtual bytes. pub const fn from_sat_per_vb_u32(sat_vb: u32) -> Self { let sat_vb = sat_vb as u64; // No `Into` in const context. @@ -88,6 +110,15 @@ impl FeeRate { } } + /// Constructs a new [`FeeRate`] from satoshis per kilo virtual bytes (1,000 vbytes). + pub const fn from_per_kvb(rate: Amount) -> NumOpResult { + // No `map()` in const context. + match rate.checked_mul(1_000) { + Some(per_mvb) => R::Valid(FeeRate::from_sat_per_mvb(per_mvb.to_sat())), + None => R::Error(E::while_doing(MathOp::Mul)), + } + } + /// Converts to sat/kwu rounding down. pub const fn to_sat_per_kwu_floor(self) -> u64 { self.to_sat_per_mvb() / 4_000 }