Skip to content

Instantly share code, notes, and snippets.

@alexroan
Last active March 16, 2021 13:23
Show Gist options
  • Save alexroan/38b8c82f139943476b2a718a15516922 to your computer and use it in GitHub Desktop.
Save alexroan/38b8c82f139943476b2a718a15516922 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.6.7+commit.b8d736ae.js&optimize=false&gist=
// This example code is designed to quickly deploy an example contract using Remix.
pragma solidity ^0.6.7;
import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.6/interfaces/AggregatorInterface.sol";
/**
* Consumer contract which retrieves the latest and historical
* price data from the ETH/USD price feed on the Ropsten network.
*/
contract HistoricalPriceConsumer {
AggregatorInterface internal priceFeed;
/**
* Network: Ropsten
* Aggregator: ETH/USD
* Address: 0x8468b2bDCE073A157E560AA4D9CcF6dB1DB98507
*/
constructor() public {
priceFeed = AggregatorInterface(0x8468b2bDCE073A157E560AA4D9CcF6dB1DB98507);
}
/**
* Returns historical data from previous update rounds
*/
function getPreviousPrice(uint256 _back) public view returns (int256) {
uint256 latest = priceFeed.latestRound();
require(_back <= latest, "Not enough history");
return priceFeed.getAnswer(latest - _back);
}
/**
* Returns historical data from previous update rounds
*/
function getPreviousPriceTimestamp(uint256 _back) public view returns (uint256) {
uint256 latest = priceFeed.latestRound();
require(_back <= latest, "Not enough history");
return priceFeed.getTimestamp(latest - _back);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment