#!/usr/bin/env bash
# Verify Ethereum Mainnet CompliantWrappedToken (cWUSDT…cWXAUT) on Etherscan.
# Constructor matches script/deploy/DeployCWTokens.s.sol (name, symbol, 6 decimals, deployer admin).
#
# Requires: ETHERSCAN_API_KEY, and either CW_VERIFY_ADMIN=0x... (deployer used at deploy) or PRIVATE_KEY
#           (address derived via cast wallet address) to ABI-encode constructor args.
#
# Usage:
#   cd smom-dbis-138 && ./scripts/deployment/verify-mainnet-cw-etherscan.sh
#   ./scripts/deployment/verify-mainnet-cw-etherscan.sh --dry-run   # print forge commands only
set -euo pipefail

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

DRY_RUN=false
for a in "$@"; do
  case "$a" in
    --dry-run) DRY_RUN=true ;;
  esac
done

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

if [[ -z "${ETHERSCAN_API_KEY:-}" ]]; then
  echo "ETHERSCAN_API_KEY not set (see https://etherscan.io/myapikey)" >&2
  exit 1
fi

ADMIN="${CW_VERIFY_ADMIN:-}"
if [[ -z "$ADMIN" && -n "${PRIVATE_KEY:-}" ]]; then
  ADMIN="$(cast wallet address --private-key "$PRIVATE_KEY")"
fi
if [[ -z "$ADMIN" || "$ADMIN" == "0x0000000000000000000000000000000000000000" ]]; then
  echo "Set CW_VERIFY_ADMIN to the deployer address used in CompliantWrappedToken constructor, or set PRIVATE_KEY." >&2
  exit 1
fi

verify_one() {
  local envkey="$1" cname="$2" sym="$3" dec="${4:-6}"
  local addr
  addr="${!envkey:-}"
  if [[ -z "$addr" ]]; then
    echo "  skip $sym: $envkey unset"
    return 0
  fi
  local enc
  enc="$(cast abi-encode "constructor(string,string,uint8,address)" "$cname" "$sym" "$dec" "$ADMIN")"
  if $DRY_RUN; then
    echo "forge verify-contract --chain-id 1 --num-of-optimizations 200 --via-ir --constructor-args \"$enc\" --etherscan-api-key \"\$ETHERSCAN_API_KEY\" \"$addr\" contracts/tokens/CompliantWrappedToken.sol:CompliantWrappedToken"
    return 0
  fi
  set +e
  out="$(forge verify-contract \
    --chain-id 1 \
    --num-of-optimizations 200 \
    --via-ir \
    --constructor-args "$enc" \
    --etherscan-api-key "$ETHERSCAN_API_KEY" \
    "$addr" \
    "contracts/tokens/CompliantWrappedToken.sol:CompliantWrappedToken" 2>&1)"
  rc=$?
  set -e
  if [[ $rc -eq 0 ]]; then
    echo "  ok $sym $addr"
  elif echo "$out" | grep -qiE 'already verified|Already Verified'; then
    echo "  already verified $sym $addr"
  else
    echo "  FAILED $sym $addr (exit $rc)" >&2
    echo "$out" >&2
    return "$rc"
  fi
}

export FOUNDRY_SRC="contracts/tokens,contracts/interfaces"
export FOUNDRY_OUT="out/scopes/tokens"
export FOUNDRY_CACHE_PATH="cache/scopes/tokens"

if ! $DRY_RUN; then
  bash "$PROJECT_ROOT/scripts/forge/scope.sh" build tokens -q
fi

echo "=== Verify Mainnet CompliantWrappedToken (admin=$ADMIN) ==="
# envVar|constructor name|symbol|decimals — must match deploy constructors
while IFS='|' read -r envkey cname sym dec; do
  [[ -z "$envkey" ]] && continue
  verify_one "$envkey" "$cname" "$sym" "${dec:-6}"
done <<'EOF'
CWUSDT_MAINNET|Wrapped cUSDT|cWUSDT|6
CWUSDC_MAINNET|Wrapped cUSDC|cWUSDC|6
CWEURC_MAINNET|Wrapped cEURC|cWEURC|6
CWEURT_MAINNET|Wrapped cEURT|cWEURT|6
CWGBPC_MAINNET|Wrapped cGBPC|cWGBPC|6
CWGBPT_MAINNET|Wrapped cGBPT|cWGBPT|6
CWAUDC_MAINNET|Wrapped cAUDC|cWAUDC|6
CWJPYC_MAINNET|Wrapped cJPYC|cWJPYC|6
CWCHFC_MAINNET|Wrapped cCHFC|cWCHFC|6
CWCADC_MAINNET|Wrapped cCADC|cWCADC|6
CWXAUC_MAINNET|Wrapped cXAUC|cWXAUC|6
CWXAUT_MAINNET|Wrapped cXAUT|cWXAUT|6
CWBTC_MAINNET|Wrapped cBTC|cWBTC|8
EOF

echo "Done."
