Last active
March 16, 2021 13:22
-
-
Save alexroan/e32b050a9a832beeee2ebb97a9d75abc to your computer and use it in GitHub Desktop.
New Historical Price Consumer
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 historical price for a round id. | |
* roundId is NOT incremental. Not all roundIds are valid. | |
* You must know a valid roundId before consuming historical data. | |
* | |
* ROUNDID VALUES: | |
* InValid: 18446744073709562300 | |
* Valid: 18446744073709562301 | |
* | |
* @dev A timestamp with zero value means the round is not complete and should not be used. | |
*/ | |
function getHistoricalPrice(uint80 roundId) public view returns (int256) { | |
( | |
uint80 id, | |
int price, | |
uint startedAt, | |
uint timeStamp, | |
uint80 answeredInRound | |
) = priceFeed.getRoundData(roundId); | |
require(timeStamp > 0, "Round not complete"); | |
return price; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment