Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak #3

Open
wants to merge 146 commits into
base: master
Choose a base branch
from
Open

Tweak #3

wants to merge 146 commits into from

Conversation

sambacha
Copy link

Foundry with on-chain contract Tweak!

Tweak allows users/developers to alter the code of an on-chain contract with the on-chain state untouched.
Almost any modification is allowed, as long as the storage layout is preserved.

With Tweak, you can:

  • Add console.log() in any on-chain contracts with source code available on Etherscan.
  • Change the logic of an on-chain contract (e.g., fixing bugs, conduct what-if analysis, etc.)

Tweaking on-chain contracts are especially useful when:

  • inspecting the execution on-chain transactions;
  • investigating the logic of a complex contract (e.g., UniswapV3) on chain;
  • debug the interaction with on-chain contracts which produce unexpected revert errors.

Here's the demo!
asciicast

Installation

This project is a fork of foundry.
The easiest way to install is to compile this project and replace the foundry toolchain originally on your machine.

cargo build

The executables are available in target/debug folder.
All the tools that you are familiar in foundry are still there.

What is enhanced is the cast and forge command.

Usage 1: Clone a verified contract code from Etherscan

forge clone <address> <path>

forge clone is a new feature that downloads the source code of a verified contract at <address> from Etherscan (or other block explorers supported by foundry) and save as a foundry project in the specified <path>.

The cloned project is fully compilable foundry project, guaranteed to generate exactly the same bytecode as the on-chain contract.

Usage 2: Tweak one on-chain contract and replay a historical transaction invoking it

cd path/to/cloned/contract/project
forge replay <transaction_hash>

If the current working directory is a foundry project created by forge clone, then the command forge replay is available to replay a historical transaction with the on-chain contract's code tweaked by the current project.
Note that the cloned project "knows" which contract address it should tweak.

You may want to modify the code of the cloned contract (e.g., add console.logs) before running forge replay.

Usage 3: Tweak multiple on-chain contracts and replay a historical transaction invoking them

cast run --tweak <path/to/cloned/project1> --tweak <path/to/cloned/project2> <transaction_hash>

cast run is also enhanced by allowing specified cloned projects to tweak the code of multiple on-chain contracts.
Note that each cloned project "knows" which contract address it should tweak.

Disclaimer

The Tweak feature is still under active development as part of the EtherDebug project, whose ultimate goal is improve the overall development and debugging experience of EVM-compatibla smart contracts.

The functionality is not stable yet and may suffer from breaking change in the future.
Any contribution (bug reports, PRs, documentation, etc.) is welcome and greatly appreciated.

Acknowledgement

Contributed by @Troublor and @ZhangZhuoSJTU with passion.

ZhangZhuoSJTU and others added 30 commits March 26, 2024 21:47
Test: fix rate limit issue for forge clone tests
Add `clone.toml` metadata to forge clone
Troublor and others added 30 commits April 1, 2024 21:49
[WIP] it is a complex sync and may induce bugs
Fixed `.clone.meta` file deserialization bug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants