global: utxos part 2

This commit is contained in:
nym21
2025-05-17 19:51:52 +02:00
parent 7b38355cd4
commit c8a25934a6
15 changed files with 435 additions and 60 deletions

View File

@@ -3,6 +3,8 @@ use std::ops::{Add, Div, Mul};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::CheckedSub;
use super::Dollars;
#[derive(
@@ -88,3 +90,9 @@ impl Mul<usize> for Cents {
Self(self.0 * rhs as u64)
}
}
impl CheckedSub for Cents {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Cents::from)
}
}

View File

@@ -1,12 +1,14 @@
use std::{
f64,
ops::{Add, Div, Mul},
ops::{Add, AddAssign, Div, Mul},
};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::CheckedSub;
use super::{Bitcoin, Cents, Close, Sats, StoredF32, StoredF64};
#[derive(
@@ -170,3 +172,17 @@ impl From<Dollars> for u128 {
u128::from(Cents::from(value))
}
}
impl AddAssign for Dollars {
fn add_assign(&mut self, rhs: Self) {
*self = Dollars::from(Cents::from(*self) + Cents::from(rhs));
}
}
impl CheckedSub for Dollars {
fn checked_sub(self, rhs: Self) -> Option<Self> {
Cents::from(self)
.checked_sub(Cents::from(rhs))
.map(Dollars::from)
}
}

View File

@@ -28,10 +28,12 @@ use super::{Bitcoin, Cents, Dollars, Height};
)]
pub struct Sats(u64);
#[allow(clippy::inconsistent_digit_grouping)]
impl Sats {
pub const ZERO: Self = Self(0);
pub const MAX: Self = Self(u64::MAX);
pub const ONE_BTC: Self = Self(100_000_000);
pub const ONE_BTC: Self = Self(1_00_000_000);
pub const FIFTY_BTC: Self = Self(50_00_000_000);
pub fn is_zero(&self) -> bool {
*self == Self::ZERO

View File

@@ -1,6 +1,6 @@
use std::ops::{Add, Div};
use derive_deref::Deref;
use derive_deref::{Deref, DerefMut};
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
@@ -15,6 +15,7 @@ use super::{
#[derive(
Debug,
Deref,
DerefMut,
Clone,
Default,
Copy,