# Deployment Scripts

This directory contains deployment automation scripts for ChainID 138.

## Scripts

### `deploy-all.sh`
Complete deployment automation script that orchestrates all deployment steps.

**Usage**:
```bash
./scripts/deployment/deploy-all.sh [options]
```

**Options**:
- `--skip-infrastructure`: Skip infrastructure deployment
- `--skip-kubernetes`: Skip Kubernetes deployment
- `--skip-blockscout`: Skip Blockscout deployment
- `--skip-contracts`: Skip contract deployment
- `--skip-cloudflare`: Skip Cloudflare DNS configuration
- `--skip-token-list`: Skip token list update

**Example**:
```bash
# Deploy everything
./scripts/deployment/deploy-all.sh

# Deploy only contracts
./scripts/deployment/deploy-all.sh \
  --skip-infrastructure \
  --skip-kubernetes \
  --skip-blockscout \
  --skip-cloudflare
```

### `cloudflare-dns.sh`
Configures Cloudflare DNS records for d-bis.org domain.

**Usage**:
```bash
./scripts/deployment/cloudflare-dns.sh \
  --zone-id <ZONE_ID> \
  --api-token <API_TOKEN> \
  --ip <IP_ADDRESS> \
  [--domain <DOMAIN>]
```

**Example**:
```bash
./scripts/deployment/cloudflare-dns.sh \
  --zone-id abc123def456 \
  --api-token your-api-token \
  --ip 1.2.3.4
```

### `update-token-list.sh`
Updates token-list.json with deployed contract addresses.

**Usage**:
```bash
./scripts/deployment/update-token-list.sh
```

**Requirements**:
- `contracts-deployed.json` file must exist
- Contract addresses must be in the file

### `verify-deployment.sh`
Comprehensive deployment verification script.

**Usage**:
```bash
./scripts/deployment/verify-deployment.sh
```

**Checks**:
- RPC endpoint accessibility
- Blockscout explorer accessibility
- Contract deployments
- Kubernetes resources
- MetaMask integration files
- DNS configuration

### `submit-ethereum-lists-pr.sh`
Automates the creation of a PR to ethereum-lists/chains.

**Usage**:
```bash
./scripts/deployment/submit-ethereum-lists-pr.sh
```

**Requirements**:
- GitHub CLI (`gh`) installed and authenticated
- Fork of ethereum-lists/chains repository

### `submit-token-list.sh`
Provides instructions for submitting token list to aggregators.

**Usage**:
```bash
./scripts/deployment/submit-token-list.sh
```

**Output**:
- Submission instructions for CoinGecko
- Submission instructions for Uniswap
- Submission instructions for Token Lists aggregator
- Submission report file

### WETH Contract Deployment Scripts

#### `deploy-weth.sh`
Deploys WETH9 contract to ChainID 138.

**Usage**:
```bash
export RPC_URL="https://rpc.d-bis.org"
export PRIVATE_KEY="your-private-key"
./scripts/deployment/deploy-weth.sh
```

#### `deploy-weth10.sh`
Deploys WETH10 contract to ChainID 138.

**Usage**:
```bash
export RPC_URL="https://rpc.d-bis.org"
export PRIVATE_KEY="your-private-key"
./scripts/deployment/deploy-weth10.sh
```

#### `deploy-weth-with-ccip.sh`
Deploys all WETH contracts (WETH9, WETH10) and CCIP bridges in a single transaction.

**Usage**:
```bash
export RPC_URL="https://rpc.d-bis.org"
export PRIVATE_KEY="your-private-key"
export CCIP_ROUTER="0x..."
export CCIP_FEE_TOKEN="0x..."  # LINK token address

# Optional: Configure what to deploy
export DEPLOY_WETH9="true"
export DEPLOY_WETH10="true"
export DEPLOY_BRIDGES="true"

# Optional: Use existing WETH addresses instead of deploying
export WETH9_ADDRESS="0x..."  # Optional
export WETH10_ADDRESS="0x..."  # Optional

./scripts/deployment/deploy-weth-with-ccip.sh
```

### CCIP Bridge Deployment Scripts

#### `deploy-ccip-weth9-bridge.sh`
Deploys CCIPWETH9Bridge for cross-chain WETH9 transfers.

**Usage**:
```bash
export RPC_URL="https://rpc.d-bis.org"
export PRIVATE_KEY="your-private-key"
export CCIP_ROUTER="0x..."
export CCIP_FEE_TOKEN="0x..."  # LINK token address
export WETH9_ADDRESS="0x..."  # WETH9 address (defaults to mainnet address)

./scripts/deployment/deploy-ccip-weth9-bridge.sh
```

#### `deploy-ccip-weth10-bridge.sh`
Deploys CCIPWETH10Bridge for cross-chain WETH10 transfers.

**Usage**:
```bash
export RPC_URL="https://rpc.d-bis.org"
export PRIVATE_KEY="your-private-key"
export CCIP_ROUTER="0x..."
export CCIP_FEE_TOKEN="0x..."  # LINK token address
export WETH10_ADDRESS="0x..."  # WETH10 address (defaults to mainnet address)

./scripts/deployment/deploy-ccip-weth10-bridge.sh
```

### Bridge Configuration Scripts

