ERGO
Oracle Integration
Intermediate
2 hours

Oracle Data Consumption (Read-Only Oracle Pattern)

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.

Lösung

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).

Funktionsweise

  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-Beispiele

{
  // 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.

Anwendungsfälle

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

Sicherheitshinweise

  • !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

Praxis-Implementierungen

SigmaUSD

Uses oracle pools for ERG/USD pricing

Spectrum Finance (sunset 2024)

Historical reference for oracle integration in an eUTXO DEX. Sunset notice issued; contracts frozen since Feb 2024.

Ressourcen

Gebührenhinweise

Data inputs don't add to transaction size fees. Oracle pools charge posting fees to data providers, not consumers.

Verbessere deine ErgoScript-Fähigkeiten

Erhalte Benachrichtigungen über neue Muster, Tutorials und Entwickler-Ressourcen.

Follow for daily updates