|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "../lib/forge-std/src/Test.sol"; |
| 5 | +import "../src/morpho-chainlink-v1/ChainlinkOracle.sol"; |
| 6 | +import "./mocks/ChainlinkAggregatorMock.sol"; |
| 7 | +import "./helpers/Constants.sol"; |
| 8 | + |
| 9 | +contract ChainlinkOracleTest is Test { |
| 10 | + function setUp() public { |
| 11 | + vm.createSelectFork(vm.envString("ETH_RPC_URL")); |
| 12 | + } |
| 13 | + |
| 14 | + function testOracleWbtcUsdc() public { |
| 15 | + ChainlinkOracle oracle = new ChainlinkOracle(vaultZero, wBtcBtcFeed, btcUsdFeed, usdcUsdFeed, feedZero, 1, 8, 6); |
| 16 | + (, int256 firstBaseAnswer,,,) = wBtcBtcFeed.latestRoundData(); |
| 17 | + (, int256 secondBaseAnswer,,,) = btcUsdFeed.latestRoundData(); |
| 18 | + (, int256 quoteAnswer,,,) = usdcUsdFeed.latestRoundData(); |
| 19 | + assertEq( |
| 20 | + oracle.price(), |
| 21 | + (uint256(firstBaseAnswer) * uint256(secondBaseAnswer) * 10 ** (36 + 8 + 6 - 8 - 8 - 8)) |
| 22 | + / uint256(quoteAnswer) |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + function testOracleUsdcWbtc() public { |
| 27 | + ChainlinkOracle oracle = new ChainlinkOracle(vaultZero, usdcUsdFeed, feedZero, wBtcBtcFeed, btcUsdFeed, 1, 6, 8); |
| 28 | + (, int256 baseAnswer,,,) = usdcUsdFeed.latestRoundData(); |
| 29 | + (, int256 firstQuoteAnswer,,,) = wBtcBtcFeed.latestRoundData(); |
| 30 | + (, int256 secondQuoteAnswer,,,) = btcUsdFeed.latestRoundData(); |
| 31 | + assertEq( |
| 32 | + oracle.price(), |
| 33 | + (uint256(baseAnswer) * 10 ** (36 + 8 + 8 + 8 - 6 - 8)) |
| 34 | + / (uint256(firstQuoteAnswer) * uint256(secondQuoteAnswer)) |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + function testOracleWbtcEth() public { |
| 39 | + ChainlinkOracle oracle = new ChainlinkOracle(vaultZero, wBtcBtcFeed, btcEthFeed, feedZero, feedZero, 1, 8, 18); |
| 40 | + (, int256 firstBaseAnswer,,,) = wBtcBtcFeed.latestRoundData(); |
| 41 | + (, int256 secondBaseAnswer,,,) = btcEthFeed.latestRoundData(); |
| 42 | + assertEq(oracle.price(), (uint256(firstBaseAnswer) * uint256(secondBaseAnswer) * 10 ** (36 + 18 - 8 - 8 - 18))); |
| 43 | + } |
| 44 | + |
| 45 | + function testOracleStEthUsdc() public { |
| 46 | + ChainlinkOracle oracle = new ChainlinkOracle(vaultZero, stEthEthFeed, feedZero, usdcEthFeed, feedZero, 1, 18, 6); |
| 47 | + (, int256 baseAnswer,,,) = stEthEthFeed.latestRoundData(); |
| 48 | + (, int256 quoteAnswer,,,) = usdcEthFeed.latestRoundData(); |
| 49 | + assertEq(oracle.price(), uint256(baseAnswer) * 10 ** (36 + 18 + 6 - 18 - 18) / uint256(quoteAnswer)); |
| 50 | + } |
| 51 | + |
| 52 | + function testOracleEthUsd() public { |
| 53 | + ChainlinkOracle oracle = new ChainlinkOracle(vaultZero, ethUsdFeed, feedZero, feedZero, feedZero, 1, 18, 0); |
| 54 | + (, int256 expectedPrice,,,) = ethUsdFeed.latestRoundData(); |
| 55 | + assertEq(oracle.price(), uint256(expectedPrice) * 10 ** (36 - 18 - 8)); |
| 56 | + } |
| 57 | + |
| 58 | + function testOracleStEthEth() public { |
| 59 | + ChainlinkOracle oracle = new ChainlinkOracle(vaultZero, stEthEthFeed, feedZero, feedZero, feedZero, 1, 18, 18); |
| 60 | + (, int256 expectedPrice,,,) = stEthEthFeed.latestRoundData(); |
| 61 | + assertEq(oracle.price(), uint256(expectedPrice) * 10 ** (36 + 18 - 18 - 18)); |
| 62 | + assertApproxEqRel(oracle.price(), 1e36, 0.01 ether); |
| 63 | + } |
| 64 | + |
| 65 | + function testOracleEthStEth() public { |
| 66 | + ChainlinkOracle oracle = new ChainlinkOracle(vaultZero, feedZero, feedZero, stEthEthFeed, feedZero, 1, 18, 18); |
| 67 | + (, int256 expectedPrice,,,) = stEthEthFeed.latestRoundData(); |
| 68 | + assertEq(oracle.price(), 10 ** (36 + 18 + 18 - 18) / uint256(expectedPrice)); |
| 69 | + assertApproxEqRel(oracle.price(), 1e36, 0.01 ether); |
| 70 | + } |
| 71 | + |
| 72 | + function testOracleUsdcUsd() public { |
| 73 | + ChainlinkOracle oracle = new ChainlinkOracle(vaultZero, usdcUsdFeed, feedZero, feedZero, feedZero, 1, 6, 0); |
| 74 | + assertApproxEqRel(oracle.price(), 1e36 / 1e6, 0.01 ether); |
| 75 | + } |
| 76 | + |
| 77 | + function testNegativeAnswer(int256 price) public { |
| 78 | + price = bound(price, type(int256).min, -1); |
| 79 | + ChainlinkAggregatorMock aggregator = new ChainlinkAggregatorMock(); |
| 80 | + ChainlinkOracle oracle = new ChainlinkOracle( |
| 81 | + vaultZero, AggregatorV3Interface(address(aggregator)), feedZero, feedZero, feedZero, 1, 18, 0 |
| 82 | + ); |
| 83 | + aggregator.setAnwser(price); |
| 84 | + vm.expectRevert(bytes(ErrorsLib.NEGATIVE_ANSWER)); |
| 85 | + oracle.price(); |
| 86 | + } |
| 87 | + |
| 88 | + function testSDaiEthOracle() public { |
| 89 | + ChainlinkOracle oracle = |
| 90 | + new ChainlinkOracle(sDaiVault, daiEthFeed, feedZero, feedZero, feedZero, 10 ** 18, 18, 18); |
| 91 | + (, int256 expectedPrice,,,) = daiEthFeed.latestRoundData(); |
| 92 | + assertEq( |
| 93 | + oracle.price(), |
| 94 | + sDaiVault.convertToAssets(1e18) * uint256(expectedPrice) * 10 ** (36 + 18 + 0 - 18 - 18 - 18) |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + function testSDaiUsdcOracle() public { |
| 99 | + ChainlinkOracle oracle = |
| 100 | + new ChainlinkOracle(sDaiVault, daiEthFeed, feedZero, usdcEthFeed, feedZero, 10 ** 18, 18, 6); |
| 101 | + (, int256 baseAnswer,,,) = daiEthFeed.latestRoundData(); |
| 102 | + (, int256 quoteAnswer,,,) = usdcEthFeed.latestRoundData(); |
| 103 | + assertEq( |
| 104 | + oracle.price(), |
| 105 | + sDaiVault.convertToAssets(1e18) * uint256(baseAnswer) * 10 ** (36 + 6 + 18 - 18 - 18 - 18) |
| 106 | + / uint256(quoteAnswer) |
| 107 | + ); |
| 108 | + // DAI has 12 more decimals than USDC. |
| 109 | + uint256 expectedPrice = 10 ** (36 - 12); |
| 110 | + // Admit a 50% interest gain before breaking this test. |
| 111 | + uint256 deviation = 0.5 ether; |
| 112 | + assertApproxEqRel(oracle.price(), expectedPrice, deviation); |
| 113 | + } |
| 114 | + |
| 115 | + function testConstructorZeroVaultConversionSample() public { |
| 116 | + vm.expectRevert(bytes(ErrorsLib.VAULT_CONVERSION_SAMPLE_IS_ZERO)); |
| 117 | + new ChainlinkOracle(sDaiVault, daiEthFeed, feedZero, usdcEthFeed, feedZero, 0, 18, 6); |
| 118 | + } |
| 119 | + |
| 120 | + function testConstructorVaultZeroNonOneSample(uint256 vaultConversionSample) public { |
| 121 | + vaultConversionSample = bound(vaultConversionSample, 2, type(uint256).max); |
| 122 | + |
| 123 | + vm.expectRevert(bytes(ErrorsLib.VAULT_CONVERSION_SAMPLE_IS_NOT_ONE)); |
| 124 | + new ChainlinkOracle(vaultZero, daiEthFeed, feedZero, usdcEthFeed, feedZero, vaultConversionSample, 18, 6); |
| 125 | + } |
| 126 | +} |
0 commit comments