#!/usr/bin/env bash
# Verify Monad (chain 143) CCIP WETH9/WETH10 bridges on monadscan (Etherscan V2 API).
#
# Usage: cd smom-dbis-138 && ./scripts/deployment/verify-monad-ccip-bridges.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$PROJECT_ROOT"

if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "$PROJECT_ROOT"
elif [[ -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
fi

ROUTER="${CCIP_MONAD_ROUTER:-0x33566fE5976AAa420F3d5C64996641Fc3858CaDB}"
WMON="${WMON_MAINNET:-0x754704Bc059F8C67012fEd69BC8A327a5aafb603}"
FEE="${CCIP_MONAD_LINK_TOKEN:-0x0000000000000000000000000000000000000000}"
W9="${CCIPWETH9_BRIDGE_MONAD:-}"
W10="${CCIPWETH10_BRIDGE_MONAD:-}"

if [[ -z "$W9" || -z "$W10" ]]; then
  echo "Set CCIPWETH9_BRIDGE_MONAD and CCIPWETH10_BRIDGE_MONAD in .env" >&2
  exit 1
fi

if [[ -z "${ETHERSCAN_API_KEY:-}" ]]; then
  echo "ETHERSCAN_API_KEY required (Etherscan V2 unified key for chainid=143)" >&2
  exit 1
fi

CTOR9=$(cast abi-encode "constructor(address,address,address)" "$ROUTER" "$WMON" "$FEE")
CTOR10=$(cast abi-encode "constructor(address,address,address)" "$ROUTER" "$WMON" "$FEE")

echo "=== Verify CCIPWETH9Bridge on Monad ($W9) ==="
forge verify-contract \
  --chain-id 143 \
  --num-of-optimizations 200 \
  --watch \
  --constructor-args "$CTOR9" \
  --etherscan-api-key "$ETHERSCAN_API_KEY" \
  "$W9" \
  "contracts/ccip/CCIPWETH9Bridge.sol:CCIPWETH9Bridge"

echo ""
echo "=== Verify CCIPWETH10Bridge on Monad ($W10) ==="
forge verify-contract \
  --chain-id 143 \
  --num-of-optimizations 200 \
  --watch \
  --constructor-args "$CTOR10" \
  --etherscan-api-key "$ETHERSCAN_API_KEY" \
  "$W10" \
  "contracts/ccip/CCIPWETH10Bridge.sol:CCIPWETH10Bridge"

echo ""
echo "Verification submitted. Check https://monadscan.com/"
