Skip to content

Instantly share code, notes, and snippets.

@PatrickAlphaC
Last active July 12, 2020 22:25
Show Gist options
  • Save PatrickAlphaC/ab642dff256732c82594e7b124a0b3a5 to your computer and use it in GitHub Desktop.
Save PatrickAlphaC/ab642dff256732c82594e7b124a0b3a5 to your computer and use it in GitHub Desktop.
Using Chainlink's chosen set of nodes to pull price feeds. Deploy to remix with: https://remix.ethereum.org/#version=soljson-v0.6.2+commit.bacdbe57.js&optimize=false&gist=ab642dff256732c82594e7b124a0b3a5
pragma solidity ^0.6.0;
import "https://github.com/smartcontractkit/chainlink/evm-contracts/src/v0.6/interfaces/AggregatorInterface.sol";
contract ReferenceConsumer {
AggregatorInterface internal ref;
/**
* @dev Ropsten example _aggregator:
* @dev ETH/USD feed: 0x8468b2bDCE073A157E560AA4D9CcF6dB1DB98507
* @dev More available from https://docs.chain.link/docs/reference-contracts
*/
constructor(address _aggregator) public {
ref = AggregatorInterface(_aggregator);
}
/**
* @notice Returns the latest answer from the _aggregator
* @dev The answer is multiplied by a constant, depending on the aggregator
* @dev For example, the Ropsten ETH/USD feed is multiplied by 100,000,000
* @dev See https://docs.chain.link/docs/using-chainlink-reference-contracts#use-the-reference-data
*/
function getLatestAnswer() public view returns (int256) {
return ref.latestAnswer();
}
/**
* @notice Returns the latest update timestamp from the _aggregator
*/
function getLatestTimestamp() public view returns (uint256) {
return ref.latestTimestamp();
}
/**
* @notice Returns historical data from previous update rounds
*/
function getPreviousAnswer(uint256 _back) public view returns (int256) {
uint256 latest = ref.latestRound();
require(_back <= latest, "Not enough history");
return ref.getAnswer(latest - _back);
}
/**
* @notice Returns historical data from previous update rounds
*/
function getPreviousTimestamp(uint256 _back) public view returns (uint256) {
uint256 latest = ref.latestRound();
require(_back <= latest, "Not enough history");
return ref.getTimestamp(latest - _back);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment