Skip to content

Instantly share code, notes, and snippets.

@what-the-func
Created May 19, 2020 07:04
Show Gist options
  • Save what-the-func/4a77858a973326dff57701a8f1b04ff0 to your computer and use it in GitHub Desktop.
Save what-the-func/4a77858a973326dff57701a8f1b04ff0 to your computer and use it in GitHub Desktop.
Chainlink Aggregator
pragma solidity ^0.6.0;
import "@chainlink/contracts/src/v0.6/dev/AggregatorInterface.sol";
contract MyContract {
AggregatorInterface internal feed;
constructor(address _aggregator) public {
feed = AggregatorInterface(_aggregator);
}
function getLatestAnswer() public view returns (int256) {
return feed.latestAnswer();
}
function getLatestTimestamp() public view returns (uint256) {
return feed.latestTimestamp();
}
function getPreviousAnswer(uint256 _back) public view returns (int256) {
uint256 latest = ref.latestRound();
require(_back <= latest, "Not enough history");
return feed.getAnswer(latest - _back);
}
function getPreviousTimestamp(uint256 _back) public view returns (uint256) {
uint256 latest = ref.latestRound();
require(_back <= latest, "Not enough history");
return feed.getTimestamp(latest - _back);
}
}
@what-the-func
Copy link
Author

what-the-func commented May 19, 2020

Working ETH/USD price feed contract on Rinkeby 0x0bF4e7bf3e1f6D6Dc29AA516A33134985cC3A5aA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment