Skip to content

Substrate pallet that fetches real-time DEX prices across multiple chains (Ethereum, BSC, Polygon, Avalanche) using off-chain workers

Notifications You must be signed in to change notification settings

alloc33/pallet-price-oracle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pallet Price Oracle

A production-ready Substrate pallet for fetching real-time cryptocurrency prices from multiple DEXs via direct smart contract calls. Part of building arbitrage infrastructure for multi-chain opportunities.

Architecture

┌─────────────────────────────────────────────────────────────────┐
│                      OFFCHAIN WORKER                            │
│                    (Runs every N blocks)                        │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  ┌──────────────┐   ┌──────────────┐   ┌──────────────┐         │
│  │  Uniswap V3  │   │  PancakeSwap │   │  Trader Joe  │         │
│  │  (Ethereum)  │   │     (BSC)    │   │ (Avalanche)  │ ...     │
│  └──────┬───────┘   └──────┬───────┘   └──────┬───────┘         │
│         │                  │                  │                 │
│         └──────────────────┼──────────────────┘                 │
│                            │                                    │
│                            ▼                                    │
│                   Fetch pool prices                             │
│                   (Direct RPC Calls)                            │
│                            │                                    │
│                            ▼                                    │
│                   Validate Price Bounds                         │
│                            │                                    │
│                            ▼                                    │
│              Create submit_price_unsigned()                     │
│                                                                 │
└────────────────────────────┬────────────────────────────────────┘
                             │
                             │ Submit Unsigned Transaction
                             │
                             ▼
┌─────────────────────────────────────────────────────────────────┐
│                   TRANSACTION POOL                              │
│                                                                 │
│  validate_unsigned() ──────► Priority: MAX                      │
│                              Longevity: 3 blocks                │
└────────────────────────────┬────────────────────────────────────┘
                             │
                             │ Include in Block
                             │
                             ▼
┌─────────────────────────────────────────────────────────────────┐
│                      ON-CHAIN STORAGE                           │
│                                                                 │
│  StorageDoubleMap<TokenPairHash, ExchangeId, (Price, Time)>     │
│                                                                 │
│  ┌─────────────┬──────────┬──────────────────┐                  │
│  │  ETH/USD    │ Exchange │  Price & Time    │                  │
│  ├─────────────┼──────────┼──────────────────┤                  │
│  │ 0x1a2b3c... │    1     │ (3200.50, t1)    │                  │
│  │ 0x1a2b3c... │    2     │ (3201.20, t2)    │                  │
│  │ 0x1a2b3c... │    3     │ (3199.80, t3)    │                  │
│  └─────────────┴──────────┴──────────────────┘                  │
│                                                                 │
│  Query via: get_price(pair_hash, exchange_id)                   │
│             get_all_prices(pair_hash)                           │
└─────────────────────────────────────────────────────────────────┘

Features

  • Direct DEX integration - No API dependencies, fetches via smart contract calls
  • Multi-chain support - Ethereum, BSC, Polygon, Avalanche
  • Unsigned transactions - Offchain workers submit prices to on-chain storage
  • Price validation - Built-in bounds checking and validation
  • Configurable - Update intervals, timeouts, and exchange limits

Current Status

ETH/USD only - Currently implements ETH/USD price fetching across 5 major DEXs. Additional trading pairs will be added in future releases.

Supported Exchanges

  • Uniswap V3 (Ethereum)
  • SushiSwap V2 (Ethereum)
  • PancakeSwap V2 (BSC)
  • QuickSwap V2 (Polygon)
  • Trader Joe (Avalanche)

Installation

[dependencies]
pallet-price-oracle = { git = "https://github.com/alloc33/pallet-price-oracle" }

Configuration

impl pallet_price_oracle::Config for Runtime {
    type UpdateInterval = ConstU64<10>;      // Update every 10 blocks
    type HttpTimeout = ConstU64<10000>;      // 10 second timeout  
    type MaxExchangesPerBlock = ConstU8<3>;  // Max 3 exchanges per block
}

Usage

use pallet_price_oracle::types::TokenPair;

// Get ETH/USD price from specific exchange
let eth_hash = TokenPair::EthUsd.to_hash();
let price = PriceOracle::get_price(eth_hash, exchange_id);

// Get all ETH/USD prices
let all_prices = PriceOracle::get_all_prices(eth_hash);

Built for FractalizeChain

About

Substrate pallet that fetches real-time DEX prices across multiple chains (Ethereum, BSC, Polygon, Avalanche) using off-chain workers

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages