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

問題

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

解決方案

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

運作方式

  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

程式碼範例

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

使用案例

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

安全注意事項

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

實際應用案例

SigmaUSD

Uses oracle pools for ERG/USD pricing

Spectrum Finance

Oracle integration for DEX

資源

手續費注意事項

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

提升您的 ErgoScript 技能

獲取新模式、教學和開發者資源的通知。

Follow for daily updates