This commit is contained in:
Jakob Notland 2026-04-24 06:43:29 +02:00
parent cc665b4a19
commit 0728c88b7a
2 changed files with 18 additions and 13 deletions

View file

@ -55,28 +55,33 @@ pub fn dec_to_f64_bounded(d: Decimal) -> f64 {
}
pub const SATS_PER_BCH: i64 = 100_000_000;
/// Oracle prices are stored in cents (38424 = $384.24), so divide by 100 to get USD/BCH.
pub const ORACLE_SCALE: i64 = 100;
/// On-chain Delphi oracle scale: prices are stored as price * 1_000_000
/// (e.g. $384.24 → 384_240_000).
pub const ORACLE_SCALE: i64 = 1_000_000;
/// Timestamps before this value are looked up in the on-chain Delphi indexed table.
/// Timestamps on or after are looked up in the oracle_cash table (oracles.cash feed).
/// Value: 2026-04-23 00:00:00 UTC (last reliable Delphi oracle update).
const ORACLE_CUTOFF_TS: i64 = 1745366400;
const ORACLE_CUTOFF_TS: i64 = 1776902400;
pub async fn usd_per_bch_at_or_before(oracle_pool: &SqlitePool, ts: i64) -> Decimal {
let price_cents = if ts < ORACLE_CUTOFF_TS {
if ts < ORACLE_CUTOFF_TS {
match get_closest(oracle_pool, &None, ts).await {
Ok(Some(e)) => e.oracle_price,
_ => return Decimal::ZERO,
Ok(Some(e)) => {
Decimal::from_i64(e.oracle_price).unwrap_or(Decimal::ZERO)
/ Decimal::from_i64(ORACLE_SCALE).unwrap()
}
_ => Decimal::ZERO,
}
} else {
// oracles.cash prices are in cents; divide by 100 to get USD/BCH
match get_oracle_cash_closest(oracle_pool, ts).await {
Ok(Some(e)) => e.oracle_price,
_ => return Decimal::ZERO,
Ok(Some(e)) => {
Decimal::from_i64(e.oracle_price).unwrap_or(Decimal::ZERO)
/ Decimal::from(100)
}
_ => Decimal::ZERO,
}
};
Decimal::from_i64(price_cents).unwrap_or(Decimal::ZERO)
/ Decimal::from_i64(ORACLE_SCALE).unwrap()
}
}
#[inline]

View file

@ -35,7 +35,7 @@ const BACKFILL_AGGREGATION: i64 = 3600; // 1-hour buckets
/// Everything before this point is served from the on-chain Delphi indexed table,
/// so oracle_cash only needs to cover from here onward.
/// Value: 2026-04-23 00:00:00 UTC (last reliable Delphi oracle update).
const ORACLE_CUTOFF_TS: i64 = 1745366400;
const ORACLE_CUTOFF_TS: i64 = 1776902400;
// ── API response types ────────────────────────────────────────────────────────