# Complete Deployment Guide

## Overview

This guide covers the complete deployment process for the DeFi Oracle Meta Mainnet (ChainID 138), including blockchain infrastructure and smart contracts.

## Prerequisites

1. **Azure CLI** installed and authenticated
2. **Terraform** >= 1.0 installed
3. **kubectl** configured for AKS
4. **Helm** 3.x installed
5. **Foundry** (forge, cast, anvil) installed
6. **.env** file configured with required variables

## Deployment Order

The deployment follows this order:

1. **Blockchain Infrastructure** (Azure/Kubernetes)
2. **Smart Contracts** (in proper dependency order)

## Step 1: Check Deployment Status

First, check the current deployment status:

```bash
./scripts/deployment/check-deployment-status.sh
```

This will show:
- Current contract deployment status
- Infrastructure status
- Missing configuration

## Step 2: Deploy Blockchain Infrastructure (if needed)

If the blockchain infrastructure is not deployed:

### Option A: Deploy to Azure/Kubernetes (Production)

1. **Deploy Infrastructure with Terraform:**
   ```bash
   cd terraform
   terraform init
   terraform plan
   terraform apply
   ```

2. **Get AKS Credentials:**
   ```bash
   az aks get-credentials --resource-group az-p-we-rg-comp-001 --name az-p-we-aks-main
   ```

3. **Deploy Kubernetes Resources:**
   ```bash
   kubectl apply -f k8s/base/namespace.yaml
   helm install besu-validators ./helm/besu-network -f helm/besu-network/values-validators.yaml -n besu-network
   helm install besu-sentries ./helm/besu-network -f helm/besu-network/values-sentries.yaml -n besu-network
   helm install besu-rpc ./helm/besu-network -f helm/besu-network/values-rpc.yaml -n besu-network
   ```

4. **Get RPC URL:**
   - After deployment, get the RPC endpoint from the Application Gateway
   - Update `.env` with `RPC_URL`

### Option B: Start Local Testnet (Development/Testing)

For local testing, start an Anvil testnet:

```bash
./scripts/deployment/start-local-testnet.sh
```

This will:
- Start Anvil testnet on port 8545
- Set Chain ID to 138
- Update `.env` with `RPC_URL=http://localhost:8545`
- Prefund test accounts

## Step 3: Deploy Smart Contracts

Deploy all contracts in proper order:

```bash
./scripts/deployment/deploy-all-ordered.sh
```

This script will:

1. **Check RPC endpoint** - Verify blockchain is accessible
2. **Deploy Mock LINK Token** (if not configured)
3. **Deploy CCIP Router** - Cross-chain message router
4. **Deploy WETH9** - Standard WETH implementation
5. **Deploy WETH10** - Enhanced WETH with flash loans
6. **Deploy CCIPWETH9Bridge** - Cross-chain WETH9 bridge
7. **Deploy CCIPWETH10Bridge** - Cross-chain WETH10 bridge
8. **Deploy Oracle Aggregator** - Oracle price feed aggregator
9. **Update .env file** - Save all deployed addresses

### Manual Deployment (Alternative)

If you prefer to deploy contracts manually:

#### 1. Deploy Mock LINK Token (if needed)

```bash
forge script script/DeployMockLinkToken.s.sol:DeployMockLinkToken \
    --rpc-url $RPC_URL \
    --broadcast \
    --private-key $PRIVATE_KEY \
    -vvv
```

Update `.env`:
```bash
CCIP_FEE_TOKEN=<deployed_address>
```

#### 2. Deploy CCIP Router

```bash
forge script script/DeployCCIPRouter.s.sol:DeployCCIPRouter \
    --sig "run(address,uint256,uint256)" \
    $CCIP_FEE_TOKEN \
    1000000000000000 \
    1000000000 \
    --rpc-url $RPC_URL \
    --broadcast \
    --private-key $PRIVATE_KEY \
    -vvv
```

Update `.env`:
```bash
CCIP_ROUTER=<deployed_address>
```

#### 3. Deploy WETH9

```bash
forge script script/DeployWETH.s.sol:DeployWETH \
    --rpc-url $RPC_URL \
    --broadcast \
    --private-key $PRIVATE_KEY \
    -vvv
```

Update `.env`:
```bash
WETH9_ADDRESS=<deployed_address>
```

#### 4. Deploy WETH10

```bash
forge script script/DeployWETH10.s.sol:DeployWETH10 \
    --rpc-url $RPC_URL \
    --broadcast \
    --private-key $PRIVATE_KEY \
    -vvv
```

Update `.env`:
```bash
WETH10_ADDRESS=<deployed_address>
```

#### 5. Deploy CCIPWETH9Bridge

```bash
forge script script/DeployCCIPWETH9Bridge.s.sol:DeployCCIPWETH9Bridge \
    --rpc-url $RPC_URL \
    --broadcast \
    --private-key $PRIVATE_KEY \
    -vvv
```

Update `.env`:
```bash
CCIPWETH9BRIDGE_ADDRESS=<deployed_address>
```

#### 6. Deploy CCIPWETH10Bridge

