|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.19; |
| 3 | + |
| 4 | +import { StdStyle } from "../lib/forge-std/src/StdStyle.sol"; |
| 5 | +import { console2 as console } from "../lib/forge-std/src/console2.sol"; |
| 6 | +import { ScriptExtended } from "./extensions/ScriptExtended.s.sol"; |
| 7 | +import { BaseGeneralConfig } from "./BaseGeneralConfig.sol"; |
| 8 | +import { LibErrorHandler } from "../lib/contract-libs/src/LibErrorHandler.sol"; |
| 9 | + |
| 10 | +contract OnchainExecutor is ScriptExtended { |
| 11 | + using LibErrorHandler for bool; |
| 12 | + |
| 13 | + modifier rollFork(uint256 forkBlock) { |
| 14 | + if (forkBlock != 0) { |
| 15 | + vm.rollFork(forkBlock); |
| 16 | + console.log("OnchainExecutor: Rolling to fork block number:", forkBlock); |
| 17 | + } |
| 18 | + _; |
| 19 | + } |
| 20 | + |
| 21 | + function _configByteCode() internal virtual override returns (bytes memory) { |
| 22 | + return abi.encodePacked(type(BaseGeneralConfig).creationCode, abi.encode("", "deployments/")); |
| 23 | + } |
| 24 | + |
| 25 | + function trace(uint256 forkBlock, address from, address to, uint256 gas, uint256 value, bytes calldata callData) |
| 26 | + public |
| 27 | + rollFork(forkBlock) |
| 28 | + { |
| 29 | + vm.prank(from); |
| 30 | + _sendRawTransaction(to, gas, value, callData); |
| 31 | + } |
| 32 | + |
| 33 | + function broadcast(address from, address to, uint256 gas, uint256 value, bytes calldata callData) public { |
| 34 | + vm.broadcast(from); |
| 35 | + _sendRawTransaction(to, gas, value, callData); |
| 36 | + } |
| 37 | + |
| 38 | + function _sendRawTransaction(address to, uint256 gas, uint256 value, bytes calldata callData) internal { |
| 39 | + bool success; |
| 40 | + bytes memory returnOrRevertData; |
| 41 | + |
| 42 | + (success, returnOrRevertData) = |
| 43 | + gas == 0 ? to.call{ value: value }(callData) : to.call{ value: value, gas: gas }(callData); |
| 44 | + |
| 45 | + if (!success) { |
| 46 | + if (returnOrRevertData.length != 0) { |
| 47 | + string[] memory commandInput = new string[](3); |
| 48 | + commandInput[0] = "cast"; |
| 49 | + commandInput[1] = returnOrRevertData.length > 4 ? "4byte-decode" : "4byte"; |
| 50 | + commandInput[2] = vm.toString(returnOrRevertData); |
| 51 | + bytes memory decodedError = vm.ffi(commandInput); |
| 52 | + console.log(StdStyle.red(string.concat("Decoded Error: ", string(decodedError)))); |
| 53 | + } else { |
| 54 | + console.log(StdStyle.red("Evm Error!")); |
| 55 | + } |
| 56 | + } else { |
| 57 | + console.log(StdStyle.green("OnchainExecutor: Call Executed Successfully!")); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + function _logDecodedError(bytes memory returnOrRevertData) internal { |
| 62 | + if (returnOrRevertData.length != 0) { |
| 63 | + string[] memory commandInput = new string[](3); |
| 64 | + commandInput[0] = "cast"; |
| 65 | + commandInput[1] = returnOrRevertData.length > 4 ? "4byte-decode" : "4byte"; |
| 66 | + commandInput[2] = vm.toString(returnOrRevertData); |
| 67 | + bytes memory decodedError = vm.ffi(commandInput); |
| 68 | + console.log(StdStyle.red(string.concat("Decoded Error: ", string(decodedError)))); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments