Skip to content

feat(cast): Cast generic Network support (foundry-rs#13624)#396

Open
Dargon789 wants to merge 205 commits intoDargon789:masterfrom
foundry-rs:master
Open

feat(cast): Cast generic Network support (foundry-rs#13624)#396
Dargon789 wants to merge 205 commits intoDargon789:masterfrom
foundry-rs:master

Conversation

@Dargon789
Copy link
Copy Markdown
Owner

Motivation

Solution

PR Checklist

mablr and others added 15 commits March 13, 2026 09:49
* Update standard_json.rs

* Update provider.rs

* Update sourcify.rs
…send` (#13747)

Extract `CastTxSender::print_tx_result()` to share the "print hash or
wait for receipt" logic between the normal send flow and the browser
wallet flow. Flatten the browser case into its own `else if` branch
so `--browser` takes priority over `--unlocked` instead of being
silently ignored.
#13736)

refactor(anvil): move network-agnostic methods to impl<N> Backend<N>

Co-authored-by: zerosnacks <95942363+zerosnacks@users.noreply.github.com>
#13752)

* refactor(evm): remove unused `snap_tx_env` from `BackendStateSnapshot`

The `snap_tx_env` field was stored on every state snapshot but never
read back on revert — it was destructured as `_`. Remove it along with
the `tx_env` parameter from `DatabaseExt::snapshot_state`, avoiding an
unnecessary `TxEnv` clone on every snapshot.

* style: fmt snapshot_state signatures

* fix: use backend_mut in CowBackend::snapshot_state to preserve initialization

Amp-Thread-ID: https://ampcode.com/threads/T-019ce8e6-b2c7-71dd-8f3e-df34110d8a20
Co-authored-by: Amp <amp@ampcode.com>

---------

Co-authored-by: Amp <amp@ampcode.com>
@codesandbox
Copy link
Copy Markdown

codesandbox bot commented Mar 14, 2026

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @Dargon789, your pull request is larger than the review limit of 150000 diff characters

@snyk-io
Copy link
Copy Markdown

snyk-io bot commented Mar 14, 2026

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@Dargon789
Copy link
Copy Markdown
Owner Author

@Mergifyio refresh

@Dargon789 Dargon789 enabled auto-merge (squash) March 14, 2026 13:26
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the foundry ecosystem by introducing generic network support to the cast CLI tool and refactoring core EVM and transaction handling components. The changes aim to improve the flexibility and extensibility of foundry by making its tools and internal logic more adaptable to different blockchain networks, particularly TempoNetwork. This involves a broad update of type parameters, trait implementations, and dependency management to ensure a robust and future-proof architecture.

Highlights

  • Generic Network Support for Cast: The cast CLI tool has been refactored to support generic Network types, moving away from hardcoded AnyNetwork in many commands. This allows for greater flexibility and future integration with different network types, such as TempoNetwork.
  • EVM Environment Simplification: EVM-related components across anvil, cheatcodes, chisel, and evm/core have been updated to streamline environment handling. This includes removing TxEnv from snapshot_state parameters and directly passing EvmEnv for fork creation and management, simplifying the internal EVM state logic.
  • Signer Trait Refactoring: The Signer trait in crates/anvil/src/eth/sign.rs has been split into MessageSigner and Signer<N: Network>. This change clearly separates the concerns of signing generic messages from network-specific transaction signing, improving modularity and type safety.
  • Dependency Updates: Key dependencies have been adjusted, with alloy-consensus, foundry-primitives, and tempo-alloy being added, and alloy-rpc-types and alloy-serde being removed from direct dependencies in crates/cast/Cargo.toml (with alloy-serde moving to dev-dependencies). This reflects the shift towards more granular and network-aware alloy components.
  • RLP Encoding Enhancements: A new TryIntoRlpEncodable trait has been introduced in crates/cast/src/rlp_converter.rs to provide a generic mechanism for RLP encoding various header types, including AnyHeader and TempoHeader.
Changelog
  • Cargo.lock
    • Updated dependencies, adding alloy-consensus, foundry-primitives, and tempo-alloy while removing alloy-rpc-types and alloy-serde.
  • crates/anvil/src/eth/api.rs
    • Updated storage_info method to return StorageInfo<FoundryNetwork>.
  • crates/anvil/src/eth/backend/info.rs
    • Modified StorageInfo struct to be generic over Network and moved receipt-related methods to a FoundryNetwork specific implementation.
  • crates/anvil/src/eth/fees.rs
    • Updated FeeHistoryService to use StorageInfo<FoundryNetwork>.
  • crates/anvil/src/eth/sign.rs
    • Refactored Signer trait into MessageSigner and Signer<N: Network> to separate message and transaction signing.
  • crates/anvil/src/evm.rs
    • Removed EvmEnv from create_eth_evm and create_op_evm functions, and updated EVM context initialization to use BlockEnv::default() and CfgEnv::new_with_spec directly.
  • crates/anvil/src/filter.rs
    • Updated LogsFilter to use StorageInfo<FoundryNetwork>.
  • crates/anvil/src/pubsub.rs
    • Updated LogsSubscription and EthSubscription to use StorageInfo<FoundryNetwork>.
  • crates/cast/Cargo.toml
    • Removed alloy-serde from main dependencies and added alloy-serde to dev-dependencies.
    • Added alloy-consensus, foundry-primitives, and tempo-alloy as dependencies.
  • crates/cast/src/args.rs
    • Introduced a transaction_raw helper function for FoundryNetwork specific raw transaction encoding.
    • Modified CastSubcommand::Tx to conditionally use transaction_raw or the generic Cast::transaction.
  • crates/cast/src/cmd/access_list.rs
    • Updated AccessListArgs to support generic Network types via run_with_network and use ProviderBuilder<N>.
  • crates/cast/src/cmd/call.rs
    • Updated CallArgs to support generic Network types via run_with_network and use ProviderBuilder<N>.
    • Modified transaction parameter accessors (e.g., tx.value, tx.inner.input) to use new network-agnostic methods.
  • crates/cast/src/cmd/estimate.rs
    • Updated EstimateArgs to support generic Network types via run_with_network and use ProviderBuilder<N>.
  • crates/cast/src/cmd/send.rs
    • Refactored transaction sending logic to use a new print_tx_result method for consistent output.
    • Simplified browser wallet handling.
  • crates/cast/src/lib.rs
    • Made Cast struct generic over Network.
    • Removed raw parameter from Cast::transaction and introduced transaction_raw helper.
    • Updated block header RLP encoding to use TryIntoRlpEncodable.
    • Modified Cast::call and Cast::access_list to accept generic N::TransactionRequest.
  • crates/cast/src/rlp_converter.rs
    • Added TryIntoRlpEncodable trait for generic RLP encoding of headers, with implementations for alloy_consensus::Header, TempoHeader, and AnyHeader.
  • crates/cast/src/tx.rs
    • Added print_tx_result method to CastTxSender for unified transaction result printing.
  • crates/cheatcodes/src/evm.rs
    • Updated inner_snapshot_state to use EvmEnv directly for snapshot creation, removing TxEnv.
  • crates/cheatcodes/src/evm/fork.rs
    • Modified create_fork_request to pass EvmEnv directly instead of a combined Env struct.
  • crates/chisel/src/executor.rs
    • Updated SessionSource to pass env.evm_env.clone() to get_fork.
  • crates/common/fmt/src/lib.rs
    • Added UIfmtHeaderExt and UIfmtSignatureExt traits for UI formatting.
  • crates/evm/core/src/backend/cow.rs
    • Updated snapshot_state to remove TxEnv parameter and use TxEnv::default() internally.
  • crates/evm/core/src/backend/mod.rs
    • Modified DatabaseExt::snapshot_state trait method to remove TxEnv parameter.
    • Updated Backend::snapshot_state and Backend::revert_state to reflect the change in snapshot_state signature.
  • crates/evm/core/src/backend/snapshot.rs
    • Removed snap_tx_env field from BackendStateSnapshot struct.
  • crates/evm/core/src/fork/mod.rs
    • Updated CreateFork struct to use EvmEnv directly instead of a combined Env struct.
  • crates/evm/core/src/fork/multi.rs
    • Modified MultiFork methods (create_fork, get_evm_env) and Request enum to use EvmEnv instead of Env.
  • crates/evm/core/src/opts.rs
    • Updated EvmOpts::get_fork to accept EvmEnv directly instead of a combined Env struct.
  • crates/evm/evm/src/executors/trace.rs
    • Updated TracingExecutor to pass env.evm_env.clone() to get_fork.
  • crates/forge/Cargo.toml
    • Added alloy-consensus, foundry-primitives, and tempo-alloy as dependencies.
    • Removed alloy-rpc-types and alloy-serde.
  • crates/forge/src/cmd/create.rs
    • Refactored CreateArgs::run to be generic over Network via run_generic.
    • Updated deploy function to be generic over Network and use ProviderBuilder<N>.
    • Modified transaction parameter application logic to use self.tx.apply::<N> and fetch defaults from the provider.
  • crates/forge/src/cmd/test/mod.rs
    • Updated TestArgs to pass env.evm_env.clone() to get_fork.
  • crates/primitives/src/transaction/envelope.rs
    • Removed SignableTransaction and RlpEcdsaEncodableTx implementations for FoundryTypedTx.
  • crates/script/src/lib.rs
    • Updated ScriptConfig to pass env.evm_env.clone() to get_fork.
  • crates/verify/src/etherscan/standard_json.rs
    • Simplified Vyper input generation by using context.get_vyper_standard_json_input().
  • crates/verify/src/provider.rs
    • Added get_vyper_standard_json_input method to VerificationContext.
  • crates/verify/src/sourcify.rs
    • Simplified Vyper input generation by using context.get_vyper_standard_json_input().
Activity
  • The pull request description is a template and does not contain specific activity details or comments.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@mergify
Copy link
Copy Markdown

mergify bot commented Mar 14, 2026

refresh

✅ Pull request refreshed

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces generic network support for the cast crate, a significant architectural refactoring. The Cast struct and its methods are now generic over the alloy_network::Network trait, allowing them to work with different network types like AnyNetwork and TempoNetwork. This required changes across multiple crates, including anvil, forge, and foundry-evm-core, to adapt to the new generic APIs. Key changes include using generic transaction requests, introducing a TryIntoRlpEncodable trait for headers, and refactoring provider creation to use a generic ProviderBuilder. The changes are well-structured and improve the flexibility of cast.

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
auto-merge was automatically disabled March 15, 2026 00:34

Head branch was pushed to by a user without write access

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 15, 2026

@github-actions[bot] is attempting to deploy a commit to the Foundry development Team on Vercel.

A member of the Team first needs to authorize it.

decofe and others added 2 commits March 16, 2026 10:47
…0.2.31 (#13763)

Co-authored-by: Matthias Seitz <19890894+mattsse@users.noreply.github.com>
zerosnacks and others added 30 commits March 30, 2026 08:25
fix(cheatcodes): import TxEnv in evm cheatcodes

Amp-Thread-ID: https://ampcode.com/threads/T-019d3dc5-6540-72d5-b37c-0dcce6a0c330

Co-authored-by: Amp <amp@ampcode.com>
refactor(anvil): extract per-tx mining loop into generic `execute_pool_transactions`

Extract the ~150-line per-tx loop duplicated in `do_mine_block` and
`with_pending_block` into a shared `execute_pool_transactions` function.

The function is generic over `B: BlockExecutor` — callers pass any
executor that satisfies the trait bounds. Also makes `build_tx_env_for_pending`
generic over transaction and tx env types, removing the `is_optimism` parameter.

Co-authored-by: zerosnacks <95942363+zerosnacks@users.noreply.github.com>
…#13959)

refactor(anvil): replace EitherEvm with compile-time dispatch

Replace runtime EitherEvm dispatch with compile-time branching on
is_optimism(). Creates concrete EthEvm/OpEvm instances directly via
their respective EvmFactory, eliminating the EitherEvm wrapper.
…`Error intrinsic gas too low` (#13999)

add megaeth and megaethtestnet to different gas calc
* Align with alloy-evm 0.29.2 and revm 36

Amp-Thread-ID: https://ampcode.com/threads/T-019d3ea9-358f-76fb-9078-b9228f314b05
Co-authored-by: Amp <amp@ampcode.com>

* Fix lint for revm handler upgrade

Amp-Thread-ID: https://ampcode.com/threads/T-019d3ea9-358f-76fb-9078-b9228f314b05
Co-authored-by: Amp <amp@ampcode.com>

* Use merged fork-db typed cache maps

Amp-Thread-ID: https://ampcode.com/threads/T-019d3ea9-358f-76fb-9078-b9228f314b05
Co-authored-by: Amp <amp@ampcode.com>

---------

Co-authored-by: Amp <amp@ampcode.com>
Add `FromEvmVersion` trait in `foundry-evm-hardforks` with impls for
`SpecId`, `OpSpecId`, and `TempoHardfork`. Delete old `evm_spec_id`
from `foundry-config` and re-export the generic version.
refactor(anvil): remove EitherEvm from tests and delete either_evm module

Replace EitherEvm usage in evm.rs tests with concrete EthEvm/OpEvm
types. Each test already knows its network at compile time — the
runtime dispatch was unnecessary.

With zero consumers remaining, delete `either_evm.rs` entirely.
…generic impl (#14019)

refactor(anvil): mk FeeHistoryService generic, mv EthApi mtds to generic impl

Co-authored-by: zerosnacks <zerosnacks@protonmail.com>
* fix(evm): use actual block number for fork pinning on Arbitrum

On Arbitrum, `block_env.number` is remapped to the L1 (Ethereum
mainnet) block number by `apply_chain_and_block_specific_env_changes`.
The fork block pinning logic introduced in #13085 read this remapped
value, causing the fork to pin to an ancient Arbitrum block where most
contracts did not exist yet.

This fix threads the actual block number returned by `fork_evm_env`
through `env()` and into `get_fork()`, so the fork is always pinned
to the correct chain block number regardless of L2 block number
remapping.

Fixes #13576

Co-authored-by: zerosnacks <95942363+zerosnacks@users.noreply.github.com>
Amp-Thread-ID: https://ampcode.com/threads/T-019cf6ac-e302-742b-92aa-b0b1ee1dd91d

* test: add regression tests for Arbitrum fork block pinning

Adds two regression tests for #13576:

1. Unit test in opts.rs: verifies that env() returns the L2 block number
   (not the remapped L1 block number) and that get_fork() pins to it.

2. Integration test in script.rs: runs a forge script that calls WETH on
   an Arbitrum fork — this would fail with "call to non-contract address"
   before the fix.

Co-Authored-By: zerosnacks <95942363+zerosnacks@users.noreply.github.com>

* test: use USDC instead of WETH in Arbitrum regression test

WETH has a non-zero ETH balance so it gets loaded into the fork
regardless of the block number bug. USDC has zero balance and is
the contract that actually fails without the fix.

Co-Authored-By: zerosnacks <95942363+zerosnacks@users.noreply.github.com>
Amp-Thread-ID: https://ampcode.com/threads/T-019cf6ac-e302-742b-92aa-b0b1ee1dd91d

* test: prefix Arbitrum regression test with flaky_

Co-authored-by: zerosnacks <95942363+zerosnacks@users.noreply.github.com>
Amp-Thread-ID: https://ampcode.com/threads/T-019d42e5-a19e-76fa-a81d-1c33d0e5df2d
Co-authored-by: Amp <amp@ampcode.com>

---------

Co-authored-by: Derek Cofausper <256792747+decofe@users.noreply.github.com>
Co-authored-by: Amp <amp@ampcode.com>
refactor(evm): make `InspectorStackBuilder::build()` generic over `SPEC` and `Network`
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.