Skip to main content

hypercall/startup/
oracles.rs

1//! Startup wiring for pricing and oracle services.
2
3#[cfg(feature = "test-endpoints")]
4use std::collections::HashMap;
5
6#[cfg(feature = "test-endpoints")]
7use tracing::warn;
8
9#[derive(Debug, Clone)]
10pub struct OracleMarket {
11    pub underlying: String,
12    pub hyperliquid_coin: String,
13}
14
15pub fn configured_oracle_markets(
16    catalog_config: &catalog_manager::CatalogConfig,
17) -> Vec<OracleMarket> {
18    catalog_config
19        .underlyings
20        .iter()
21        .map(|(underlying, cfg)| {
22            let normalized = underlying.to_ascii_uppercase();
23            let hyperliquid_coin = cfg.hl_symbol.clone().unwrap_or_else(|| normalized.clone());
24
25            OracleMarket {
26                underlying: normalized,
27                hyperliquid_coin,
28            }
29        })
30        .collect()
31}
32
33#[cfg(feature = "test-endpoints")]
34pub fn apply_mock_oracle_price_overrides(default_prices: &mut HashMap<String, f64>) {
35    let Ok(raw) = std::env::var("MOCK_ORACLE_PRICE_MAP") else {
36        return;
37    };
38
39    for entry in raw.split(',') {
40        let Some((symbol, price)) = entry.split_once('=') else {
41            warn!(
42                value = entry,
43                "Ignoring malformed MOCK_ORACLE_PRICE_MAP entry"
44            );
45            continue;
46        };
47
48        let symbol = symbol.trim();
49        if symbol.is_empty() {
50            warn!(
51                value = entry,
52                "Ignoring MOCK_ORACLE_PRICE_MAP entry with empty symbol"
53            );
54            continue;
55        }
56
57        match price.trim().parse::<f64>() {
58            Ok(parsed) if parsed.is_finite() && parsed > 0.0 => {
59                default_prices.insert(symbol.to_string(), parsed);
60            }
61            Ok(parsed) => warn!(
62                symbol,
63                price = parsed,
64                "Ignoring non-positive MOCK_ORACLE_PRICE_MAP price"
65            ),
66            Err(e) => warn!(
67                symbol,
68                price = price.trim(),
69                error = %e,
70                "Ignoring unparsable MOCK_ORACLE_PRICE_MAP price"
71            ),
72        }
73    }
74}