The code for reading Data Push is the same across all EVM-compatible blockchains and price feed types. You choose different types of price feed for different uses, but the request and response format are the same. To read a price feed, specify the following variables:
RPC endpoint URL: This determines which network that your smart contracts will run on. You can use a node provider service or point to your xown client. If you are using a Web3 wallet, it is already configured with the RPC endpoints for several networks and the will automatically detect them for you.
Feed contract address: This determines which Price Feed your smart contract will read. Contract addresses are different for each network. You can find the available contract addresses on the following pages:
The examples in this document indicate these variables, but you can modify the examples to work on different networks and read different feeds.
This guide shows example code that reads Price Feed using the following languages:
Onchain consumer contracts:
Offchain reads using Web3 packages:
with
Reading data feeds on-chain
These code examples demonstrate how to deploy a consumer contract on-chain that reads a data feed and stores the value.
Solidity
To consume price data, your smart contract should reference , which defines the external functions implemented by Data Feeds.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
/**
* THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED
* VALUES FOR CLARITY.
* THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE.
* DO NOT USE THIS CODE IN PRODUCTION.
*/
interface AggregatorV3Interface {
function decimals() external view returns (uint8);
function description() external view returns (string memory);
function version() external view returns (uint256);
// getRoundData and latestRoundData should both raise "No data present"
// if they do not have data to report, instead of returning unset values
// which could be misinterpreted as actual reported values.
function getRoundData(uint80 _roundId)
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
function latestRoundData()
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
}
contract DataConsumer {
AggregatorV3Interface internal dataFeed;
/**
* Network: Bitlayer
* Aggregator: BTC/USD
* Address: 0x62d2c5dEe038FaEbc3F6ec498fD2Bbb3b0080B03
*/
constructor() {
dataFeed = AggregatorV3Interface(
0x62d2c5dEe038FaEbc3F6ec498fD2Bbb3b0080B03
);
}
/**
* Returns the latest answer.
*/
function getDataFeedLatestAnswer() public view returns (int) {
// prettier-ignore
(
/* uint80 roundId */,
int256 answer,
/*uint256 startedAt*/,
/*uint256 timeStamp*/,
/*uint80 answeredInRound*/
) = dataFeed.latestRoundData();
return answer;
}
/**
* Returns decimals.
*/
function getDecimals() public view returns (uint8) {
uint8 decimals = dataFeed.decimals();
return decimals;
}
}
Reading data feeds off-chain
These code examples demonstrate how to read data feeds directly off-chain using Web3 packages for each language.