# Smart CREATE2 Deployment Guide

## Overview

Instead of brute-forcing salts, we use **Foundry's impersonation features** and **direct salt calculation** to deploy contracts to exact addresses from genesis.json.

## Key Insights

1. **Foundry Impersonation**: Use `vm.startBroadcast(address)` to impersonate any address (even if we don't have the private key)
2. **Salt Calculation**: Since addresses are in genesis.json, we can calculate the salt for known deployers
3. **No Brute Force Needed**: Try common salts first (0, 1, chain ID, contract name, etc.)

## Why This Works Better

### Old Approach (Brute Force)
- ❌ Tries thousands of salt values
- ❌ Slow and gas-intensive
- ❌ May never find the correct salt

### New Approach (Smart Calculation)
- ✅ Tries common salts first (instant)
- ✅ Uses Foundry's impersonation (no private key needed)
- ✅ Leverages the fact that addresses are pre-allocated in genesis.json

## Usage

### Option 1: Use the Smart Script

```bash
# Deploy WETH9
forge script script/DeployWETH9Smart.s.sol:DeployWETH9Smart \
    --rpc-url http://localhost:8545 \
    --broadcast \
    --sender <deployer-address> \
    --legacy
```

Note: When using `--sender`, Foundry will impersonate that address using `vm.startBroadcast(address)`.

### Option 2: Calculate Parameters First

Run the calculation script to find the salt:

```bash
./scripts/deployment/calculate-create2-parameters.sh
```

This will output the deployer address and salt needed, which you can then use in your deployment script.

## How It Works

### Step 1: Identify Potential Deployers

Since the addresses are in genesis.json, likely deployers are:
1. Standard CREATE2 deployer (`0x4e59b44847b379578588920cA78FbF26c0B4956C`)
2. Genesis addresses (those with high balances in genesis.json)
3. CREATE2Factory address (if one exists)

### Step 2: Calculate Salt for Each Deployer

For each potential deployer, try common salts:
- `0` (zero)
- `1` (one)
- `138` (chain ID)
- `keccak256("WETH9")` or `keccak256("WETH10")`
- `keccak256("Wrapped Ether")`
- Address-specific values

### Step 3: Impersonate and Deploy

Once we find the correct deployer/salt combination:
1. Use `vm.startBroadcast(deployerAddress)` to impersonate
2. Deploy using CREATE2 with the calculated salt
3. Verify the deployed address matches the target

## Example

```solidity
// Impersonate the CREATE2 deployer (no private key needed!)
vm.startBroadcast(CREATE2_DEPLOYER);

// Deploy with calculated salt
address deployed = deployCreate2(bytecode, calculatedSalt);

// Verify
require(deployed == TARGET_WETH9, "Address mismatch!");
```

## Benefits

1. **Faster**: Common salts are checked instantly
2. **No Private Keys Needed**: Uses Foundry's impersonation
3. **More Reliable**: Leverages genesis.json information
4. **Gas Efficient**: Only deploys once, doesn't try thousands of salts

## Limitations

- If the deployer is unknown or uses an unusual salt, we still need to search
- CREATE2 is a one-way function, so we can't directly reverse it
- Some salts may require sequential search (limited to first 1000)

## See Also

- [Foundry Impersonation Docs](https://book.getfoundry.sh/reference/cheatcodes/start-prank)
- [CREATE2 EIP-1014](https://eips.ethereum.org/EIPS/eip-1014)
- [Deterministic Deployments](https://getfoundry.sh/guides/deterministic-deployments-using-create2)

