Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate fork tests from integration tests #122

Merged
merged 5 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions test/TestApproval.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ pragma solidity ^0.8.17;
import {Morpho} from "../src/Morpho.sol";
import {Errors} from "../src/libraries/Errors.sol";
import "./helpers/SigUtils.sol";
import "./setup/TestSetup.sol";

contract TestApproval is TestSetup, Morpho {
using TestConfig for TestConfig.Config;
import "./helpers/MorphoTest.sol";

contract TestApproval is Morpho, MorphoTest {
SigUtils internal sigUtils;
uint256 internal ownerPrivateKey;
uint256 internal managerPrivateKey;
address internal ownerAdd;
address internal managerAdd;

constructor() Morpho(config.load(vm.envString("NETWORK")).getAddress("addressesProvider")) {}

function setUp() public override {
super.setUp();

Expand Down
15 changes: 4 additions & 11 deletions test/TestMatchingEngine.sol
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {TestConfig} from "./helpers/TestConfig.sol";
import {TestSetup} from "./setup/TestSetup.sol";

import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";
import {Math} from "@morpho-utils/math/Math.sol";

import {MatchingEngine} from "../src/MatchingEngine.sol";
import {MorphoInternal} from "../src/MorphoInternal.sol";
import {MorphoStorage} from "../src/MorphoStorage.sol";

import {Types} from "../src/libraries/Types.sol";
import {MarketLib} from "../src/libraries/MarketLib.sol";
import {MarketBalanceLib} from "../src/libraries/MarketBalanceLib.sol";

contract TestMatchingEngine is TestSetup, MatchingEngine {
import "./helpers/InternalTest.sol";

contract TestMatchingEngine is InternalTest, MatchingEngine {
using MarketLib for Types.Market;
using MarketBalanceLib for Types.MarketBalances;
using WadRayMath for uint256;
using TestConfig for TestConfig.Config;
using Math for uint256;

uint256 internal constant TOTAL_AMOUNT = 20 ether;
uint256 internal constant USER_AMOUNT = 1 ether;

constructor() TestSetup() MorphoStorage(config.load(vm.envString("NETWORK")).getAddress("addressesProvider")) {}

function setUp() public virtual override {
super.setUp();

_market[dai].setIndexes(
Types.Indexes256(
Types.MarketSideIndexes256(WadRayMath.RAY, WadRayMath.RAY),
Expand Down
8 changes: 2 additions & 6 deletions test/TestMorphoGetters.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ pragma solidity ^0.8.17;
import {MorphoStorage} from "../src/MorphoStorage.sol";
import {MorphoGetters} from "../src/MorphoGetters.sol";

import "./setup/TestSetup.sol";

contract TestMorphoGetters is TestSetup, MorphoGetters {
using TestConfig for TestConfig.Config;

constructor() MorphoStorage(config.load(vm.envString("NETWORK")).getAddress("addressesProvider")) {}
import "./helpers/InternalTest.sol";

contract TestMorphoGetters is InternalTest, MorphoGetters {
function testIsManaging(address owner, address manager, bool isAllowed) public {
_approveManager(owner, manager, isAllowed);
assertEq(this.isManaging(owner, manager), isAllowed);
Expand Down
61 changes: 28 additions & 33 deletions test/TestMorphoInternal.sol
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import {TestHelpers} from "./helpers/TestHelpers.sol";
import {TestConfig} from "./helpers/TestConfig.sol";

import {TestSetup} from "./setup/TestSetup.sol";
import {console2} from "@forge-std/console2.sol";

import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";
import {PercentageMath} from "@morpho-utils/math/PercentageMath.sol";
import {ThreeHeapOrdering} from "@morpho-data-structures/ThreeHeapOrdering.sol";

import {SafeTransferLib, ERC20} from "@solmate/utils/SafeTransferLib.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

import {IPool, IPoolAddressesProvider} from "../src/interfaces/aave/IPool.sol";
import {IPriceOracleGetter} from "@aave/core-v3/contracts/interfaces/IPriceOracleGetter.sol";
import {DataTypes} from "../src/libraries/aave/DataTypes.sol";
import {ReserveConfiguration} from "../src/libraries/aave/ReserveConfiguration.sol";

import {MorphoInternal, MorphoStorage} from "../src/MorphoInternal.sol";
import {Types} from "../src/libraries/Types.sol";
import {MarketLib} from "../src/libraries/MarketLib.sol";
import {MarketBalanceLib} from "../src/libraries/MarketBalanceLib.sol";
import {PoolLib} from "../src/libraries/PoolLib.sol";

contract TestMorphoInternal is TestSetup, MorphoInternal {
import "./helpers/InternalTest.sol";

contract TestMorphoInternal is InternalTest, MorphoInternal {
using MarketLib for Types.Market;
using MarketBalanceLib for Types.MarketBalances;
using PoolLib for IPool;
using WadRayMath for uint256;
using PercentageMath for uint256;
using SafeTransferLib for ERC20;
using ThreeHeapOrdering for ThreeHeapOrdering.HeapArray;
using TestConfig for TestConfig.Config;
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;
using EnumerableSet for EnumerableSet.AddressSet;

IPriceOracleGetter internal oracle;

constructor() TestSetup() MorphoStorage(config.load(vm.envString("NETWORK")).getAddress("addressesProvider")) {}

function setUp() public virtual override {
super.setUp();

_defaultMaxLoops = Types.MaxLoops(10, 10, 10, 10);
_maxSortedUsers = 20;

Expand All @@ -51,7 +41,6 @@ contract TestMorphoInternal is TestSetup, MorphoInternal {
createTestMarket(usdt, 0, 3_333);
createTestMarket(wNative, 0, 3_333);

fillBalance(address(this), type(uint256).max);
ERC20(dai).approve(address(_POOL), type(uint256).max);
ERC20(wbtc).approve(address(_POOL), type(uint256).max);
ERC20(usdc).approve(address(_POOL), type(uint256).max);
Expand Down Expand Up @@ -449,23 +438,29 @@ contract TestMorphoInternal is TestSetup, MorphoInternal {
}

function testSetPauseStatus() public {
Types.PauseStatuses storage pauseStatuses = _market[dai].pauseStatuses;

assertFalse(pauseStatuses.isSupplyPaused);
assertFalse(pauseStatuses.isBorrowPaused);
assertFalse(pauseStatuses.isRepayPaused);
assertFalse(pauseStatuses.isWithdrawPaused);
assertFalse(pauseStatuses.isLiquidateCollateralPaused);
assertFalse(pauseStatuses.isLiquidateBorrowPaused);

_setPauseStatus(dai, true);

assertTrue(pauseStatuses.isSupplyPaused);
assertTrue(pauseStatuses.isBorrowPaused);
assertTrue(pauseStatuses.isRepayPaused);
assertTrue(pauseStatuses.isWithdrawPaused);
assertTrue(pauseStatuses.isLiquidateCollateralPaused);
assertTrue(pauseStatuses.isLiquidateBorrowPaused);
for (uint256 marketIndex; marketIndex < testMarkets.length; ++marketIndex) {
_revert();

address underlying = testMarkets[marketIndex];

Types.PauseStatuses storage pauseStatuses = _market[underlying].pauseStatuses;

assertFalse(pauseStatuses.isSupplyPaused);
assertFalse(pauseStatuses.isBorrowPaused);
assertFalse(pauseStatuses.isRepayPaused);
assertFalse(pauseStatuses.isWithdrawPaused);
assertFalse(pauseStatuses.isLiquidateCollateralPaused);
assertFalse(pauseStatuses.isLiquidateBorrowPaused);

_setPauseStatus(underlying, true);

assertTrue(pauseStatuses.isSupplyPaused);
assertTrue(pauseStatuses.isBorrowPaused);
assertTrue(pauseStatuses.isRepayPaused);
assertTrue(pauseStatuses.isWithdrawPaused);
assertTrue(pauseStatuses.isLiquidateCollateralPaused);
assertTrue(pauseStatuses.isLiquidateBorrowPaused);
}
}

function testApproveManager(address owner, address manager, bool isAllowed) public {
Expand Down
8 changes: 2 additions & 6 deletions test/TestPositionsManagerInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import {Errors} from "../src/libraries/Errors.sol";
import {MorphoStorage} from "../src/MorphoStorage.sol";
import {PositionsManagerInternal} from "../src/PositionsManagerInternal.sol";

import "./setup/TestSetup.sol";

contract TestPositionsManager is TestSetup, PositionsManagerInternal {
using TestConfig for TestConfig.Config;

constructor() MorphoStorage(config.load(vm.envString("NETWORK")).getAddress("addressesProvider")) {}
import "./helpers/InternalTest.sol";

contract TestPositionsManager is InternalTest, PositionsManagerInternal {
function testValidatePermission(address owner, address manager) public {
_validatePermission(owner, owner);

Expand Down
19 changes: 19 additions & 0 deletions test/TestSupply.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

import "./helpers/IntegrationTest.sol";

contract TestSupply is IntegrationTest {
function testShouldRevertWithZero() public {
for (uint256 marketIndex; marketIndex < markets.length; ++marketIndex) {
vm.expectRevert(Errors.AmountIsZero.selector);
user1.supply(markets[marketIndex].underlying, 0);
}
}

/// @dev The index calculation reverts if the market has no initialized indexes due to division by 0, so no revert reason should be given.
function testShouldRevertWithMarketNotCreated() public {
vm.expectRevert();
user1.supply(sAvax, 100);
}
}
120 changes: 120 additions & 0 deletions test/helpers/ForkTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.17;

import {IPool, IPoolAddressesProvider} from "../../src/interfaces/aave/IPool.sol";

import {Types} from "../../src/libraries/Types.sol";
import {Events} from "../../src/libraries/Events.sol";
import {Errors} from "../../src/libraries/Errors.sol";
import {DataTypes} from "../../src/libraries/aave/DataTypes.sol";
import {TestConfig, TestConfigLib} from "../helpers/TestConfigLib.sol";
import {SafeTransferLib, ERC20} from "@solmate/utils/SafeTransferLib.sol";

import {Math} from "@morpho-utils/math/Math.sol";
import {WadRayMath} from "@morpho-utils/math/WadRayMath.sol";
import {PercentageMath} from "@morpho-utils/math/PercentageMath.sol";

import {console2} from "@forge-std/console2.sol";
import {Test} from "@forge-std/Test.sol";

contract ForkTest is Test {
using TestConfigLib for TestConfig;

string internal network;
uint256 internal forkId;
TestConfig internal config;

address internal dai;
address internal frax;
address internal mai;
address internal usdc;
address internal usdt;
address internal aave;
address internal btcb;
address internal link;
address internal sAvax;
address internal wavax;
address internal wbtc;
address internal weth;
address internal wNative;
address[] internal testMarkets;

IPool internal pool;
IPoolAddressesProvider internal addressesProvider;

uint256 snapshotId = type(uint256).max;

constructor() {
_initConfig();
_loadConfig();

_setBalances(address(this), type(uint256).max);
}

function setUp() public virtual {}

function _network() internal view returns (string memory) {
try vm.envString("NETWORK") returns (string memory configNetwork) {
return configNetwork;
} catch {
return "avalanche-mainnet";
}
}

function _initConfig() internal returns (TestConfig storage) {
network = _network();

return config.load(network);
}

function _loadConfig() internal {
forkId = config.createFork();

addressesProvider = IPoolAddressesProvider(config.getAddress("addressesProvider"));
pool = IPool(addressesProvider.getPool());

dai = config.getAddress("DAI");
frax = config.getAddress("FRAX");
mai = config.getAddress("MAI");
usdc = config.getAddress("USDC");
usdt = config.getAddress("USDT");
aave = config.getAddress("AAVE");
btcb = config.getAddress("BTCb");
link = config.getAddress("LINK");
sAvax = config.getAddress("sAVAX");
wavax = config.getAddress("WAVAX");
wbtc = config.getAddress("WBTC");
weth = config.getAddress("WETH");
wNative = config.getAddress("wrappedNative");

testMarkets = config.getTestMarkets();
}

function _setBalances(address user, uint256 balance) internal {
deal(dai, user, balance);
deal(frax, user, balance);
deal(mai, user, balance);
deal(usdc, user, balance);
deal(usdt, user, balance);
deal(aave, user, balance);
deal(btcb, user, balance);
deal(link, user, balance);
deal(sAvax, user, balance);
deal(wavax, user, balance);
deal(wbtc, user, balance);
deal(weth, user, balance);
deal(wNative, user, balance);
}

/// @dev Rolls & warps `_blocks` blocks forward the blockchain.
function _forward(uint256 _blocks) internal {
vm.roll(block.number + _blocks);
vm.warp(block.timestamp + _blocks * 12);
}

/// @dev Reverts the fork to its initial fork state.
function _revert() internal {
if (snapshotId < type(uint256).max) vm.revertTo(snapshotId);
snapshotId = vm.snapshot();
}
}
Loading