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