> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dokrypt.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Chain Forking

> Fork Ethereum mainnet, Polygon, Arbitrum, and other EVM chains at any block.

## Overview

Chain forking creates a local copy of a live blockchain network. Your local environment gets the complete state of the target network — all contracts, all balances, all storage — at a specific block. This lets you:

* **Test against real DeFi protocols** — Interact with Uniswap, Aave, Compound, etc. locally
* **Reproduce mainnet bugs** — Fork at the block before a bug and replay the transaction
* **Test token integrations** — Use real USDC, WETH, DAI balances in your tests
* **Simulate governance** — Impersonate any address and execute transactions

## Quick Start

```bash theme={null}
# Start your environment
dokrypt up

# Fork Ethereum mainnet at the latest block
dokrypt fork mainnet

# Fork at a specific block
dokrypt fork mainnet --block 18500000

# Fork Polygon
dokrypt fork polygon
```

## Supported Networks

| Network   | Aliases               | Chain ID | Public RPC                              |
| --------- | --------------------- | -------- | --------------------------------------- |
| Ethereum  | `mainnet`, `ethereum` | 1        | `https://eth.llamarpc.com`              |
| Sepolia   | `sepolia`             | 11155111 | `https://rpc.sepolia.org`               |
| Goerli    | `goerli`              | 5        | `https://rpc.ankr.com/eth_goerli`       |
| Polygon   | `polygon`             | 137      | `https://polygon-rpc.com`               |
| Arbitrum  | `arbitrum`            | 42161    | `https://arb1.arbitrum.io/rpc`          |
| Optimism  | `optimism`            | 10       | `https://mainnet.optimism.io`           |
| Base      | `base`                | 8453     | `https://mainnet.base.org`              |
| BSC       | `bsc`                 | 56       | `https://bsc-dataseed.binance.org`      |
| Avalanche | `avalanche`           | 43114    | `https://api.avax.network/ext/bc/C/rpc` |

## Using Custom RPC URLs

For faster forks or private access, use your own RPC URL:

```bash theme={null}
dokrypt fork --url https://eth-mainnet.alchemyapi.io/v2/YOUR_KEY
```

Or with the `--fork` flag on `dokrypt up`:

```bash theme={null}
dokrypt up --fork mainnet
dokrypt up --fork polygon --fork-block 50000000
```

## Fork on Startup via Config

You can configure forking directly in `dokrypt.yaml`:

```yaml theme={null}
chains:
  ethereum:
    engine: anvil
    chain_id: 31337
    fork:
      network: mainnet
      block_number: 18500000
```

## Use Cases

### Test Against Uniswap

```bash theme={null}
# Fork mainnet
dokrypt fork mainnet

# Your local chain now has all of Uniswap's contracts
# Interact with the Router at 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
```

### Impersonate a Whale

```bash theme={null}
# Fork mainnet
dokrypt fork mainnet

# Impersonate a USDC whale
dokrypt accounts impersonate 0x47ac0Fb4F2D84898e4D9E7b4DaB3C24507a6D503

# Now you can send USDC from this address without the private key
```

### Reproduce a Bug

```bash theme={null}
# Fork at the block just before the bug
dokrypt fork mainnet --block 17999999

# Impersonate the affected account
dokrypt accounts impersonate 0xAffectedAddress...

# Replay or investigate
```

### Multi-chain Fork

If your `dokrypt.yaml` has multiple chains, specify which one to fork:

```bash theme={null}
dokrypt fork mainnet --chain l1
dokrypt fork polygon --chain l2
```

## How It Works

1. **Resolution** — The network name is resolved to an RPC URL
2. **Reset** — `anvil_reset` (or `hardhat_reset`) is called with the fork configuration
3. **Wait** — Dokrypt waits up to 120 seconds for the fork to complete
4. **Funding** — Test accounts are funded with 10,000 ETH each
5. **Ready** — The local chain is now a fork of the target network

<Warning>
  Forking from public RPCs can be slow. For production testing, use a dedicated RPC provider (Alchemy, Infura, QuickNode) for much faster fork times.
</Warning>
