Skip to main content

brk_types/
sats_fract.rs

1use std::{
2    cmp::Ordering,
3    f64,
4    iter::Sum,
5    ops::{Add, AddAssign, Div, Mul, Sub},
6};
7
8use derive_more::Deref;
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11use vecdb::{CheckedSub, Formattable, Pco};
12
13use crate::{Close, Dollars};
14
15/// Fractional satoshis (f64) - for representing USD prices in sats
16///
17/// Formula: `sats_fract = usd_value * 100_000_000 / btc_price`
18///
19/// When BTC is $100,000:
20/// - $1 = 1,000 sats
21/// - $0.001 = 1 sat
22/// - $0.0001 = 0.1 sats (fractional)
23#[derive(Debug, Deref, Default, Clone, Copy, Serialize, Deserialize, Pco, JsonSchema)]
24pub struct SatsFract(f64);
25
26impl SatsFract {
27    pub const ZERO: Self = Self(0.0);
28    pub const NAN: Self = Self(f64::NAN);
29    pub const ONE_BTC: Self = Self(100_000_000.0);
30    pub const SATS_PER_BTC: f64 = 100_000_000.0;
31
32    pub const fn new(sats: f64) -> Self {
33        Self(sats)
34    }
35
36    pub fn is_nan(&self) -> bool {
37        self.0.is_nan()
38    }
39}
40
41impl From<f64> for SatsFract {
42    #[inline]
43    fn from(value: f64) -> Self {
44        Self(value)
45    }
46}
47
48impl From<f32> for SatsFract {
49    #[inline]
50    fn from(value: f32) -> Self {
51        Self(value as f64)
52    }
53}
54
55impl From<usize> for SatsFract {
56    #[inline]
57    fn from(value: usize) -> Self {
58        Self(value as f64)
59    }
60}
61
62impl From<SatsFract> for f64 {
63    #[inline]
64    fn from(value: SatsFract) -> Self {
65        value.0
66    }
67}
68
69impl From<SatsFract> for f32 {
70    #[inline]
71    fn from(value: SatsFract) -> Self {
72        value.0 as f32
73    }
74}
75
76impl Add for SatsFract {
77    type Output = Self;
78    fn add(self, rhs: Self) -> Self::Output {
79        Self(self.0 + rhs.0)
80    }
81}
82
83impl AddAssign for SatsFract {
84    fn add_assign(&mut self, rhs: Self) {
85        *self = *self + rhs
86    }
87}
88
89impl Sub for SatsFract {
90    type Output = Self;
91    fn sub(self, rhs: Self) -> Self::Output {
92        Self(self.0 - rhs.0)
93    }
94}
95
96impl Mul for SatsFract {
97    type Output = Self;
98    fn mul(self, rhs: Self) -> Self::Output {
99        Self(self.0 * rhs.0)
100    }
101}
102
103impl Mul<usize> for SatsFract {
104    type Output = Self;
105    fn mul(self, rhs: usize) -> Self::Output {
106        Self(self.0 * rhs as f64)
107    }
108}
109
110impl Div<usize> for SatsFract {
111    type Output = Self;
112    fn div(self, rhs: usize) -> Self::Output {
113        if rhs == 0 {
114            Self::NAN
115        } else {
116            Self(self.0 / rhs as f64)
117        }
118    }
119}
120
121impl Div for SatsFract {
122    type Output = Self;
123    fn div(self, rhs: Self) -> Self::Output {
124        if rhs.0 == 0.0 {
125            Self::NAN
126        } else {
127            Self(self.0 / rhs.0)
128        }
129    }
130}
131
132impl CheckedSub for SatsFract {
133    fn checked_sub(self, rhs: Self) -> Option<Self> {
134        Some(Self(self.0 - rhs.0))
135    }
136}
137
138impl CheckedSub<usize> for SatsFract {
139    fn checked_sub(self, rhs: usize) -> Option<Self> {
140        Some(Self(self.0 - rhs as f64))
141    }
142}
143
144impl PartialEq for SatsFract {
145    fn eq(&self, other: &Self) -> bool {
146        match (self.0.is_nan(), other.0.is_nan()) {
147            (true, true) => true,
148            (true, false) | (false, true) => false,
149            (false, false) => self.0 == other.0,
150        }
151    }
152}
153
154impl Eq for SatsFract {}
155
156#[allow(clippy::derive_ord_xor_partial_ord)]
157impl PartialOrd for SatsFract {
158    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
159        Some(self.cmp(other))
160    }
161}
162
163#[allow(clippy::derive_ord_xor_partial_ord)]
164impl Ord for SatsFract {
165    fn cmp(&self, other: &Self) -> Ordering {
166        match (self.0.is_nan(), other.0.is_nan()) {
167            (true, true) => Ordering::Equal,
168            (true, false) => Ordering::Less,
169            (false, true) => Ordering::Greater,
170            (false, false) => self.0.partial_cmp(&other.0).unwrap(),
171        }
172    }
173}
174
175impl Sum for SatsFract {
176    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
177        Self(iter.map(|v| v.0).sum::<f64>())
178    }
179}
180
181impl std::fmt::Display for SatsFract {
182    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183        let mut buf = ryu::Buffer::new();
184        let str = buf.format(self.0);
185        f.write_str(str)
186    }
187}
188
189impl Formattable for SatsFract {
190    #[inline(always)]
191    fn write_to(&self, buf: &mut Vec<u8>) {
192        if self.0.is_finite() {
193            let mut b = ryu::Buffer::new();
194            buf.extend_from_slice(b.format(self.0).as_bytes());
195        }
196    }
197
198    #[inline(always)]
199    fn fmt_json(&self, buf: &mut Vec<u8>) {
200        if self.0.is_finite() {
201            self.write_to(buf);
202        } else {
203            buf.extend_from_slice(b"null");
204        }
205    }
206}
207
208impl Div<Dollars> for SatsFract {
209    type Output = Self;
210    fn div(self, rhs: Dollars) -> Self::Output {
211        let rhs = f64::from(rhs);
212        if rhs == 0.0 {
213            Self::NAN
214        } else {
215            Self(self.0 / rhs)
216        }
217    }
218}
219
220impl Div<Close<Dollars>> for SatsFract {
221    type Output = Self;
222    fn div(self, rhs: Close<Dollars>) -> Self::Output {
223        self / *rhs
224    }
225}