|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "forge-std/Test.sol"; |
| 5 | +import "src/chainlink/ChainlinkOracle.sol"; |
| 6 | +import "src/chainlink/libraries/ErrorsLib.sol"; |
| 7 | + |
| 8 | +AggregatorV3Interface constant feedZero = AggregatorV3Interface(address(0)); |
| 9 | +// 8 decimals of precision |
| 10 | +AggregatorV3Interface constant btcUsdFeed = AggregatorV3Interface(0xF4030086522a5bEEa4988F8cA5B36dbC97BeE88c); |
| 11 | +// 8 decimals of precision |
| 12 | +AggregatorV3Interface constant usdcUsdFeed = AggregatorV3Interface(0x8fFfFfd4AfB6115b954Bd326cbe7B4BA576818f6); |
| 13 | +// 18 decimals of precision |
| 14 | +AggregatorV3Interface constant btcEthFeed = AggregatorV3Interface(0xdeb288F737066589598e9214E782fa5A8eD689e8); |
| 15 | +// 8 decimals of precision |
| 16 | +AggregatorV3Interface constant wBtcBtcFeed = AggregatorV3Interface(0xfdFD9C85aD200c506Cf9e21F1FD8dd01932FBB23); |
| 17 | +// 18 decimals of precision |
| 18 | +AggregatorV3Interface constant stEthEthFeed = AggregatorV3Interface(0x86392dC19c0b719886221c78AB11eb8Cf5c52812); |
| 19 | +// 18 decimals of precision |
| 20 | +AggregatorV3Interface constant usdcEthFeed = AggregatorV3Interface(0x986b5E1e1755e3C2440e960477f25201B0a8bbD4); |
| 21 | +// 8 decimals of precision |
| 22 | +AggregatorV3Interface constant ethUsdFeed = AggregatorV3Interface(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419); |
| 23 | + |
| 24 | +contract FakeAggregator { |
| 25 | + int256 public answer; |
| 26 | + |
| 27 | + function setAnwser(int256 newAnswer) external { |
| 28 | + answer = newAnswer; |
| 29 | + } |
| 30 | + |
| 31 | + function latestRoundData() external view returns (uint80, int256, uint256, uint256, uint80) { |
| 32 | + return (0, answer, 0, 0, 0); |
| 33 | + } |
| 34 | + |
| 35 | + function decimals() external pure returns (uint256) { |
| 36 | + return 8; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +contract ChainlinkOracleTest is Test { |
| 41 | + function setUp() public { |
| 42 | + vm.createSelectFork(vm.envString("ETH_RPC_URL")); |
| 43 | + } |
| 44 | + |
| 45 | + function testOracleWbtcUsdc() public { |
| 46 | + ChainlinkOracle oracle = |
| 47 | + new ChainlinkOracle(wBtcBtcFeed, btcUsdFeed, usdcUsdFeed, feedZero, 8, 6); |
| 48 | + (, int256 firstBaseAnswer,,,) = wBtcBtcFeed.latestRoundData(); |
| 49 | + (, int256 secondBaseAnswer,,,) = btcUsdFeed.latestRoundData(); |
| 50 | + (, int256 quoteAnswer,,,) = usdcUsdFeed.latestRoundData(); |
| 51 | + assertEq( |
| 52 | + oracle.price(), |
| 53 | + (uint256(firstBaseAnswer) * uint256(secondBaseAnswer) * 10 ** (36 + 8 + 6 - 8 - 8 - 8)) |
| 54 | + / uint256(quoteAnswer) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + function testOracleUsdcWbtc() public { |
| 59 | + ChainlinkOracle oracle = |
| 60 | + new ChainlinkOracle(usdcUsdFeed, feedZero, wBtcBtcFeed, btcUsdFeed, 6, 8); |
| 61 | + (, int256 baseAnswer,,,) = usdcUsdFeed.latestRoundData(); |
| 62 | + (, int256 firstQuoteAnswer,,,) = wBtcBtcFeed.latestRoundData(); |
| 63 | + (, int256 secondQuoteAnswer,,,) = btcUsdFeed.latestRoundData(); |
| 64 | + assertEq( |
| 65 | + oracle.price(), |
| 66 | + (uint256(baseAnswer) * 10 ** (36 + 8 + 8 + 8 - 6 - 8)) |
| 67 | + / (uint256(firstQuoteAnswer) * uint256(secondQuoteAnswer)) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + function testOracleWbtcEth() public { |
| 72 | + ChainlinkOracle oracle = |
| 73 | + new ChainlinkOracle(wBtcBtcFeed, btcEthFeed, feedZero, feedZero, 8, 18); |
| 74 | + (, int256 firstBaseAnswer,,,) = wBtcBtcFeed.latestRoundData(); |
| 75 | + (, int256 secondBaseAnswer,,,) = btcEthFeed.latestRoundData(); |
| 76 | + assertEq(oracle.price(), (uint256(firstBaseAnswer) * uint256(secondBaseAnswer) * 10 ** (36 + 18 - 8 - 8 - 18))); |
| 77 | + } |
| 78 | + |
| 79 | + function testOracleStEthUsdc() public { |
| 80 | + ChainlinkOracle oracle = new ChainlinkOracle(stEthEthFeed, feedZero, usdcEthFeed, feedZero, 18, 6); |
| 81 | + (, int256 baseAnswer,,,) = stEthEthFeed.latestRoundData(); |
| 82 | + (, int256 quoteAnswer,,,) = usdcEthFeed.latestRoundData(); |
| 83 | + assertEq(oracle.price(), uint256(baseAnswer) * 10 ** (36 + 18 + 6 - 18 - 18) / uint256(quoteAnswer)); |
| 84 | + } |
| 85 | + |
| 86 | + function testOracleEthUsd() public { |
| 87 | + ChainlinkOracle oracle = new ChainlinkOracle(ethUsdFeed, feedZero, feedZero, feedZero, 18, 0); |
| 88 | + (, int256 expectedPrice,,,) = ethUsdFeed.latestRoundData(); |
| 89 | + assertEq(oracle.price(), uint256(expectedPrice) * 10 ** (36 - 18 - 8)); |
| 90 | + } |
| 91 | + |
| 92 | + function testOracleStEthEth() public { |
| 93 | + ChainlinkOracle oracle = new ChainlinkOracle(stEthEthFeed, feedZero, feedZero, feedZero, 18, 18); |
| 94 | + (, int256 expectedPrice,,,) = stEthEthFeed.latestRoundData(); |
| 95 | + assertEq(oracle.price(), uint256(expectedPrice) * 10 ** (36 + 18 - 18 - 18)); |
| 96 | + assertApproxEqRel(oracle.price(), 1e36, 0.01 ether); |
| 97 | + } |
| 98 | + |
| 99 | + function testOracleEthStEth() public { |
| 100 | + ChainlinkOracle oracle = new ChainlinkOracle(feedZero, feedZero, stEthEthFeed, feedZero, 18, 18); |
| 101 | + (, int256 expectedPrice,,,) = stEthEthFeed.latestRoundData(); |
| 102 | + assertEq(oracle.price(), 10 ** (36 + 18 + 18 - 18) / uint256(expectedPrice)); |
| 103 | + assertApproxEqRel(oracle.price(), 1e36, 0.01 ether); |
| 104 | + } |
| 105 | + |
| 106 | + function testOracleUsdcUsd() public { |
| 107 | + ChainlinkOracle oracle = new ChainlinkOracle(usdcUsdFeed, feedZero, feedZero, feedZero, 6, 0); |
| 108 | + assertApproxEqRel(oracle.price(), 1e36 / 1e6, 0.01 ether); |
| 109 | + } |
| 110 | + |
| 111 | + function testNegativeAnswer(int256 price) public { |
| 112 | + price = bound(price, type(int256).min, -1); |
| 113 | + FakeAggregator aggregator = new FakeAggregator(); |
| 114 | + ChainlinkOracle oracle = |
| 115 | + new ChainlinkOracle(AggregatorV3Interface(address(aggregator)), feedZero, feedZero, feedZero, 18, 0); |
| 116 | + aggregator.setAnwser(price); |
| 117 | + vm.expectRevert(bytes(ErrorsLib.NEGATIVE_ANSWER)); |
| 118 | + oracle.price(); |
| 119 | + } |
| 120 | +} |
0 commit comments