mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-06-03 11:43:39 -07:00
global: snapshot
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
use brk_error::Result;
|
||||
use brk_traversable::Traversable;
|
||||
use brk_types::{Cents, CentsSigned, Dollars, Version};
|
||||
use schemars::JsonSchema;
|
||||
use vecdb::{Database, ReadableCloneableVec, Rw, StorageMode, UnaryTransform};
|
||||
|
||||
use super::{ComputedFromHeightLast, LazyFromHeightLast};
|
||||
use crate::{
|
||||
indexes,
|
||||
internal::{CentsSignedToDollars, CentsUnsignedToDollars, NumericValue},
|
||||
};
|
||||
|
||||
/// Trait that associates a cents type with its transform to Dollars.
|
||||
pub trait CentsType: NumericValue + JsonSchema {
|
||||
type ToDollars: UnaryTransform<Self, Dollars>;
|
||||
}
|
||||
|
||||
impl CentsType for Cents {
|
||||
type ToDollars = CentsUnsignedToDollars;
|
||||
}
|
||||
|
||||
impl CentsType for CentsSigned {
|
||||
type ToDollars = CentsSignedToDollars;
|
||||
}
|
||||
|
||||
/// Height-indexed fiat monetary value: cents (eager, integer) + usd (lazy, float).
|
||||
/// Generic over `C` to support both `Cents` (unsigned) and `CentsSigned` (signed).
|
||||
#[derive(Traversable)]
|
||||
pub struct FiatFromHeightLast<C: CentsType, M: StorageMode = Rw> {
|
||||
pub cents: ComputedFromHeightLast<C, M>,
|
||||
pub usd: LazyFromHeightLast<Dollars, C>,
|
||||
}
|
||||
|
||||
impl<C: CentsType> FiatFromHeightLast<C> {
|
||||
pub(crate) fn forced_import(
|
||||
db: &Database,
|
||||
name: &str,
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
let cents = ComputedFromHeightLast::forced_import(
|
||||
db,
|
||||
&format!("{name}_cents"),
|
||||
version,
|
||||
indexes,
|
||||
)?;
|
||||
let usd = LazyFromHeightLast::from_computed::<C::ToDollars>(
|
||||
&format!("{name}_usd"),
|
||||
version,
|
||||
cents.height.read_only_boxed_clone(),
|
||||
¢s,
|
||||
);
|
||||
Ok(Self { cents, usd })
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
mod by_unit;
|
||||
mod constant;
|
||||
mod cumulative;
|
||||
mod fiat;
|
||||
mod cumulative_full;
|
||||
mod cumulative_sum;
|
||||
mod distribution;
|
||||
@@ -23,6 +24,7 @@ pub use by_unit::*;
|
||||
pub use constant::*;
|
||||
pub use cumulative::*;
|
||||
pub use cumulative_full::*;
|
||||
pub use fiat::*;
|
||||
pub use cumulative_sum::*;
|
||||
pub use distribution::*;
|
||||
pub use full::*;
|
||||
|
||||
@@ -32,12 +32,8 @@ impl Price<ComputedFromHeightLast<Cents>> {
|
||||
version: Version,
|
||||
indexes: &indexes::Vecs,
|
||||
) -> Result<Self> {
|
||||
let cents = ComputedFromHeightLast::forced_import(
|
||||
db,
|
||||
&format!("{name}_cents"),
|
||||
version,
|
||||
indexes,
|
||||
)?;
|
||||
let cents =
|
||||
ComputedFromHeightLast::forced_import(db, &format!("{name}_cents"), version, indexes)?;
|
||||
let usd = LazyFromHeightLast::from_computed::<CentsUnsignedToDollars>(
|
||||
&format!("{name}_usd"),
|
||||
version,
|
||||
@@ -51,26 +47,6 @@ impl Price<ComputedFromHeightLast<Cents>> {
|
||||
);
|
||||
Ok(Self { cents, usd, sats })
|
||||
}
|
||||
|
||||
/// Wrap an already-imported ComputedFromHeightLast<Cents> with lazy USD + sats.
|
||||
pub(crate) fn from_cents(
|
||||
name: &str,
|
||||
version: Version,
|
||||
cents: ComputedFromHeightLast<Cents>,
|
||||
) -> Self {
|
||||
let usd = LazyFromHeightLast::from_computed::<CentsUnsignedToDollars>(
|
||||
&format!("{name}_usd"),
|
||||
version,
|
||||
cents.height.read_only_boxed_clone(),
|
||||
¢s,
|
||||
);
|
||||
let sats = LazyFromHeightLast::from_lazy::<DollarsToSatsFract, Cents>(
|
||||
&format!("{name}_sats"),
|
||||
version,
|
||||
&usd,
|
||||
);
|
||||
Self { cents, usd, sats }
|
||||
}
|
||||
}
|
||||
|
||||
impl<ST> Price<LazyFromHeightLast<Cents, ST>>
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
use brk_types::{Cents, CentsSigned};
|
||||
use vecdb::BinaryTransform;
|
||||
|
||||
/// (Cents, Cents) -> CentsSigned (a - b)
|
||||
/// Produces a signed result from two unsigned inputs.
|
||||
pub struct CentsSubtractToCentsSigned;
|
||||
|
||||
impl BinaryTransform<Cents, Cents, CentsSigned> for CentsSubtractToCentsSigned {
|
||||
#[inline(always)]
|
||||
fn apply(a: Cents, b: Cents) -> CentsSigned {
|
||||
CentsSigned::from(a.inner() as i64 - b.inner() as i64)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
mod block_count_target;
|
||||
mod cents_plus;
|
||||
mod cents_signed_to_dollars;
|
||||
mod cents_subtract_to_cents_signed;
|
||||
mod cents_times_tenths;
|
||||
mod cents_to_dollars;
|
||||
mod cents_to_sats;
|
||||
@@ -42,6 +43,7 @@ mod volatility_sqrt7;
|
||||
pub use block_count_target::*;
|
||||
pub use cents_plus::*;
|
||||
pub use cents_signed_to_dollars::*;
|
||||
pub use cents_subtract_to_cents_signed::*;
|
||||
pub use cents_times_tenths::*;
|
||||
pub use cents_to_dollars::*;
|
||||
pub use cents_to_sats::*;
|
||||
|
||||
Reference in New Issue
Block a user