From 0c07aa26879b8e3d24adf93455d7c50806c893c4 Mon Sep 17 00:00:00 2001 From: MathisGD Date: Wed, 4 Oct 2023 10:15:14 +0200 Subject: [PATCH 1/2] test: fuzz testNegativeAnswer --- test/chainlink/Oracle2Test.sol | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/chainlink/Oracle2Test.sol b/test/chainlink/Oracle2Test.sol index a9966d3..740faca 100644 --- a/test/chainlink/Oracle2Test.sol +++ b/test/chainlink/Oracle2Test.sol @@ -67,11 +67,12 @@ contract OracleTest is Test { assertApproxEqRel(oracle.price(), 1e36 / 1e6, 0.01 ether); } - function testNegativeAnswer() public { + function testNegativeAnswer(int price) public { + vm.assume(price < 0); FakeAggregator aggregator = new FakeAggregator(); Oracle2 oracle = new Oracle2(AggregatorV3Interface(address(aggregator)), AggregatorV3Interface(address(0)), 18, 0); - aggregator.setAnwser(-1); + aggregator.setAnwser(price); vm.expectRevert(bytes(ErrorsLib.NEGATIVE_ANSWER)); oracle.price(); } From a5fdad8a382ed372ec20a9e427441f1960fee12b Mon Sep 17 00:00:00 2001 From: MathisGD Date: Wed, 4 Oct 2023 10:17:26 +0200 Subject: [PATCH 2/2] style: rename contracts --- .../{Oracle4.sol => OracleFourFeeds.sol} | 2 +- .../{Oracle2.sol => OracleTwoFeeds.sol} | 2 +- ...Oracle4Test.sol => OracleFourFeedsTest.sol} | 12 ++++++------ ...{Oracle2Test.sol => OracleTwoFeedsTest.sol} | 18 +++++++++--------- 4 files changed, 17 insertions(+), 17 deletions(-) rename src/chainlink/{Oracle4.sol => OracleFourFeeds.sol} (98%) rename src/chainlink/{Oracle2.sol => OracleTwoFeeds.sol} (98%) rename test/chainlink/{Oracle4Test.sol => OracleFourFeedsTest.sol} (79%) rename test/chainlink/{Oracle2Test.sol => OracleTwoFeedsTest.sol} (77%) diff --git a/src/chainlink/Oracle4.sol b/src/chainlink/OracleFourFeeds.sol similarity index 98% rename from src/chainlink/Oracle4.sol rename to src/chainlink/OracleFourFeeds.sol index 3c64f10..084f5cb 100644 --- a/src/chainlink/Oracle4.sol +++ b/src/chainlink/OracleFourFeeds.sol @@ -5,7 +5,7 @@ import {IOracle} from "morpho-blue/interfaces/IOracle.sol"; import {AggregatorV3Interface, DataFeedLib} from "./libraries/DataFeedLib.sol"; -contract Oracle4 is IOracle { +contract OracleFourFeeds is IOracle { using DataFeedLib for AggregatorV3Interface; /* CONSTANT */ diff --git a/src/chainlink/Oracle2.sol b/src/chainlink/OracleTwoFeeds.sol similarity index 98% rename from src/chainlink/Oracle2.sol rename to src/chainlink/OracleTwoFeeds.sol index 3183203..a08e259 100644 --- a/src/chainlink/Oracle2.sol +++ b/src/chainlink/OracleTwoFeeds.sol @@ -5,7 +5,7 @@ import {IOracle} from "morpho-blue/interfaces/IOracle.sol"; import {AggregatorV3Interface, DataFeedLib} from "./libraries/DataFeedLib.sol"; -contract Oracle2 is IOracle { +contract OracleTwoFeeds is IOracle { using DataFeedLib for AggregatorV3Interface; /* CONSTANT */ diff --git a/test/chainlink/Oracle4Test.sol b/test/chainlink/OracleFourFeedsTest.sol similarity index 79% rename from test/chainlink/Oracle4Test.sol rename to test/chainlink/OracleFourFeedsTest.sol index c687a4c..52981a5 100644 --- a/test/chainlink/Oracle4Test.sol +++ b/test/chainlink/OracleFourFeedsTest.sol @@ -2,7 +2,7 @@ pragma solidity ^0.8.0; import "forge-std/Test.sol"; -import "src/chainlink/Oracle4.sol"; +import "src/chainlink/OracleFourFeeds.sol"; import "src/chainlink/libraries/ErrorsLib.sol"; // 8 decimals of precision @@ -14,13 +14,13 @@ AggregatorV3Interface constant btcEthFeed = AggregatorV3Interface(0xdeb288F73706 // 8 decimals of precision AggregatorV3Interface constant wBtcBtcFeed = AggregatorV3Interface(0xfdFD9C85aD200c506Cf9e21F1FD8dd01932FBB23); -contract OracleTest is Test { +contract OracleFourFeedsTest is Test { function setUp() public { vm.selectFork(vm.createFork(vm.envString("ETH_RPC_URL"))); } function testOracleWbtcUsdc() public { - Oracle4 oracle = new Oracle4(wBtcBtcFeed, btcUsdFeed, usdcUsdFeed, AggregatorV3Interface(address(0)), 8, 6); + OracleFourFeeds oracle = new OracleFourFeeds(wBtcBtcFeed, btcUsdFeed, usdcUsdFeed, AggregatorV3Interface(address(0)), 8, 6); (, int256 firstBaseAnswer,,,) = wBtcBtcFeed.latestRoundData(); (, int256 secondBaseAnswer,,,) = btcUsdFeed.latestRoundData(); (, int256 quoteAnswer,,,) = usdcUsdFeed.latestRoundData(); @@ -31,7 +31,7 @@ contract OracleTest is Test { } function testOracleUsdcWbtc() public { - Oracle4 oracle = new Oracle4(usdcUsdFeed, AggregatorV3Interface(address(0)), wBtcBtcFeed, btcUsdFeed, 6, 8); + OracleFourFeeds oracle = new OracleFourFeeds(usdcUsdFeed, AggregatorV3Interface(address(0)), wBtcBtcFeed, btcUsdFeed, 6, 8); (, int256 baseAnswer,,,) = usdcUsdFeed.latestRoundData(); (, int256 firstQuoteAnswer,,,) = wBtcBtcFeed.latestRoundData(); (, int256 secondQuoteAnswer,,,) = btcUsdFeed.latestRoundData(); @@ -42,8 +42,8 @@ contract OracleTest is Test { } function testOracleWbtcEth() public { - Oracle4 oracle = - new Oracle4(wBtcBtcFeed, btcEthFeed, AggregatorV3Interface(address(0)), AggregatorV3Interface(address(0)), 8, 18); + OracleFourFeeds oracle = + new OracleFourFeeds(wBtcBtcFeed, btcEthFeed, AggregatorV3Interface(address(0)), AggregatorV3Interface(address(0)), 8, 18); (, int256 firstBaseAnswer,,,) = wBtcBtcFeed.latestRoundData(); (, int256 secondBaseAnswer,,,) = btcEthFeed.latestRoundData(); assertEq(oracle.price(), (uint256(firstBaseAnswer) * uint256(secondBaseAnswer) * 10 ** (36 + 18 - 8 - 8 - 18))); diff --git a/test/chainlink/Oracle2Test.sol b/test/chainlink/OracleTwoFeedsTest.sol similarity index 77% rename from test/chainlink/Oracle2Test.sol rename to test/chainlink/OracleTwoFeedsTest.sol index 740faca..091f832 100644 --- a/test/chainlink/Oracle2Test.sol +++ b/test/chainlink/OracleTwoFeedsTest.sol @@ -2,7 +2,7 @@ pragma solidity ^0.8.0; import "forge-std/Test.sol"; -import "src/chainlink/Oracle2.sol"; +import "src/chainlink/OracleTwoFeeds.sol"; import "src/chainlink/libraries/ErrorsLib.sol"; // 18 decimals of precision @@ -30,48 +30,48 @@ contract FakeAggregator { } } -contract OracleTest is Test { +contract OracleTwoFeedsTest is Test { function setUp() public { vm.selectFork(vm.createFork(vm.envString("ETH_RPC_URL"))); } function testOracleStEthUsdc() public { - Oracle2 oracle = new Oracle2(stEthEthFeed, usdcEthFeed, 18, 6); + OracleTwoFeeds oracle = new OracleTwoFeeds(stEthEthFeed, usdcEthFeed, 18, 6); (, int256 baseAnswer,,,) = stEthEthFeed.latestRoundData(); (, int256 quoteAnswer,,,) = usdcEthFeed.latestRoundData(); assertEq(oracle.price(), uint256(baseAnswer) * 10 ** (36 + 18 + 6 - 18 - 18) / uint256(quoteAnswer)); } function testOracleEthUsd() public { - Oracle2 oracle = new Oracle2(ethUsdFeed, AggregatorV3Interface(address(0)), 18, 0); + OracleTwoFeeds oracle = new OracleTwoFeeds(ethUsdFeed, AggregatorV3Interface(address(0)), 18, 0); (, int256 expectedPrice,,,) = ethUsdFeed.latestRoundData(); assertEq(oracle.price(), uint256(expectedPrice) * 10 ** (36 - 18 - 8)); } function testOracleStEthEth() public { - Oracle2 oracle = new Oracle2(stEthEthFeed, AggregatorV3Interface(address(0)), 18, 18); + OracleTwoFeeds oracle = new OracleTwoFeeds(stEthEthFeed, AggregatorV3Interface(address(0)), 18, 18); (, int256 expectedPrice,,,) = stEthEthFeed.latestRoundData(); assertEq(oracle.price(), uint256(expectedPrice) * 10 ** (36 + 18 - 18 - 18)); assertApproxEqRel(oracle.price(), 1e36, 0.01 ether); } function testOracleEthStEth() public { - Oracle2 oracle = new Oracle2(AggregatorV3Interface(address(0)), stEthEthFeed, 18, 18); + OracleTwoFeeds oracle = new OracleTwoFeeds(AggregatorV3Interface(address(0)), stEthEthFeed, 18, 18); (, int256 expectedPrice,,,) = stEthEthFeed.latestRoundData(); assertEq(oracle.price(), 10 ** (36 + 18 + 18 - 18) / uint256(expectedPrice)); assertApproxEqRel(oracle.price(), 1e36, 0.01 ether); } function testOracleUsdcUsd() public { - Oracle2 oracle = new Oracle2(usdcUsd, AggregatorV3Interface(address(0)), 6, 0); + OracleTwoFeeds oracle = new OracleTwoFeeds(usdcUsd, AggregatorV3Interface(address(0)), 6, 0); assertApproxEqRel(oracle.price(), 1e36 / 1e6, 0.01 ether); } function testNegativeAnswer(int price) public { vm.assume(price < 0); FakeAggregator aggregator = new FakeAggregator(); - Oracle2 oracle = - new Oracle2(AggregatorV3Interface(address(aggregator)), AggregatorV3Interface(address(0)), 18, 0); + OracleTwoFeeds oracle = + new OracleTwoFeeds(AggregatorV3Interface(address(aggregator)), AggregatorV3Interface(address(0)), 18, 0); aggregator.setAnwser(price); vm.expectRevert(bytes(ErrorsLib.NEGATIVE_ANSWER)); oracle.price();