global: snapshot

This commit is contained in:
nym21
2025-03-10 23:08:07 +01:00
parent 9428beeae5
commit db70b05088
30 changed files with 326 additions and 189 deletions

View File

@@ -1,14 +1,31 @@
use std::ops::Mul;
use super::Sats;
#[derive(Debug, Default, Clone, Copy)]
pub struct Bitcoin(f64);
impl Bitcoin {
const ONE: Self = Self(100_000_000.0);
impl Mul for Bitcoin {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self(self.0 * rhs.0)
}
}
impl From<Sats> for Bitcoin {
fn from(value: Sats) -> Self {
Self(f64::from(value) / Self::ONE.0)
Self(u64::from(value) as f64 / (u64::from(Sats::ONE_BTC) as f64))
}
}
impl From<f64> for Bitcoin {
fn from(value: f64) -> Self {
Self(value)
}
}
impl From<Bitcoin> for f64 {
fn from(value: Bitcoin) -> Self {
value.0
}
}

View File

@@ -1,13 +1,13 @@
use std::{
iter::Sum,
ops::{Add, AddAssign, Mul, Sub, SubAssign},
ops::{Add, AddAssign, Div, Mul, Sub, SubAssign},
};
use bitcoin::Amount;
use serde::Serialize;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
use super::Height;
use super::{Bitcoin, Dollars, Height};
#[derive(
Debug,
@@ -28,6 +28,7 @@ pub struct Sats(u64);
impl Sats {
pub const ZERO: Self = Self(0);
pub const ONE_BTC: Self = Self(100_000_000);
pub fn is_zero(&self) -> bool {
*self == Self::ZERO
@@ -88,6 +89,13 @@ impl Sum for Sats {
}
}
impl Div<Dollars> for Sats {
type Output = Self;
fn div(self, rhs: Dollars) -> Self::Output {
Self((self.0 as f64 / f64::from(rhs)) as u64)
}
}
impl From<u64> for Sats {
fn from(value: u64) -> Self {
Self(value)
@@ -105,8 +113,14 @@ impl From<Sats> for Amount {
}
}
impl From<Sats> for f64 {
fn from(value: Sats) -> Self {
value.0 as f64
impl From<Bitcoin> for Sats {
fn from(value: Bitcoin) -> Self {
Self((f64::from(value) * (u64::from(Sats::ONE_BTC) as f64)) as u64)
}
}
impl From<Sats> for u64 {
fn from(value: Sats) -> Self {
value.0
}
}