# Deployment Automation Guide

Complete guide for using the automated deployment scripts.

## Overview

All deployment tasks have been automated with phase-specific scripts. You can run them individually or use the orchestration script to deploy everything.

## Quick Start

### Option 1: Deploy All Phases (Interactive)

```bash
cd /home/intlc/projects/proxmox/smom-dbis-138
./scripts/deployment/deploy-all-phases.sh
```

This script will:
- Guide you through each phase
- Prompt for confirmation before each deployment
- Pause for you to update `.env` with contract addresses
- Verify each phase before proceeding

### Option 2: Deploy Phases Individually

```bash
# Phase 1: Environment Setup
./scripts/deployment/phase1-env-setup.sh

# Phase 2: Deploy Core Contracts
./scripts/deployment/phase2-deploy-core.sh

# Phase 3: Deploy Enhanced Router
./scripts/deployment/phase3-deploy-router.sh

# Phase 4: Deploy Integration Contracts
./scripts/deployment/phase4-deploy-integration.sh

# Phase 5: Initialize System
./scripts/deployment/phase5-initialize.sh

# Phase 6: Provide Liquidity
./scripts/deployment/phase6-provide-liquidity.sh

# Phase 7: Configure Access Control
./scripts/deployment/phase7-configure.sh

# Phase 8: Deploy Backend Services
./scripts/deployment/phase8-deploy-services.sh

# Phase 9: Deploy Frontend
./scripts/deployment/phase9-deploy-frontend.sh

# Phase 10: Verify Deployment
./scripts/deployment/phase10-verify.sh
```

## Prerequisites

1. **Foundry installed**: Required for contract deployment
   ```bash
   curl -L https://foundry.paradigm.xyz | bash
   foundryup
   ```

2. **Docker installed**: Required for backend services
   ```bash
   # Install Docker (varies by OS)
   ```

3. **Node.js installed**: Required for frontend builds
   ```bash
   # Install Node.js 20+
   ```

4. **Environment file**: Create `.env` from template
   ```bash
   cp .env.template .env
   # Edit .env with your values
   ```

## Phase Details

### Phase 1: Environment Setup
- Creates `.env` file from template if missing
- Verifies all required environment variables
- Checks RPC endpoint accessibility
- Verifies ETH balance

**Requirements:**
- `.env.template` file
- Foundry installed (for RPC verification)

### Phase 2: Deploy Core Contracts
- Deploys Lockbox on ChainID 138
- Deploys core contracts on Ethereum Mainnet:
  - BondManager
  - ChallengeManager
  - LiquidityPoolETH
  - InboxETH
  - SwapRouter
  - BridgeSwapCoordinator
- Verifies contracts on Etherscan

**Requirements:**
- All Phase 1 requirements met
- Sufficient ETH for gas
- Etherscan API key

**After completion:**
- Update `.env` with all deployed contract addresses

### Phase 3: Deploy Enhanced Router
- Deploys EnhancedSwapRouter
- Configures default routing logic
- Verifies contract on Etherscan

**Requirements:**
- Phase 2 completed
- Core contract addresses in `.env`

**After completion:**
- Update `.env` with `ENHANCED_SWAP_ROUTER` address
- Optionally configure Balancer pool IDs

### Phase 4: Deploy Integration Contracts
- Deploys StablecoinPegManager
- Deploys CommodityPegManager
- Deploys ISOCurrencyManager
- Deploys BridgeReserveCoordinator
- Initializes peg managers with default assets

**Requirements:**
- Phase 2 completed
- `BRIDGE_SWAP_COORDINATOR` and `RESERVE_SYSTEM` in `.env`

**After completion:**
- Update `.env` with all integration contract addresses

### Phase 5: Initialize System
- Configures EnhancedSwapRouter roles
- Configures BridgeSwapCoordinator
- Configures BridgeReserveCoordinator

**Requirements:**
- Phases 2, 3, 4 completed
- All contract addresses in `.env`

### Phase 6: Provide Initial Liquidity
- Provides liquidity to LiquidityPoolETH (default: 100 ETH)
- Funds ReserveSystem with USDT (default: 100k USDT)

**Requirements:**
- Phase 2 completed
- Sufficient ETH and USDT balance

**Configuration:**
- Set `LIQUIDITY_AMOUNT` in `.env` (default: 100)
- Set `RESERVE_AMOUNT` in `.env` (default: 100000)

### Phase 7: Configure Access Control
- Grants COORDINATOR_ROLE to BridgeSwapCoordinator
- Documents routing configuration

**Requirements:**
- Phases 3 and 5 completed

### Phase 8: Deploy Backend Services
- Creates Docker network
- Deploys all backend services:
  - Liquidity Engine Service
  - Market Reporting Service
  - Bridge Reserve Service
  - ISO Currency Service

**Requirements:**
- Docker installed
- Service directories exist
- Service environment variables configured

### Phase 9: Deploy Frontend
- Builds Frontend DApp
- Builds Admin Dashboard

**Requirements:**
- Node.js installed
- Frontend directories exist

**After completion:**
- Deploy `dist/` directories to hosting provider

### Phase 10: Verification
- Runs verification script
- Checks contract codes
- Verifies service status
- Provides end-to-end test instructions

**Requirements:**
- All previous phases completed

## Environment Variables

All scripts read from `.env` file. Required variables:

```bash
# Deployment
PRIVATE_KEY=0x...
ETHEREUM_MAINNET_RPC=https://...
RPC_URL_138=http://...
ETHERSCAN_API_KEY=...

# Contract Addresses (populated during deployment)
BOND_MANAGER=0x...
CHALLENGE_MANAGER=0x...
LIQUIDITY_POOL=0x...
# ... etc
```

See `.env.template` for complete list.

## Troubleshooting

### Script Fails with "Command not found"
- Ensure Foundry is installed: `foundryup`
- Ensure Docker is installed and running
- Ensure Node.js is installed

### Contract Deployment Fails
- Check ETH balance (need 5-10 ETH)
- Verify RPC endpoint is accessible
- Check gas prices
- Review contract compilation errors

### Service Deployment Fails
- Ensure Docker is running: `docker ps`
- Check Docker network exists: `docker network ls`
- Review service logs: `docker logs <service-name>`

### Environment Variable Issues
- Ensure `.env` file exists
- Check all required variables are set
- Verify no typos in variable names

## Next Steps

After successful deployment:

1. **Set up monitoring**: Configure dashboards and alerts
2. **Train team**: Conduct operations training
3. **Test thoroughly**: Run end-to-end tests
4. **Begin operations**: Start bridge operations

## Support

For issues:
1. Check script logs
2. Review `DEPLOYMENT_GUIDE.md`
3. Check `OPERATIONS_GUIDE.md`
4. Review service logs

