Last active
July 12, 2020 22:25
-
-
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
This file contains hidden or 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
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