How to Use First-Party Data Feeds in Your Smart Contracts

Using API3’s Managed and Self-funded dAPIs in your Solidity Smart Contracts

How to Use First-Party Data Feeds in Your Smart Contracts

It is crucial for dApps interacting with off-chain data to have access to secure oracles. With the coming new ZK chains like Polygon zkEVM and zkSync Era, it is extremely hard to find secure oracles serving off-chain data with complete transparency, especially for major DeFi protocols securing millions in TVL and serving a large user base 24/7.

Apart from availability and security, developers have long sought a straightforward, plug-and-play data feed solution for their smart contracts that is easy to integrate and natively supports multiple chains. With these goals in mind, API3 has built a first-party oracle solution that offers secure, transparent, and accurate data feeds on most major EVM chains, while being simple to integrate and use.

Start Using Managed dAPIs in Your Smart Contracts

Connecting smart contracts to price reference data is easy with API3’s decentralized first-party data feeds. Managed dAPIs are data feeds that are sourced directly from multiple first-party oracles and are then aggregated on-chain. They utilize API3’s Airnode, which is a first-party oracle node run by the API provider to serve price data on-chain to DeFi protocols. As a managed service, API3 manages the gas costs and monitoring, which is currently available on 11 EVM-compatible chains.

Getting Started

Head over to the API3 Market to browse through all the available dAPI services. You can select which dAPIs you want to use. You can also filter the list by chains and data providers. After selecting the dAPI, you can select what type of dAPI you want to subscribe to.

Managed dAPIs gives you the option to configure the dAPI according to the needs of your protocol. You get the option to configure the deviation threshold and heartbeat for the specified dAPI. Select the required configuration, review the price and time frame, and click on Add to Cart.

If your required dAPI configuration has already been purchased by someone else, you can check the remaining operational time for the dAPI and can start using it directly.

Placing the Order

Once you have added all desired dAPIs on the same chain, proceed to checkout. You will be presented with a summary of your order. Select Proceed to Payment.

This will take you to the payment page where you can place the order for the selected feeds. You can see the order summary and the total price. If it looks fine, select Connect Wallet, and confirm the transaction on MetaMask.

Now wait for the transaction to get confirmed. Once it’s done, you will be able to see the confirmation page.

After making the payment, you will have to wait for the dAPI to get updated to your requested configuration. It usually takes up to 5 business days for the API3 dAPI team to update the dAPI. Once the dAPI is updated, you can start using it.

Reading the dAPI

Each dAPI is represented by a proxy contract. Each proxy contract is linked to a single dAPI on a particular network. You can use the address of a proxy contract to read dAPIs with the IProxy interface.

The process to read from a dAPI remains the same for both Managed and Self-funded dAPIs.

  1. Head over to Remix IDE where you will code a simple contract that imports the IProxy interface and calls the read() function to read the latest value from a dAPI.
  2. Click here to open the contract in Remix.
  3. Start by importing the Ownable from the @openzeppelin library and the IProxy interface from the @api3/contracts library.
  1. Next, define a public address proxyAddress which is the address of the proxy contract that will be used to access the desired dAPI via its proxy contract.
  2. The setProxyAddress() function is a public function that allows the owner of the contract to update the proxy contract address.

The only thing left now is to make a function to read the dAPI. The readDataFeed() function is a public function that reads from a dAPI through the IProxy interface. The function returns the latest value and timestamp of the dAPI that the proxy is reading from.

Here’s the full contract. You can copy and paste it into Remix IDE.

As Managed dAPIs are only available on mainnets, we’re going to use the ETH/USD Self-funded dAPI on Polygon Mumbai for this example. Reading from a dAPI Proxy Contract remains the same in both cases.

Click on the COMPILE tab on the left side of the dashboard and click on Compile DataFeedReader.sol

Head to Deploy and Run Transactions tab and select Injected Provider — MetaMask option under Environment.

Connect your MetaMask. Make sure you’re on the Polygon Mumbai Testnet.

Updating the Proxy Address

Head over to the Deploy & Run Transactions tab, click on the dropdown under Deployed Contracts.

Now select the setProxyAddress dropdown to see all the parameters you need to pass in order to set the Proxy contract address on-chain.

If you placed an order for a managed dAPI service, you will have to wait for 4–5 days to get the dAPI updated with your requested configuration.

As we’re using Self-funded dAPI for this guide, head over to https://market.api3.org and search for ETH/USD dAPI on Polygon Mumbai Testnet.

Here, you can Connect Wallet and then fund the dAPI to cover gas costs.

In this case, the gas for this ETH/USD dAPI is already funded on Polygon Mumbai for now, so we can start using it directly. Copy the proxy contract address, paste it into Remix under setProxyAddress() function and click on transact.

Confirm the transaction in your wallet.

After the transaction is complete, you can now select and call the readDataFeed function that will return the latest value and timestamp from the dAPI, i.e., ETH/USD in this case.

The value returned is the latest data feed value and the timestamp is when the value was last updated.

So that’s how you can utilize secure first-party data feeds in your smart contracts. You can start using dAPIs in your smart contract now at the API3 Market. Head over to the API3 technical documentation and learn more about how dAPIs work.

Have questions? Join our Discord and ask our community of developers.

Other Resources