# Bidirectional Bridge Configuration Guide

**Date**: 2025-01-18  
**Purpose**: Complete guide for configuring full bidirectional bridge between ChainID 138 and Ethereum Mainnet

## Overview

This guide provides step-by-step instructions for configuring both directions of the cross-chain bridge:
- ChainID 138 → Ethereum Mainnet
- Ethereum Mainnet → ChainID 138

---

## Prerequisites

### Required

1. **Admin Private Key**: Must have access to bridge admin accounts
2. **RPC Access**: 
   - Mainnet RPC: `https://eth.llamarpc.com` or similar
   - ChainID 138 RPC: `http://192.168.11.211:8545` (VMID 2101)
3. **Chain Selectors**:
   - Mainnet: `5009297550715157269` ✅ (Known)
   - ChainID 138: See selector determination below

---

## Step 1: Determine ChainID 138 Selector

### Option A: Use networks.json Value

**From `networks.json`**:
```json
"chainSelector": "5009297550715157269"
```

**Note**: This is the same as Mainnet. Verify if correct for your CCIP implementation.

**To use**:
```bash
export CHAIN138_SELECTOR="5009297550715157269"
# Or add to .env:
echo "CHAIN138_SELECTOR=5009297550715157269" >> .env
```

### Option B: Use Chain ID Directly (Custom CCIP)

**For custom CCIP implementations**, the selector may be the chain ID:
```bash
export CHAIN138_SELECTOR="138"
# Or add to .env:
echo "CHAIN138_SELECTOR=138" >> .env
```

### Option C: Official Chainlink CCIP Selector

If using official Chainlink CCIP:
1. Query Chainlink CCIP Directory
2. Contact Chainlink for ChainID 138 registration
3. Use official selector provided

### Helper Script

Run the helper script to check sources:
```bash
./scripts/configuration/find-chain-selector.sh
```

---

## Step 2: Configure ChainID 138 → Mainnet (Direction 1)

**Status**: ✅ **Ready to execute** (doesn't require ChainID 138 selector)

```bash
./scripts/configuration/configure-chain138-to-mainnet.sh
```

**What it does**:
- Configures WETH9 Bridge on ChainID 138 to send to Mainnet
- Configures WETH10 Bridge on ChainID 138 to send to Mainnet
- Uses Mainnet selector (`5009297550715157269`) which is known

**Expected Output**:
```
✓ WETH9 Bridge configured (ChainID 138 → Mainnet)
✓ WETH10 Bridge configured (ChainID 138 → Mainnet)
```

---

## Step 3: Configure Mainnet → ChainID 138 (Direction 2)

**Status**: ⚠️ **Requires ChainID 138 selector**

```bash
# Ensure CHAIN138_SELECTOR is set in .env or environment
./scripts/configuration/configure-mainnet-to-chain138.sh
```

**What it does**:
- Configures WETH9 Bridge on Mainnet to send to ChainID 138
- Configures WETH10 Bridge on Mainnet to send to ChainID 138
- Uses ChainID 138 selector (from environment)

**Expected Output**:
```
✓ WETH9 Bridge configured (Mainnet → ChainID 138)
✓ WETH10 Bridge configured (Mainnet → ChainID 138)
```

---

## Step 4: Full Bidirectional Configuration (Alternative)

**Use the full configuration script** (if selector is set):

```bash
# Ensure CHAIN138_SELECTOR is in .env
./scripts/configuration/configure-bridge-destinations.sh
```

This script configures both directions in one run.

---

## Step 5: Verify Configuration

**Verify all bridge configurations**:

```bash
./scripts/configuration/verify-bridge-configuration.sh
```

**Expected Output**:
- Mainnet bridges should show ChainID 138 selector in destinations
- ChainID 138 bridges should show Mainnet selector in destinations

---

## Configuration Summary

### Bridge Addresses

| Bridge | Mainnet Address | ChainID 138 Address |
|--------|----------------|---------------------|
| CCIPWETH9Bridge | `0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6` | `0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6` |
| CCIPWETH10Bridge | `0x8078A09637e47Fa5Ed34F626046Ea2094a5CDE5e` | `0x8078A09637e47Fa5Ed34F626046Ea2094a5CDE5e` |

### Chain Selectors

| Chain | Selector | Status |
|-------|----------|--------|
| Ethereum Mainnet | `5009297550715157269` | ✅ Known |
| ChainID 138 | See Step 1 | ⚠️ To be determined |

### Destination Configuration

**ChainID 138 Bridges → Mainnet**:
- Destination Selector: `5009297550715157269`
- Receiver WETH9 Bridge: `0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6`
- Receiver WETH10 Bridge: `0x8078A09637e47Fa5Ed34F626046Ea2094a5CDE5e`

**Mainnet Bridges → ChainID 138**:
- Destination Selector: `<CHAIN138_SELECTOR>` (from Step 1)
- Receiver WETH9 Bridge: `0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6`
- Receiver WETH10 Bridge: `0x8078A09637e47Fa5Ed34F626046Ea2094a5CDE5e`

---

## Troubleshooting

### Configuration Fails

**Check**:
- `PRIVATE_KEY` is set and correct
- Private key has admin access to bridges
- RPC endpoints are accessible
- ChainID 138 selector is correct

**Error: "destination already exists"**:
- Destination may already be configured
- Verify with `verify-bridge-configuration.sh`

**Error: "only admin"**:
- Private key doesn't have admin access
- Verify admin address: `cast call <BRIDGE> "admin()" --rpc-url <RPC>`

### Selector Issues

**If selector is incorrect**:
1. Bridges can be reconfigured with correct selector
2. Use `removeDestination()` to remove incorrect entry
3. Use `addDestination()` with correct selector

**To check current destinations**:
```bash
cast call <BRIDGE_ADDRESS> "getDestinationChains()(uint64[])" --rpc-url <RPC>
```

---

## Testing After Configuration

### Test ChainID 138 → Mainnet

1. Approve WETH9 on ChainID 138 for bridge
2. Send small amount via bridge
3. Verify receipt on Mainnet

### Test Mainnet → ChainID 138

1. Approve WETH9 on Mainnet for bridge
2. Send small amount via bridge
3. Verify receipt on ChainID 138

---

## Quick Start (If Selector Known)

```bash
# 1. Set selector in .env
echo "CHAIN138_SELECTOR=5009297550715157269" >> .env  # Or appropriate value

# 2. Configure ChainID 138 → Mainnet
./scripts/configuration/configure-chain138-to-mainnet.sh

# 3. Configure Mainnet → ChainID 138
./scripts/configuration/configure-mainnet-to-chain138.sh

# 4. Verify
./scripts/configuration/verify-bridge-configuration.sh
```

---

## Status

✅ **Configuration scripts ready**
- ChainID 138 → Mainnet: Ready to execute
- Mainnet → ChainID 138: Ready to execute (requires selector)

⚠️ **Selector determination needed**
- See Step 1 for selector options
- Use helper script: `find-chain-selector.sh`

---

**Next Steps**: Determine ChainID 138 selector and run configuration scripts for full bidirectional setup.
