foundry-rs / foundry-rs/foundry

feat: add `--rpc-only` mode to fetch creation code and constructor args without a block explorer

Open
#11,104 1 comment 0 reactions 0 assignees View on GitHub
C-cast C-forge T-feature
Dominant language
Rust
Stars
10.6k
Forks
2.6k
Avg merge
18h 20m
Merged PRs (30d)
510

Description

### Component

Forge, Cast

### Describe the feature you would like

Currently, commands like `forge verify-bytecode`, `cast creation-code`, and `cast constructor-args` require an Etherscan API key to fetch a contract's creation transaction data. This dependency limits their utility in two key scenarios:
1. On chains without a block explorer (e.g., private networks, local testnets, or new L2s).
2. For users who prefer not to rely on a centralized service like Etherscan, even on supported chains.

I propose adding a new mode (e.g., via a flag like `--rpc-only` or by default when no Etherscan key is provided) for these commands to work using only an RPC URL.

The implementation could fetch the necessary data via RPC calls as follows:

1. **Find Deployment Block:** Identify the block number where the contract was deployed. This can be achieved efficiently by performing a binary search over the blockchain's history (from block 0 to `latest`).
* For each probed block `n`, use `eth_getCode(address, n)` to check if the contract's code exists. The first block where code is present is the deployment block.
* Alternatively, a binary search on `eth_getTransactionCount(address, n)` could find the block where the account's nonce changed from 0 to 1.

2. **Find Deployment Transaction:** Once the deployment block is found, iterate through its transactions to find the one that created the contract.

3. **Retrieve Creation Code:** With the deployment transaction hash, use the RPC to get the transaction trace.

This feature would significantly enhance Foundry's versatility, making it a more powerful tool for developers working across a wide range of EVM-compatible environments. Thank you for your consideration

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.