|
| 1 | +# Custom EVM Networks |
| 2 | + |
| 3 | +The evm-networks crate defines custom network features that are shared across Foundry's tooling (`anvil`, `forge` and |
| 4 | +`cast`). Currently, it supports custom precompiles, with planned support for custom transaction types. |
| 5 | + |
| 6 | +## Adding a Custom Network |
| 7 | +To add configuration support for a custom network (e.g. `my_network`), add a new field to the `NetworkConfigs` struct: |
| 8 | +```rust |
| 9 | + /// Enable my custom network features. |
| 10 | + #[arg(help_heading = "Networks", long)] |
| 11 | + #[serde(default)] |
| 12 | + pub my_network: bool, |
| 13 | +``` |
| 14 | +This automatically enables:: |
| 15 | +- `my_network = true` in foundry.toml |
| 16 | +- `--my-network` anvil CLI flag |
| 17 | +``` |
| 18 | +Networks: |
| 19 | + --my-network |
| 20 | + Enable my custom network features |
| 21 | +
|
| 22 | +``` |
| 23 | +If you'd like network features to be enabled automatically based on the chain ID, update the implementation of: |
| 24 | +```rust |
| 25 | +impl NetworkConfigs { |
| 26 | + pub fn with_chain_id(chain_id: u64) -> Self { |
| 27 | + // Enable custom network features here |
| 28 | + } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +## Adding a custom precompile |
| 33 | + |
| 34 | +- Create a module for your network-specific logic, e.g., `my_network/transfer`. |
| 35 | +- Implement the precompile logic as a function that accepts a `PrecompileInput` containing execution context and hooks for |
| 36 | +interacting with EVM state, and returns a `PrecompileResult`: |
| 37 | +```rust |
| 38 | +pub fn custom_precompile( |
| 39 | + input: alloy_evm::precompiles::PrecompileInput<'_> |
| 40 | +) -> revm::precompile::PrecompileResult { |
| 41 | + // Your logic here |
| 42 | +} |
| 43 | +``` |
| 44 | +- Enable the precompile in the `NetworkConfigs` implementation by conditionally applying it to an address: |
| 45 | +```rust |
| 46 | +if self.my_network { |
| 47 | + precompiles.apply_precompile(&MY_NETWORK_TRANSFER_ADDRESS, move |_| { |
| 48 | + Some(my_network::transfer::custom_precompile()) |
| 49 | + }); |
| 50 | +} |
| 51 | +``` |
0 commit comments