```bash
forge script script/DeployCCIPWETH10Bridge.s.sol:DeployCCIPWETH10Bridge \
    --rpc-url $RPC_URL \
    --broadcast \
    --private-key $PRIVATE_KEY \
    -vvv
```

Update `.env`:
```bash
CCIPWETH10BRIDGE_ADDRESS=<deployed_address>
```

#### 7. Deploy Oracle Aggregator

```bash
forge script script/DeployOracle.s.sol:DeployOracle \
    --rpc-url $RPC_URL \
    --broadcast \
    --private-key $PRIVATE_KEY \
    -vvv
```

Update `.env`:
```bash
ORACLE_AGGREGATOR_ADDRESS=<deployed_address>
```

## Step 4: Verify Deployment

Verify all contracts are deployed:

```bash
./scripts/deployment/check-deployment-status.sh
```

This will check:
- Contract addresses in `.env`
- Contract existence on-chain
- Infrastructure status

## Step 5: Configure Contracts

### Configure CCIP Router

Add supported chains:

```bash
cast send $CCIP_ROUTER "addSupportedChain(uint64)" 5009297550715157269 \
    --rpc-url $RPC_URL \
    --private-key $PRIVATE_KEY
```

### Configure CCIP Bridges

Add destination chains for bridges:

```bash
# For WETH9 Bridge
cast send $CCIPWETH9BRIDGE_ADDRESS "addDestination(uint64,address)" \
    5009297550715157269 \
    <destination_bridge_address> \
    --rpc-url $RPC_URL \
    --private-key $PRIVATE_KEY

# For WETH10 Bridge
cast send $CCIPWETH10BRIDGE_ADDRESS "addDestination(uint64,address)" \
    5009297550715157269 \
    <destination_bridge_address> \
    --rpc-url $RPC_URL \
    --private-key $PRIVATE_KEY
```

## Environment Variables

Ensure `.env` file contains:

```bash
# Deployer
PRIVATE_KEY=<your_private_key>

# Blockchain
RPC_URL=<rpc_endpoint>

# CCIP Configuration
CCIP_ROUTER=<ccip_router_address>
CCIP_FEE_TOKEN=<link_token_address>

# WETH Configuration
WETH9_ADDRESS=<weth9_address>
WETH10_ADDRESS=<weth10_address>

# Bridge Configuration
CCIPWETH9BRIDGE_ADDRESS=<weth9_bridge_address>
CCIPWETH10BRIDGE_ADDRESS=<weth10_bridge_address>

# Oracle Configuration
ORACLE_AGGREGATOR_ADDRESS=<oracle_aggregator_address>
```

## Deployment Checklist

- [ ] Blockchain infrastructure deployed (or local testnet running)
- [ ] RPC endpoint accessible
- [ ] PRIVATE_KEY configured in `.env`
- [ ] Mock LINK Token deployed (if needed)
- [ ] CCIP Router deployed and configured
- [ ] WETH9 deployed
- [ ] WETH10 deployed
- [ ] CCIPWETH9Bridge deployed
- [ ] CCIPWETH10Bridge deployed
- [ ] Oracle Aggregator deployed
- [ ] All addresses updated in `.env`
- [ ] Contracts verified on explorer (if applicable)
- [ ] Configuration verified

## Troubleshooting

### RPC Endpoint Not Accessible

1. Check if blockchain is running:
   ```bash
   curl -X POST $RPC_URL -H "Content-Type: application/json" \
     -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'
   ```

2. For local testnet, check if Anvil is running:
   ```bash
   lsof -Pi :8545 -sTCP:LISTEN
   ```

### Contract Deployment Fails

1. Check deployer balance:
   ```bash
   cast balance <deployer_address> --rpc-url $RPC_URL
   ```

2. Check gas prices:
   ```bash
   cast gas-price --rpc-url $RPC_URL
   ```

3. Verify contract compilation:
   ```bash
   forge build
   ```

### Missing Dependencies

1. Check if OpenZeppelin is installed:
   ```bash
   ls lib/openzeppelin-contracts
   ```

2. Install dependencies:
   ```bash
   forge install OpenZeppelin/openzeppelin-contracts@v4.9.6
   ```

## Next Steps

After deployment:

1. **Verify Contracts** - Verify contracts on explorer
2. **Test Contracts** - Run test suite
3. **Configure Monitoring** - Set up monitoring and alerts
4. **Documentation** - Update documentation with deployed addresses
5. **Integration** - Integrate with external services

## Support

For issues or questions:
- Check deployment logs
- Review contract documentation
- Verify configuration
- Check troubleshooting section

## References

- [Deployment Order](DEPLOYMENT_ORDER.md)
- [WETH CCIP Deployment](../operations/integrations/WETH_CCIP_DEPLOYMENT.md)
- [Contract Deployment Environment Setup](../configuration/CONTRACT_DEPLOYMENT_ENV_SETUP.md)
- [Azure/Cloudflare Environment Setup](../configuration/AZURE_CLOUDFLARE_ENV_SETUP.md)
- [Deployment Credentials](DEPLOYMENT_CREDENTIALS.md)
