API3 and Metis have joined forces to bring first-party oracles and decentralized price feed data to the flourishing Metis ecosystem. This integration enables developers on Metis to access a vast catalog of OEV ready Price Feeds directly, allowing dApps to harness transparent and scalable data feeds without intermediaries.
Here’s a guide on how to leverage API3 and Metis to build next-generation dApps with reliable, real-time data from the Web3 API Catalog.
API3 is a leading provider of first-party oracles, offering a comprehensive Web3 API catalog with over 180 APIs from premium providers. Through Airnode, API3 connects data directly from the source to your smart contract, removing third-party reliance and reducing costs for developers.
Key Features of API3 on Metis:
Integrating API3 into your dApp on Metis is straightforward. Here’s a step-by-step guide to getting started.
Before starting, ensure you have the following:
Import the API3 Price Feed Contract: You’ll need to import the API3-specific price feed contract in Solidity. This contract will handle reading real-time price data from API3 Beacons directly.
import "@api3/contracts/api3-server-v1/proxies/interfaces/IProxy.sol";
Define Your Price Feed Contract: Create a variable for the price feed proxy address, which you will use to call the data feed functions. This address points to the data feed proxy on the Metis network, and you can find it on the API3 Market.
address public priceFeedProxy;
constructor(address _priceFeedProxy) {
priceFeedProxy = _priceFeedProxy;
}
Set Up the Data Fetching Logic: Define a function in your smart contract to read data from the API3 price feed. The proxy contract provides the latest data, which includes price, timestamp, and other metadata necessary for DeFi applications.
function getLatestPrice() public view returns (int256 price) {
price = IProxy(priceFeedProxy).read();
}
This setup will allow your dApp to fetch real-time price data from API3, formatted for integration within Metis’ Layer 2 environment .
If your dApp uses another Oracle setup, like Chainlink’s AggregatorV3Interface, but you wish to integrate API3, you can implement an adapter. This adapter will fetch data from API3 and format it according to the expected structure of your existing oracle interface.
Create the Adapter Contract: Implement an adapter that translates API3 data into the format required by your existing interface. In the adapter contract, retrieve data from API3’s proxy and convert it to match the structure expected by AggregatorV3Interface.
For additional guidance:
import "@api3/contracts/api3-server-v1/proxies/interfaces/IProxy.sol";
import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
contract API3Adapter is AggregatorV3Interface {
address public api3Proxy;
constructor(address _api3Proxy) {
api3Proxy = _api3Proxy;
}
function latestAnswer() public view override returns (int256) {
return IProxy(api3Proxy).read();
}
// Implement other required functions by AggregatorV3Interface
}
Integrate the Adapter with Your dApp: Replace the existing oracle contract address in your dApp with the adapter’s address. This change allows your contract to use API3 data seamlessly without modifying the entire contract’s logic.
Testing on Metis’ testnet ensures that your price feeds and adapters are set up correctly:
Please check this github repo for full code reference!
https://github.com/metis-edu/Community-contributions/tree/main/API3/api3-dapi-workshop
Once deployed, you can monitor the price feed status on the API3 Market to ensure it remains active. API3 Beacons are automatically updated to prevent stale data, but for added security, you may implement mechanisms within your contract to check data freshness.
By integrating API3 into your Metis dApp, you gain access to first-party oracles that bring reliable, direct-from-provider data feeds to your application. This setup not only reduces costs but enhances transparency and scalability for Web3 applications, especially DeFi solutions that require accurate, up-to-date market data.
This approach demonstrates a modular and flexible oracle integration for developers building on Metis, bridging the benefits of decentralized data with the low-latency, high-throughput environment that Metis Layer 2 provides.