mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-05-06 12:19:09 -07:00
51 lines
1.3 KiB
Rust
51 lines
1.3 KiB
Rust
use std::ops::{Add, AddAssign, SubAssign};
|
|
|
|
use brk_core::{CheckedSub, LoadedAddressData, Sats};
|
|
use serde::Serialize;
|
|
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
|
|
|
#[derive(Debug, Default, Clone, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize)]
|
|
pub struct SupplyState {
|
|
pub utxos: usize,
|
|
pub value: Sats,
|
|
}
|
|
|
|
impl Add<SupplyState> for SupplyState {
|
|
type Output = Self;
|
|
fn add(self, rhs: SupplyState) -> Self::Output {
|
|
Self {
|
|
utxos: self.utxos + rhs.utxos,
|
|
value: self.value + rhs.value,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AddAssign<SupplyState> for SupplyState {
|
|
fn add_assign(&mut self, rhs: Self) {
|
|
*self += &rhs;
|
|
}
|
|
}
|
|
|
|
impl AddAssign<&SupplyState> for SupplyState {
|
|
fn add_assign(&mut self, rhs: &Self) {
|
|
self.utxos += rhs.utxos;
|
|
self.value += rhs.value;
|
|
}
|
|
}
|
|
|
|
impl SubAssign<&SupplyState> for SupplyState {
|
|
fn sub_assign(&mut self, rhs: &Self) {
|
|
self.utxos = self.utxos.checked_sub(rhs.utxos).unwrap();
|
|
self.value = self.value.checked_sub(rhs.value).unwrap();
|
|
}
|
|
}
|
|
|
|
impl From<&LoadedAddressData> for SupplyState {
|
|
fn from(value: &LoadedAddressData) -> Self {
|
|
Self {
|
|
utxos: value.outputs_len as usize,
|
|
value: value.amount(),
|
|
}
|
|
}
|
|
}
|