#!/bin/bash

# Phase 6: Provide Initial Liquidity
# This script provides initial liquidity to the bridge system

set -e

echo "=== Phase 6: Provide Initial Liquidity ==="

# Load environment variables
if [ -f .env ]; then
    export $(cat .env | grep -v '^#' | grep -v '^$' | xargs)
fi

# Check required variables
REQUIRED_VARS=(
    "PRIVATE_KEY"
    "ETHEREUM_MAINNET_RPC"
    "LIQUIDITY_POOL"
    "RESERVE_SYSTEM"
)

for var in "${REQUIRED_VARS[@]}"; do
    if [ -z "${!var}" ] || [ "${!var}" == "0x..." ]; then
        echo "Error: $var is not set in .env"
        exit 1
    fi
done

LIQUIDITY_AMOUNT=${LIQUIDITY_AMOUNT:-100}  # Default 100 ETH
RESERVE_AMOUNT=${RESERVE_AMOUNT:-100000}   # Default 100k USDT

echo ""
echo "--- Providing Liquidity to LiquidityPoolETH ---"
echo "Amount: $LIQUIDITY_AMOUNT ETH"

cast send "$LIQUIDITY_POOL" \
    "provideLiquidity(uint8)" \
    0 \
    --value "${LIQUIDITY_AMOUNT}ether" \
    --rpc-url "$ETHEREUM_MAINNET_RPC" \
    --private-key "$PRIVATE_KEY"

echo "✓ Liquidity provided"

echo ""
echo "--- Funding ReserveSystem ---"
echo "Amount: $RESERVE_AMOUNT USDT"

# Approve USDT
USDT=${USDT:-0xdAC17F958D2ee523a2206206994597C13D831ec7}
cast send "$USDT" \
    "approve(address,uint256)" \
    "$RESERVE_SYSTEM" \
    "$(cast --to-wei $RESERVE_AMOUNT 6)" \
    --rpc-url "$ETHEREUM_MAINNET_RPC" \
    --private-key "$PRIVATE_KEY"

echo "✓ USDT approved"

# Deposit to ReserveSystem
cast send "$RESERVE_SYSTEM" \
    "depositReserve(address,uint256)" \
    "$USDT" \
    "$(cast --to-wei $RESERVE_AMOUNT 6)" \
    --rpc-url "$ETHEREUM_MAINNET_RPC" \
    --private-key "$PRIVATE_KEY"

echo "✓ Reserves deposited"

echo ""
echo "=== Phase 6 Complete ==="
echo "Initial liquidity and reserves provided"

