Oracle Integration
Intermediate
2 hoursПотребление данных Oracle (только для чтения образец Oracle)
Safely read latest price/feed from an Oracle Pool inside a contract
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
- 1Oracle pool operators post data to a known box address
- 2Data is stored in registers (R4-R9) with timestamps
- 3Your contract includes oracle box as a data input
- 4Read values from oracle box registers
- 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
Resources
Fee Considerations
Data inputs don't add to transaction size fees. Oracle pools charge posting fees to data providers, not consumers.