|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.13; |
| 3 | + |
| 4 | +import {console} from "./console.sol"; |
| 5 | +import {StdConfig} from "./StdConfig.sol"; |
| 6 | +import {CommonBase} from "./Base.sol"; |
| 7 | + |
| 8 | +/// @notice Boilerplate to streamline the setup of multi-chain environments. |
| 9 | +abstract contract Config is CommonBase { |
| 10 | + // -- STORAGE (CONFIG + CHAINS + FORKS) ------------------------------------ |
| 11 | + |
| 12 | + /// @dev Contract instance holding the data from the TOML config file. |
| 13 | + StdConfig internal config; |
| 14 | + |
| 15 | + /// @dev Array of chain IDs for which forks have been created. |
| 16 | + uint256[] internal chainIds; |
| 17 | + |
| 18 | + /// @dev A mapping from a chain ID to its initialized fork ID. |
| 19 | + mapping(uint256 => uint256) internal forkOf; |
| 20 | + |
| 21 | + // -- HELPER FUNCTIONS ----------------------------------------------------- |
| 22 | + |
| 23 | + /// @notice Loads configuration from a file. |
| 24 | + /// |
| 25 | + /// @dev This function instantiates a `Config` contract, caching all its config variables. |
| 26 | + /// |
| 27 | + /// @param filePath: the path to the TOML configuration file. |
| 28 | + /// @param writeToFile: whether updates are written back to the TOML file. |
| 29 | + function _loadConfig(string memory filePath, bool writeToFile) internal { |
| 30 | + console.log("----------"); |
| 31 | + console.log(string.concat("Loading config from '", filePath, "'")); |
| 32 | + config = new StdConfig(filePath, writeToFile); |
| 33 | + vm.makePersistent(address(config)); |
| 34 | + console.log("Config successfully loaded"); |
| 35 | + console.log("----------"); |
| 36 | + } |
| 37 | + |
| 38 | + /// @notice Loads configuration from a file and creates forks for each specified chain. |
| 39 | + /// |
| 40 | + /// @dev This function instantiates a `Config` contract, caching all its config variables, |
| 41 | + /// reads the configured chain ids, and iterates through them to create a fork for each one. |
| 42 | + /// It also creates a map `forkOf[chainId] -> forkId` to easily switch between forks. |
| 43 | + /// |
| 44 | + /// @param filePath: the path to the TOML configuration file. |
| 45 | + /// @param writeToFile: whether updates are written back to the TOML file. |
| 46 | + function _loadConfigAndForks(string memory filePath, bool writeToFile) internal { |
| 47 | + _loadConfig(filePath, writeToFile); |
| 48 | + |
| 49 | + console.log("Setting up forks for the configured chains..."); |
| 50 | + uint256[] memory chains = config.getChainIds(); |
| 51 | + for (uint256 i = 0; i < chains.length; i++) { |
| 52 | + uint256 chainId = chains[i]; |
| 53 | + uint256 forkId = vm.createFork(config.getRpcUrl(chainId)); |
| 54 | + forkOf[chainId] = forkId; |
| 55 | + chainIds.push(chainId); |
| 56 | + } |
| 57 | + console.log("Forks successfully created"); |
| 58 | + console.log("----------"); |
| 59 | + } |
| 60 | +} |
0 commit comments