mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-05-14 07:38:36 -07:00
brk: first commit
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
use std::ops::AddAssign;
|
||||
|
||||
use allocative::Allocative;
|
||||
|
||||
use crate::structs::Price;
|
||||
|
||||
#[derive(Debug, Default, Allocative)]
|
||||
pub struct CapitalizationState {
|
||||
realized_cap: Price,
|
||||
}
|
||||
|
||||
impl CapitalizationState {
|
||||
pub fn realized_cap(&self) -> Price {
|
||||
self.realized_cap
|
||||
}
|
||||
|
||||
pub fn increment(&mut self, realized_cap: Price) {
|
||||
self.realized_cap += realized_cap;
|
||||
}
|
||||
|
||||
pub fn decrement(&mut self, realized_cap: Price) {
|
||||
self.realized_cap -= realized_cap;
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for CapitalizationState {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
self.realized_cap += rhs.realized_cap;
|
||||
}
|
||||
}
|
||||
50
_src/parser/states/cohorts_states/any/durable_states.rs
Normal file
50
_src/parser/states/cohorts_states/any/durable_states.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use std::ops::AddAssign;
|
||||
|
||||
use allocative::Allocative;
|
||||
|
||||
use crate::structs::{Amount, Price};
|
||||
|
||||
use super::{CapitalizationState, SupplyState, UTXOState};
|
||||
|
||||
#[derive(Default, Debug, Allocative)]
|
||||
pub struct DurableStates {
|
||||
pub capitalization_state: CapitalizationState,
|
||||
pub supply_state: SupplyState,
|
||||
pub utxo_state: UTXOState,
|
||||
}
|
||||
|
||||
impl DurableStates {
|
||||
pub fn increment(
|
||||
&mut self,
|
||||
amount: Amount,
|
||||
utxo_count: f64,
|
||||
realized_cap: Price,
|
||||
) -> color_eyre::Result<()> {
|
||||
self.utxo_state.increment(utxo_count);
|
||||
self.capitalization_state.increment(realized_cap);
|
||||
self.supply_state.increment(amount);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn decrement(
|
||||
&mut self,
|
||||
amount: Amount,
|
||||
utxo_count: f64,
|
||||
realized_cap: Price,
|
||||
) -> color_eyre::Result<()> {
|
||||
self.utxo_state.decrement(utxo_count)?;
|
||||
self.capitalization_state.decrement(realized_cap);
|
||||
self.supply_state.decrement(amount)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for DurableStates {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
self.capitalization_state += rhs.capitalization_state;
|
||||
self.supply_state += rhs.supply_state;
|
||||
self.utxo_state += rhs.utxo_state;
|
||||
}
|
||||
}
|
||||
22
_src/parser/states/cohorts_states/any/input_state.rs
Normal file
22
_src/parser/states/cohorts_states/any/input_state.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use crate::structs::Amount;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct InputState {
|
||||
count: f64,
|
||||
volume: Amount,
|
||||
}
|
||||
|
||||
impl InputState {
|
||||
pub fn count(&self) -> f64 {
|
||||
self.count
|
||||
}
|
||||
|
||||
pub fn volume(&self) -> Amount {
|
||||
self.volume
|
||||
}
|
||||
|
||||
pub fn iterate(&mut self, count: f64, volume: Amount) {
|
||||
self.count += count;
|
||||
self.volume += volume;
|
||||
}
|
||||
}
|
||||
23
_src/parser/states/cohorts_states/any/mod.rs
Normal file
23
_src/parser/states/cohorts_states/any/mod.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
mod capitalization_state;
|
||||
mod durable_states;
|
||||
mod input_state;
|
||||
mod one_shot_states;
|
||||
mod output_state;
|
||||
mod price_paid_state;
|
||||
mod price_to_value;
|
||||
mod realized_state;
|
||||
mod supply_state;
|
||||
mod unrealized_state;
|
||||
mod utxo_state;
|
||||
|
||||
pub use capitalization_state::*;
|
||||
pub use durable_states::*;
|
||||
pub use input_state::*;
|
||||
pub use one_shot_states::*;
|
||||
pub use output_state::*;
|
||||
pub use price_paid_state::*;
|
||||
pub use price_to_value::*;
|
||||
pub use realized_state::*;
|
||||
pub use supply_state::*;
|
||||
pub use unrealized_state::*;
|
||||
pub use utxo_state::*;
|
||||
9
_src/parser/states/cohorts_states/any/one_shot_states.rs
Normal file
9
_src/parser/states/cohorts_states/any/one_shot_states.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use super::{PricePaidState, UnrealizedState};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct OneShotStates {
|
||||
pub price_paid_state: PricePaidState,
|
||||
|
||||
pub unrealized_block_state: UnrealizedState,
|
||||
pub unrealized_date_state: Option<UnrealizedState>,
|
||||
}
|
||||
22
_src/parser/states/cohorts_states/any/output_state.rs
Normal file
22
_src/parser/states/cohorts_states/any/output_state.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use crate::structs::Amount;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct OutputState {
|
||||
count: f64,
|
||||
volume: Amount,
|
||||
}
|
||||
|
||||
impl OutputState {
|
||||
// pub fn count(&self) -> f64 {
|
||||
// self.count
|
||||
// }
|
||||
|
||||
// pub fn volume(&self) -> Amount {
|
||||
// self.volume
|
||||
// }
|
||||
|
||||
pub fn iterate(&mut self, count: f64, volume: Amount) {
|
||||
self.count += count;
|
||||
self.volume += volume;
|
||||
}
|
||||
}
|
||||
286
_src/parser/states/cohorts_states/any/price_paid_state.rs
Normal file
286
_src/parser/states/cohorts_states/any/price_paid_state.rs
Normal file
@@ -0,0 +1,286 @@
|
||||
use crate::structs::{Amount, Price};
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct PricePaidState {
|
||||
pp_05p: Option<Price>,
|
||||
pp_10p: Option<Price>,
|
||||
pp_15p: Option<Price>,
|
||||
pp_20p: Option<Price>,
|
||||
pp_25p: Option<Price>,
|
||||
pp_30p: Option<Price>,
|
||||
pp_35p: Option<Price>,
|
||||
pp_40p: Option<Price>,
|
||||
pp_45p: Option<Price>,
|
||||
pp_median: Option<Price>,
|
||||
pp_55p: Option<Price>,
|
||||
pp_60p: Option<Price>,
|
||||
pp_65p: Option<Price>,
|
||||
pp_70p: Option<Price>,
|
||||
pp_75p: Option<Price>,
|
||||
pp_80p: Option<Price>,
|
||||
pp_85p: Option<Price>,
|
||||
pp_90p: Option<Price>,
|
||||
pp_95p: Option<Price>,
|
||||
|
||||
processed_amount: Amount,
|
||||
}
|
||||
|
||||
impl PricePaidState {
|
||||
pub fn pp_05p(&self) -> Option<Price> {
|
||||
self.pp_05p
|
||||
}
|
||||
|
||||
pub fn pp_10p(&self) -> Option<Price> {
|
||||
self.pp_10p
|
||||
}
|
||||
|
||||
pub fn pp_15p(&self) -> Option<Price> {
|
||||
self.pp_15p
|
||||
}
|
||||
|
||||
pub fn pp_20p(&self) -> Option<Price> {
|
||||
self.pp_20p
|
||||
}
|
||||
|
||||
pub fn pp_25p(&self) -> Option<Price> {
|
||||
self.pp_25p
|
||||
}
|
||||
|
||||
pub fn pp_30p(&self) -> Option<Price> {
|
||||
self.pp_30p
|
||||
}
|
||||
|
||||
pub fn pp_35p(&self) -> Option<Price> {
|
||||
self.pp_35p
|
||||
}
|
||||
|
||||
pub fn pp_40p(&self) -> Option<Price> {
|
||||
self.pp_40p
|
||||
}
|
||||
|
||||
pub fn pp_45p(&self) -> Option<Price> {
|
||||
self.pp_45p
|
||||
}
|
||||
|
||||
pub fn pp_median(&self) -> Option<Price> {
|
||||
self.pp_median
|
||||
}
|
||||
|
||||
pub fn pp_55p(&self) -> Option<Price> {
|
||||
self.pp_55p
|
||||
}
|
||||
|
||||
pub fn pp_60p(&self) -> Option<Price> {
|
||||
self.pp_60p
|
||||
}
|
||||
|
||||
pub fn pp_65p(&self) -> Option<Price> {
|
||||
self.pp_65p
|
||||
}
|
||||
|
||||
pub fn pp_70p(&self) -> Option<Price> {
|
||||
self.pp_70p
|
||||
}
|
||||
|
||||
pub fn pp_75p(&self) -> Option<Price> {
|
||||
self.pp_75p
|
||||
}
|
||||
|
||||
pub fn pp_80p(&self) -> Option<Price> {
|
||||
self.pp_80p
|
||||
}
|
||||
|
||||
pub fn pp_85p(&self) -> Option<Price> {
|
||||
self.pp_85p
|
||||
}
|
||||
|
||||
pub fn pp_90p(&self) -> Option<Price> {
|
||||
self.pp_90p
|
||||
}
|
||||
|
||||
pub fn pp_95p(&self) -> Option<Price> {
|
||||
self.pp_95p
|
||||
}
|
||||
|
||||
pub fn iterate(&mut self, price: Price, amount: Amount, supply: Amount) {
|
||||
let PricePaidState {
|
||||
processed_amount: processed_supply,
|
||||
pp_05p,
|
||||
pp_10p,
|
||||
pp_15p,
|
||||
pp_20p,
|
||||
pp_25p,
|
||||
pp_30p,
|
||||
pp_35p,
|
||||
pp_40p,
|
||||
pp_45p,
|
||||
pp_median,
|
||||
pp_55p,
|
||||
pp_60p,
|
||||
pp_65p,
|
||||
pp_70p,
|
||||
pp_75p,
|
||||
pp_80p,
|
||||
pp_85p,
|
||||
pp_90p,
|
||||
pp_95p,
|
||||
} = self;
|
||||
|
||||
*processed_supply += amount;
|
||||
|
||||
if pp_95p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
let processed_sat_amount = processed_supply.to_sat();
|
||||
let total_sat_supply = supply.to_sat();
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 95 / 100 {
|
||||
pp_95p.replace(price);
|
||||
}
|
||||
|
||||
if pp_90p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 90 / 100 {
|
||||
pp_90p.replace(price);
|
||||
}
|
||||
|
||||
if pp_85p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 85 / 100 {
|
||||
pp_85p.replace(price);
|
||||
}
|
||||
|
||||
if pp_80p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 80 / 100 {
|
||||
pp_80p.replace(price);
|
||||
}
|
||||
|
||||
if pp_75p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 75 / 100 {
|
||||
pp_75p.replace(price);
|
||||
}
|
||||
|
||||
if pp_70p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 70 / 100 {
|
||||
pp_70p.replace(price);
|
||||
}
|
||||
|
||||
if pp_65p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 65 / 100 {
|
||||
pp_65p.replace(price);
|
||||
}
|
||||
|
||||
if pp_60p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 60 / 100 {
|
||||
pp_60p.replace(price);
|
||||
}
|
||||
|
||||
if pp_55p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 55 / 100 {
|
||||
pp_55p.replace(price);
|
||||
}
|
||||
|
||||
if pp_median.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply / 2 {
|
||||
pp_median.replace(price);
|
||||
}
|
||||
|
||||
if pp_45p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 45 / 100 {
|
||||
pp_45p.replace(price);
|
||||
}
|
||||
|
||||
if pp_40p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 40 / 100 {
|
||||
pp_40p.replace(price);
|
||||
}
|
||||
|
||||
if pp_35p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 35 / 100 {
|
||||
pp_35p.replace(price);
|
||||
}
|
||||
|
||||
if pp_30p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 30 / 100 {
|
||||
pp_30p.replace(price);
|
||||
}
|
||||
|
||||
if pp_25p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply / 4 {
|
||||
pp_25p.replace(price);
|
||||
}
|
||||
|
||||
if pp_20p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply / 5 {
|
||||
pp_20p.replace(price);
|
||||
}
|
||||
|
||||
if pp_15p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply * 15 / 100 {
|
||||
pp_15p.replace(price);
|
||||
}
|
||||
|
||||
if pp_10p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply / 10 {
|
||||
pp_10p.replace(price);
|
||||
}
|
||||
|
||||
if pp_05p.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if processed_sat_amount >= total_sat_supply / 20 {
|
||||
pp_05p.replace(price);
|
||||
}
|
||||
}
|
||||
}
|
||||
113
_src/parser/states/cohorts_states/any/price_to_value.rs
Normal file
113
_src/parser/states/cohorts_states/any/price_to_value.rs
Normal file
@@ -0,0 +1,113 @@
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
fmt::Debug,
|
||||
ops::{AddAssign, SubAssign},
|
||||
};
|
||||
|
||||
use allocative::Allocative;
|
||||
use color_eyre::eyre::eyre;
|
||||
use derive_deref::{Deref, DerefMut};
|
||||
|
||||
use crate::structs::{Amount, Price};
|
||||
|
||||
#[derive(Deref, DerefMut, Default, Debug, Allocative)]
|
||||
pub struct PriceToValue<T>(BTreeMap<u32, T>);
|
||||
|
||||
impl<T> PriceToValue<T>
|
||||
where
|
||||
T: Default
|
||||
+ Debug
|
||||
+ AddAssign
|
||||
+ SubAssign
|
||||
+ CanSubtract
|
||||
+ Default
|
||||
+ Copy
|
||||
+ Clone
|
||||
+ PartialEq
|
||||
+ IsZero,
|
||||
{
|
||||
pub fn increment(&mut self, price: Price, value: T) {
|
||||
*self.entry(price.to_cent() as u32).or_default() += value;
|
||||
}
|
||||
|
||||
pub fn decrement(&mut self, price: Price, value: T) -> color_eyre::Result<()> {
|
||||
let cent = price.to_cent() as u32;
|
||||
|
||||
let delete = {
|
||||
let self_value = self.get_mut(¢);
|
||||
|
||||
if self_value.is_none() {
|
||||
dbg!(&self.0, price, value);
|
||||
return Err(eyre!("self_value is none"));
|
||||
}
|
||||
|
||||
let self_value = self_value.unwrap();
|
||||
|
||||
if !self_value.can_subtract(&value) {
|
||||
dbg!(*self_value, &self.0, price, value);
|
||||
return Err(eyre!("self value < value"));
|
||||
}
|
||||
|
||||
*self_value -= value;
|
||||
|
||||
self_value.is_zero()?
|
||||
};
|
||||
|
||||
if delete {
|
||||
self.remove(¢).unwrap();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn iterate(&self, supply: T, mut iterate: impl FnMut(Price, T)) {
|
||||
let mut processed = T::default();
|
||||
|
||||
self.iter().for_each(|(cent, value)| {
|
||||
let value = *value;
|
||||
|
||||
processed += value;
|
||||
|
||||
iterate(Price::from_cent(*cent as u64), value)
|
||||
});
|
||||
|
||||
if processed != supply {
|
||||
dbg!(processed, supply);
|
||||
panic!("processed_amount isn't equal to supply")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AddAssign for PriceToValue<T>
|
||||
where
|
||||
T: AddAssign + Copy,
|
||||
{
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
rhs.0.into_iter().for_each(|(key, value)| {
|
||||
self.0
|
||||
.entry(key)
|
||||
.and_modify(|previous| *previous += value)
|
||||
.or_insert(value);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CanSubtract {
|
||||
fn can_subtract(&self, other: &Self) -> bool;
|
||||
}
|
||||
|
||||
impl CanSubtract for Amount {
|
||||
fn can_subtract(&self, other: &Self) -> bool {
|
||||
self >= other
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IsZero {
|
||||
fn is_zero(&self) -> color_eyre::Result<bool>;
|
||||
}
|
||||
|
||||
impl IsZero for Amount {
|
||||
fn is_zero(&self) -> color_eyre::Result<bool> {
|
||||
Ok(*self == Amount::ZERO)
|
||||
}
|
||||
}
|
||||
54
_src/parser/states/cohorts_states/any/realized_state.rs
Normal file
54
_src/parser/states/cohorts_states/any/realized_state.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use crate::structs::Price;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct RealizedState {
|
||||
realized_profit: Price,
|
||||
realized_loss: Price,
|
||||
value_created: Price,
|
||||
adjusted_value_created: Price,
|
||||
value_destroyed: Price,
|
||||
adjusted_value_destroyed: Price,
|
||||
}
|
||||
|
||||
impl RealizedState {
|
||||
pub fn realized_profit(&self) -> Price {
|
||||
self.realized_profit
|
||||
}
|
||||
|
||||
pub fn realized_loss(&self) -> Price {
|
||||
self.realized_loss
|
||||
}
|
||||
|
||||
pub fn value_created(&self) -> Price {
|
||||
self.value_created
|
||||
}
|
||||
|
||||
pub fn adjusted_value_created(&self) -> Price {
|
||||
self.adjusted_value_created
|
||||
}
|
||||
|
||||
pub fn value_destroyed(&self) -> Price {
|
||||
self.value_destroyed
|
||||
}
|
||||
|
||||
pub fn adjusted_value_destroyed(&self) -> Price {
|
||||
self.adjusted_value_destroyed
|
||||
}
|
||||
|
||||
pub fn iterate(
|
||||
&mut self,
|
||||
realized_profit: Price,
|
||||
realized_loss: Price,
|
||||
value_created: Price,
|
||||
adjusted_value_created: Price,
|
||||
value_destroyed: Price,
|
||||
adjusted_value_destroyed: Price,
|
||||
) {
|
||||
self.realized_profit += realized_profit;
|
||||
self.realized_loss += realized_loss;
|
||||
self.value_created += value_created;
|
||||
self.adjusted_value_created += adjusted_value_created;
|
||||
self.value_destroyed += value_destroyed;
|
||||
self.adjusted_value_destroyed += adjusted_value_destroyed;
|
||||
}
|
||||
}
|
||||
39
_src/parser/states/cohorts_states/any/supply_state.rs
Normal file
39
_src/parser/states/cohorts_states/any/supply_state.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use std::ops::AddAssign;
|
||||
|
||||
use allocative::Allocative;
|
||||
use color_eyre::eyre::eyre;
|
||||
|
||||
use crate::structs::Amount;
|
||||
|
||||
#[derive(Debug, Default, Allocative)]
|
||||
pub struct SupplyState {
|
||||
supply: Amount,
|
||||
}
|
||||
|
||||
impl SupplyState {
|
||||
pub fn supply(&self) -> Amount {
|
||||
self.supply
|
||||
}
|
||||
|
||||
pub fn increment(&mut self, amount: Amount) {
|
||||
self.supply += amount;
|
||||
}
|
||||
|
||||
pub fn decrement(&mut self, amount: Amount) -> color_eyre::Result<()> {
|
||||
if self.supply < amount {
|
||||
dbg!(self.supply, amount);
|
||||
|
||||
return Err(eyre!("supply smaller than supply"));
|
||||
}
|
||||
|
||||
self.supply -= amount;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for SupplyState {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
self.supply += rhs.supply;
|
||||
}
|
||||
}
|
||||
50
_src/parser/states/cohorts_states/any/unrealized_state.rs
Normal file
50
_src/parser/states/cohorts_states/any/unrealized_state.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use std::{cmp::Ordering, ops::Add};
|
||||
|
||||
use crate::structs::{Amount, Price};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct UnrealizedState {
|
||||
supply_in_profit: Amount,
|
||||
unrealized_profit: Price,
|
||||
unrealized_loss: Price,
|
||||
}
|
||||
|
||||
impl UnrealizedState {
|
||||
pub fn supply_in_profit(&self) -> Amount {
|
||||
self.supply_in_profit
|
||||
}
|
||||
|
||||
pub fn unrealized_profit(&self) -> Price {
|
||||
self.unrealized_profit
|
||||
}
|
||||
|
||||
pub fn unrealized_loss(&self) -> Price {
|
||||
self.unrealized_loss
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn iterate(&mut self, price_then: Price, price_now: Price, amount: Amount) {
|
||||
match price_then.cmp(&price_now) {
|
||||
Ordering::Less => {
|
||||
self.unrealized_profit += (price_now - price_then) * amount;
|
||||
self.supply_in_profit += amount;
|
||||
}
|
||||
Ordering::Greater => {
|
||||
self.unrealized_loss += (price_then - price_now) * amount;
|
||||
}
|
||||
Ordering::Equal => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<UnrealizedState> for UnrealizedState {
|
||||
type Output = UnrealizedState;
|
||||
|
||||
fn add(self, other: UnrealizedState) -> UnrealizedState {
|
||||
UnrealizedState {
|
||||
supply_in_profit: self.supply_in_profit + other.supply_in_profit,
|
||||
unrealized_profit: self.unrealized_profit + other.unrealized_profit,
|
||||
unrealized_loss: self.unrealized_loss + other.unrealized_loss,
|
||||
}
|
||||
}
|
||||
}
|
||||
37
_src/parser/states/cohorts_states/any/utxo_state.rs
Normal file
37
_src/parser/states/cohorts_states/any/utxo_state.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use std::ops::AddAssign;
|
||||
|
||||
use allocative::Allocative;
|
||||
use color_eyre::eyre::eyre;
|
||||
|
||||
#[derive(Debug, Default, Allocative)]
|
||||
pub struct UTXOState {
|
||||
count: f64,
|
||||
}
|
||||
|
||||
impl UTXOState {
|
||||
pub fn count(&self) -> f64 {
|
||||
self.count
|
||||
}
|
||||
|
||||
pub fn increment(&mut self, utxo_count: f64) {
|
||||
self.count += utxo_count;
|
||||
}
|
||||
|
||||
pub fn decrement(&mut self, utxo_count: f64) -> color_eyre::Result<()> {
|
||||
if self.count < utxo_count {
|
||||
dbg!(self.count, utxo_count);
|
||||
|
||||
return Err(eyre!("self.count smaller than utxo_count"));
|
||||
}
|
||||
|
||||
self.count -= utxo_count;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for UTXOState {
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
self.count += rhs.count;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user