# Chain 138 Contract Deployment Guide

**Date**: $(date)  
**Purpose**: Complete guide for deploying smart contracts to Chain 138

---

## 📋 Prerequisites

### 1. Network Readiness

Verify Chain 138 network is ready:

```bash
# Check block production
cast block-number --rpc-url http://192.168.11.250:8545

# Check chain ID
cast chain-id --rpc-url http://192.168.11.250:8545
```

**Expected Results**:
- Block number > 0
- Chain ID = 138

### 2. Environment Setup

Create `.env` file in source project:

```bash
cd /home/intlc/projects/smom-dbis-138
cp .env.example .env  # If exists
```

Required variables:

```bash
# Chain 138 RPC
RPC_URL_138=http://192.168.11.250:8545

# Deployer
PRIVATE_KEY=<your-deployer-private-key>

# Oracle Configuration (deploy Oracle first)
ORACLE_PRICE_FEED=<oracle-price-feed-address>

# Reserve Configuration
RESERVE_ADMIN=<admin-address>
TOKEN_FACTORY=<token-factory-address>  # Optional

# Keeper Configuration
KEEPER_ADDRESS=<keeper-address>  # Address that will execute upkeep
```

### 3. Required Tools

- **Foundry** (forge, cast)
- **jq** (for address extraction)
- **Access to Proxmox** (for service updates)

---

## 🚀 Deployment Methods

### Method 1: Automated Deployment Script

Use the automated script:

```bash
cd /home/intlc/projects/proxmox
./scripts/deploy-contracts-chain138.sh
```

**What it does**:
1. Verifies network readiness
2. Deploys Oracle contract
3. Deploys CCIP Router
4. Deploys CCIP Sender
5. Deploys Keeper (if Oracle Price Feed configured)
6. Logs all deployments

### Method 2: Manual Deployment

Deploy contracts individually:

#### 1. Deploy Oracle

```bash
cd /home/intlc/projects/smom-dbis-138
forge script script/DeployOracle.s.sol:DeployOracle \
  --rpc-url http://192.168.11.250:8545 \
  --private-key $PRIVATE_KEY \
  --broadcast \
  --verify --verifier blockscout --verifier-url https://explorer.d-bis.org/api \
  -vvvv
```

**Note**: ChainID 138 uses Blockscout for contract verification. The `--verify` flag with `--verifier blockscout` enables verification on the self-hosted Blockscout instance at `https://explorer.d-bis.org`.

#### 2. Deploy CCIP Router

```bash
cd /home/intlc/projects/smom-dbis-138
forge script script/DeployCCIPRouter.s.sol:DeployCCIPRouter \
  --rpc-url http://192.168.11.250:8545 \
  --private-key $PRIVATE_KEY \
  --broadcast \
  --verify --verifier blockscout --verifier-url https://explorer.d-bis.org/api \
  -vvvv
```

#### 3. Deploy CCIP Sender

```bash
forge script script/DeployCCIPSender.s.sol:DeployCCIPSender \
  --rpc-url http://192.168.11.250:8545 \
  --private-key $PRIVATE_KEY \
  --broadcast --verify -vvvv
```

#### 4. Deploy Keeper

```bash
# Set Oracle Price Feed address first
export ORACLE_PRICE_FEED=<oracle-price-feed-address>

forge script script/reserve/DeployKeeper.s.sol:DeployKeeper \
  --rpc-url http://192.168.11.250:8545 \
  --private-key $PRIVATE_KEY \
  --broadcast --verify -vvvv
```

#### 5. Deploy Reserve System

```bash
# Set Token Factory address if using
export TOKEN_FACTORY=<token-factory-address>

forge script script/reserve/DeployReserveSystem.s.sol:DeployReserveSystem \
  --rpc-url http://192.168.11.250:8545 \
  --private-key $PRIVATE_KEY \
  --broadcast --verify -vvvv
```

---

## 📝 Extract Contract Addresses

After deployment, extract addresses:

```bash
cd /home/intlc/projects/proxmox
./scripts/extract-contract-addresses.sh 138
```

This creates: `/home/intlc/projects/smom-dbis-138/deployed-addresses-chain138.txt`

**Manual Extraction**:

