mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-05-06 12:19:09 -07:00
computer: store part 8
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user