Skip to content

Commit

Permalink
fix(test): fork at construction
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Jan 5, 2023
1 parent 0740885 commit 0b11e35
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 78 deletions.
45 changes: 25 additions & 20 deletions config/avalanche-mainnet.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
{
"usesRpcPrefix" : false,
"rpc" : "https://api.avax.network/ext/bc/C/rpc",
"chainId" : 43114,
"testBlock" : 24437213,
"testMarkets" : ["DAI", "USDC", "WBTC", "WETH"],
"LendingPoolAddressesProvider" : "0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb",
"DAI" : "0xd586E7F844cEa2F87f50152665BCbc2C279D8d70",
"FRAX" : "0xD24C2Ad096400B6FBcd2ad8B24E7acBc21A1da64",
"MAI" : "0x5c49b268c9841AFF1Cc3B0a418ff5c3442eE3F3b",
"USDC" : "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
"USDT" : "0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7",
"AAVE" : "0x63a72806098Bd3D9520cC43356dD78afe5D386D9",
"BTCb" : "0x152b9d0FdC40C096757F570A51E494bd4b943E50",
"LINK" : "0x5947BB275c521040051D82396192181b413227A3",
"sAVAX" : "0x2b2C81e08f1Af8835a78Bb2A90AE924ACE0eA4bE",
"WAVAX" : "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7",
"WBTC" : "0x50b7545627a5162F82A992c33b87aDc75187B218",
"WETH" : "0x49D5c2BdFfac6CE2BFdB6640F4F80f226bc10bAB",
"wrappedNative" : "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7"
}
"usesRpcPrefix": false,
"rpc": "https://api.avax.network/ext/bc/C/rpc",
"chainId": 43114,
"testBlock": 24437213,
"testMarkets": [
"DAI",
"USDC",
"WBTC",
"WETH"
],
"addressesProvider": "0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb",
"DAI": "0xd586E7F844cEa2F87f50152665BCbc2C279D8d70",
"FRAX": "0xD24C2Ad096400B6FBcd2ad8B24E7acBc21A1da64",
"MAI": "0x5c49b268c9841AFF1Cc3B0a418ff5c3442eE3F3b",
"USDC": "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
"USDT": "0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7",
"AAVE": "0x63a72806098Bd3D9520cC43356dD78afe5D386D9",
"BTCb": "0x152b9d0FdC40C096757F570A51E494bd4b943E50",
"LINK": "0x5947BB275c521040051D82396192181b413227A3",
"sAVAX": "0x2b2C81e08f1Af8835a78Bb2A90AE924ACE0eA4bE",
"WAVAX": "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7",
"WBTC": "0x50b7545627a5162F82A992c33b87aDc75187B218",
"WETH": "0x49D5c2BdFfac6CE2BFdB6640F4F80f226bc10bAB",
"wrappedNative": "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7"
}
40 changes: 40 additions & 0 deletions test/TestMorphoInternal.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.17;

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

import "./setup/TestSetup.sol";

contract TestMorphoInternal is TestSetup, MorphoInternal {
using TestConfig for TestConfig.Config;

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

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

_setPauseStatus(dai, true);

assertEq(pauseStatuses.isSupplyPaused, true);
}

/// TESTS TO ADD:

// _computeIndexes
// _updateIndexes
// _getUserSupplyBalanceFromIndexes
// _getUserBorrowBalanceFromIndexes
// _getUserSupplyBalance
// _getUserBorrowBalance

// _assetLiquidityData
// _liquidityDataCollateral
// _liquidityDataDebt
// _liquidityData

// _updateInDS
// _updateSupplierInDS
// _updateBorrowerInDS
}
50 changes: 50 additions & 0 deletions test/helpers/TestConfig.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

import "@forge-std/Vm.sol";
import "@forge-std/StdJson.sol";

library TestConfig {
using stdJson for string;

struct Config {
string json;
}

Vm private constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));
string private constant CONFIG_PATH = "/config/";

function load(Config storage config, string memory network) internal returns (Config storage) {
string memory root = vm.projectRoot();
string memory path = string(abi.encodePacked(root, CONFIG_PATH, network, ".json"));

config.json = vm.readFile(path);

return config;
}

function getAddress(Config storage config, string memory key) internal view returns (address) {
return config.json.readAddress(string(abi.encodePacked(key)));
}

function getTestMarkets(Config storage config) internal view returns (address[] memory) {
string[] memory marketNames = config.json.readStringArray(string(abi.encodePacked("testMarkets")));
address[] memory markets = new address[](marketNames.length);

for (uint256 i; i < markets.length; i++) {
markets[i] = getAddress(config, marketNames[i]);
}

return markets;
}

function createFork(Config storage config) internal returns (uint256 forkId) {
bool rpcPrefixed = stdJson.readBool(config.json, string(abi.encodePacked("usesRpcPrefix")));
string memory endpoint = rpcPrefixed
? string(abi.encodePacked(config.json.readString(string(abi.encodePacked("rpc"))), vm.envString("ALCHEMY_KEY")))
: config.json.readString(string(abi.encodePacked("rpc")));

forkId = vm.createSelectFork(endpoint, config.json.readUint(string(abi.encodePacked("testBlock"))));
vm.chainId(config.json.readUint(string(abi.encodePacked("chainId"))));
}
}
37 changes: 1 addition & 36 deletions test/helpers/TestHelpers.sol
Original file line number Diff line number Diff line change
@@ -1,50 +1,15 @@
// SPDX-License-Identifier: GNU AGPLv3
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

