# CREATE Deployment Guide

## Overview

This guide explains how to deploy WETH9 and WETH10 contracts to the exact addresses from `genesis.json` using CREATE (not CREATE2).

## Problem

The `genesis.json` file contains reserved addresses:
- **WETH9**: `0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2`
- **WETH10**: `0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9F`

These are Ethereum mainnet addresses that were originally deployed using CREATE. To deploy to these exact addresses on ChainID 138, we need to use CREATE with the correct deployer address and nonce.

## CREATE Address Calculation

CREATE address is calculated using:
```
address = keccak256(RLP(deployer_address, nonce))[12:]
```

Where:
- `deployer_address`: The address that creates the contract
- `nonce`: The nonce of the deployer address (transaction count)

## Deployment Methods

### Method 1: vm.etch (Test/Simulation Only)

**File**: `script/DeployWETHToGenesisAddresses.s.sol`

Uses `vm.etch` to directly set bytecode at target addresses. This only works in Foundry's test/simulation mode, not in actual broadcasts.

```solidity
vm.etch(TARGET_WETH9, deployedBytecode);
```

**Limitation**: `vm.etch` is a cheatcode that doesn't work in production broadcasts.

### Method 2: CREATE with Calculated Nonce

**Files**: 
- `script/DeployWETHWithCREATEDirect.s.sol`
- `scripts/utils/calculate-create-address.js`

Calculates the nonce needed for a deployer to produce the target address.

```bash
# Calculate CREATE nonce
node scripts/utils/calculate-create-address.js <target-address> [deployer] [max-nonce]

# Example
node scripts/utils/calculate-create-address.js 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
```

### Method 3: Direct CREATE Deployment

Once we know the deployer and nonce, we can deploy directly:

```solidity
vm.startBroadcast(deployerAddress);
// Ensure deployer has correct nonce
WETH weth = new WETH(); // Will deploy to calculated address
```

## Current Status

### Attempts Made

1. ✅ **vm.etch Deployment**: Successfully set bytecode in simulation, but doesn't work in broadcasts
2. ✅ **CREATE Nonce Calculation**: Tried to calculate nonce for common deployers (0-10,000), not found
3. ✅ **Direct CREATE Deployment**: Tried deploying from genesis addresses, addresses don't match

### Challenges

1. **Nonce Calculation**: Finding the exact nonce that produces the target address requires brute-force searching
2. **Deployer Unknown**: We don't know which deployer address was used originally
3. **vm.etch Limitation**: Only works in tests, not in production broadcasts

## Solutions

### Solution 1: Use vm.etch in Fork Mode

If deploying in fork mode or testnet, `vm.etch` can work:

```bash
forge script script/DeployWETHToGenesisAddresses.s.sol \
    --fork-url <rpc-url> \
    --broadcast
```

### Solution 2: Calculate Nonce (Longer Search)

Increase the nonce search range:

```bash
node scripts/utils/calculate-create-address.js \
    0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 \
    0x0742D35CC6634c0532925A3b844bc9E7595f0Beb \
    1000000  # Search up to nonce 1,000,000
```

### Solution 3: Use Known Deployer

If you know the deployer address used when creating genesis.json, use it directly:

```solidity
vm.startBroadcast(knownDeployer);
// Deploy will use deployer's current nonce
WETH weth = new WETH();
```

### Solution 4: Pre-calculate and Reserve Nonce

1. Calculate what nonce produces the target address
2. Send transactions to reach that nonce
3. Deploy contract - it will be at the target address

## Recommendation

For production deployment on ChainID 138:

1. **If addresses are just reserved**: Keep them reserved in genesis.json and use the deployed addresses from earlier deployments
2. **If exact addresses are required**: Calculate the CREATE nonce with a longer search range, or use the deployer that created genesis.json
3. **For testing**: Use `vm.etch` in test/fork mode

## Files Created

1. `script/DeployWETHToGenesisAddresses.s.sol` - vm.etch deployment
2. `script/DeployWETH9WithCREATE.s.sol` - CREATE nonce calculation
3. `script/DeployWETHWithCREATEDirect.s.sol` - Direct CREATE deployment
4. `scripts/utils/calculate-create-address.js` - CREATE address calculator

## See Also

- [Ethereum CREATE Opcode](https://ethereum.org/en/developers/docs/evm/opcodes/)
- [Foundry vm.etch](https://book.getfoundry.sh/cheatcodes/etch)
- [RLP Encoding](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/)

