DEXAggregatorV1.sol
1 pragma solidity >=0.6.0; 2 3 import "../interfaces/AggregatorV3Interface.sol"; 4 import "../interfaces/IDEXOracleV1.sol"; 5 6 7 contract DEXAggregatorV1 is AggregatorV3Interface { 8 9 mapping(uint => uint) rounds; 10 11 uint latestRound; 12 int[] answers; 13 uint[] updatedAts; 14 15 bool private lockedRound; 16 bool private lockedAnswers; 17 bool private lockedUpdatedAts; 18 19 address _dexOracle; 20 21 22 constructor(address dexOracle) public { 23 _dexOracle = dexOracle; 24 } 25 26 function decimals() override external view returns (uint8) { 27 28 return 8; 29 } 30 31 function description() override external view returns (string memory) { 32 33 } 34 35 function version() override external view returns (uint256) { 36 37 } 38 39 /* SEEDING FOR INITIALIZATION BELOW */ 40 41 function setRoundIds(uint[] calldata rids) external { 42 require(lockedRound == false && latestRound == 0, "already init round"); 43 for (uint i = 0; i < rids.length; i++) { 44 rounds[rids[i]] = i; 45 } 46 47 latestRound = rids[ rids.length - 1]; 48 lockedRound = true; 49 } 50 51 function setAnswers(int[] calldata ans) external { 52 require(lockedAnswers == false && answers.length == 0, "already init answers"); 53 answers = ans; 54 lockedAnswers = true; 55 } 56 57 function setUpdatedAts(uint[] calldata uts) external { 58 require(lockedUpdatedAts == false && updatedAts.length == 0, "already init answers"); 59 updatedAts = uts; 60 lockedUpdatedAts = true; 61 } 62 63 /* SEEDING FOR INITIALIZATION ABOVE */ 64 65 function oracle() external view returns (address) { 66 return _dexOracle; 67 } 68 69 function incrementRound() external { 70 appendUpdatedAt(); 71 appendAnswer(); 72 appendRoundId(); 73 } 74 75 function appendRoundId() private { 76 if (answers.length > 1) { 77 rounds[latestRound++] = answers.length; 78 } else { 79 rounds[latestRound] = answers.length; 80 } 81 } 82 83 function appendAnswer() private { 84 answers.push(IDEXOracleV1(_dexOracle).latestPrice()); 85 } 86 87 function appendUpdatedAt() private { 88 uint256 ct = IDEXOracleV1(_dexOracle).latestCapture(); 89 require(ct != updatedAts[updatedAts.length-1], "DEXAggregatorV1: too soon"); 90 updatedAts.push(ct); 91 } 92 93 function getRoundData(uint80 _roundId) 94 override 95 external 96 view 97 returns 98 ( 99 uint80 roundId, 100 int256 answer, 101 uint256, 102 uint256 updatedAt, 103 uint80 104 ) 105 { 106 roundId = _roundId; 107 answer = answers[rounds[_roundId]]; 108 updatedAt = updatedAts[rounds[_roundId]]; 109 } 110 111 function latestRoundId() public view returns (uint) { 112 return latestRound; 113 } 114 115 function latestRoundData() 116 override 117 external 118 view 119 returns 120 ( 121 uint80 roundId, 122 int256 answer, 123 uint256, 124 uint256 updatedAt, 125 uint80 126 ) 127 { 128 roundId = uint80(latestRound); 129 answer = answers[rounds[latestRound]]; 130 updatedAt = updatedAts[rounds[latestRound]]; 131 } 132 }