Skip to main content

Feed Adapter

Feed Adapters adapt to AggregatorV2V3Interface and therefore provide compatibility to already deployed contracts.

For a complete list of functions and parameters for the FeedAdapterInterface, see the Feed Adapter API Reference.

Usages and Examples:

Querying round data using adapter contract address

To retrieve the latest data using the FeedAdapter smart contract address, call:

// Call the adapter at the right contract address to get the price.

latestRoundData()

Ex: To query the latest ETH/USD price, find the adapter address for this pair and simply call the above endpoint.

Code Examples

Solidity

To consume price data from the adapter, one can use the FeedAdapterInterface or the AggregatorV2V3Interface. Which are equivalent.

PriceConsumerWithAdapter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../interfaces/FeedAdapterInterface.sol";

contract PriceConsumerWithAdapter {
AggregatorV2V3Interface private s_feedAdapter;

constructor(address adapter) {
s_feedAdapter = AggregatorV2V3Interface(adapter);
}

function getLatestPrice()
external
view
returns (int256 answer)
{
return s_feedAdapter.latestAnswer();
}
}

Hardhat/ Ethers

getPriceFromTestnetAdapter.ts
import { ethers } from "hardhat";

async function main() {
const adapterAddress = "0x491fD333937522e69D1c3FB944fbC5e95eEF9f59"; //Testnet address
//Pass the name or ABI of the contract
const adapterBTCUSD = await ethers.getContractAt("AggregatorV2V3Interface", adapterAddress);
const price = await adapterBTCUSD.latestAnswer();
console.log("Answer for BTC/USD: ", price.toString());
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});

JavaScript

PriceConsumerWithAdapter.test.ts
const Web3 = require('web3')

const contract_abi = [
{
"inputs": [
{ "internalType": "contract FeedRegistryInterface", "name": "feedRegistry_", "type": "address" }
...
]
},
...
]

async function getBTCUSDAnswer() {
const adapterAddress = '0x491fD333937522e69D1c3FB944fbC5e95eEF9f59'; // testnet address
const provider = new Web3.providers.HttpProvider('https://data-seed-prebsc-1-s1.binance.org:8545');
const web3 = new Web3(provider);
const contract = new web3.eth.Contract(contract_abi, adapterAddress);
const result = await contract.methods.latestAnswer().call();
console.log(result)
}

/**
Output: 1997642165957
*/

Contract addresses

FeedAdapters are deployed on BNB Testnet and Mainnet. Please refer to the contract addresses