```bash
cd /home/intlc/projects/smom-dbis-138
LATEST_RUN=$(find broadcast -type d -path "*/138/run-*" | sort -V | tail -1)

# Extract Oracle address
jq -r '.transactions[] | select(.transactionType == "CREATE") | .contractAddress' \
  "$LATEST_RUN/DeployOracle.s.sol/DeployOracle.json" | head -1

# Extract CCIP Router address
jq -r '.transactions[] | select(.transactionType == "CREATE") | .contractAddress' \
  "$LATEST_RUN/DeployCCIPRouter.s.sol/DeployCCIPRouter.json" | head -1
```

---

## ⚙️ Update Service Configurations

After extracting addresses, update service configs:

```bash
cd /home/intlc/projects/proxmox

# Source addresses
source /home/intlc/projects/smom-dbis-138/deployed-addresses-chain138.txt

# Update all services
./scripts/update-service-configs.sh
```

**Manual Update**:

```bash
# Oracle Publisher (VMID 3500)
pct exec 3500 -- bash -c "cat >> /opt/oracle-publisher/.env <<EOF
ORACLE_CONTRACT_ADDRESS=<deployed-address>
EOF"

# CCIP Monitor (VMID 3501)
pct exec 3501 -- bash -c "cat >> /opt/ccip-monitor/.env <<EOF
CCIP_ROUTER_ADDRESS=<deployed-address>
CCIP_SENDER_ADDRESS=<deployed-address>
EOF"

# Keeper (VMID 3502)
pct exec 3502 -- bash -c "cat >> /opt/keeper/.env <<EOF
PRICE_FEED_KEEPER_ADDRESS=<deployed-address>
EOF"
```

---

## ✅ Verification

### 1. Verify Contracts on Chain

```bash
# Check contract code
cast code <contract-address> --rpc-url http://192.168.11.250:8545

# Check contract balance
cast balance <contract-address> --rpc-url http://192.168.11.250:8545
```

### 2. Verify Service Connections

```bash
# Test Oracle Publisher
pct exec 3500 -- curl -X POST http://localhost:8000/health

# Test CCIP Monitor
pct exec 3501 -- curl -X POST http://localhost:8000/health

# Test Keeper
pct exec 3502 -- curl -X POST http://localhost:3000/health
```

### 3. Check Service Logs

```bash
# Oracle Publisher
pct exec 3500 -- journalctl -u oracle-publisher -f

# CCIP Monitor
pct exec 3501 -- journalctl -u ccip-monitor -f

# Keeper
pct exec 3502 -- journalctl -u price-feed-keeper -f
```

---

## 📊 Deployment Checklist

- [ ] Network producing blocks (block number > 0)
- [ ] Chain ID verified (138)
- [ ] Deployer account has sufficient balance
- [ ] `.env` file configured with PRIVATE_KEY
- [ ] Oracle contract deployed
- [ ] CCIP Router deployed
- [ ] CCIP Sender deployed
- [ ] Keeper deployed (if using)
- [ ] Reserve System deployed (if using)
- [ ] Contract addresses extracted
- [ ] Service .env files updated
- [ ] Services restarted
- [ ] Service health checks passing

---

## 🔧 Troubleshooting

### Network Not Ready

**Error**: `Network is not producing blocks yet`

**Solution**:
- Wait for validators to initialize
- Check validator logs: `pct exec <vmid> -- journalctl -u besu -f`
- Verify network connectivity

### Deployment Fails

**Error**: `insufficient funds` or `nonce too low`

**Solution**:
- Check deployer balance: `cast balance <deployer-address> --rpc-url http://192.168.11.250:8545`
- Check nonce: `cast nonce <deployer-address> --rpc-url http://192.168.11.250:8545`
- Ensure sufficient balance for gas

### Contract Address Not Found

**Error**: Address extraction returns empty

**Solution**:
- Check broadcast files: `ls -la broadcast/*/138/run-*/`
- Verify deployment succeeded (check logs)
- Manually extract from broadcast JSON files

---

## 📚 Related Documentation

- [Source Project Contract Deployment Info](./SOURCE_PROJECT_CONTRACT_DEPLOYMENT_INFO.md)
- [Deployed Smart Contracts Inventory](./DEPLOYED_SMART_CONTRACTS_INVENTORY.md)
- [Smart Contract Connections & Next LXCs](./SMART_CONTRACT_CONNECTIONS_AND_NEXT_LXCS.md)

---

**Last Updated**: $(date)

