|
| 1 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | +pragma solidity ^0.8.4; |
| 3 | + |
| 4 | +import {Vm} from "./../unit/utils/Vm.sol"; |
| 5 | +import {DSTest} from "./../unit/utils/DSTest.sol"; |
| 6 | +import {getCore, getAddresses, FeiTestAddresses} from "./../unit/utils/Fixtures.sol"; |
| 7 | +import {MockScalingPriceOracle} from "../../mock/MockScalingPriceOracle.sol"; |
| 8 | +import {MockL2ScalingPriceOracle} from "../../mock/MockL2ScalingPriceOracle.sol"; |
| 9 | +import {MockChainlinkToken} from "../../mock/MockChainlinkToken.sol"; |
| 10 | +import {Decimal} from "./../../external/Decimal.sol"; |
| 11 | +import {ScalingPriceOracle} from "./../../oracle/ScalingPriceOracle.sol"; |
| 12 | +import {L2ScalingPriceOracle} from "./../../oracle/L2ScalingPriceOracle.sol"; |
| 13 | +import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol"; |
| 14 | +import {LinkTokenInterface} from "@chainlink/contracts/src/v0.8/interfaces/LinkTokenInterface.sol"; |
| 15 | + |
| 16 | +import {console} from "hardhat/console.sol"; |
| 17 | + |
| 18 | +contract IntegrationTestL2ScalingPriceOracle is DSTest { |
| 19 | + using Decimal for Decimal.D256; |
| 20 | + using SafeCast for *; |
| 21 | + |
| 22 | + ScalingPriceOracle private scalingPriceOracle = |
| 23 | + ScalingPriceOracle(0x79412660E95F94a4D2d02a050CEA776200939917); |
| 24 | + L2ScalingPriceOracle private l2scalingPriceOracle; |
| 25 | + |
| 26 | + /// @notice increase price by x% per month |
| 27 | + int256 public monthlyChangeRateBasisPoints = |
| 28 | + scalingPriceOracle.monthlyChangeRateBasisPoints(); |
| 29 | + |
| 30 | + /// @notice the current month's CPI data from ScalingPriceOracle |
| 31 | + uint128 public currentMonth = scalingPriceOracle.currentMonth(); |
| 32 | + |
| 33 | + /// @notice the previous month's CPI data from ScalingPriceOracle |
| 34 | + uint128 public previousMonth = scalingPriceOracle.previousMonth(); |
| 35 | + |
| 36 | + /// @notice address of chainlink oracle to send request |
| 37 | + address public oracle = scalingPriceOracle.oracle(); |
| 38 | + |
| 39 | + /// @notice job id that retrieves the latest CPI data |
| 40 | + bytes32 public jobId = scalingPriceOracle.jobId(); |
| 41 | + |
| 42 | + /// @notice fee of 10 link |
| 43 | + uint256 public fee = scalingPriceOracle.fee(); |
| 44 | + |
| 45 | + /// @notice starting time of the current mainnet scaling price oracle |
| 46 | + uint256 public startTime = scalingPriceOracle.startTime(); |
| 47 | + |
| 48 | + /// @notice starting price of the current mainnet scaling price oracle |
| 49 | + uint256 public startOraclePrice = scalingPriceOracle.oraclePrice(); |
| 50 | + |
| 51 | + Vm public constant vm = Vm(HEVM_ADDRESS); |
| 52 | + FeiTestAddresses public addresses = getAddresses(); |
| 53 | + |
| 54 | + function setUp() public { |
| 55 | + l2scalingPriceOracle = new L2ScalingPriceOracle( |
| 56 | + oracle, |
| 57 | + jobId, |
| 58 | + fee, |
| 59 | + currentMonth, |
| 60 | + previousMonth, |
| 61 | + startTime, |
| 62 | + startOraclePrice |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + function testSetup() public { |
| 67 | + assertEq( |
| 68 | + scalingPriceOracle.remainingTime(), |
| 69 | + l2scalingPriceOracle.remainingTime() |
| 70 | + ); |
| 71 | + assertTrue( |
| 72 | + scalingPriceOracle.isTimeEnded() == |
| 73 | + l2scalingPriceOracle.isTimeEnded() |
| 74 | + ); |
| 75 | + assertEq( |
| 76 | + scalingPriceOracle.startTime(), |
| 77 | + l2scalingPriceOracle.startTime() |
| 78 | + ); |
| 79 | + assertEq( |
| 80 | + scalingPriceOracle.oraclePrice(), |
| 81 | + l2scalingPriceOracle.oraclePrice() |
| 82 | + ); /// starting price is correct |
| 83 | + assertEq(scalingPriceOracle.oracle(), l2scalingPriceOracle.oracle()); |
| 84 | + assertEq(scalingPriceOracle.jobId(), l2scalingPriceOracle.jobId()); |
| 85 | + assertEq(scalingPriceOracle.fee(), l2scalingPriceOracle.fee()); |
| 86 | + assertEq( |
| 87 | + scalingPriceOracle.currentMonth(), |
| 88 | + l2scalingPriceOracle.currentMonth() |
| 89 | + ); |
| 90 | + assertEq( |
| 91 | + scalingPriceOracle.previousMonth(), |
| 92 | + l2scalingPriceOracle.previousMonth() |
| 93 | + ); |
| 94 | + assertEq( |
| 95 | + scalingPriceOracle.getMonthlyAPR(), |
| 96 | + l2scalingPriceOracle.getMonthlyAPR() |
| 97 | + ); |
| 98 | + assertEq( |
| 99 | + scalingPriceOracle.getCurrentOraclePrice(), |
| 100 | + l2scalingPriceOracle.getCurrentOraclePrice() |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + function _testOraclePriceEquivalence() internal { |
| 105 | + assertEq( |
| 106 | + scalingPriceOracle.getCurrentOraclePrice(), |
| 107 | + l2scalingPriceOracle.getCurrentOraclePrice() |
| 108 | + ); |
| 109 | + assertEq( |
| 110 | + scalingPriceOracle.oraclePrice(), |
| 111 | + l2scalingPriceOracle.oraclePrice() |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + /// positive price action from oracle -- inflation case |
| 116 | + function testGetCurrentOraclePriceAfterInterpolation() public { |
| 117 | + vm.warp(block.timestamp + 28 days); |
| 118 | + _testOraclePriceEquivalence(); |
| 119 | + } |
| 120 | + |
| 121 | + /// negative price action from oracle -- deflation case |
| 122 | + function testPriceDecreaseAfterInterpolation() public { |
| 123 | + scalingPriceOracle = new MockScalingPriceOracle( |
| 124 | + oracle, |
| 125 | + jobId, |
| 126 | + fee, |
| 127 | + previousMonth, /// flip current and previous months so that rate is -3% |
| 128 | + currentMonth |
| 129 | + ); |
| 130 | + l2scalingPriceOracle = new MockL2ScalingPriceOracle( |
| 131 | + oracle, |
| 132 | + jobId, |
| 133 | + fee, |
| 134 | + previousMonth, /// flip current and previous months so that rate is -3% |
| 135 | + currentMonth, |
| 136 | + scalingPriceOracle.startTime(), |
| 137 | + 1e18 |
| 138 | + ); |
| 139 | + |
| 140 | + vm.warp(block.timestamp + 29 days); |
| 141 | + assertEq( |
| 142 | + l2scalingPriceOracle.getCurrentOraclePrice(), |
| 143 | + scalingPriceOracle.getCurrentOraclePrice() |
| 144 | + ); |
| 145 | + |
| 146 | + assertEq( |
| 147 | + l2scalingPriceOracle.oraclePrice().toInt256() + |
| 148 | + (l2scalingPriceOracle.monthlyChangeRateBasisPoints() * 1e18) / |
| 149 | + 10_000, |
| 150 | + l2scalingPriceOracle.getCurrentOraclePrice().toInt256() |
| 151 | + ); |
| 152 | + assertEq( |
| 153 | + scalingPriceOracle.oraclePrice().toInt256() + |
| 154 | + (scalingPriceOracle.monthlyChangeRateBasisPoints() * 1e18) / |
| 155 | + 10_000, |
| 156 | + scalingPriceOracle.getCurrentOraclePrice().toInt256() |
| 157 | + ); |
| 158 | + assertEq(l2scalingPriceOracle.remainingTime(), 0); |
| 159 | + assertEq(scalingPriceOracle.remainingTime(), 0); |
| 160 | + |
| 161 | + _testOraclePriceEquivalence(); |
| 162 | + } |
| 163 | + |
| 164 | + function testFulfillFailureTimed() public { |
| 165 | + if (scalingPriceOracle.isTimeEnded()) { |
| 166 | + vm.warp(scalingPriceOracle.startTime()); /// warp to start time if time has ended |
| 167 | + } |
| 168 | + |
| 169 | + assertTrue(!scalingPriceOracle.isTimeEnded()); |
| 170 | + assertTrue(!l2scalingPriceOracle.isTimeEnded()); |
| 171 | + |
| 172 | + vm.expectRevert(bytes("Timed: time not ended, init")); |
| 173 | + l2scalingPriceOracle.requestCPIData(); |
| 174 | + |
| 175 | + vm.expectRevert(bytes("Timed: time not ended, init")); |
| 176 | + scalingPriceOracle.requestCPIData(); |
| 177 | + } |
| 178 | +} |
0 commit comments