#!/bin/bash
# Verify USDC contract deployment on ALL Mainnet (651940)
# This script helps check if USDC is deployed on ALL Mainnet

set -euo pipefail

CHAIN_ID=651940
RPC_URL="https://mainnet-rpc.alltra.global"
EXPLORER_URL="https://alltra.global"

echo "╔══════════════════════════════════════════════════════════════╗"
echo "║     ALL Mainnet (651940) USDC Verification Script            ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""
echo "Chain ID: $CHAIN_ID"
echo "RPC URL: $RPC_URL"
echo "Explorer: $EXPLORER_URL"
echo ""

# Check if cast (Foundry) is available
if ! command -v cast &> /dev/null; then
    echo "⚠️  Warning: 'cast' command not found. Install Foundry to use RPC queries."
    echo "   Install: curl -L https://foundry.paradigm.xyz | bash && foundryup"
    echo ""
    echo "📋 Manual Verification Steps:"
    echo "   1. Visit: $EXPLORER_URL"
    echo "   2. Search for 'USDC' or 'USD Coin'"
    echo "   3. Check token contract addresses"
    echo "   4. Verify contract is verified and has standard USDC interface"
    echo "   5. Record contract address and decimals (typically 6)"
    echo ""
    exit 0
fi

echo "🔍 Checking RPC connectivity..."
if ! cast block-number --rpc-url "$RPC_URL" > /dev/null 2>&1; then
    echo "❌ Error: Cannot connect to RPC at $RPC_URL"
    echo "   Please check network connectivity or RPC endpoint"
    exit 1
fi

BLOCK_NUM=$(cast block-number --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
ACTUAL_CHAIN_ID=$(cast chain-id --rpc-url "$RPC_URL" 2>/dev/null || echo "0")

if [ "$ACTUAL_CHAIN_ID" != "$CHAIN_ID" ]; then
    echo "⚠️  Warning: Chain ID mismatch (expected: $CHAIN_ID, got: $ACTUAL_CHAIN_ID)"
fi

echo "✅ RPC accessible (block: $BLOCK_NUM, chain: $ACTUAL_CHAIN_ID)"
echo ""

echo "📋 Common USDC Contract Addresses to Check:"
echo ""
echo "Standard USDC addresses (may differ on ALL Mainnet):"
echo "  - Ethereum Mainnet: 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
echo "  - Base: 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
echo "  - Arbitrum: 0xaf88d065e77c8cC2239327C5EDb3A432268e5831"
echo ""
echo "🔍 To verify USDC on ALL Mainnet:"
echo ""
echo "Method 1: Explorer Search"
echo "  1. Visit: $EXPLORER_URL"
echo "  2. Search for 'USDC' in token search"
echo "  3. Check contract verification status"
echo "  4. Verify token symbol, name, and decimals"
echo ""
echo "Method 2: RPC Query (if you have a suspected address)"
echo "  cast call <ADDRESS> 'symbol()(string)' --rpc-url $RPC_URL"
echo "  cast call <ADDRESS> 'name()(string)' --rpc-url $RPC_URL"
echo "  cast call <ADDRESS> 'decimals()(uint8)' --rpc-url $RPC_URL"
echo ""
echo "Method 3: Check Token Lists"
echo "  - Check if ALL Mainnet has a token list (similar to Uniswap token lists)"
echo "  - Look for USDC in official token registries"
echo ""
echo "📝 After finding USDC address, update:"
echo "  - alltra-lifi-settlement/src/config/chains.ts"
echo "    Set: usdcAddress: '<FOUND_ADDRESS>'"
echo ""
echo "✅ Script complete. Manual verification required for USDC deployment."
