Skip to content

Commit

Permalink
refactor: fork setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Grimal committed Sep 14, 2023
2 parents 36d8356 + a7db1a9 commit 6532a7b
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 67 deletions.
11 changes: 1 addition & 10 deletions config/Configured.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ abstract contract Configured is StdChains {
address internal USDC;
address internal USDT;
address internal LINK;
address internal ST_ETH;
address internal WBTC;
address internal WETH;
address internal CB_ETH;
address internal WNATIVE;
address[] internal lsdNatives;
address[] internal allAssets;

address internal AAVE_V2_POOL;
Expand Down Expand Up @@ -57,23 +54,17 @@ abstract contract Configured is StdChains {
USDC = CONFIG.getAddress("USDC");
USDT = CONFIG.getAddress("USDT");
LINK = CONFIG.getAddress("LINK");
ST_ETH = CONFIG.getAddress("stETH");
WBTC = CONFIG.getAddress("WBTC");
WETH = CONFIG.getAddress("WETH");
CB_ETH = CONFIG.getAddress("cbETH");
WNATIVE = CONFIG.getWrappedNative();

lsdNatives = CONFIG.getLsdNatives();
allAssets = [DAI, USDC, USDT, LINK, ST_ETH, WBTC, WETH];
allAssets = [DAI, USDC, USDT, LINK, WBTC, WETH];

ConfigMarket[] memory allConfigMarkets = CONFIG.getMarkets();
for (uint256 i; i < allConfigMarkets.length; ++i) {
configMarkets.push(allConfigMarkets[i]);
}

for (uint256 i; i < lsdNatives.length; ++i) {
allAssets.push(lsdNatives[i]);
}
AAVE_V3_POOL = CONFIG.getAddress("aaveV3Pool");
AAVE_V2_POOL = CONFIG.getAddress("aaveV2Pool");
AAVE_V3_OPTIMIZER = CONFIG.getAddress("aaveV3Optimizer");
Expand Down
33 changes: 33 additions & 0 deletions config/ConfiguredEthereumMainnet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

import "./Configured.sol";

abstract contract ConfiguredEthereumMainnet is Configured {
using ConfigLib for Config;

address internal ST_ETH;
address internal WST_ETH;
address internal CB_ETH;
address internal R_ETH;
address[] internal allEthereumMainnetAssets;

function _network() internal view virtual override returns (string memory){
return "ethereum-mainnet";
}

function _loadConfig() internal virtual override {
super._loadConfig();

ST_ETH = CONFIG.getAddress("stETH");
WST_ETH = CONFIG.getAddress("wstETH");
CB_ETH = CONFIG.getAddress("cbETH");
R_ETH = CONFIG.getAddress("rETH");

allEthereumMainnetAssets = [ST_ETH, WST_ETH, CB_ETH, R_ETH];

for (uint256 i; i < allEthereumMainnetAssets.length; ++i) {
allAssets.push(allEthereumMainnetAssets[i]);
}
}
}
7 changes: 1 addition & 6 deletions config/ethereum-mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@
"USDC": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
"USDT": "0xdAC17F958D2ee523a2206206994597C13D831ec7",
"LINK": "0x514910771AF9Ca656af840dff83E8264EcF986CA",
"stETH": "0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84",
"WBTC": "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
"WETH": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"stETH": "0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84",
"wstETH": "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0",
"cbETH": "0xBe9895146f7AF43049ca1c1AE358B0541Ea49704",
"rETH": "0xae78736Cd615f374D3085123A210448E74Fc6393",
"wrappedNative": "WETH",
"lsdNatives": [
"wstETH",
"cbETH",
"rETH"
],
"aaveV2Pool": "0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9",
"aaveV3Pool": "0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2",
"aaveV3Optimizer": "0x33333aea097c193e66081E930c33020272b33333",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import "contracts/ethereum-mainnet/EthereumBundler.sol";
import {MorphoBundler} from "contracts/MorphoBundler.sol";
import {Permit2Bundler} from "contracts/Permit2Bundler.sol";

import "../helpers/ForkTest.sol";
import "./EthereumTest.sol";

contract EthereumBundlerEthereumTest is ForkTest {
contract EthereumBundlerEthereumTest is EthereumTest {
using MathLib for uint256;
using MorphoLib for IMorpho;
using MorphoBalancesLib for IMorpho;
Expand All @@ -20,10 +20,6 @@ contract EthereumBundlerEthereumTest is ForkTest {

EthereumBundler private bundler;

function _network() internal pure override returns (string memory) {
return "ethereum-mainnet";
}

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

Expand Down
15 changes: 15 additions & 0 deletions test/forge/ethereum-mainnet/EthereumTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import "../helpers/ForkTest.sol";
import "config/ConfiguredEthereumMainnet.sol";

contract EthereumTest is ConfiguredEthereumMainnet, ForkTest {
function _network() internal view virtual override(Configured, ConfiguredEthereumMainnet) returns (string memory) {
return ConfiguredEthereumMainnet._network();
}

function _loadConfig() internal virtual override(Configured, ConfiguredEthereumMainnet) {
ConfiguredEthereumMainnet._loadConfig();
}
}
9 changes: 2 additions & 7 deletions test/forge/ethereum-mainnet/StEthBundlerEthereumTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@ import {IAllowanceTransfer} from "@permit2/interfaces/IAllowanceTransfer.sol";
import {IStEth} from "contracts/ethereum-mainnet/interfaces/IStEth.sol";
import {IWStEth} from "contracts/ethereum-mainnet/interfaces/IWStEth.sol";

import "../helpers/ForkTest.sol";
import "./EthereumTest.sol";

import "../mocks/StEthBundlerMock.sol";

contract StEthBundlerEthereumTest is ForkTest {
address public constant WST_ETH = 0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;
contract StEthBundlerEthereumTest is EthereumTest {
StEthBundlerMock private bundler;

function _network() internal pure override returns (string memory) {
return "ethereum-mainnet";
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ pragma solidity ^0.8.0;

import {ErrorsLib as BulkerErrorsLib} from "contracts/libraries/ErrorsLib.sol";

import "../helpers/ForkTest.sol";
import "./EthereumTest.sol";

import "../mocks/WNativeBundlerMock.sol";

contract WNativeBundlerEthereumTest is ForkTest {
contract WNativeBundlerEthereumTest is EthereumTest {
WNativeBundlerMock private bundler;

function _network() internal pure override returns (string memory) {
return "ethereum-mainnet";
}

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

Expand Down
8 changes: 2 additions & 6 deletions test/forge/ethereum-mainnet/migration/BaseMigrationTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {MarketParamsLib} from "@morpho-blue/libraries/MarketParamsLib.sol";
import {MorphoLib} from "@morpho-blue/libraries/periphery/MorphoLib.sol";
import {MorphoBalancesLib} from "@morpho-blue/libraries/periphery/MorphoBalancesLib.sol";

import "../../helpers/ForkTest.sol";
import "../EthereumTest.sol";
import {MorphoBundler} from "contracts/MorphoBundler.sol";
import {ERC4626Bundler} from "contracts/ERC4626Bundler.sol";
import {Permit2Bundler} from "contracts/Permit2Bundler.sol";
import {ERC4626Mock} from "../../mocks/ERC4626Mock.sol";

contract BaseMigrationTest is ForkTest {
contract BaseMigrationTest is EthereumTest {
using SafeTransferLib for ERC20;
using MarketParamsLib for MarketParams;
using MorphoLib for IMorpho;
Expand Down Expand Up @@ -49,10 +49,6 @@ contract BaseMigrationTest is ForkTest {
return (privateKey, user);
}

function _network() internal pure override returns (string memory) {
return "ethereum-mainnet";
}

function _morphoSetAuthorizationWithSigCall(
uint256 privateKey,
address authorized,
Expand Down
27 changes: 1 addition & 26 deletions test/forge/helpers/ForkTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract contract ForkTest is BaseTest, Configured {
_initConfig();
_loadConfig();

_setBalances(address(this), type(uint96).max);
_fork();
}

function setUp() public virtual override {
Expand Down Expand Up @@ -58,12 +58,6 @@ abstract contract ForkTest is BaseTest, Configured {
vm.chainId(CONFIG.getChainId());
}

function _loadConfig() internal virtual override {
super._loadConfig();

_fork();
}

function _label() internal virtual {
for (uint256 i; i < allAssets.length; ++i) {
address asset = allAssets[i];
Expand All @@ -73,15 +67,6 @@ abstract contract ForkTest is BaseTest, Configured {
}
}

function _setBalances(address user, uint256 balance) internal {
for (uint256 i; i < allAssets.length; ++i) {
address asset = allAssets[i];

if (asset == ST_ETH) return;
deal(asset, user, balance / (10 ** (18 - ERC20(asset).decimals())));
}
}

/// @dev Avoids to revert because of AAVE token snapshots:
/// https://github.com/aave/aave-token-v2/blob/master/contracts/token/base/GovernancePowerDelegationERC20.sol#L174
function _deal(address asset, address user, uint256 amount) internal {
Expand All @@ -104,20 +89,10 @@ abstract contract ForkTest is BaseTest, Configured {
}
}

function _assumeNotLsdNative(address input) internal view {
for (uint256 i; i < lsdNatives.length; ++i) {
vm.assume(input != lsdNatives[i]);
}
}

function _randomAsset(uint256 seed) internal view returns (address) {
return allAssets[seed % allAssets.length];
}

function _randomLsdNative(uint256 seed) internal view returns (address) {
return lsdNatives[seed % lsdNatives.length];
}

function _randomMarketParams(uint256 seed) internal view returns (MarketParams memory) {
return allMarketParams[seed % allMarketParams.length];
}
Expand Down

0 comments on commit 6532a7b

Please sign in to comment.