computer: store part 4

This commit is contained in:
nym21
2025-07-01 13:57:48 +02:00
parent 6e996797b8
commit a0a13eb2a8
10 changed files with 455 additions and 60 deletions

View File

@@ -31,22 +31,26 @@ impl AddressData {
}
}
pub fn receive(&mut self, amount: Sats, price: Dollars) {
pub fn receive(&mut self, amount: Sats, price: Option<Dollars>) {
self.received += amount;
self.outputs_len += 1;
self.realized_cap += price * amount;
if let Some(price) = price {
self.realized_cap += price * amount;
}
}
pub fn send(&mut self, amount: Sats, previous_price: Dollars) -> Result<()> {
pub fn send(&mut self, amount: Sats, previous_price: Option<Dollars>) -> Result<()> {
if self.amount() < amount {
return Err(Error::String("Previous_amount smaller than sent amount"));
}
self.sent += amount;
self.outputs_len -= 1;
self.realized_cap = self
.realized_cap
.checked_sub(previous_price * amount)
.unwrap();
if let Some(previous_price) = previous_price {
self.realized_cap = self
.realized_cap
.checked_sub(previous_price * amount)
.unwrap();
}
Ok(())
}
}

View File

@@ -50,6 +50,23 @@ impl OutputType {
}
}
pub fn is_address(&self) -> bool {
match self {
Self::P2PK65 => true,
Self::P2PK33 => true,
Self::P2PKH => true,
Self::P2MS => false,
Self::P2SH => true,
Self::OpReturn => false,
Self::P2WPKH => true,
Self::P2WSH => true,
Self::P2TR => true,
Self::P2A => true,
Self::Empty => false,
Self::Unknown => false,
}
}
pub fn is_unspendable(&self) -> bool {
!self.is_spendable()
}