๐ฆ Smart Contract Integration
This section explains how developers can integrate the AggregatorV3 oracle contract into their own decentralized applications (dApps) to access off-chain or externally-fed data within smart contracts.
Your dApp can consume reliable, on-chain data using our AggregatorV3 oracle, which is compatible with the standard Chainlink AggregatorV3 interface โ making integration simple and familiar for many Web3 developers.
๐ Integration Overview
To use the oracle in your dApp:
Import the AggregatorV3Interface
Set the oracle contract address
Call the oracleโs functions (
latestRoundData,getRoundData, etc.)Handle data appropriately in your business logic
๐ ๏ธ Step-by-Step Guide
1. Import the AggregatorV3Interface
If you're using Solidity:
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";Note: This works because our oracle implements the same interface as Chainlink, so it's fully compatible.
2. Declare the Oracle Interface
Declare a reference to the oracle in your consuming contract:
Youโll initialize this reference in the constructor or a setup function.
3. Initialize with the Oracle Address
Replace
_oracleAddresswith the actual deployed address of theAggregatorV3oracle contract.
4. Read the Latest Data
To get the most recent answer:
You can also access timestamps and round information if needed:
5. Read Specific Round Data (Optional)
If your use case requires historical data, you can retrieve data from a specific round:
โ ๏ธ Ensure the round exists โ otherwise, the call will revert.
๐ง Use Cases
Here are a few example use cases where your dApp might benefit from this oracle:
Price Feeds: Display the current price of a token or asset.
Stablecoin Collateralization: Check price before minting/burning.
Betting & Prediction Markets: Resolve outcomes based on real-world values.
Rebasing Tokens: Use oracle data for supply adjustments.
โ ๏ธ Best Practices
Validate Oracle Output: Ensure the returned data is not stale or zero.
Handle Errors: If no data is available,
latestRoundData()will revert. Usetry/catchin higher-level frameworks or check availability on-chain.Secure the Oracle Address: Set the oracle address only once or via a governance mechanism to avoid manipulation.
๐งช Testing Integration
When testing locally or in staging:
You can deploy a mock oracle using the same interface (
AggregatorV3Interface) and manually update values using theupdateAnswer(int256)function.This is useful for simulating real-world scenarios and testing dApp logic against different oracle values.
โ
Summary
By following the standard AggregatorV3Interface, our oracle contract allows seamless integration into any dApp that supports Chainlink-style data feeds.
Integration steps are straightforward:
Import the interface
Set the oracle address
Call
latestRoundData()orgetRoundData()Use the result in your business logic
With this, your smart contracts can interact with externally-fed data reliably and efficiently.
Last updated