# CCIP Chain Selector Calculation for ChainID 138

**Date**: 2025-01-27  
**Chain ID**: 138  
**Network**: DeFi Oracle Meta Mainnet

> **Automation:** Run [`scripts/ccip/calculate-chain-selector.sh`](../../../scripts/ccip/calculate-chain-selector.sh) to compute or verify the Chain 138 selector.

---

## Overview

CCIP chain selectors are unique identifiers used to route messages between chains in the Chainlink CCIP network. For ChainID 138, we need to calculate or define a chain selector that is consistent and unique.

---

## Calculation Methods

### Method 1: Simple ChainID (Recommended for Custom CCIP)

For custom CCIP router implementations, the simplest approach is to use the chain ID directly as the selector:

```
Chain Selector = Chain ID (as uint64)
Chain Selector = 138
Hex Format: 0x000000000000008a
```

**Advantages**:
- Simple and deterministic
- Easy to remember and verify
- Compatible with custom CCIP implementations
- No calculation required

**Usage**:
```solidity
uint64 chainSelector = 138; // or 0x000000000000008a
```

### Method 2: Chainlink Official Format

If integrating with Chainlink's official CCIP network, chain selectors are calculated using a deterministic hash-based method. The exact formula used by Chainlink is:

```solidity
uint64(uint256(keccak256(abi.encodePacked(chainId, "CCIP"))) >> 192)
```

However, for custom chains not yet registered with Chainlink, you may need to:
1. Register the chain with Chainlink
2. Use Chainlink's assigned selector
3. Or use a custom format for your implementation

### Method 3: Deterministic Hash-Based

For a deterministic but more complex selector, you can use:

```bash
# Using cast (Foundry)
cast keccak "$(printf '0x%064x' 138)CCIP"
# Extract first 8 bytes (uint64) from the hash
```

---

## Recommended Selector for ChainID 138

**Selected Method**: Method 1 (Simple ChainID)

**Selector Value**:
- **Decimal**: `138`
- **Hex**: `0x000000000000008a`
- **Type**: `uint64`

**Rationale**:
- Simple and deterministic
- Easy to verify and document
- Compatible with custom CCIP router implementation
- No external dependencies or calculations

---

## Implementation

### Environment Variable

```bash
CHAIN138_SELECTOR=0x000000000000008a
# Or simply:
CHAIN138_SELECTOR=138
```

### In Solidity

```solidity
uint64 constant CHAIN138_SELECTOR = 138;
// Or
uint64 constant CHAIN138_SELECTOR = 0x000000000000008a;
```

### In Scripts

```bash
# Use the calculated selector
CHAIN138_SELECTOR=138
# Or hex format
CHAIN138_SELECTOR=0x000000000000008a
```

---

## Usage in CCIP Configuration

### Router Configuration

When configuring the CCIP Router to support ChainID 138:

```solidity
router.addSupportedChain(138); // or 0x000000000000008a
```

### Bridge Configuration

When configuring bridges to send to ChainID 138:

```solidity
bridge.addDestination(138, receiverBridgeAddress);
```

### Message Sending

When sending CCIP messages to ChainID 138:

```solidity
router.ccipSend(138, message);
```

---

## Verification

To verify the selector is correct:

1. **Uniqueness**: Ensure no other chain uses selector `138`
2. **Consistency**: Use the same selector across all configurations
3. **Documentation**: Document the selector in all relevant files

### Verification Script

Run the calculation script to verify:

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

---

## Integration with Other Chains

When configuring bidirectional bridges, ensure:

1. **ChainID 138 → Ethereum Mainnet**:
   - Source selector: `138`
   - Destination selector: `5009297550715157269` (Ethereum Mainnet)

2. **Ethereum Mainnet → ChainID 138**:
   - Source selector: `5009297550715157269`
   - Destination selector: `138`

---

## Chain Selector Reference Table

| Network | Chain ID | CCIP Chain Selector |
|---------|----------|---------------------|
| Ethereum Mainnet | 1 | `5009297550715157269` |
| BSC | 56 | `11344663589394136015` |
| Polygon | 137 | `4051577828743386545` |
| Avalanche | 43114 | `6433500567565415381` |
| Base | 8453 | `15971525489660198786` |
| Arbitrum | 42161 | `4949039107694359620` |
| Optimism | 10 | `3734403246176062136` |
| **ChainID 138** | **138** | **`138`** (or `0x000000000000008a`) |

---

## Notes

- For custom CCIP implementations, you have flexibility in selector format
- The selector must be consistent across all contracts and configurations
- If integrating with Chainlink's official CCIP, you may need to register the chain
- The selector is used in all CCIP message routing and bridge configurations

---

## Related Files

- `scripts/ccip/calculate-chain-selector.sh` - Calculation script
- `docs/ETH_WETH_CHAINLINK_GUIDE.md` - Main guide with selector reference
- `docs/deployment/ENV_EXAMPLE_CONTENT.md` - Environment variable examples

---

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