import "@forge-std/Vm.sol";
import "@forge-std/StdJson.sol";
import "@forge-std/console2.sol";

library TestHelpers {
using stdJson for string;

Vm private constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));
string private constant CONFIG_PATH = "/config/";

function getJsonConfig(string memory network) internal view returns (string memory json) {
string memory root = vm.projectRoot();
string memory path = string(abi.encodePacked(root, CONFIG_PATH, network, ".json"));
json = vm.readFile(path);
}

function getAddressFromJson(string memory json, string memory key) internal pure returns (address) {
return json.readAddress(string(abi.encodePacked(key)));
}

function getTestMarkets(string memory json) internal pure returns (address[] memory) {
string[] memory marketNames = json.readStringArray(string(abi.encodePacked("testMarkets")));
address[] memory markets = new address[](marketNames.length);

for (uint256 i; i < markets.length; i++) {
markets[i] = getAddressFromJson(json, marketNames[i]);
}
return markets;
}

function setForkFromEnv() internal returns (uint256 forkId) {
string memory endpoint = vm.envString("FOUNDRY_ETH_RPC_URL");
uint256 blockNumber = vm.envUint("FOUNDRY_FORK_BLOCK_NUMBER");

forkId = vm.createSelectFork(endpoint, blockNumber);
}

function setForkFromJson(string memory json) internal returns (uint256 forkId) {
bool rpcPrefixed = stdJson.readBool(json, string(abi.encodePacked("usesRpcPrefix")));
string memory endpoint = rpcPrefixed
? string(abi.encodePacked(json.readString(string(abi.encodePacked("rpc"))), vm.envString("ALCHEMY_KEY")))
: json.readString(string(abi.encodePacked("rpc")));

forkId = vm.createSelectFork(endpoint, json.readUint(string(abi.encodePacked("testBlock"))));
vm.chainId(json.readUint(string(abi.encodePacked("chainId"))));
}
}
1 change: 0 additions & 1 deletion test/libraries/TestPoolLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ contract TestPoolLib is TestSetup {
uint256 internal constant MAX_AMOUNT = 100 ether;

function setUp() public virtual override {
configSetUp();
fillBalance(address(this), type(uint256).max);
ERC20(dai).approve(address(pool), type(uint256).max);
DataTypes.ReserveData memory reserveData = pool.getReserveData(dai);
Expand Down
49 changes: 28 additions & 21 deletions test/setup/TestSetup.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GNU AGPLv3
pragma solidity 0.8.17;

import {TestConfig} from "../helpers/TestConfig.sol";
import {TestHelpers} from "../helpers/TestHelpers.sol";
import {User} from "../helpers/User.sol";
import {console2} from "@forge-std/console2.sol";
Expand All @@ -17,8 +18,14 @@ import {Types} from "../../src/libraries/Types.sol";
import {Test} from "@forge-std/Test.sol";

contract TestSetup is Test {
using TestConfig for TestConfig.Config;

uint256 internal constant INITIAL_BALANCE = 1_000_000 ether;

string internal network = vm.envString("NETWORK");
uint256 internal forkId;
TestConfig.Config internal config;

// Common test variables between all networks
IPoolAddressesProvider internal addressesProvider;
IPool internal pool;
Expand All @@ -37,8 +44,6 @@ contract TestSetup is Test {
// The full list of markets to be tested when fuzzing or looping through all markets
address[] internal markets;

uint256 internal forkId;

User[] internal users;

User internal supplier1;
Expand All @@ -49,8 +54,28 @@ contract TestSetup is Test {
User internal borrower2;
User internal borrower3;

constructor() {
_loadConfig();
}

function _loadConfig() internal {
config.load(network);

forkId = config.createFork();

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

dai = config.getAddress("DAI");
usdc = config.getAddress("USDC");
usdt = config.getAddress("USDT");
wbtc = config.getAddress("WBTC");
wNative = config.getAddress("wrappedNative");

markets = config.getTestMarkets();
}

function setUp() public virtual {
configSetUp();
deployAndSet();
initUsers(10, INITIAL_BALANCE);
fillBalance(address(this), type(uint256).max);
Expand All @@ -63,24 +88,6 @@ contract TestSetup is Test {
borrower3 = users[5];
}

function configSetUp() internal {
string memory network = vm.envString("NETWORK");
string memory config = TestHelpers.getJsonConfig(network);

forkId = TestHelpers.setForkFromJson(config);

addressesProvider =
IPoolAddressesProvider(TestHelpers.getAddressFromJson(config, "LendingPoolAddressesProvider"));
pool = IPool(addressesProvider.getPool());
dai = TestHelpers.getAddressFromJson(config, "DAI");
usdc = TestHelpers.getAddressFromJson(config, "USDC");
usdt = TestHelpers.getAddressFromJson(config, "USDT");
wbtc = TestHelpers.getAddressFromJson(config, "WBTC");
wNative = TestHelpers.getAddressFromJson(config, "wrappedNative");

markets = TestHelpers.getTestMarkets(config);
}

function deployAndSet() internal {
positionsManager = new PositionsManager(address(addressesProvider));
morphoImplementation = new Morpho(address(addressesProvider));
Expand Down

0 comments on commit 0b11e35

Please sign in to comment.