Designing an On-Chain Price Oracle in Substrate

Accurate price data is foundational to any DeFi system.

DEXs, lending protocols, liquidations, MEV strategies —
all depend on reliable and timely asset pricing.

Instead of relying purely on external infrastructure, I wanted to explore a runtime-native approach using Substrate.

The Goal

Design a modular price oracle pallet that:

  • Fetches asset prices from external sources
  • Aggregates them deterministically
  • Stores them on-chain
  • Exposes them to other runtime modules

The constraint: maintain composability and determinism at the runtime level.

Architecture Overview

The design uses Substrate’s off-chain workers:

  1. Off-chain worker fetches external price data
  2. Data is validated and formatted
  3. An unsigned transaction submits updated price information
  4. Runtime storage is updated

This keeps:

  • Network requests off the runtime path
  • Deterministic execution within the state machine
  • Minimal trust assumptions beyond data sources

Key Design Decisions

1. Off-Chain Workers

External HTTP requests cannot block runtime execution.

Off-chain workers allow background fetching without compromising block production.

2. Fixed-Point Arithmetic

Floating-point operations are not deterministic across architectures.

All price calculations use fixed-point arithmetic to ensure:

  • Deterministic execution
  • Predictable rounding
  • Cross-node consistency

3. Storage Layout

Prices are stored in optimized maps keyed by asset identifiers.

Design priorities:

  • Fast reads (price access is frequent)
  • Minimal storage overhead
  • Clear upgrade path

Why Not Just Use an External Oracle?

External oracle networks are powerful — but they introduce:

  • Latency
  • Trust layers
  • Dependency on third-party infrastructure

Embedding oracle logic inside the runtime:

  • Improves composability
  • Reduces architectural complexity
  • Enables tighter integration with DeFi pallets

For experimentation and MEV research, this approach gives full control over the data lifecycle.

What I Learned

Building runtime logic reinforces an important principle:

Blockchain development is systems programming.

You’re writing deterministic state machines, with strict performance constraints, and zero room for undefined behavior.

Rust and Substrate make that not only possible — but enjoyable.