Skip to content

Commit aab9c2d

Browse files
committed
Merge rust-bitcoin#4614: Add fee rate constructors that take Amount as arg
6ed3fd6 Add fee rate constructors that take Amount as arg (Tobin C. Harding) c1a760b units: Use singular in rustdoc (Tobin C. Harding) Pull request description: Some users may find it more ergonomic to pass in an `Amount` when constructing fee rates. Also the strong type adds some semantic meaning as well as imposes the `Amount::MAX` limit. Add an equivalent constructor for each of the existing ones that uses an argument of type `Amount` instead of `u64` sats. (This was pulled out of rust-bitcoin#4610.) Close: rust-bitcoin#4734 ACKs for top commit: apoelstra: ACK 6ed3fd6; successfully ran local tests Tree-SHA512: 92609457ba181370e1484c08028cec9158d6e1fb2ee984e4e8caeacc5c9828bc70febeb4fd3601d9a2fee57c65d5cee0af0ef95ea15e2387d5fd886879c2afb1
2 parents 9b88d87 + 6ed3fd6 commit aab9c2d

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

units/src/fee_rate/mod.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ use core::ops;
1111
#[cfg(feature = "arbitrary")]
1212
use arbitrary::{Arbitrary, Unstructured};
1313

14+
use NumOpResult as R;
15+
16+
use crate::{Amount,MathOp, NumOpError as E, NumOpResult};
17+
1418
mod encapsulate {
1519
/// Fee rate.
1620
///
@@ -62,7 +66,16 @@ impl FeeRate {
6266
}
6367
}
6468

65-
/// Constructs a new [`FeeRate`] from satoshis per virtual bytes,
69+
/// Constructs a new [`FeeRate`] from amount per 1000 weight units.
70+
pub const fn from_per_kwu(rate: Amount) -> NumOpResult<Self> {
71+
// No `map()` in const context.
72+
match rate.checked_mul(4_000) {
73+
Some(per_mvb) => R::Valid(FeeRate::from_sat_per_mvb(per_mvb.to_sat())),
74+
None => R::Error(E::while_doing(MathOp::Mul)),
75+
}
76+
}
77+
78+
/// Constructs a new [`FeeRate`] from satoshis per virtual byte,
6679
/// returning `None` if overflow occurred.
6780
pub const fn from_sat_per_vb(sat_vb: u64) -> Option<Self> {
6881
// No `map()` in const context.
@@ -72,6 +85,15 @@ impl FeeRate {
7285
}
7386
}
7487

88+
/// Constructs a new [`FeeRate`] from amount per virtual byte.
89+
pub const fn from_per_vb(rate: Amount) -> NumOpResult<Self> {
90+
// No `map()` in const context.
91+
match rate.checked_mul(1_000_000) {
92+
Some(per_mvb) => R::Valid(FeeRate::from_sat_per_mvb(per_mvb.to_sat())),
93+
None => R::Error(E::while_doing(MathOp::Mul)),
94+
}
95+
}
96+
7597
/// Constructs a new [`FeeRate`] from satoshis per virtual bytes.
7698
pub const fn from_sat_per_vb_u32(sat_vb: u32) -> Self {
7799
let sat_vb = sat_vb as u64; // No `Into` in const context.
@@ -88,6 +110,15 @@ impl FeeRate {
88110
}
89111
}
90112

113+
/// Constructs a new [`FeeRate`] from satoshis per kilo virtual bytes (1,000 vbytes).
114+
pub const fn from_per_kvb(rate: Amount) -> NumOpResult<Self> {
115+
// No `map()` in const context.
116+
match rate.checked_mul(1_000) {
117+
Some(per_mvb) => R::Valid(FeeRate::from_sat_per_mvb(per_mvb.to_sat())),
118+
None => R::Error(E::while_doing(MathOp::Mul)),
119+
}
120+
}
121+
91122
/// Converts to sat/kwu rounding down.
92123
pub const fn to_sat_per_kwu_floor(self) -> u64 { self.to_sat_per_mvb() / 4_000 }
93124

0 commit comments

Comments
 (0)