-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from axieinfinity/feature/onchain-executor
feat: add OnchainExecutor contract to debug and broadcast raw tx
- Loading branch information
Showing
7 changed files
with
122 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))); | ||
} | ||
} | ||
} |