Skip to content

Commit

Permalink
Merge pull request #33 from axieinfinity/feature/onchain-executor
Browse files Browse the repository at this point in the history
feat: add OnchainExecutor contract to debug and broadcast raw tx
  • Loading branch information
TuDo1403 authored Dec 28, 2023
2 parents d524af3 + 27143b5 commit 52ad574
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 55 deletions.
12 changes: 12 additions & 0 deletions .broadcast.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Fork block number to debug
BLOCK=0x0
# Caller
FROM=0x0000000000000000000000000000000000000000
# Callee
TO=0x0000000000000000000000000000000000000000
# Sent Value
VALUE=0
# Gas Amount
GAS=0
# Call Data
CALLDATA=0x0
4 changes: 3 additions & 1 deletion .debug.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ FROM=0x0000000000000000000000000000000000000000
# Callee
TO=0x0000000000000000000000000000000000000000
# Sent Value
VALUE=0x27cdb0997a65b2de99
VALUE=0
# Gas Amount
GAS=0
# Call Data
CALLDATA=0x0
31 changes: 31 additions & 0 deletions broadcast.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Source (or "dot") the .env file to load environment variables
if [ -f .env ]; then
source .broadcast.env
else
echo "Error: .broadcast.env file not found."
fi

verify_arg=""
extra_argument=""

for arg in "$@"; do
case $arg in
--trezor)
extra_argument+=trezor@
;;
*) ;;
esac
done

# Remove the @ character from the end of extra_argument
extra_argument="${extra_argument%%@}"

echo Broadcast Tx...
echo From: ${FROM}
echo To: ${TO}
echo Value: ${VALUE}
echo GasAmount: ${GAS}
echo Calldata:
cast pretty-calldata ${CALLDATA}
calldata=$(cast calldata 'broadcast(address,address,uint256,uint256,bytes)' ${FROM} ${TO} ${GAS} ${VALUE} ${CALLDATA})
forge script ${verify_arg} ${@} -g 200 OnchainExecutor --sig 'run(bytes,string)' ${calldata} "${extra_argument}"
14 changes: 3 additions & 11 deletions debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@ fi

verify_arg=""
extra_argument=""
op_command="op run --env-file="./.env" --"

for arg in "$@"; do
case $arg in
--trezor)
op_command=""
extra_argument+=trezor@
;;
--broadcast)
op_command="op run --env-file="./.env" --"
;;
--log)
set -- "${@/#--log/}"
extra_argument+=log@
;;
*) ;;
esac
done
Expand All @@ -33,7 +24,8 @@ echo Debug Tx...
echo From: ${FROM}
echo To: ${TO}
echo Value: ${VALUE}
echo GasAmount: ${GAS}
echo Calldata:
cast pretty-calldata ${CALLDATA}
calldata=$(cast calldata 'trace(uint256,address,address,uint256,bytes)' ${BLOCK} ${FROM} ${TO} ${VALUE} ${CALLDATA})
${op_command} forge script ${verify_arg} --legacy ${@} OnchainDebugger --sig 'run(bytes,string)' ${calldata} "${extra_argument}"
calldata=$(cast calldata 'trace(uint256,address,address,uint256,uint256,bytes)' ${BLOCK} ${FROM} ${TO} ${GAS} ${VALUE} ${CALLDATA})
forge script ${verify_arg} --legacy ${@} OnchainExecutor --sig 'run(bytes,string)' ${calldata} "${extra_argument}"
4 changes: 2 additions & 2 deletions script/BaseMigration.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 { OnchainExecutor } from "./OnchainExecutor.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";
Expand Down Expand Up @@ -272,4 +272,4 @@ abstract contract BaseMigration is ScriptExtended {
function _setDependencyDeployScript(TContract contractType, address deployScript) internal virtual {
_deployScript[contractType] = IMigrationScript(deployScript);
}
}
}
41 changes: 0 additions & 41 deletions script/OnchainDebugger.s.sol

This file was deleted.

71 changes: 71 additions & 0 deletions script/OnchainExecutor.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import { StdStyle } from "../lib/forge-std/src/StdStyle.sol";
import { console2 as console } from "../lib/forge-std/src/console2.sol";
import { ScriptExtended } from "./extensions/ScriptExtended.s.sol";
import { BaseGeneralConfig } from "./BaseGeneralConfig.sol";
import { LibErrorHandler } from "../lib/contract-libs/src/LibErrorHandler.sol";

contract OnchainExecutor is ScriptExtended {
using LibErrorHandler for bool;

modifier rollFork(uint256 forkBlock) {
if (forkBlock != 0) {
vm.rollFork(forkBlock);
console.log("OnchainExecutor: Rolling to fork block number:", forkBlock);
}
_;
}

function _configByteCode() internal virtual override returns (bytes memory) {
return abi.encodePacked(type(BaseGeneralConfig).creationCode, abi.encode("", "deployments/"));
}

function trace(uint256 forkBlock, address from, address to, uint256 gas, uint256 value, bytes calldata callData)
public
rollFork(forkBlock)
{
vm.prank(from);
_sendRawTransaction(to, gas, value, callData);
}

function broadcast(address from, address to, uint256 gas, uint256 value, bytes calldata callData) public {
vm.broadcast(from);
_sendRawTransaction(to, gas, value, callData);
}

function _sendRawTransaction(address to, uint256 gas, uint256 value, bytes calldata callData) internal {
bool success;
bytes memory returnOrRevertData;

(success, returnOrRevertData) =
gas == 0 ? to.call{ value: value }(callData) : to.call{ value: value, gas: gas }(callData);

if (!success) {
if (returnOrRevertData.length != 0) {
string[] memory commandInput = new string[](3);
commandInput[0] = "cast";
commandInput[1] = returnOrRevertData.length > 4 ? "4byte-decode" : "4byte";
commandInput[2] = vm.toString(returnOrRevertData);
bytes memory decodedError = vm.ffi(commandInput);
console.log(StdStyle.red(string.concat("Decoded Error: ", string(decodedError))));
} else {
console.log(StdStyle.red("Evm Error!"));
}
} else {
console.log(StdStyle.green("OnchainExecutor: Call Executed Successfully!"));
}
}

function _logDecodedError(bytes memory returnOrRevertData) internal {
if (returnOrRevertData.length != 0) {
string[] memory commandInput = new string[](3);
commandInput[0] = "cast";
commandInput[1] = returnOrRevertData.length > 4 ? "4byte-decode" : "4byte";
commandInput[2] = vm.toString(returnOrRevertData);
bytes memory decodedError = vm.ffi(commandInput);
console.log(StdStyle.red(string.concat("Decoded Error: ", string(decodedError))));
}
}
}

0 comments on commit 52ad574

Please sign in to comment.