# WETH9 and WETH10 CREATE2 Deployment Guide

## Overview

This guide explains how to deploy WETH9 and WETH10 contracts to the exact addresses specified in `genesis.json` using CREATE2.

## Target Addresses

From `genesis.json`:
- **WETH9**: `0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2`
- **WETH10**: `0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f`

These addresses are pre-allocated in the genesis block with balance `0x0` and no code.

## CREATE2 Address Calculation

The CREATE2 address is calculated using:
```
address = keccak256(0xff ++ deployer ++ salt ++ keccak256(bytecode))[12:]
```

To deploy to the exact target addresses, we need:
1. **Contract Bytecode**: Must match exactly (we compile our contracts)
2. **Deployer Address**: Must match the address used when calculating the genesis addresses
3. **Salt**: Must match the salt used when calculating the genesis addresses

## Deployment Scripts

### 1. `script/DeployWETH9ToExactAddress.s.sol`
- Attempts to find the salt that produces the WETH9 target address
- Uses CREATE2Factory to deploy
- Tries common salts first, then brute forces if needed

### 2. `script/DeployWETH10ToExactAddress.s.sol`
- Attempts to find the salt that produces the WETH10 target address
- Uses CREATE2Factory to deploy
- Tries common salts first, then brute forces if needed

### 3. `scripts/deployment/calculate-create2-salt.js`
- Node.js utility to calculate CREATE2 salt
- Can be used to find the salt that produces a target address
- Supports brute-force search

### 4. `scripts/deployment/deploy-weth-create2.sh`
- Main deployment script
- Compiles contracts
- Checks if contracts already exist
- Deploys WETH9 and WETH10 sequentially

## Deployment Process

### Prerequisites

1. **Environment Variables** (in `.env`):
   ```bash
   PRIVATE_KEY=0x...
   RPC_URL=http://localhost:8545
   ```

2. **Compiled Contracts**:
   ```bash
   forge build
   ```

### Step 1: Calculate Salt (Optional)

If you know the deployer address used when creating genesis.json:

```bash
node scripts/deployment/calculate-create2-salt.js WETH <deployer-address>
node scripts/deployment/calculate-create2-salt.js WETH10 <deployer-address>
```

This will find the salt that produces the target addresses.

### Step 2: Deploy Contracts

**Option A: Use the automated script**
```bash
./scripts/deployment/deploy-weth-create2.sh
```

**Option B: Deploy manually using Foundry**
```bash
# Deploy WETH9
forge script script/DeployWETH9ToExactAddress.s.sol:DeployWETH9ToExactAddress \
    --rpc-url $RPC_URL \
    --broadcast \
    --private-key $PRIVATE_KEY \
    --legacy

# Deploy WETH10
forge script script/DeployWETH10ToExactAddress.s.sol:DeployWETH10ToExactAddress \
    --rpc-url $RPC_URL \
    --broadcast \
    --private-key $PRIVATE_KEY \
    --legacy
```

## Troubleshooting

### Issue: Salt not found

If the scripts cannot find a salt that produces the target address:

1. **Check Deployer Address**: The deployer address must match the one used when calculating the genesis addresses
2. **Verify Bytecode**: Ensure the compiled bytecode matches what was used in genesis.json
3. **Try Different Deployer**: If genesis.json used a different deployer, you may need to use that address

### Issue: Address mismatch

If the deployed address doesn't match the target:

1. **Verify Salt**: Double-check the salt calculation
2. **Check Factory Address**: If using CREATE2Factory, ensure the factory address matches
3. **Review Genesis**: Confirm the target addresses in genesis.json are correct

### Issue: Contract already exists

If the contract already exists at the target address:

1. **Verify Deployment**: Check if the contract is already deployed
2. **Check Code**: Verify the existing code matches what you expect
3. **Skip Deployment**: The scripts will skip deployment if contracts already exist

## Alternative Approaches

### Approach 1: Direct Deployment to Pre-allocated Addresses

If genesis.json pre-allocates these addresses, you might be able to deploy directly to them without CREATE2, depending on the blockchain client's implementation.

### Approach 2: Use Known CREATE2 Deployer

Use a well-known CREATE2 deployer (like `0x4e59b44847b379578588920cA78FbF26c0B4956C`) and calculate the appropriate salt.

### Approach 3: Genesis Pre-deployment

If the blockchain client supports it, you can include the contract bytecode directly in genesis.json instead of deploying via CREATE2.

## Verification

After deployment, verify the contracts:

```bash
# Check WETH9
cast code 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 --rpc-url $RPC_URL

# Check WETH10
cast code 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f --rpc-url $RPC_URL

# Interact with contracts
cast call 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 "name()" --rpc-url $RPC_URL
cast call 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f "name()" --rpc-url $RPC_URL
```

## Important Notes

1. **Gas Costs**: CREATE2 deployment with salt finding can be gas-intensive, especially if brute-force is needed
2. **Deployer Balance**: Ensure the deployer account has sufficient balance for deployment
3. **Network**: Make sure you're deploying to the correct network (ChainID 138)
4. **Genesis Alignment**: The addresses in genesis.json must match the CREATE2 calculation for deployment to work

## References

- [EIP-1014: CREATE2](https://eips.ethereum.org/EIPS/eip-1014)
- [Foundry CREATE2 Guide](https://getfoundry.sh/guides/deterministic-deployments-using-create2)
- [Alchemy CREATE2 Guide](https://www.alchemy.com/docs/create2-an-alternative-to-deriving-contract-addresses)

