<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <title>In Rust We Trust 🦀 - substrate</title>
      <link>https://nikolaishelekhov.com</link>
      <description>Nikolai is a Rust software engineer building high-performance systems, backend and blockchain infrastructure.</description>
      <generator>Zola</generator>
      <language>en</language>
      <atom:link href="https://nikolaishelekhov.com/tags/substrate/rss.xml" rel="self" type="application/rss+xml"/>
      <lastBuildDate>Sun, 09 Nov 2025 00:00:00 +0000</lastBuildDate>
      <item>
          <title>Designing an On-Chain Price Oracle in Substrate</title>
          <pubDate>Sun, 09 Nov 2025 00:00:00 +0000</pubDate>
          <author>Nikolai Shelekhov</author>
          <link>https://nikolaishelekhov.com/blog/substrate-price-oracle/</link>
          <guid>https://nikolaishelekhov.com/blog/substrate-price-oracle/</guid>
          <description xml:base="https://nikolaishelekhov.com/blog/substrate-price-oracle/">&lt;p&gt;Accurate price data is foundational to any DeFi system.&lt;&#x2F;p&gt;
&lt;p&gt;DEXs, lending protocols, liquidations, MEV strategies —&lt;br &#x2F;&gt;
all depend on reliable and timely asset pricing.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of relying purely on external infrastructure, I wanted to explore a runtime-native approach using Substrate.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-goal&quot;&gt;The Goal&lt;&#x2F;h2&gt;
&lt;p&gt;Design a modular price oracle pallet that:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Fetches asset prices from external sources&lt;&#x2F;li&gt;
&lt;li&gt;Aggregates them deterministically&lt;&#x2F;li&gt;
&lt;li&gt;Stores them on-chain&lt;&#x2F;li&gt;
&lt;li&gt;Exposes them to other runtime modules&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The constraint: maintain composability and determinism at the runtime level.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;architecture-overview&quot;&gt;Architecture Overview&lt;&#x2F;h2&gt;
&lt;p&gt;The design uses Substrate’s off-chain workers:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Off-chain worker fetches external price data&lt;&#x2F;li&gt;
&lt;li&gt;Data is validated and formatted&lt;&#x2F;li&gt;
&lt;li&gt;An unsigned transaction submits updated price information&lt;&#x2F;li&gt;
&lt;li&gt;Runtime storage is updated&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;This keeps:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Network requests off the runtime path&lt;&#x2F;li&gt;
&lt;li&gt;Deterministic execution within the state machine&lt;&#x2F;li&gt;
&lt;li&gt;Minimal trust assumptions beyond data sources&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;key-design-decisions&quot;&gt;Key Design Decisions&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;1-off-chain-workers&quot;&gt;1. Off-Chain Workers&lt;&#x2F;h3&gt;
&lt;p&gt;External HTTP requests cannot block runtime execution.&lt;&#x2F;p&gt;
&lt;p&gt;Off-chain workers allow background fetching without compromising block production.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;2-fixed-point-arithmetic&quot;&gt;2. Fixed-Point Arithmetic&lt;&#x2F;h3&gt;
&lt;p&gt;Floating-point operations are not deterministic across architectures.&lt;&#x2F;p&gt;
&lt;p&gt;All price calculations use fixed-point arithmetic to ensure:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Deterministic execution&lt;&#x2F;li&gt;
&lt;li&gt;Predictable rounding&lt;&#x2F;li&gt;
&lt;li&gt;Cross-node consistency&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;3-storage-layout&quot;&gt;3. Storage Layout&lt;&#x2F;h3&gt;
&lt;p&gt;Prices are stored in optimized maps keyed by asset identifiers.&lt;&#x2F;p&gt;
&lt;p&gt;Design priorities:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Fast reads (price access is frequent)&lt;&#x2F;li&gt;
&lt;li&gt;Minimal storage overhead&lt;&#x2F;li&gt;
&lt;li&gt;Clear upgrade path&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;why-not-just-use-an-external-oracle&quot;&gt;Why Not Just Use an External Oracle?&lt;&#x2F;h2&gt;
&lt;p&gt;External oracle networks are powerful — but they introduce:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Latency&lt;&#x2F;li&gt;
&lt;li&gt;Trust layers&lt;&#x2F;li&gt;
&lt;li&gt;Dependency on third-party infrastructure&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Embedding oracle logic inside the runtime:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Improves composability&lt;&#x2F;li&gt;
&lt;li&gt;Reduces architectural complexity&lt;&#x2F;li&gt;
&lt;li&gt;Enables tighter integration with DeFi pallets&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;For experimentation and MEV research, this approach gives full control over the data lifecycle.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-i-learned&quot;&gt;What I Learned&lt;&#x2F;h2&gt;
&lt;p&gt;Building runtime logic reinforces an important principle:&lt;&#x2F;p&gt;
&lt;p&gt;Blockchain development is systems programming.&lt;&#x2F;p&gt;
&lt;p&gt;You’re writing deterministic state machines,
with strict performance constraints,
and zero room for undefined behavior.&lt;&#x2F;p&gt;
&lt;p&gt;Rust and Substrate make that not only possible —
but enjoyable.&lt;&#x2F;p&gt;
</description>
      </item>
    </channel>
</rss>
