mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-27 16:19:59 -07:00
computer: fixes
This commit is contained in:
@@ -45,8 +45,8 @@ pub struct RealizedMetrics {
|
||||
pub investor_price_extra: ComputedFromDateRatio,
|
||||
|
||||
// === Floor/Ceiling Price Bands (lazy: realized²/investor, investor²/realized) ===
|
||||
pub floor_price: LazyBinaryPriceFromHeight,
|
||||
pub ceiling_price: LazyBinaryPriceFromHeight,
|
||||
pub lower_price_band: LazyBinaryPriceFromHeight,
|
||||
pub upper_price_band: LazyBinaryPriceFromHeight,
|
||||
|
||||
// === Raw values for aggregation (needed to compute investor_price for aggregated cohorts) ===
|
||||
/// Raw Σ(price × sats) for realized cap aggregation
|
||||
@@ -292,24 +292,22 @@ impl RealizedMetrics {
|
||||
)?;
|
||||
|
||||
// Floor price = realized² / investor (lower band)
|
||||
let floor_price = LazyBinaryPriceFromHeight::forced_import::<DollarsSquaredDivide>(
|
||||
cfg.db,
|
||||
&cfg.name("floor_price"),
|
||||
cfg.version,
|
||||
realized_price.dollars.height.boxed_clone(),
|
||||
investor_price.dollars.height.boxed_clone(),
|
||||
cfg.indexes,
|
||||
)?;
|
||||
let lower_price_band =
|
||||
LazyBinaryPriceFromHeight::from_price_and_lazy_price::<DollarsSquaredDivide>(
|
||||
&cfg.name("lower_price_band"),
|
||||
cfg.version,
|
||||
&realized_price,
|
||||
&investor_price,
|
||||
);
|
||||
|
||||
// Ceiling price = investor² / realized (upper band)
|
||||
let ceiling_price = LazyBinaryPriceFromHeight::forced_import::<DollarsSquaredDivide>(
|
||||
cfg.db,
|
||||
&cfg.name("ceiling_price"),
|
||||
cfg.version,
|
||||
investor_price.dollars.height.boxed_clone(),
|
||||
realized_price.dollars.height.boxed_clone(),
|
||||
cfg.indexes,
|
||||
)?;
|
||||
let upper_price_band =
|
||||
LazyBinaryPriceFromHeight::from_lazy_price_and_price::<DollarsSquaredDivide>(
|
||||
&cfg.name("upper_price_band"),
|
||||
cfg.version,
|
||||
&investor_price,
|
||||
&realized_price,
|
||||
);
|
||||
|
||||
// Raw values for aggregation
|
||||
let cap_raw = BytesVec::forced_import(cfg.db, &cfg.name("cap_raw"), cfg.version)?;
|
||||
@@ -456,8 +454,8 @@ impl RealizedMetrics {
|
||||
investor_price_extra,
|
||||
|
||||
// === Floor/Ceiling Price Bands ===
|
||||
floor_price,
|
||||
ceiling_price,
|
||||
lower_price_band,
|
||||
upper_price_band,
|
||||
|
||||
cap_raw,
|
||||
investor_cap_raw,
|
||||
@@ -1178,10 +1176,6 @@ impl RealizedMetrics {
|
||||
)?;
|
||||
}
|
||||
|
||||
// Floor/ceiling price bands (derive stored dateindex from lazy height)
|
||||
self.floor_price.dollars.derive_from(indexes, starting_indexes, exit)?;
|
||||
self.ceiling_price.dollars.derive_from(indexes, starting_indexes, exit)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,11 +68,17 @@ impl CohortState {
|
||||
}
|
||||
|
||||
pub fn cost_basis_data_first_key_value(&self) -> Option<(CentsUnsigned, &Sats)> {
|
||||
self.cost_basis_data.as_ref()?.first_key_value().map(|(k, v)| (k.into(), v))
|
||||
self.cost_basis_data
|
||||
.as_ref()?
|
||||
.first_key_value()
|
||||
.map(|(k, v)| (k.into(), v))
|
||||
}
|
||||
|
||||
pub fn cost_basis_data_last_key_value(&self) -> Option<(CentsUnsigned, &Sats)> {
|
||||
self.cost_basis_data.as_ref()?.last_key_value().map(|(k, v)| (k.into(), v))
|
||||
self.cost_basis_data
|
||||
.as_ref()?
|
||||
.last_key_value()
|
||||
.map(|(k, v)| (k.into(), v))
|
||||
}
|
||||
|
||||
pub fn reset_single_iteration_values(&mut self) {
|
||||
@@ -94,11 +100,10 @@ impl CohortState {
|
||||
pub fn increment_snapshot(&mut self, s: &CostBasisSnapshot) {
|
||||
self.supply += &s.supply_state;
|
||||
|
||||
if s.supply_state.value > Sats::ZERO && self.realized.is_some() {
|
||||
self.realized
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.increment_snapshot(s.price_sats, s.investor_cap);
|
||||
if s.supply_state.value > Sats::ZERO
|
||||
&& let Some(realized) = self.realized.as_mut()
|
||||
{
|
||||
realized.increment_snapshot(s.price_sats, s.investor_cap);
|
||||
self.cost_basis_data.as_mut().unwrap().increment(
|
||||
s.realized_price,
|
||||
s.supply_state.value,
|
||||
@@ -118,11 +123,10 @@ impl CohortState {
|
||||
pub fn decrement_snapshot(&mut self, s: &CostBasisSnapshot) {
|
||||
self.supply -= &s.supply_state;
|
||||
|
||||
if s.supply_state.value > Sats::ZERO && self.realized.is_some() {
|
||||
self.realized
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.decrement_snapshot(s.price_sats, s.investor_cap);
|
||||
if s.supply_state.value > Sats::ZERO
|
||||
&& let Some(realized) = self.realized.as_mut()
|
||||
{
|
||||
realized.decrement_snapshot(s.price_sats, s.investor_cap);
|
||||
self.cost_basis_data.as_mut().unwrap().decrement(
|
||||
s.realized_price,
|
||||
s.supply_state.value,
|
||||
@@ -147,12 +151,10 @@ impl CohortState {
|
||||
|
||||
realized.receive(price, sats);
|
||||
|
||||
self.cost_basis_data.as_mut().unwrap().increment(
|
||||
price,
|
||||
sats,
|
||||
price_sats,
|
||||
investor_cap,
|
||||
);
|
||||
self.cost_basis_data
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.increment(price, sats, price_sats, investor_cap);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,7 +300,10 @@ impl CohortState {
|
||||
) -> (UnrealizedState, Option<UnrealizedState>) {
|
||||
match self.cost_basis_data.as_mut() {
|
||||
Some(p) => p.compute_unrealized_states(height_price, date_price),
|
||||
None => (UnrealizedState::ZERO, date_price.map(|_| UnrealizedState::ZERO)),
|
||||
None => (
|
||||
UnrealizedState::ZERO,
|
||||
date_price.map(|_| UnrealizedState::ZERO),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,16 +315,22 @@ impl CohortState {
|
||||
}
|
||||
|
||||
pub fn min_price(&self) -> Option<CentsUnsigned> {
|
||||
self.cost_basis_data.as_ref()?.first_key_value().map(|(k, _)| k.into())
|
||||
self.cost_basis_data
|
||||
.as_ref()?
|
||||
.first_key_value()
|
||||
.map(|(k, _)| k.into())
|
||||
}
|
||||
|
||||
pub fn max_price(&self) -> Option<CentsUnsigned> {
|
||||
self.cost_basis_data.as_ref()?.last_key_value().map(|(k, _)| k.into())
|
||||
self.cost_basis_data
|
||||
.as_ref()?
|
||||
.last_key_value()
|
||||
.map(|(k, _)| k.into())
|
||||
}
|
||||
|
||||
pub fn cost_basis_data_iter(
|
||||
&self,
|
||||
) -> Option<impl Iterator<Item = (CentsUnsigned, &Sats)>> {
|
||||
self.cost_basis_data.as_ref().map(|p| p.iter().map(|(k, v)| (k.into(), v)))
|
||||
pub fn cost_basis_data_iter(&self) -> Option<impl Iterator<Item = (CentsUnsigned, &Sats)>> {
|
||||
self.cost_basis_data
|
||||
.as_ref()
|
||||
.map(|p| p.iter().map(|(k, v)| (k.into(), v)))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user