Overview
Theank-oracle module provides minimal price oracle types for querying time-series price data in backtests and agent-based models.
Key features:
- In-memory time-series oracle with 1e18-scaled integer storage
- CSV-based price feed loading
- Float and high-precision e18 integer views
Types
Oracle (trait)
Trait for querying prices from an oracle. Implementations may store higher-precision values (e.g., e18 integers) and convert on the fly.Methods
Return the most recent price at or before
ts for key, as f64.Return the latest available
(ts, price) for key, as f64.SimpleOracle
An in-memory oracle with both float and e18 integer storage. Canonical storage: Theseries field stores prices as 1e18-scaled integers in BTreeMaps, providing exact arithmetic and efficient lookups.
Legacy storage: The optional data field provides a float view for backward compatibility.
Fields
Optional float view:
key → [DataPoint] (should be sorted by ts).Canonical e18 time series:
key → { ts → price_e18 }.Prices are stored as 1e18-scaled integers (U128S wrapper for JSON string serialization).Methods
Insert a
(ts, price_e18) sample for key.Latest
(ts, price_e18) at or before ts for key.Latest
(ts, price_e18) available for key.Convenience:
f64 price at or before ts, derived from e18 storage.Convenience: latest
f64 price derived from e18 storage.DataPoint
A single floating-point datapoint (legacy / optional).Timestamp of the datapoint.
Price as
f64 (informational; prefer series for canonical values).Functions
load_prices_csv
Load{ts,key,price_e18} CSV into a SimpleOracle.
Signature:
ts,key,price_e18
Timestamp in seconds or milliseconds
Asset identifier (e.g., “ETH”, “BTC”, “USDC”)
Decimal string representing the price scaled by 1e18. Underscores are allowed and will be stripped.
- Rows with
price_e18 == 0or unparsable values are skipped - Prices are stored as 1e18-scaled integers for exact arithmetic
Usage Examples
Loading prices from CSV
Querying historical prices
Creating a CSV price feed
Create a CSV file with the following format:Implementation Notes
1e18 Scaling
All prices are stored internally asu128 values scaled by 1e18 (WAD scale):
- $1.00 →
1_000_000_000_000_000_000(1e18) - $3,847.52 →
3_847_520_000_000_000_000_000(~3.85e21)
- ✅ Exact arithmetic (no floating-point errors)
- ✅ High precision for small values
- ✅ Compatibility with on-chain representations
Time Series Lookup
TheSimpleOracle uses BTreeMap for ordered timestamp storage, enabling efficient:
- Binary search for “latest at or before” queries: O(log n)
- Range queries for batch processing
- Guaranteed ordering for reproducible backtests
Oracle Trait Implementation
SimpleOracle implements the Oracle trait with a fallback strategy:
- First, try to query the e18
series(preferred) - If not found, fall back to legacy float
data