-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,306 @@ | ||
pragma solidity ^0.7.0; | ||
|
||
import "./Owned.sol"; | ||
import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV2V3Interface.sol"; | ||
|
||
/** | ||
* @title A trusted proxy for updating where current answers are read from | ||
* @notice This contract provides a consistent address for the | ||
* CurrentAnwerInterface but delegates where it reads from to the owner, who is | ||
* trusted to update it. | ||
*/ | ||
contract AggregatorProxy is AggregatorV2V3Interface, Owned { | ||
struct Phase { | ||
uint16 id; | ||
AggregatorV2V3Interface aggregator; | ||
} | ||
Phase private currentPhase; | ||
AggregatorV2V3Interface public proposedAggregator; | ||
mapping(uint16 => AggregatorV2V3Interface) public phaseAggregators; | ||
|
||
uint256 private constant PHASE_OFFSET = 64; | ||
uint256 private constant PHASE_SIZE = 16; | ||
uint256 private constant MAX_ID = 2**(PHASE_OFFSET + PHASE_SIZE) - 1; | ||
|
||
constructor(address _aggregator) public Owned() { | ||
setAggregator(_aggregator); | ||
} | ||
|
||
/** | ||
* @notice Reads the current answer from aggregator delegated to. | ||
* | ||
* @dev #[deprecated] Use latestRoundData instead. This does not error if no | ||
* answer has been reached, it will simply return 0. Either wait to point to | ||
* an already answered Aggregator or use the recommended latestRoundData | ||
* instead which includes better verification information. | ||
*/ | ||
|
||
|
||
/** | ||
* @notice Reads the last updated height from aggregator delegated to. | ||
* | ||
* @dev #[deprecated] Use latestRoundData instead. This does not error if no | ||
* answer has been reached, it will simply return 0. Either wait to point to | ||
* an already answered Aggregator or use the recommended latestRoundData | ||
* instead which includes better verification information. | ||
*/ | ||
|
||
|
||
/** | ||
* @notice get past rounds answers | ||
* @param _roundId the answer number to retrieve the answer for | ||
* | ||
* @dev #[deprecated] Use getRoundData instead. This does not error if no | ||
* answer has been reached, it will simply return 0. Either wait to point to | ||
* an already answered Aggregator or use the recommended getRoundData | ||
* instead which includes better verification information. | ||
*/ | ||
|
||
|
||
/** | ||
* @notice get block timestamp when an answer was last updated | ||
* @param _roundId the answer number to retrieve the updated timestamp for | ||
* | ||
* @dev #[deprecated] Use getRoundData instead. This does not error if no | ||
* answer has been reached, it will simply return 0. Either wait to point to | ||
* an already answered Aggregator or use the recommended getRoundData | ||
* instead which includes better verification information. | ||
*/ | ||
|
||
|
||
/** | ||
* @notice get the latest completed round where the answer was updated. This | ||
* ID includes the proxy's phase, to make sure round IDs increase even when | ||
* switching to a newly deployed aggregator. | ||
* | ||
* @dev #[deprecated] Use latestRoundData instead. This does not error if no | ||
* answer has been reached, it will simply return 0. Either wait to point to | ||
* an already answered Aggregator or use the recommended latestRoundData | ||
* instead which includes better verification information. | ||
*/ | ||
|
||
|
||
/** | ||
* @notice get data about a round. Consumers are encouraged to check | ||
* that they're receiving fresh data by inspecting the updatedAt and | ||
* answeredInRound return values. | ||
* Note that different underlying implementations of AggregatorV3Interface | ||
* have slightly different semantics for some of the return values. Consumers | ||
* should determine what implementations they expect to receive | ||
* data from and validate that they can properly handle return data from all | ||
* of them. | ||
* @param _roundId the requested round ID as presented through the proxy, this | ||
* is made up of the aggregator's round ID with the phase ID encoded in the | ||
* two highest order bytes | ||
* @return roundId is the round ID from the aggregator for which the data was | ||
* retrieved combined with an phase to ensure that round IDs get larger as | ||
* time moves forward. | ||
* @return answer is the answer for the given round | ||
* @return startedAt is the timestamp when the round was started. | ||
* (Only some AggregatorV3Interface implementations return meaningful values) | ||
* @return updatedAt is the timestamp when the round last was updated (i.e. | ||
* answer was last computed) | ||
* @return answeredInRound is the round ID of the round in which the answer | ||
* was computed. | ||
* (Only some AggregatorV3Interface implementations return meaningful values) | ||
* @dev Note that answer and updatedAt may change between queries. | ||
*/ | ||
|
||
|
||
/** | ||
* @notice get data about the latest round. Consumers are encouraged to check | ||
* that they're receiving fresh data by inspecting the updatedAt and | ||
* answeredInRound return values. | ||
* Note that different underlying implementations of AggregatorV3Interface | ||
* have slightly different semantics for some of the return values. Consumers | ||
* should determine what implementations they expect to receive | ||
* data from and validate that they can properly handle return data from all | ||
* of them. | ||
* @return roundId is the round ID from the aggregator for which the data was | ||
* retrieved combined with an phase to ensure that round IDs get larger as | ||
* time moves forward. | ||
* @return answer is the answer for the given round | ||
* @return startedAt is the timestamp when the round was started. | ||
* (Only some AggregatorV3Interface implementations return meaningful values) | ||
* @return updatedAt is the timestamp when the round last was updated (i.e. | ||
* answer was last computed) | ||
* @return answeredInRound is the round ID of the round in which the answer | ||
* was computed. | ||
* (Only some AggregatorV3Interface implementations return meaningful values) | ||
* @dev Note that answer and updatedAt may change between queries. | ||
*/ | ||
|
||
|
||
/** | ||
* @notice Used if an aggregator contract has been proposed. | ||
* @param _roundId the round ID to retrieve the round data for | ||
* @return roundId is the round ID for which data was retrieved | ||
* @return answer is the answer for the given round | ||
* @return startedAt is the timestamp when the round was started. | ||
* (Only some AggregatorV3Interface implementations return meaningful values) | ||
* @return updatedAt is the timestamp when the round last was updated (i.e. | ||
* answer was last computed) | ||
* @return answeredInRound is the round ID of the round in which the answer | ||
* was computed. | ||
*/ | ||
function proposedGetRoundData(uint80 _roundId) | ||
public | ||
view | ||
virtual | ||
hasProposal() | ||
returns ( | ||
uint80 roundId, | ||
int256 answer, | ||
uint256 startedAt, | ||
uint256 updatedAt, | ||
uint80 answeredInRound | ||
) | ||
{ | ||
return proposedAggregator.getRoundData(_roundId); | ||
} | ||
|
||
/** | ||
* @notice Used if an aggregator contract has been proposed. | ||
* @return roundId is the round ID for which data was retrieved | ||
* @return answer is the answer for the given round | ||
* @return startedAt is the timestamp when the round was started. | ||
* (Only some AggregatorV3Interface implementations return meaningful values) | ||
* @return updatedAt is the timestamp when the round last was updated (i.e. | ||
* answer was last computed) | ||
* @return answeredInRound is the round ID of the round in which the answer | ||
* was computed. | ||
*/ | ||
function proposedLatestRoundData() | ||
public | ||
view | ||
virtual | ||
hasProposal() | ||
returns ( | ||
uint80 roundId, | ||
int256 answer, | ||
uint256 startedAt, | ||
uint256 updatedAt, | ||
uint80 answeredInRound | ||
) | ||
{ | ||
return proposedAggregator.latestRoundData(); | ||
} | ||
|
||
/** | ||
* @notice returns the current phase's aggregator address. | ||
*/ | ||
function aggregator() external view returns (address) { | ||
return address(currentPhase.aggregator); | ||
} | ||
|
||
/** | ||
* @notice returns the current phase's ID. | ||
*/ | ||
function phaseId() external view returns (uint16) { | ||
return currentPhase.id; | ||
} | ||
|
||
/** | ||
* @notice represents the number of decimals the aggregator responses represent. | ||
*/ | ||
|
||
|
||
/** | ||
* @notice the version number representing the type of aggregator the proxy | ||
* points to. | ||
*/ | ||
|
||
|
||
/** | ||
* @notice returns the description of the aggregator the proxy points to. | ||
*/ | ||
|
||
|
||
/** | ||
* @notice Allows the owner to propose a new address for the aggregator | ||
* @param _aggregator The new address for the aggregator contract | ||
*/ | ||
function proposeAggregator(address _aggregator) external onlyOwner() { | ||
proposedAggregator = AggregatorV2V3Interface(_aggregator); | ||
} | ||
|
||
/** | ||
* @notice Allows the owner to confirm and change the address | ||
* to the proposed aggregator | ||
* @dev Reverts if the given address doesn't match what was previously | ||
* proposed | ||
* @param _aggregator The new address for the aggregator contract | ||
*/ | ||
function confirmAggregator(address _aggregator) external onlyOwner() { | ||
require( | ||
_aggregator == address(proposedAggregator), | ||
"Invalid proposed aggregator" | ||
); | ||
delete proposedAggregator; | ||
setAggregator(_aggregator); | ||
} | ||
|
||
/* | ||
* Internal | ||
*/ | ||
|
||
function setAggregator(address _aggregator) internal { | ||
uint16 id = currentPhase.id + 1; | ||
currentPhase = Phase(id, AggregatorV2V3Interface(_aggregator)); | ||
phaseAggregators[id] = AggregatorV2V3Interface(_aggregator); | ||
} | ||
|
||
function addPhase(uint16 _phase, uint64 _originalId) | ||
internal | ||
view | ||
returns (uint80) | ||
{ | ||
return uint80((uint256(_phase) << PHASE_OFFSET) | _originalId); | ||
} | ||
|
||
function parseIds(uint256 _roundId) internal view returns (uint16, uint64) { | ||
uint16 phaseId = uint16(_roundId >> PHASE_OFFSET); | ||
uint64 aggregatorRoundId = uint64(_roundId); | ||
|
||
return (phaseId, aggregatorRoundId); | ||
} | ||
|
||
function addPhaseIds( | ||
uint80 roundId, | ||
int256 answer, | ||
uint256 startedAt, | ||
uint256 updatedAt, | ||
uint80 answeredInRound, | ||
uint16 phaseId | ||
) | ||
internal | ||
view | ||
returns ( | ||
uint80, | ||
int256, | ||
uint256, | ||
uint256, | ||
uint80 | ||
) | ||
{ | ||
return ( | ||
addPhase(phaseId, uint64(roundId)), | ||
answer, | ||
startedAt, | ||
updatedAt, | ||
addPhase(phaseId, uint64(answeredInRound)) | ||
); | ||
} | ||
|
||
/* | ||
* Modifiers | ||
*/ | ||
|
||
modifier hasProposal() { | ||
require( | ||
address(proposedAggregator) != address(0), | ||
"No proposed aggregator present" | ||
); | ||
_; | ||
} | ||
} |