#### `configure-weth9-bridge.sh`
Provides instructions for configuring CCIPWETH9Bridge destinations.

**Usage**:
```bash
# Ensure .env file contains bridge addresses
./scripts/deployment/configure-weth9-bridge.sh
```

#### `configure-weth10-bridge.sh`
Provides instructions for configuring CCIPWETH10Bridge destinations.

**Usage**:
```bash
# Ensure .env file contains bridge addresses
./scripts/deployment/configure-weth10-bridge.sh
```

## Prerequisites

### Required Tools
- `az` - Azure CLI (must be authenticated with `az login`)
- `terraform` - Terraform
- `kubectl` - Kubernetes CLI
- `helm` - Helm
- `forge` - Foundry
- `cast` - Foundry
- `jq` - JSON processor
- `curl` - HTTP client
- `gh` - GitHub CLI (for PR submission)

### Azure Authentication

**Important**: Azure CLI must be authenticated before running deployment scripts.

#### For WSL Users

1. **Install Azure CLI** (if not already installed):
   ```bash
   curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
   ```

2. **Login to Azure**:
   ```bash
   az login
   ```
   This will open a browser window for authentication.

3. **Verify login**:
   ```bash
   az account show
   ```

4. **Set subscription** (if needed):
   ```bash
   az account set --subscription <subscription-id>
   ```

#### Using the Azure Login Helper Script

```bash
# Interactive login (opens browser)
./scripts/deployment/azure-login.sh interactive

# Service principal login
./scripts/deployment/azure-login.sh service-principal

# Managed identity login (for Azure VM/Container)
./scripts/deployment/azure-login.sh managed-identity
```

#### Service Principal Authentication

For CI/CD or automated deployments, use service principal:

```bash
az login --service-principal \
  --username <app-id> \
  --password <app-secret> \
  --tenant <tenant-id>
```

Set these in your `.env` file:
- `AZURE_CLIENT_ID` - Service principal app ID
- `AZURE_CLIENT_SECRET` - Service principal secret
- `AZURE_TENANT_ID` - Azure tenant ID
- `AZURE_SUBSCRIPTION_ID` - Azure subscription ID

### Required Environment Variables
- `AZURE_SUBSCRIPTION_ID`
- `AZURE_TENANT_ID`
- `AZURE_CLIENT_ID`
- `AZURE_CLIENT_SECRET`
- `AZURE_RESOURCE_GROUP`
- `CLOUDFLARE_API_TOKEN`
- `CLOUDFLARE_ZONE_ID`
- `PRIVATE_KEY`
- `RPC_URL`
- `EXPLORER_URL`

## Deployment Workflow

### 1. Initial Setup
```bash
# Create .env file
cp .env.example .env
# Edit .env with your values

# Authenticate with Azure (required for infrastructure/Kubernetes/Cloudflare tasks)
# For WSL users:
az login

# Or use the helper script:
./scripts/deployment/azure-login.sh

# Verify authentication
az account show

# Verify prerequisites
./scripts/deployment/deploy-all.sh --help
```

### 2. Deploy Infrastructure
```bash
# Deploy Azure infrastructure
./scripts/deployment/deploy-all.sh \
  --skip-kubernetes \
  --skip-blockscout \
  --skip-contracts \
  --skip-cloudflare
```

### 3. Configure DNS
```bash
# Get Application Gateway IP
APP_GATEWAY_IP=$(az network application-gateway show ...)

# Configure Cloudflare DNS
./scripts/deployment/cloudflare-dns.sh \
  --zone-id $CLOUDFLARE_ZONE_ID \
  --api-token $CLOUDFLARE_API_TOKEN \
  --ip $APP_GATEWAY_IP
```

### 4. Deploy Kubernetes
```bash
# Deploy Kubernetes resources
./scripts/deployment/deploy-all.sh \
  --skip-infrastructure \
  --skip-blockscout \
  --skip-contracts \
  --skip-cloudflare
```

### 5. Deploy Blockscout
```bash
# Deploy Blockscout
./scripts/deployment/deploy-all.sh \
  --skip-infrastructure \
  --skip-kubernetes \
  --skip-contracts \
  --skip-cloudflare
```

### 6. Deploy Contracts
```bash
# Deploy contracts
./scripts/deployment/deploy-all.sh \
  --skip-infrastructure \
  --skip-kubernetes \
  --skip-blockscout \
  --skip-cloudflare
```

### 7. Update Token List
```bash
# Update token list
./scripts/deployment/update-token-list.sh
```

### 8. Verify Deployment
```bash
# Verify deployment
./scripts/deployment/verify-deployment.sh
```

## Troubleshooting

### Common Issues

#### Terraform Errors
- Check Azure credentials
- Verify resource group exists
- Check Terraform state

#### Kubernetes Errors
- Verify kubectl is configured
- Check AKS cluster is accessible
- Verify namespace exists

#### Contract Deployment Errors
- Check RPC URL is accessible
- Verify private key is correct
- Check account has sufficient balance

#### DNS Errors
- Verify Cloudflare credentials
- Check DNS zone exists
- Wait for DNS propagation

## Support

For issues or questions:
- Review deployment logs
- Check troubleshooting guide
- Open an issue on GitHub

