You can use APRO Data Feeds to connect your smart contracts to asset pricing data. These data feeds use data aggregated from many independent APRO node operators. Each price feed has an onchain address and functions that enable contracts to read pricing data from that address.
This guide shows you how to read Data Feeds and store the value onchain using Solidity.
The code for reading Data Feeds on BitLayer or other EVM-compatible blockchains is the same for each chain and each Data Feed types. You choose different types of feeds for different uses, but the request and response format are the same. The answer decimal length and expected value ranges might change depending on what feed you use.
The contract has the following components:
// SPDX-License-Identifier: MITpragmasolidity ^0.8.7;import {AggregatorV3Interface} from"./AggregatorV3Interface.sol";/** * 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. */contract DataConsumer { AggregatorV3Interface internal dataFeed;/** * Network: Bitlayer * Aggregator: BTC/USD * Address: 0x62d2c5dEe038FaEbc3F6ec498fD2Bbb3b0080B03 */constructor() { dataFeed =AggregatorV3Interface(0x62d2c5dEe038FaEbc3F6ec498fD2Bbb3b0080B03 ); }/** * Returns the latest answer. */functiongetDataFeedLatestAnswer() publicviewreturns (int) {// prettier-ignore (/* uint80 roundId */,int256 answer,/*uint256 startedAt*/,/*uint256 timeStamp*/,/*uint80 answeredInRound*/ ) = dataFeed.latestRoundData();return answer; }/** * Returns decimals. */functiongetDecimals() publicviewreturns (uint8) {uint8 decimals = dataFeed.decimals();return decimals; }}
This example contract obtains the latest price answer from the BTC / USD feed on the Bitlayer testnet.