Last active
March 16, 2021 13:23
-
-
Save alexroan/779f3f0183a5b089a018fc337502531e to your computer and use it in GitHub Desktop.
HistoricalPriceConsumerV3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This example code is designed to quickly deploy an example contract using Remix. | |
pragma solidity ^0.6.7; | |
import "https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/interfaces/AggregatorV3Interface.sol"; | |
contract HistoricalPriceConsumerV3 { | |
AggregatorV3Interface internal priceFeed; | |
/** | |
* Network: Kovan | |
* Aggregator: ETH/USD | |
* Address: 0x9326BFA02ADD2366b30bacB125260Af641031331 | |
*/ | |
constructor() public { | |
priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331); | |
} | |
/** | |
* Returns the latest price | |
*/ | |
function getHistoricalPrice(uint80 roundsBack) public view returns (int256) { | |
(uint80 roundId,,,,) = priceFeed.latestRoundData(); | |
uint80 historicalRoundId = roundId - roundsBack; | |
( | |
uint80 id, | |
int price, | |
uint startedAt, | |
uint timeStamp, | |
uint80 answeredInRound | |
) = priceFeed.getRoundData(historicalRoundId); | |
return price; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment