mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-24 09:28:11 -07:00
global: refactor
This commit is contained in:
@@ -3,7 +3,7 @@ use brk_types::{Height, Sats};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
use vecdb::ReadableVec;
|
||||
|
||||
use crate::internal::AmountPerBlock;
|
||||
use crate::internal::ValuePerBlock;
|
||||
|
||||
use super::vecs::ExposedAddrSupplyVecs;
|
||||
|
||||
@@ -24,7 +24,7 @@ impl From<(&ExposedAddrSupplyVecs, Height)> for AddrTypeToExposedSupply {
|
||||
fn from((vecs, starting_height): (&ExposedAddrSupplyVecs, Height)) -> Self {
|
||||
if let Some(prev_height) = starting_height.decremented() {
|
||||
let read =
|
||||
|v: &AmountPerBlock| -> Sats { v.sats.height.collect_one(prev_height).unwrap() };
|
||||
|v: &ValuePerBlock| -> Sats { v.sats.height.collect_one(prev_height).unwrap() };
|
||||
Self(ByAddrType {
|
||||
p2pk65: read(&vecs.by_addr_type.p2pk65),
|
||||
p2pk33: read(&vecs.by_addr_type.p2pk33),
|
||||
|
||||
@@ -6,7 +6,7 @@ use vecdb::{Database, Rw, StorageMode};
|
||||
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{AmountPerBlock, WithAddrTypes},
|
||||
internal::{ValuePerBlock, WithAddrTypes},
|
||||
};
|
||||
|
||||
/// Exposed address supply (sats/btc/cents/usd) — `all` + per-address-type.
|
||||
@@ -15,7 +15,7 @@ use crate::{
|
||||
/// post-hoc from sats × spot price.
|
||||
#[derive(Deref, DerefMut, Traversable)]
|
||||
pub struct ExposedAddrSupplyVecs<M: StorageMode = Rw>(
|
||||
#[traversable(flatten)] pub WithAddrTypes<AmountPerBlock<M>>,
|
||||
#[traversable(flatten)] pub WithAddrTypes<ValuePerBlock<M>>,
|
||||
);
|
||||
|
||||
impl ExposedAddrSupplyVecs {
|
||||
@@ -24,7 +24,7 @@ impl ExposedAddrSupplyVecs {
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
Ok(Self(WithAddrTypes::<AmountPerBlock>::forced_import(
|
||||
Ok(Self(WithAddrTypes::<ValuePerBlock>::forced_import(
|
||||
db,
|
||||
"exposed_supply",
|
||||
version,
|
||||
|
||||
@@ -25,7 +25,7 @@ use crate::{
|
||||
state::UTXOCohortState,
|
||||
},
|
||||
indexes,
|
||||
internal::{AmountPerBlockCumulativeRolling, WindowStartVec, Windows},
|
||||
internal::{ValuePerBlockCumulativeRolling, WindowStartVec, Windows},
|
||||
prices,
|
||||
};
|
||||
|
||||
@@ -50,7 +50,7 @@ pub struct UTXOCohorts<M: StorageMode = Rw> {
|
||||
#[traversable(rename = "type")]
|
||||
pub type_: SpendableType<UTXOCohortVecs<TypeCohortMetrics<M>>>,
|
||||
pub profitability: ProfitabilityMetrics<M>,
|
||||
pub matured: AgeRange<AmountPerBlockCumulativeRolling<M>>,
|
||||
pub matured: AgeRange<ValuePerBlockCumulativeRolling<M>>,
|
||||
#[traversable(skip)]
|
||||
pub(super) caches: UTXOCohortsTransientState,
|
||||
}
|
||||
@@ -264,8 +264,8 @@ impl UTXOCohorts<Rw> {
|
||||
let prefix = CohortContext::Utxo.prefix();
|
||||
let matured = AgeRange::try_new(&|_f: Filter,
|
||||
name: &'static str|
|
||||
-> Result<AmountPerBlockCumulativeRolling> {
|
||||
AmountPerBlockCumulativeRolling::forced_import(
|
||||
-> Result<ValuePerBlockCumulativeRolling> {
|
||||
ValuePerBlockCumulativeRolling::forced_import(
|
||||
db,
|
||||
&format!("{prefix}_{name}_matured_supply"),
|
||||
v,
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::{
|
||||
metrics::ImportConfig,
|
||||
state::{CohortState, CostBasisOps, RealizedOps},
|
||||
},
|
||||
internal::{AmountPerBlockCumulativeRolling, PerBlockCumulativeRolling},
|
||||
internal::{ValuePerBlockCumulativeRolling, PerBlockCumulativeRolling},
|
||||
prices,
|
||||
};
|
||||
|
||||
@@ -24,9 +24,9 @@ pub struct ActivityCore<M: StorageMode = Rw> {
|
||||
|
||||
pub coindays_destroyed: PerBlockCumulativeRolling<StoredF64, StoredF64, M>,
|
||||
#[traversable(wrap = "transfer_volume", rename = "in_profit")]
|
||||
pub transfer_volume_in_profit: AmountPerBlockCumulativeRolling<M>,
|
||||
pub transfer_volume_in_profit: ValuePerBlockCumulativeRolling<M>,
|
||||
#[traversable(wrap = "transfer_volume", rename = "in_loss")]
|
||||
pub transfer_volume_in_loss: AmountPerBlockCumulativeRolling<M>,
|
||||
pub transfer_volume_in_loss: ValuePerBlockCumulativeRolling<M>,
|
||||
}
|
||||
|
||||
impl ActivityCore {
|
||||
|
||||
@@ -8,13 +8,13 @@ use crate::{
|
||||
metrics::ImportConfig,
|
||||
state::{CohortState, CostBasisOps, RealizedOps},
|
||||
},
|
||||
internal::AmountPerBlockCumulativeRolling,
|
||||
internal::ValuePerBlockCumulativeRolling,
|
||||
prices,
|
||||
};
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct ActivityMinimal<M: StorageMode = Rw> {
|
||||
pub transfer_volume: AmountPerBlockCumulativeRolling<M>,
|
||||
pub transfer_volume: ValuePerBlockCumulativeRolling<M>,
|
||||
}
|
||||
|
||||
impl ActivityMinimal {
|
||||
|
||||
@@ -7,7 +7,7 @@ use vecdb::{BytesVec, BytesVecValue, Database, ImportableVec};
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{
|
||||
AmountPerBlock, AmountPerBlockCumulative, AmountPerBlockCumulativeRolling, CentsType,
|
||||
ValuePerBlock, ValuePerBlockCumulative, ValuePerBlockCumulativeRolling, FiatType,
|
||||
FiatPerBlock, FiatPerBlockCumulativeWithSums, NumericValue, PerBlock,
|
||||
PerBlockCumulativeRolling, PercentPerBlock, PercentRollingWindows, Price,
|
||||
PriceWithRatioExtendedPerBlock, PriceWithRatioPerBlock, RatioPerBlock,
|
||||
@@ -35,8 +35,8 @@ macro_rules! impl_config_import {
|
||||
|
||||
// Non-generic types
|
||||
impl_config_import!(
|
||||
AmountPerBlock,
|
||||
AmountPerBlockCumulative,
|
||||
ValuePerBlock,
|
||||
ValuePerBlockCumulative,
|
||||
PriceWithRatioPerBlock,
|
||||
PriceWithRatioExtendedPerBlock,
|
||||
RatioPerBlock<BasisPoints32>,
|
||||
@@ -79,7 +79,7 @@ impl<T: NumericValue + JsonSchema> ConfigImport for RollingWindow24hPerBlock<T>
|
||||
Self::forced_import(cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes)
|
||||
}
|
||||
}
|
||||
impl ConfigImport for AmountPerBlockCumulativeRolling {
|
||||
impl ConfigImport for ValuePerBlockCumulativeRolling {
|
||||
fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result<Self> {
|
||||
Self::forced_import(
|
||||
cfg.db,
|
||||
@@ -90,7 +90,7 @@ impl ConfigImport for AmountPerBlockCumulativeRolling {
|
||||
)
|
||||
}
|
||||
}
|
||||
impl<C: CentsType> ConfigImport for FiatPerBlockCumulativeWithSums<C> {
|
||||
impl<C: FiatType> ConfigImport for FiatPerBlockCumulativeWithSums<C> {
|
||||
fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result<Self> {
|
||||
Self::forced_import(
|
||||
cfg.db,
|
||||
@@ -106,7 +106,7 @@ impl<T: NumericValue + JsonSchema> ConfigImport for RollingWindowsFrom1w<T> {
|
||||
Self::forced_import(cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes)
|
||||
}
|
||||
}
|
||||
impl<C: CentsType> ConfigImport for FiatPerBlock<C> {
|
||||
impl<C: FiatType> ConfigImport for FiatPerBlock<C> {
|
||||
fn config_import(cfg: &ImportConfig, suffix: &str, offset: Version) -> Result<Self> {
|
||||
Self::forced_import(cfg.db, &cfg.name(suffix), cfg.version + offset, cfg.indexes)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use vecdb::{AnyStoredVec, AnyVec, Database, Exit, Rw, StorageMode, WritableVec};
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{
|
||||
AmountPerBlock, AmountPerBlockWithDeltas, PerBlock, RatioPerBlock, WindowStartVec, Windows,
|
||||
ValuePerBlock, ValuePerBlockWithDeltas, PerBlock, RatioPerBlock, WindowStartVec, Windows,
|
||||
},
|
||||
prices,
|
||||
};
|
||||
@@ -20,7 +20,7 @@ pub struct WithSth<All, Sth = All> {
|
||||
|
||||
#[derive(Traversable)]
|
||||
pub struct ProfitabilityBucket<M: StorageMode = Rw> {
|
||||
pub supply: WithSth<AmountPerBlockWithDeltas<M>, AmountPerBlock<M>>,
|
||||
pub supply: WithSth<ValuePerBlockWithDeltas<M>, ValuePerBlock<M>>,
|
||||
pub realized_cap: WithSth<PerBlock<Dollars, M>>,
|
||||
pub unrealized_pnl: WithSth<PerBlock<Dollars, M>>,
|
||||
pub nupl: RatioPerBlock<BasisPointsSigned32, M>,
|
||||
@@ -47,14 +47,14 @@ impl ProfitabilityBucket {
|
||||
) -> Result<Self> {
|
||||
Ok(Self {
|
||||
supply: WithSth {
|
||||
all: AmountPerBlockWithDeltas::forced_import(
|
||||
all: ValuePerBlockWithDeltas::forced_import(
|
||||
db,
|
||||
&format!("{name}_supply"),
|
||||
version,
|
||||
indexes,
|
||||
cached_starts,
|
||||
)?,
|
||||
sth: AmountPerBlock::forced_import(
|
||||
sth: ValuePerBlock::forced_import(
|
||||
db,
|
||||
&format!("{name}_sth_supply"),
|
||||
version,
|
||||
|
||||
@@ -11,7 +11,7 @@ use crate::{
|
||||
blocks,
|
||||
distribution::state::{CohortState, CostBasisData, RealizedState, WithCapital},
|
||||
internal::{
|
||||
AmountPerBlockCumulativeRolling, FiatPerBlockCumulativeWithSums, PercentPerBlock,
|
||||
ValuePerBlockCumulativeRolling, FiatPerBlockCumulativeWithSums, PercentPerBlock,
|
||||
PercentRollingWindows, PriceWithRatioExtendedPerBlock, RatioCents64, RatioCentsBp32,
|
||||
RatioCentsSignedCentsBps32, RatioCentsSignedDollarsBps32, RatioDollarsBp32,
|
||||
RatioPerBlockPercentiles, RatioPerBlockStdDevBands, RatioSma, RollingWindows,
|
||||
@@ -243,7 +243,7 @@ impl RealizedFull {
|
||||
starting_indexes: &Indexes,
|
||||
height_to_supply: &impl ReadableVec<Height, Bitcoin>,
|
||||
height_to_market_cap: &impl ReadableVec<Height, Dollars>,
|
||||
activity_transfer_volume: &AmountPerBlockCumulativeRolling,
|
||||
activity_transfer_volume: &ValuePerBlockCumulativeRolling,
|
||||
exit: &Exit,
|
||||
) -> Result<()> {
|
||||
self.core.compute_rest_part2(
|
||||
|
||||
@@ -3,15 +3,15 @@ use brk_traversable::Traversable;
|
||||
use brk_types::{Height, Sats, StoredU64, Version};
|
||||
use vecdb::{AnyStoredVec, Database, Exit, ReadableVec, Rw, StorageMode, WritableVec};
|
||||
|
||||
use crate::{indexes, internal::AmountPerBlock, prices};
|
||||
use crate::{indexes, internal::ValuePerBlock, prices};
|
||||
|
||||
/// Average amount held per UTXO and per funded address.
|
||||
///
|
||||
/// `utxo = supply / utxo_count`, `addr = supply / funded_addr_count`.
|
||||
#[derive(Traversable)]
|
||||
pub struct AvgAmountMetrics<M: StorageMode = Rw> {
|
||||
pub utxo: AmountPerBlock<M>,
|
||||
pub addr: AmountPerBlock<M>,
|
||||
pub utxo: ValuePerBlock<M>,
|
||||
pub addr: ValuePerBlock<M>,
|
||||
}
|
||||
|
||||
impl AvgAmountMetrics {
|
||||
@@ -29,8 +29,8 @@ impl AvgAmountMetrics {
|
||||
}
|
||||
};
|
||||
Ok(Self {
|
||||
utxo: AmountPerBlock::forced_import(db, &name("avg_utxo_amount"), version, indexes)?,
|
||||
addr: AmountPerBlock::forced_import(db, &name("avg_addr_amount"), version, indexes)?,
|
||||
utxo: ValuePerBlock::forced_import(db, &name("avg_utxo_amount"), version, indexes)?,
|
||||
addr: ValuePerBlock::forced_import(db, &name("avg_addr_amount"), version, indexes)?,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::{
|
||||
};
|
||||
|
||||
use crate::internal::{
|
||||
AmountPerBlock, LazyRollingDeltasFromHeight, PercentPerBlock, RatioSatsBp16,
|
||||
LazyRollingDeltasAmountFromHeight, PercentPerBlock, RatioSatsBp16, ValuePerBlock,
|
||||
};
|
||||
|
||||
use crate::distribution::metrics::ImportConfig;
|
||||
@@ -17,17 +17,17 @@ use crate::distribution::metrics::ImportConfig;
|
||||
/// Base supply metrics: total supply + dominance (share of circulating).
|
||||
#[derive(Traversable)]
|
||||
pub struct SupplyBase<M: StorageMode = Rw> {
|
||||
pub total: AmountPerBlock<M>,
|
||||
pub delta: LazyRollingDeltasFromHeight<Sats, SatsSigned, BasisPointsSigned32>,
|
||||
pub total: ValuePerBlock<M>,
|
||||
pub delta: LazyRollingDeltasAmountFromHeight<Sats, SatsSigned, BasisPointsSigned32>,
|
||||
#[traversable(rename = "dominance")]
|
||||
pub dominance: PercentPerBlock<BasisPoints16, M>,
|
||||
}
|
||||
|
||||
impl SupplyBase {
|
||||
pub(crate) fn forced_import(cfg: &ImportConfig) -> Result<Self> {
|
||||
let supply: AmountPerBlock = cfg.import("supply", Version::ZERO)?;
|
||||
let supply: ValuePerBlock = cfg.import("supply", Version::ZERO)?;
|
||||
|
||||
let delta = LazyRollingDeltasFromHeight::new(
|
||||
let delta = LazyRollingDeltasAmountFromHeight::new(
|
||||
&cfg.name("supply_delta"),
|
||||
cfg.version + Version::ONE,
|
||||
&supply.sats.height,
|
||||
|
||||
@@ -7,7 +7,7 @@ use vecdb::{AnyStoredVec, AnyVec, Exit, Rw, StorageMode, WritableVec};
|
||||
use crate::{distribution::state::UnrealizedState, prices};
|
||||
|
||||
use crate::internal::{
|
||||
AmountPerBlock, HalveCents, HalveDollars, HalveSats, HalveSatsToBitcoin, LazyAmountPerBlock,
|
||||
ValuePerBlock, HalveCents, HalveDollars, HalveSats, HalveSatsToBitcoin, LazyValuePerBlock,
|
||||
};
|
||||
|
||||
use crate::distribution::metrics::ImportConfig;
|
||||
@@ -22,9 +22,9 @@ pub struct SupplyCore<M: StorageMode = Rw> {
|
||||
#[traversable(flatten)]
|
||||
pub base: SupplyBase<M>,
|
||||
|
||||
pub half: LazyAmountPerBlock,
|
||||
pub in_profit: AmountPerBlock<M>,
|
||||
pub in_loss: AmountPerBlock<M>,
|
||||
pub half: LazyValuePerBlock,
|
||||
pub in_profit: ValuePerBlock<M>,
|
||||
pub in_loss: ValuePerBlock<M>,
|
||||
}
|
||||
|
||||
impl SupplyCore {
|
||||
@@ -32,7 +32,7 @@ impl SupplyCore {
|
||||
let v0 = Version::ZERO;
|
||||
let base = SupplyBase::forced_import(cfg)?;
|
||||
|
||||
let half = LazyAmountPerBlock::from_block_source::<
|
||||
let half = LazyValuePerBlock::from_block_source::<
|
||||
HalveSats,
|
||||
HalveSatsToBitcoin,
|
||||
HalveCents,
|
||||
|
||||
Reference in New Issue
Block a user