From 0728c88b7a063ff824c8177b3272d3638b2b1e51 Mon Sep 17 00:00:00 2001 From: Jakob Notland Date: Fri, 24 Apr 2026 06:43:29 +0200 Subject: [PATCH] Fixes --- src/db/cauldron/tokenlist/token_utils.rs | 29 ++++++++++++++---------- src/oracle_cash.rs | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/db/cauldron/tokenlist/token_utils.rs b/src/db/cauldron/tokenlist/token_utils.rs index eef4222..0245b63 100644 --- a/src/db/cauldron/tokenlist/token_utils.rs +++ b/src/db/cauldron/tokenlist/token_utils.rs @@ -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] diff --git a/src/oracle_cash.rs b/src/oracle_cash.rs index 04514a6..05c83e9 100644 --- a/src/oracle_cash.rs +++ b/src/oracle_cash.rs @@ -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 ────────────────────────────────────────────────────────