computer: store part 8

This commit is contained in:
nym21
2025-07-03 18:19:36 +02:00
parent 5810276156
commit be4e693a27
29 changed files with 745 additions and 364 deletions

View File

@@ -1,10 +1,9 @@
use byteview::ByteView;
use zerocopy::{FromBytes, IntoBytes};
use zerocopy_derive::{FromBytes, Immutable, KnownLayout};
use crate::{CheckedSub, Dollars, EmptyAddressData, Error, Result, Sats};
use crate::{Bitcoin, CheckedSub, Dollars, EmptyAddressData, Error, Result, Sats};
#[derive(Debug, Default, Clone, FromBytes, Immutable, KnownLayout)]
#[derive(Debug, Default, Clone)]
#[repr(C)]
pub struct AddressData {
pub sent: Sats,
@@ -18,6 +17,10 @@ impl AddressData {
(u64::from(self.received) - u64::from(self.sent)).into()
}
pub fn realized_price(&self) -> Dollars {
(self.realized_cap / Bitcoin::from(self.amount())).round_nearest_cent()
}
#[inline(always)]
pub fn has_0_sats(&self) -> bool {
self.amount() == Sats::ZERO
@@ -70,7 +73,13 @@ impl From<&EmptyAddressData> for AddressData {
impl From<ByteView> for AddressData {
fn from(value: ByteView) -> Self {
Self::read_from_bytes(&value).unwrap()
Self {
// MUST be same order as impl From<&AddressData> for ByteView
sent: Sats::read_from_bytes(&value[..8]).unwrap(),
received: Sats::read_from_bytes(&value[8..16]).unwrap(),
realized_cap: Dollars::read_from_bytes(&value[16..24]).unwrap(),
outputs_len: u32::read_from_bytes(&value[24..]).unwrap(),
}
}
}
impl From<AddressData> for ByteView {

View File

@@ -38,6 +38,10 @@ impl Dollars {
pub const fn mint(dollars: f64) -> Self {
Self(dollars)
}
pub fn round_nearest_cent(self) -> Self {
Dollars((self.0 * 100.0).round() / 100.0)
}
}
impl From<f32> for Dollars {