use core::fmt;
use core::ops::{Div, Mul};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::amount::Amount;
use crate::weight::Weight;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct FeeRate(u64);
impl FeeRate {
pub const ZERO: FeeRate = FeeRate(0);
pub const MIN: FeeRate = FeeRate::ZERO;
pub const MAX: FeeRate = FeeRate(u64::MAX);
pub const BROADCAST_MIN: FeeRate = FeeRate::from_sat_per_vb_unchecked(1);
pub const DUST: FeeRate = FeeRate::from_sat_per_vb_unchecked(3);
pub const fn from_sat_per_kwu(sat_kwu: u64) -> Self { FeeRate(sat_kwu) }
pub fn from_sat_per_vb(sat_vb: u64) -> Option<Self> {
Some(FeeRate(sat_vb.checked_mul(1000 / 4)?))
}
pub const fn from_sat_per_vb_unchecked(sat_vb: u64) -> Self { FeeRate(sat_vb * (1000 / 4)) }
pub const fn to_sat_per_kwu(self) -> u64 { self.0 }
pub const fn to_sat_per_vb_floor(self) -> u64 { self.0 / (1000 / 4) }
pub const fn to_sat_per_vb_ceil(self) -> u64 { (self.0 + (1000 / 4 - 1)) / (1000 / 4) }
pub fn checked_mul(self, rhs: u64) -> Option<Self> { self.0.checked_mul(rhs).map(Self) }
pub fn checked_div(self, rhs: u64) -> Option<Self> { self.0.checked_div(rhs).map(Self) }
pub fn checked_mul_by_weight(self, rhs: Weight) -> Option<Amount> {
let sats = self.0.checked_mul(rhs.to_wu())?.checked_add(999)? / 1000;
Some(Amount::from_sat(sats))
}
pub fn fee_wu(self, weight: Weight) -> Option<Amount> { self.checked_mul_by_weight(weight) }
pub fn fee_vb(self, vb: u64) -> Option<Amount> {
Weight::from_vb(vb).and_then(|w| self.fee_wu(w))
}
}
impl fmt::Display for FeeRate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if f.alternate() {
write!(f, "{}.00 sat/vbyte", self.to_sat_per_vb_ceil())
} else {
fmt::Display::fmt(&self.0, f)
}
}
}
impl From<FeeRate> for u64 {
fn from(value: FeeRate) -> Self { value.to_sat_per_kwu() }
}
impl Mul<FeeRate> for Weight {
type Output = Amount;
fn mul(self, rhs: FeeRate) -> Self::Output {
Amount::from_sat((rhs.to_sat_per_kwu() * self.to_wu() + 999) / 1000)
}
}
impl Mul<Weight> for FeeRate {
type Output = Amount;
fn mul(self, rhs: Weight) -> Self::Output { rhs * self }
}
impl Div<Weight> for Amount {
type Output = FeeRate;
fn div(self, rhs: Weight) -> Self::Output { FeeRate(self.to_sat() * 1000 / rhs.to_wu()) }
}
crate::impl_parse_str_from_int_infallible!(FeeRate, u64, from_sat_per_kwu);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fee_rate_const_test() {
assert_eq!(0, FeeRate::ZERO.to_sat_per_kwu());
assert_eq!(u64::MIN, FeeRate::MIN.to_sat_per_kwu());
assert_eq!(u64::MAX, FeeRate::MAX.to_sat_per_kwu());
assert_eq!(250, FeeRate::BROADCAST_MIN.to_sat_per_kwu());
assert_eq!(750, FeeRate::DUST.to_sat_per_kwu());
}
#[test]
fn fee_rate_from_sat_per_vb_test() {
let fee_rate = FeeRate::from_sat_per_vb(10).expect("expected feerate in sat/kwu");
assert_eq!(FeeRate(2500), fee_rate);
}
#[test]
fn fee_rate_from_sat_per_vb_overflow_test() {
let fee_rate = FeeRate::from_sat_per_vb(u64::MAX);
assert!(fee_rate.is_none());
}
#[test]
fn from_sat_per_vb_unchecked_test() {
let fee_rate = FeeRate::from_sat_per_vb_unchecked(10);
assert_eq!(FeeRate(2500), fee_rate);
}
#[test]
#[cfg(debug_assertions)]
#[should_panic]
fn from_sat_per_vb_unchecked_panic_test() { FeeRate::from_sat_per_vb_unchecked(u64::MAX); }
#[test]
fn raw_feerate_test() {
let fee_rate = FeeRate(333);
assert_eq!(333, fee_rate.to_sat_per_kwu());
assert_eq!(1, fee_rate.to_sat_per_vb_floor());
assert_eq!(2, fee_rate.to_sat_per_vb_ceil());
}
#[test]
fn checked_mul_test() {
let fee_rate = FeeRate(10).checked_mul(10).expect("expected feerate in sat/kwu");
assert_eq!(FeeRate(100), fee_rate);
let fee_rate = FeeRate(10).checked_mul(u64::MAX);
assert!(fee_rate.is_none());
}
#[test]
fn checked_weight_mul_test() {
let weight = Weight::from_vb(10).unwrap();
let fee: Amount = FeeRate::from_sat_per_vb(10)
.unwrap()
.checked_mul_by_weight(weight)
.expect("expected Amount");
assert_eq!(Amount::from_sat(100), fee);
let fee = FeeRate(10).checked_mul_by_weight(Weight::MAX);
assert!(fee.is_none());
let weight = Weight::from_vb(3).unwrap();
let fee_rate = FeeRate::from_sat_per_vb(3).unwrap();
let fee = fee_rate.checked_mul_by_weight(weight).unwrap();
assert_eq!(Amount::from_sat(9), fee);
}
#[test]
fn checked_div_test() {
let fee_rate = FeeRate(10).checked_div(10).expect("expected feerate in sat/kwu");
assert_eq!(FeeRate(1), fee_rate);
let fee_rate = FeeRate(10).checked_div(0);
assert!(fee_rate.is_none());
}
}