diff --git a/script/BaseMigration.s.sol b/script/BaseMigration.s.sol index 888d369..7821568 100644 --- a/script/BaseMigration.s.sol +++ b/script/BaseMigration.s.sol @@ -9,7 +9,7 @@ import { import { LibString } from "../lib/solady/src/utils/LibString.sol"; import { console, LibSharedAddress, StdStyle, IScriptExtended, ScriptExtended } from "./extensions/ScriptExtended.s.sol"; import { IArtifactFactory, ArtifactFactory } from "./ArtifactFactory.sol"; -import { OnchainDebugger } from "./OnchainDebugger.s.sol"; // cheat to load artifact to parent `out` directory +import { OnchainDebugger } from "./OnchainDebugger.s.sol"; // cheat to load artifact to parent `out` directory import { IMigrationScript } from "./interfaces/IMigrationScript.sol"; import { LibProxy } from "./libraries/LibProxy.sol"; import { DefaultContract } from "./utils/DefaultContract.sol"; diff --git a/script/OnchainDebugger.s.sol b/script/OnchainDebugger.s.sol index 86614c3..a63ac0c 100644 --- a/script/OnchainDebugger.s.sol +++ b/script/OnchainDebugger.s.sol @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.19; +import { StdStyle } from "forge-std/StdStyle.sol"; import { console2 as console } from "forge-std/console2.sol"; import { ScriptExtended } from "./extensions/ScriptExtended.s.sol"; import { BaseGeneralConfig } from "./BaseGeneralConfig.sol"; @@ -27,6 +28,14 @@ contract OnchainDebugger is ScriptExtended { { vm.prank(from); (bool success, bytes memory returnOrRevertData) = to.call{ value: value }(callData); + if (!success) { + string[] memory commandInput = new string[](3); + commandInput[0] = "cast"; + commandInput[1] = "4byte-decode"; + commandInput[2] = vm.toString(returnOrRevertData); + bytes memory decodedError = vm.ffi(commandInput); + console.log(StdStyle.red(string(decodedError))); + } success.handleRevert(msg.sig, returnOrRevertData); } } diff --git a/script/configs/NetworkConfig.sol b/script/configs/NetworkConfig.sol index 53466da..5edd86a 100644 --- a/script/configs/NetworkConfig.sol +++ b/script/configs/NetworkConfig.sol @@ -82,14 +82,13 @@ abstract contract NetworkConfig is INetworkConfig { console.log(StdStyle.yellow("NetworkConfig: fork mode disabled, no active fork")); return NULL_FORK_ID; } - if (chainId == block.chainid) { - console.log( - StdStyle.yellow(string.concat("NetworkConfig: ", chainAlias, " is already created and active at forkId:")), - currentFork - ); - return currentFork; - } + + if (chainId == block.chainid) return currentFork; if (!_isForkModeEnabled) return NULL_FORK_ID; + if (_networkDataMap[_networkMap[chainId]].forkId != NULL_FORK_ID) { + return _networkDataMap[_networkMap[chainId]].forkId; + } + try vm.createFork(vm.rpcUrl(chainAlias)) returns (uint256 forkId) { console.log(StdStyle.blue(string.concat("NetworkConfig: ", chainAlias, " fork created with forkId:")), forkId); return forkId; diff --git a/script/extensions/ScriptExtended.s.sol b/script/extensions/ScriptExtended.s.sol index 052e091..4b9029e 100644 --- a/script/extensions/ScriptExtended.s.sol +++ b/script/extensions/ScriptExtended.s.sol @@ -17,13 +17,19 @@ abstract contract ScriptExtended is Script, StdAssertions, IScriptExtended { IGeneralConfig public constant CONFIG = IGeneralConfig(LibSharedAddress.CONFIG); modifier logFn(string memory fnName) { - console.log("> ", StdStyle.blue(fnName), "..."); + _logFn(fnName); _; } modifier onlyOn(TNetwork networkType) { - require(network() == networkType, string.concat("ScriptExtended: Only allowed on ", CONFIG.getAlias(networkType))); + _requireOn(networkType); + _; + } + + modifier onNetwork(TNetwork networkType) { + TNetwork currentNetwork = _before(networkType); _; + _after(currentNetwork); } function setUp() public virtual { @@ -86,4 +92,22 @@ abstract contract ScriptExtended is Script, StdAssertions, IScriptExtended { assertTrue(success, "ScriptExtended: Failed to create runtime bytecode."); vm.etch(where, runtimeBytecode); } + + function _logFn(string memory fnName) private view { + console.log("> ", StdStyle.blue(fnName), "..."); + } + + function _requireOn(TNetwork networkType) private view { + require(network() == networkType, string.concat("ScriptExtended: Only allowed on ", CONFIG.getAlias(networkType))); + } + + function _before(TNetwork networkType) private returns (TNetwork currentNetwork) { + currentNetwork = network(); + CONFIG.createFork(networkType); + CONFIG.switchTo(networkType); + } + + function _after(TNetwork currentNetwork) private { + CONFIG.switchTo(currentNetwork); + } }