# All Mainnets Deployment Runbook — CCIP, Trustless Bridge, Oracle, AMB, Mapper, PMM (Anchored to ChainID 138)

This runbook ensures deployment of **CCIP Bridge**, **Trustless Bridge**, **Oracle**, **AMB Messaging** (relay), **Mapper**, and **PMM Liquidity Pools** across all mainnets, anchored to ChainID 138.

---

## 1. Prerequisites

- **One-shot check:** Run `scripts/deployment/ensure-prerequisites.sh` from `smom-dbis-138` to validate .env and deps (see [PREREQUISITES.md](./PREREQUISITES.md)).
- `.env` with `PRIVATE_KEY`, `RPC_URL_138`, `ETHEREUM_MAINNET_RPC`, and per-chain RPC/CCIP vars (see below).
- **Dependencies (CCIPLogger):** From `smom-dbis-138` run `npm install --legacy-peer-deps` so Hardhat is available locally.
- Sufficient gas token on each target chain (run `scripts/deployment/check-balances-gas-and-deploy.sh`).
- For **LINK funding:** deployer must have LINK on each chain. For **PMM:** set `DODO_VENDING_MACHINE_ADDRESS` on Chain 138 when available.
- Verify CCIP router/LINK/selector per chain from [Chainlink CCIP Supported Networks](https://docs.chain.link/ccip/supported-networks) if you add new chains.

---

## 2. .env Updates (Completed)

- **Ethereum Mainnet**: `MAINNET_CCIP_WETH9_BRIDGE`, `MAINNET_CCIP_WETH10_BRIDGE`, `CCIP_ETH_*`, `ETH_MAINNET_SELECTOR`, `WETH9_MAINNET`, `WETH10_MAINNET`.
- **Avalanche, Arbitrum, Cronos**: `CCIPWETH9_BRIDGE_*`, `CCIPWETH10_BRIDGE_*` updated to latest deployed addresses.
- **BSC, Polygon, Base, Optimism, Gnosis**: `CCIP_*_ROUTER`, `CCIP_*_LINK_TOKEN`, `*_SELECTOR` added for DeployAll.

---

## 3. Deployment Matrix (Anchored to ChainID 138)

| Component | Chain 138 | Ethereum | BSC | Polygon | Base | Arbitrum | Optimism | Avalanche | Cronos | Gnosis |
|-----------|-----------|----------|-----|---------|------|----------|----------|-----------|--------|--------|
| **CCIP Bridge** (WETH9/WETH10) | ✅ | ✅ | script | script | script | ✅ | script | ✅ | ✅ | script |
| **Trustless Bridge** | Lockbox | Bond/Challenge/LP/Inbox/Swap/Coord | — | — | — | — | — | — | — | — |
| **Oracle** | ✅ | optional | optional | optional | optional | optional | optional | optional | ✅ | optional |
| **AMB / Relay** | Source (router) | Relay Router+Bridge | — | — | — | — | — | — | — | — |
| **Mapper** | ✅ AddressMapper | AddressMapperEmpty | script | script | script | script | script | script | script | script |
| **PMM Liquidity Pools** | ✅ DODO (138) | — | — | — | — | — | — | — | — | — |

- **CCIP**: DeployAll.s.sol per chain. Chain 138 and Ethereum use existing WETH/bridges; other chains deploy or reuse WETH+bridges.
- **Trustless Bridge**: DeployTrustlessBridge.s.sol on **Chain 138** (Lockbox) and **Ethereum** (BondManager, ChallengeManager, LiquidityPool, InboxETH, SwapRouter, BridgeSwapCoordinator). Other mainnets not in scope for trustless bridge.
- **Oracle**: DeployOracle.s.sol — primary on Chain 138; optional on others if needed for price feeds.
- **AMB Messaging**: Relay (138→Ethereum) = CCIP Relay Router + Relay Bridge on Mainnet; source = Chain 138 CCIP router. Already deployed per `services/relay/README.md`.
- **Mapper**: DeployAddressMapper.s.sol on **Chain 138** (with mappings); DeployAddressMapperOtherChain.s.sol on **other mainnets** (empty mapper for address mapping layer).
- **PMM Liquidity Pools**: DODOPMMIntegration + pool creation on **Chain 138** (anchored to 138). Other chains use DEX integrations as configured.

---

## 4. Run Order (High Level)

1. **Chain 138** (anchor): Core (01_DeployCore, 02_DeployBridges), Oracle, AddressMapper, ReserveSystem, DODO PMM Integration, Trustless Lockbox.
2. **Ethereum Mainnet**: CCIP bridges (done), Trustless (BondManager, ChallengeManager, LP, Inbox, SwapRouter, Coordinator), Relay Router+Bridge, optional Oracle, Mapper (empty).
3. **Other mainnets** (BSC, Polygon, Base, Optimism, Avalanche, Cronos, Gnosis): DeployAll.s.sol (CCIP bridges + existing WETH where set), then DeployAddressMapperOtherChain.s.sol per chain.

---

## 5. Commands Reference

### 5.1 CCIP Bridges (all mainnets with CCIP config in .env)

```bash
cd /home/intlc/projects/proxmox/smom-dbis-138
set +u && source .env && set -u 2>/dev/null || true
[[ -f scripts/lib/infura.sh ]] && source scripts/lib/infura.sh

# RPC with Infura Basic Auth when secret set
for chain in BSC:56 POLYGON:137 BASE:8453 OPTIMISM:10 GNOSIS:100; do
  name="${chain%:*}"; id="${chain#*:}"
  rpc_var="${name}_RPC_URL"; rpc_var="${rpc_var/BNB/MAINNET}"
  [ "$name" = "BSC" ] && rpc_var="BSC_RPC_URL"
  [ "$name" = "POLYGON" ] && rpc_var="POLYGON_MAINNET_RPC"
  [ "$name" = "BASE" ] && rpc_var="BASE_MAINNET_RPC"
  [ "$name" = "OPTIMISM" ] && rpc_var="OPTIMISM_MAINNET_RPC"
  [ "$name" = "GNOSIS" ] && rpc_var="GNOSIS_MAINNET_RPC"
  rpc="${!rpc_var}"
  type ensure_infura_rpc_url &>/dev/null && rpc=$(ensure_infura_rpc_url "$rpc")
  [ -z "$rpc" ] && continue
  echo "DeployAll to $name (chain $id)..."
  forge script script/DeployAll.s.sol:DeployAll --rpc-url "$rpc" --chain-id "$id" --private-key "$PRIVATE_KEY" --broadcast --slow -vvv
done
```

### 5.2 Trustless Bridge (Chain 138 + Ethereum)

```bash
# Chain 138: Lockbox
forge script script/bridge/trustless/DeployTrustlessBridge.s.sol:DeployTrustlessBridge \
  --rpc-url "$RPC_URL_138" --broadcast --via-ir --private-key "$PRIVATE_KEY"

# Ethereum: BondManager, ChallengeManager, LiquidityPool, InboxETH, SwapRouter, BridgeSwapCoordinator
MAINNET_RPC="${ETHEREUM_MAINNET_RPC}"; type ensure_infura_rpc_url &>/dev/null && MAINNET_RPC=$(ensure_infura_rpc_url "$MAINNET_RPC")
forge script script/bridge/trustless/DeployTrustlessBridge.s.sol:DeployTrustlessBridge \
  --rpc-url "$MAINNET_RPC" --broadcast --via-ir --private-key "$PRIVATE_KEY" --verify --etherscan-api-key "$ETHERSCAN_API_KEY"
```

### 5.3 Oracle (Chain 138 first; optional others)

```bash
forge script script/DeployOracle.s.sol:DeployOracle \
  --rpc-url "$RPC_URL_138" --broadcast --private-key "$PRIVATE_KEY" --with-gas-price 1000000000 --legacy
```

### 5.4 Mapper (Chain 138 = full; others = empty)

```bash
# Chain 138 (full AddressMapper with mappings)
forge script script/DeployAddressMapper.s.sol:DeployAddressMapper \
  --rpc-url "$RPC_URL_138" --broadcast --private-key "$PRIVATE_KEY" --with-gas-price 1000000000 --legacy

# Other mainnets (AddressMapperEmpty)
for chain in ETHEREUM:1 BSC:56 POLYGON:137 BASE:8453 ARBITRUM:42161 OPTIMISM:10 AVALANCHE:43114 CRONOS:25 GNOSIS:100; do
  name="${chain%:*}"; id="${chain#*:}"
  rpc_var="${name}_MAINNET_RPC"; [ "$name" = "ETHEREUM" ] && rpc_var="ETHEREUM_MAINNET_RPC"
  [ "$name" = "BSC" ] && rpc_var="BSC_RPC_URL"; [ "$name" = "AVALANCHE" ] && rpc_var="AVALANCHE_RPC_URL"
  [ "$name" = "ARBITRUM" ] && rpc_var="ARBITRUM_MAINNET_RPC"; [ "$name" = "CRONOS" ] && rpc_var="CRONOS_RPC_URL"
  rpc="${!rpc_var}"; type ensure_infura_rpc_url &>/dev/null && rpc=$(ensure_infura_rpc_url "$rpc")
  [ -z "$rpc" ] && continue
  forge script script/DeployAddressMapperOtherChain.s.sol:DeployAddressMapperOtherChain \
    --rpc-url "$rpc" --chain-id "$id" --broadcast --private-key "$PRIVATE_KEY"
done
```

### 5.5 PMM Liquidity Pools (Chain 138)

```bash
# Requires: DODO_VENDING_MACHINE_ADDRESS, COMPLIANT_USDT_ADDRESS, COMPLIANT_USDC_ADDRESS (or equiv) in .env
forge script script/dex/DeployDODOPMMIntegration.s.sol:DeployDODOPMMIntegration \
  --rpc-url "$RPC_URL_138" --broadcast --private-key "$PRIVATE_KEY" --with-gas-price 1000000000 --legacy

# Create pools (e.g. XAU-anchored): set DODOPMM_INTEGRATION_ADDRESS, XAU_ADDRESS_138
forge script script/dex/DeployPrivatePoolRegistryAndPools.s.sol:DeployPrivatePoolRegistryAndPools \
  --rpc-url "$RPC_URL_138" --broadcast --private-key "$PRIVATE_KEY" --with-gas-price 1000000000 --legacy
```

### 5.6 AMB / Relay (Ethereum Mainnet)

- Relay Router and Relay Bridge are already deployed (see `CCIP_RELAY_ROUTER_MAINNET`, `CCIP_RELAY_BRIDGE_MAINNET` in .env).
- To redeploy: `forge script script/DeployCCIPRelay.s.sol:DeployCCIPRelay --rpc-url $ETHEREUM_MAINNET_RPC --broadcast --legacy --via-ir`

---

## 6. Next Steps Checklist

- [x] Run balance check: `DEPLOYER_ADDRESS=0x4A66... bash scripts/deployment/check-balances-gas-and-deploy.sh`
- [x] Deploy CCIP to BSC, Polygon, Base, Optimism, Gnosis (DeployAll.s.sol)
- [x] Run Trustless Bridge on Chain 138 + Ethereum: `bash scripts/deployment/deploy-all-mainnets-with-mapper-oracle-pmm.sh trustless` (Lockbox 138: 0xC3a91d46…; Mainnet: BondManager, ChallengeManager, LP, Inbox, SwapRouter, Coordinator — update .env)
- [x] Deploy Oracle on Chain 138 (Aggregator: 0x452a4701..., Proxy: 0x404DcD22...)
- [x] Deploy Mapper: AddressMapper on 138 (0xe48E3f24...); AddressMapperEmpty on Ethereum, BSC, Polygon, Base, Arbitrum, Optimism, Avalanche, Cronos, Gnosis
- [ ] Deploy DODO PMM Integration + pools on Chain 138 (see **Remaining gaps: PMM** below)
- [x] Update config/smart-contracts-master.json with mapper addresses per chain (and Oracle on 138)
- [x] Configure bridge destinations (addDestination): Step B done for all mainnets (Ethereum, BSC, Polygon, Base, Optimism, Arbitrum, Avalanche, Cronos, Gnosis); Step A (138 → mainnets) uses `--gas-limit` in script (see **CCIP_BRIDGE_DESTINATIONS_AND_LINK_FUNDING.md**)
- [ ] Fund bridges with LINK on each chain (see **CCIP_BRIDGE_DESTINATIONS_AND_LINK_FUNDING.md**)
- [ ] Deploy CCIPLogger per chain if required (see **Remaining gaps: CCIPLogger** below)

### Remaining gaps: PMM and CCIPLogger

- **PMM (Chain 138):** The `pmm` phase of `deploy-all-mainnets-with-mapper-oracle-pmm.sh` is skipped unless `DODO_VENDING_MACHINE_ADDRESS` (and related DODO/stablecoin env vars) are set in `.env`. Deploy DODO PMM Integration and pools only after setting these; see §5.5 and run the DODO scripts manually.
- **CCIPLogger:** DeployAll.s.sol does **not** deploy CCIPLogger (outputs a Hardhat-based instruction). To have a logger on each mainnet, deploy per chain using the Hardhat script (e.g. `npm run deploy:logger:mainnet` or a chain-specific deployment). Optional for basic bridge operation.

### PMM (Chain 138) completion checklist

1. Set **DODO_VENDING_MACHINE_ADDRESS** in `.env` to the DODO DVM factory adapter address on Chain 138. **Chain 138 is not in DODO’s official list** — see [DVM_DEPLOYMENT_CHECK.md](DVM_DEPLOYMENT_CHECK.md). Use `scripts/deployment/deploy-official-dvm-chain138.sh` to deploy the official DVM `0.6.9` stack from the vendored DODO tree and then deploy the local adapter.
2. Set **OFFICIAL_USDT_ADDRESS** and **OFFICIAL_USDC_ADDRESS** to the live Chain 138 quote-side mirror stables:
   - `OFFICIAL_USDT_ADDRESS=0x004b63A7B5b0E06f6bB6adb4a5F9f590BF3182D1`
   - `OFFICIAL_USDC_ADDRESS=0x71D6687F38b93CCad569Fa6352c876eea967201b`
3. Run: `bash scripts/deployment/deploy-all-mainnets-with-mapper-oracle-pmm.sh pmm` to deploy DODOPMMIntegration.
4. Set **DODOPMM_INTEGRATION_ADDRESS** (or DODO_PMM_INTEGRATION) in .env from the deploy output, then create pools:
   - cUSDT/USDT and cUSDC/USDC: call `createCUSDTUSDTPool` and `createCUSDCUSDCPool` on the integration (see `docs/integration/DODO_PMM_INTEGRATION.md`).
   - XAU-anchored: set **XAU_ADDRESS_138** and run `forge script script/dex/DeployPrivatePoolRegistryAndPools.s.sol:DeployPrivatePoolRegistryAndPools --rpc-url $RPC_URL_138 --broadcast --private-key $PRIVATE_KEY --with-gas-price 1000000000 --legacy`.

### Deployed Mapper Addresses (for config/smart-contracts-master.json)

| Chain ID | Network   | Mapper (AddressMapperEmpty)     |
|----------|-----------|----------------------------------|
| 138      | Chain 138 | 0x439Fcb2d2ab2f890DCcAE50461Fa7d978F9Ffe1A (AddressMapper; legacy dup `0xe48E3f248698610e18Db865457fcd935Bb3da856`) |
| 1        | Ethereum  | 0x0ea68F5B5A8427bB58e54ECcee941F543Dc538c5 |
| 56       | BSC       | 0x6e94e53F73893b2a6784Df663920D31043A6dE07 |
| 137      | Polygon   | 0xb689c1C69DAa08DEb5D8feA2aBF0F64bFD409727 |
| 8453     | Base      | 0x6e94e53F73893b2a6784Df663920D31043A6dE07 |
| 42161    | Arbitrum  | 0xB142e7f4D3Cc0dd231BB1867E815342932431Bb5 |
| 10       | Optimism  | 0xDAB0591e5e89295fFaD75A71Dcfc30c5625C4fA2 |
| 43114    | Avalanche | 0x73376eB92c16977B126dB9112936A20Fa0De3442 |
| 25       | Cronos    | 0x3f8C409C6072a2B6a4Ff17071927bA70F80c725F |
| 100      | Gnosis    | 0x99B3511A2d315A497C8112C1fdd8D508d4B1E506 |

---

## 7. Script: Deploy All Mainnets (Orchestrator)

Use `scripts/deployment/deploy-all-mainnets-with-mapper-oracle-pmm.sh` to run the above in order (see script for exact steps and guards).

## 8. Remaining operator tasks (scripts provided)

| Task | Script / Notes |
|------|----------------|
| Fund bridges with LINK | `scripts/deployment/fund-ccip-bridges-with-link.sh` — use `--link <amount>` (default 10 LINK), `--dry-run` to print only. See [CCIP_BRIDGE_DESTINATIONS_AND_LINK_FUNDING.md](./CCIP_BRIDGE_DESTINATIONS_AND_LINK_FUNDING.md). |
| PMM (Chain 138) | `scripts/deployment/run-pmm-and-pools.sh` — set `DODO_VENDING_MACHINE_ADDRESS` (and official tokens) in .env first; script runs pmm phase then optional XAU pools if `XAU_ADDRESS_138` and `DODOPMM_INTEGRATION_ADDRESS` are set. |
| CCIPLogger | `scripts/deployment/deploy-ccip-logger-all-chains.sh` — from `smom-dbis-138` run after `pnpm install`; deploys to mainnet, BSC, Polygon, Gnosis, Cronos. Optional. |

## 9. Related docs

- **Bridge destinations and LINK funding:** [CCIP_BRIDGE_DESTINATIONS_AND_LINK_FUNDING.md](./CCIP_BRIDGE_DESTINATIONS_AND_LINK_FUNDING.md) — `addDestination` per bridge and LINK funding for CCIP fees.
