Oracle Integration
Intermediate
2 hours

Потребление данных Oracle (только для чтения образец Oracle)

Safely read latest price/feed from an Oracle Pool inside a contract

GitHub

Problem

Your smart contract needs real-world data (prices, weather, sports scores) that doesn't exist on-chain.

Solution

Ergo's Oracle Pools provide decentralized data feeds stored in special boxes. Contracts read this data by including oracle boxes as data inputs (read-only, not consumed).

How It Works

  1. 1Oracle pool operators post data to a known box address
  2. 2Data is stored in registers (R4-R9) with timestamps
  3. 3Your contract includes oracle box as a data input
  4. 4Read values from oracle box registers
  5. 5Validate oracle identity via NFT and check data freshness

Code Examples

{
  // Read ERG/USD price from oracle pool
  val oracleBox = CONTEXT.dataInputs(0)
  
  // Verify oracle identity (known pool NFT)
  val oracleNFT = fromBase64("ORACLE_POOL_NFT_ID")
  val validOracle = oracleBox.tokens(0)._1 == oracleNFT
  
  // Read price from R4 (nanoERG per USD, scaled)
  // Example: 500000000 = 0.5 ERG per USD = $2 per ERG
  val ergUsdPrice = oracleBox.R4[Long].get
  
  // Check data freshness (within 30 blocks = ~1 hour)
  val dataHeight = oracleBox.R5[Int].get
  val isFresh = HEIGHT - dataHeight < 30
  
  // Use price in your logic
  val collateralErg = SELF.value
  val collateralUsd = collateralErg * 1000000000L / ergUsdPrice
  val requiredUsd = 100000000L  // $100 in cents
  
  validOracle && isFresh && (collateralUsd >= requiredUsd)
}

Pattern for consuming oracle price data. Validates oracle identity via NFT, checks freshness, and uses price for collateral calculation.

Use Cases

  • Stablecoin collateral pricing
  • Derivatives and options
  • Insurance contracts
  • Prediction markets
  • Cross-chain price verification
  • Dynamic NFT pricing

Security Considerations

  • !Always verify oracle identity via NFT
  • !Check data freshness before use
  • !Use multiple oracles for critical applications
  • !Implement circuit breakers for extreme price movements
  • !Consider TWAP for manipulation resistance

Real-World Implementations

SigmaUSD

Uses oracle pools for ERG/USD pricing

Spectrum Finance

Oracle integration for DEX

Level Up Your ErgoScript Skills

Get notified about new patterns, tutorials, and developer resources.

Follow for daily updates