# CCIP Deployment Guide for ChainID 138

**Date**: 2025-01-27  
**Network**: ChainID 138 (DeFi Oracle Meta Mainnet)

---

## Overview

This guide provides step-by-step instructions for deploying and configuring Chainlink CCIP on ChainID 138. Follow these steps in order to ensure proper configuration.

---

## Prerequisites

- ChainID 138 network running and accessible
- RPC endpoint configured (`RPC_URL_138` or `CHAIN138_RPC_URL`)
- Private key configured (`PRIVATE_KEY`)
- Sufficient native token balance for deployment
- Foundry/Forge installed
- `cast` command available (from Foundry)

---

## Phase 1: Calculate Chain Selector

### Step 1.1: Calculate Chain Selector

Run the chain selector calculation script:

```bash
./scripts/ccip/calculate-chain-selector.sh
```

This will:
- Calculate the recommended chain selector for ChainID 138
- Update `.env` with `CHAIN138_SELECTOR=0x000000000000008a` (or `138`)

**Result**: Chain selector is `138` (hex: `0x000000000000008a`)

---

## Phase 2: Deploy CCIP Router

### Step 2.1: Decide on Fee Token

Choose one:
- **Option A**: Use native ETH (recommended for simplicity)
  - Set `CCIP_CHAIN138_LINK_TOKEN=0x0000000000000000000000000000000000000000`
- **Option B**: Deploy LINK token
  - Deploy ERC-20 LINK token contract
  - Set `CCIP_CHAIN138_LINK_TOKEN=<deployed_link_address>`

### Step 2.2: Configure Environment Variables

Add to `.env`:

```bash
# CCIP Router Configuration
CCIP_CHAIN138_LINK_TOKEN=0x0000000000000000000000000000000000000000  # Use native ETH
CCIP_BASE_FEE=1000000000000000  # 0.001 ETH (1e15 wei)
CCIP_DATA_FEE_PER_BYTE=100000000  # 0.1 gwei per byte (1e8 wei)
```

### Step 2.3: Deploy CCIP Router

```bash
forge script script/DeployCCIPRouter.s.sol \
  --rpc-url $RPC_URL_138 \
  --broadcast \
  --private-key $PRIVATE_KEY
```

### Step 2.4: Update Environment

After deployment, update `.env`:

```bash
CCIP_CHAIN138_ROUTER=<deployed_router_address>
```

### Step 2.5: Configure Router

Run the configuration script:

```bash
./scripts/deployment/configure-ccip-router.sh
```

This will:
- Add Ethereum Mainnet as a supported chain
- Add WETH9 and WETH10 as supported tokens

---

## Phase 3: Deploy CCIP Bridges

### Step 3.1: Deploy CCIPWETH9Bridge

```bash
# Ensure CCIP_CHAIN138_ROUTER is set in .env
forge script script/DeployCCIPWETH9Bridge.s.sol \
  --rpc-url $RPC_URL_138 \
  --broadcast \
  --private-key $PRIVATE_KEY
```

Update `.env`:

```bash
CCIPWETH9_BRIDGE_CHAIN138=<deployed_bridge_address>
```

### Step 3.2: Deploy CCIPWETH10Bridge

```bash
forge script script/DeployCCIPWETH10Bridge.s.sol \
  --rpc-url $RPC_URL_138 \
  --broadcast \
  --private-key $PRIVATE_KEY
```

Update `.env`:

```bash
CCIPWETH10_BRIDGE_CHAIN138=<deployed_bridge_address>
```

---

## Phase 4: Configure Bridge Destinations

### Step 4.1: Configure CCIPWETH9Bridge

Run the configuration script:

```bash
./scripts/deployment/configure-weth9-bridge.sh
```

Or manually:

```bash
# Add ChainID 138 destination on Ethereum Mainnet bridge
cast send $CCIPWETH9_BRIDGE_MAINNET \
  "addDestination(uint64,address)" \
  138 \
  $CCIPWETH9_BRIDGE_CHAIN138 \
  --rpc-url $ETHEREUM_MAINNET_RPC \
  --private-key $PRIVATE_KEY

# Add Ethereum Mainnet destination on ChainID 138 bridge
cast send $CCIPWETH9_BRIDGE_CHAIN138 \
  "addDestination(uint64,address)" \
  5009297550715157269 \
  $CCIPWETH9_BRIDGE_MAINNET \
  --rpc-url $RPC_URL_138 \
  --private-key $PRIVATE_KEY
```

### Step 4.2: Configure CCIPWETH10Bridge

Run the configuration script:

```bash
./scripts/deployment/configure-weth10-bridge.sh
```

Or manually (similar to WETH9 above).

---

## Phase 5: Verification

### Step 5.1: Verify Deployment

Run the verification script:

```bash
./scripts/deployment/verify-ccip-deployment.sh
```

This will check:
- All contracts are deployed
- Router configuration
- Bridge configuration
- Chain selector

### Step 5.2: Manual Verification

Verify each contract on the explorer:

```bash
# Check router
cast code $CCIP_CHAIN138_ROUTER --rpc-url $RPC_URL_138

# Check bridges
cast code $CCIPWETH9_BRIDGE_CHAIN138 --rpc-url $RPC_URL_138
cast code $CCIPWETH10_BRIDGE_CHAIN138 --rpc-url $RPC_URL_138
```

---

## Phase 6: Testing

### Step 6.1: Test Router Message Sending

```solidity
// Test sending a message via router
router.ccipSend(
    5009297550715157269, // Ethereum Mainnet selector
    message
);
```

### Step 6.2: Test Bridge Token Transfer

```bash
# Test WETH9 bridge transfer
# 1. Approve bridge to spend WETH9
# 2. Call sendCrossChain()
# 3. Verify on destination chain
```

---

## Environment Variables Summary

After complete deployment, your `.env` should include:

```bash
# ChainID 138 CCIP Configuration
CHAIN138_SELECTOR=0x000000000000008a
CCIP_CHAIN138_ROUTER=<deployed_router_address>
CCIP_CHAIN138_LINK_TOKEN=0x0000000000000000000000000000000000000000  # or LINK address
CCIPWETH9_BRIDGE_CHAIN138=<deployed_bridge_address>
CCIPWETH10_BRIDGE_CHAIN138=<deployed_bridge_address>

# Router Configuration
CCIP_BASE_FEE=1000000000000000
CCIP_DATA_FEE_PER_BYTE=100000000
```

---

## Troubleshooting

### Issue: Router deployment fails

**Solution**: 
- Check RPC connectivity
- Verify sufficient balance
- Check gas price settings

### Issue: Bridge deployment fails

**Solution**:
- Ensure router is deployed first
- Verify router address in environment
- Check WETH addresses match genesis

### Issue: Configuration fails

**Solution**:
- Verify contract addresses
- Check chain selectors are correct
- Ensure admin key has permissions

---

## Next Steps

After deployment:

1. **Update Documentation**: Update all docs with deployed addresses
2. **Contract Verification**: Verify contracts on explorer
3. **Testing**: Run comprehensive tests
4. **Monitoring**: Set up monitoring and alerting
5. **Security**: Review security measures

---

## Related Documentation

- [Chain Selector Calculation](CHAIN_SELECTOR_CALCULATION.md)
- [CCIP Review](../CCIP_CHAIN138_REVIEW.md)
- [Main Guide](../ETH_WETH_CHAINLINK_GUIDE.md)

---

**Last Updated**: 2025-01-27

