API3
API3 is a collaborative project to deliver price feeds to smart contract platforms in a decentralized and trust-minimized way. The Price feeds provided by API3 allow dapps to regain lost value with Oracle Extractable Value built in to the price feed allowing the ability to earn additional revenue for your dapp.
Querying the price of ARB
through API3
Here’s an example of how to use an API3 data feed to query the current price of ARB
onchain. The API3 market provides a list of all the dAPIs available across multiple chains including testnets. You can go forward and activate the dAPI you want to use.
API3 provides an npm package with the contracts needed to access their feeds. We first install that package in our project:
yarn add @api3/contracts
To use a data feed, we retrieve the information through the specific proxy address for that feed. We’ll use the IProxy interface to do so.
import "@api3/contracts/interfaces/IApi3ReaderProxy.sol";
In this case, we want to obtain the current price of ARB
in USD
in Arbitrum One, so we need to know the proxy address that will provide that information. We will search the feed on the API3 Market and connect our wallet. We would then want to see if the feed is active, and if it is, we can check its configuration parameters, deploy the proxy contract and click on Integrate.
You can find the proxy address of ARB/USD here.
If a dAPI is already active, you can use the proxy address directly. If it is not active, you can activate it by clicking on Activate and following the instructions to deploy a proxy contract.
We can now build the function to get the latest price of ARB
. We’ll use this example contract:
contract ARBPriceConsumer {
/**
* Network: Arbitrum One
* Aggregator: ARB/USD
* Proxy: 0xE135626568cc83aE32De671adad0FB871B40aF8d
*/
address constant PROXY = 0xE135626568cc83aE32De671adad0FB871B40aF8d;
/**
* Returns the latest price.
*/
function getLatestPrice()
external
view
returns (int224 value, uint256 timestamp)
{
(value, timestamp) = IProxy(PROXY).read();
// If you have any assumptions about `value` and `timestamp`, make sure
// to validate them right after reading from the proxy.
}
}
You can adapt this contract to your needs. Just remember to use the address of the asset you want to request the price for in the appropriate network and to deploy your contract to the same network. Remember we have a Quickstart available that goes through the process of compiling and deploying a contract.
More examples
Refer to API3’s documentation for more examples of querying other data feeds and learn about Oracle Extractable Value.
You can also check out some other detailed guides: