## Forking

Anvil can fork any EVM-compatible chain, allowing you to test against real-world state without deploying to a live network.

### Basic forking

```bash
$ anvil --fork-url https://ethereum.reth.rs/rpc
```

You can also fork from a specific point in the chain's history:

:::code-group
```bash [At block]
$ anvil --fork-url https://ethereum.reth.rs/rpc --fork-block-number 18000000
```

```bash [At transaction]
$ anvil --fork-url https://ethereum.reth.rs/rpc --fork-transaction-hash $TX_HASH
```
:::

### Chain configuration

When forking, Anvil inherits the chain ID from the remote endpoint by default.

```bash [Override chain ID]
$ anvil --fork-url https://ethereum.reth.rs/rpc --chain-id 1337
```

```bash [Specify hardfork]
$ anvil --fork-url https://ethereum.reth.rs/rpc --hardfork cancun
```

### Rate limiting

RPC providers often rate-limit requests. Anvil handles this automatically but you can tune the behavior:

```bash [Set compute units per second]
$ anvil --fork-url $RPC_URL --compute-units-per-second 100
```

```bash [Disable rate limiting]
$ anvil --fork-url $RPC_URL --no-rate-limit
```

```bash [Set retry count]
$ anvil --fork-url $RPC_URL --retries 10
```

### Caching

Anvil caches forked state to disk at `~/.foundry/cache/rpc/<chain>/<block>/` to speed up subsequent runs.

```bash [Disable storage caching]
$ anvil --fork-url $RPC_URL --no-storage-caching
```

### Custom headers

Some RPC providers require authentication headers:

```bash [Add custom header]
$ anvil --fork-url $RPC_URL --fork-header "Authorization: Bearer $TOKEN"
```

### L2 support

Anvil supports L2-specific features:

```bash [Enable Optimism features]
$ anvil --fork-url https://mainnet.optimism.io --optimism
```

```bash [Enable Celo features]
$ anvil --fork-url https://forno.celo.org --celo
```
