From d2d74c72e446c22d30a272e79fffa05e4f47311d Mon Sep 17 00:00:00 2001 From: Jourdan Dunkley Date: Tue, 21 May 2024 23:34:45 -0500 Subject: [PATCH] added more tests for new oracle fallback feature --- .../oracles/1337/MockRevertPriceOracle.sol | 29 +++++++++++ .../default/MasterPriceOracleTest.t.sol | 49 +++++++++++++++++-- 2 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 contracts/oracles/1337/MockRevertPriceOracle.sol diff --git a/contracts/oracles/1337/MockRevertPriceOracle.sol b/contracts/oracles/1337/MockRevertPriceOracle.sol new file mode 100644 index 000000000..8308d2ee5 --- /dev/null +++ b/contracts/oracles/1337/MockRevertPriceOracle.sol @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity >=0.8.0; + +import "../BasePriceOracle.sol"; + +/** + * @title MockRevertPriceOracle + * @notice Mocks a failing price oracle. Used for testing purposes only + * @author Jourdan Dunkley (https://github.com/jourdanDunkley) + */ +contract MockRevertPriceOracle is BasePriceOracle { + constructor() {} + + /** + * @dev Returns the price in ETH of `underlying` (implements `BasePriceOracle`). + */ + function price(address underlying) external view override returns (uint256) { + revert("MockPriceOracle: price function is failing."); + } + + /** + * @notice Returns the price in ETH of the token underlying `cToken`. + * @dev Implements the `PriceOracle` interface for Ionic pools (and Compound v2). + * @return Price in ETH of the token underlying `cToken`, scaled by `10 ** (36 - underlyingDecimals)`. + */ + function getUnderlyingPrice(ICErc20 cToken) external view override returns (uint256) { + revert("MockPriceOracle: getUnderlyingPrice function is failing."); + } +} diff --git a/contracts/test/oracles/default/MasterPriceOracleTest.t.sol b/contracts/test/oracles/default/MasterPriceOracleTest.t.sol index 8aa43d0ed..7314f99ec 100644 --- a/contracts/test/oracles/default/MasterPriceOracleTest.t.sol +++ b/contracts/test/oracles/default/MasterPriceOracleTest.t.sol @@ -7,11 +7,13 @@ import { BasePriceOracle } from "../../../oracles/BasePriceOracle.sol"; import { ICErc20 } from "../../../compound/CTokenInterfaces.sol"; import { SimplePriceOracle } from "../../../oracles/default/SimplePriceOracle.sol"; import { TransparentUpgradeableProxy } from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol"; +import { MockRevertPriceOracle } from "../../../oracles/1337/MockRevertPriceOracle.sol"; contract MasterPriceOracleTest is BaseTest { MasterPriceOracle mpo; SimplePriceOracle mainOracle; SimplePriceOracle fallbackOracle; + MockRevertPriceOracle revertingOracle; ICErc20 mockCToken; address someAdminAccount = address(94949); address ezETH = 0x2416092f143378750bb29b79eD961ab195CcEea5; @@ -64,6 +66,8 @@ contract MasterPriceOracleTest is BaseTest { mpo.add(tokens, oraclesToAdd); mpo.addFallbacks(tokens, fallbackOraclesToAdd); vm.stopPrank(); + + revertingOracle = new MockRevertPriceOracle(); } function testGetUnderlyingPrice() public fork(MODE_MAINNET) { @@ -94,22 +98,57 @@ contract MasterPriceOracleTest is BaseTest { } function testGetUnderlyingPriceOracleReverts() public fork(MODE_MAINNET) { - revert("TODO"); + address[] memory tokens = new address[](1); + tokens[0] = ezETH; + + BasePriceOracle[] memory oraclesToAdd = new BasePriceOracle[](1); + oraclesToAdd[0] = BasePriceOracle(revertingOracle); + + vm.prank(someAdminAccount); + mpo.add(tokens, oraclesToAdd); + + uint256 price = mpo.getUnderlyingPrice(ICErc20(ionezETH)); + assertEq(price, 2000, "Price should match the mock price"); } function testPrice() public fork(MODE_MAINNET) { - revert("TODO"); + vm.prank(someAdminAccount); + uint256 price = mpo.price(ezETH); + assertEq(price, 2000, "Price should match the mock price"); } function testPriceZero() public fork(MODE_MAINNET) { - revert("TODO"); + vm.prank(someAdminAccount); + mainOracle.setDirectPrice(ezETH, 0); + uint256 price = mpo.price(ezETH); + assertEq(price, 2000, "Price should match the mock price"); } function testPriceZeroAddressOracle() public fork(MODE_MAINNET) { - revert("TODO"); + address[] memory tokens = new address[](1); + tokens[0] = ezETH; + + BasePriceOracle[] memory oraclesToAdd = new BasePriceOracle[](1); + oraclesToAdd[0] = BasePriceOracle(0x0000000000000000000000000000000000000000); + + vm.prank(someAdminAccount); + mpo.add(tokens, oraclesToAdd); + + uint256 price = mpo.price(ezETH); + assertEq(price, 2000, "Price should match the mock price"); } function testPriceOracleReverts() public fork(MODE_MAINNET) { - revert("TODO"); + address[] memory tokens = new address[](1); + tokens[0] = ezETH; + + BasePriceOracle[] memory oraclesToAdd = new BasePriceOracle[](1); + oraclesToAdd[0] = BasePriceOracle(revertingOracle); + + vm.prank(someAdminAccount); + mpo.add(tokens, oraclesToAdd); + + uint256 price = mpo.price(ezETH); + assertEq(price, 2000, "Price should match the mock price"); } }