Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
added more tests for new oracle fallback feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdanDunkley committed May 22, 2024
1 parent 2e02584 commit d2d74c7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
29 changes: 29 additions & 0 deletions contracts/oracles/1337/MockRevertPriceOracle.sol
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> (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.");
}
}
49 changes: 44 additions & 5 deletions contracts/test/oracles/default/MasterPriceOracleTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
}
}

0 comments on commit d2d74c7

Please sign in to comment.