#!/usr/bin/env bash
# Inspect WETH9 contract implementation
# Checks if the contract matches standard WETH9 behavior
# Usage: ./inspect-weth9-contract.sh

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "$PROJECT_ROOT/scripts/lib/address-inventory.sh"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

load_explorer_runtime_env

# Configuration
RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
WETH9_ADDRESS="$(resolve_address_value WETH9_ADDRESS WETH9_ADDRESS 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2)"

log_info "========================================="
log_info "WETH9 Contract Inspection"
log_info "========================================="
log_info ""
log_info "Contract Address: $WETH9_ADDRESS"
log_info "RPC URL: $RPC_URL"
log_info ""

# Step 1: Check contract exists and has code
log_info "Step 1: Checking contract existence..."
BYTECODE=$(cast code "$WETH9_ADDRESS" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -z "$BYTECODE" ] || [ "$BYTECODE" = "0x" ]; then
    log_error "Contract has no bytecode at address $WETH9_ADDRESS"
    exit 1
fi

BYTECODE_LENGTH=$(echo -n "$BYTECODE" | wc -c)
log_success "✓ Contract exists (bytecode length: $BYTECODE_LENGTH chars)"
log_info ""

# Step 2: Check standard ERC-20 functions
log_info "Step 2: Checking ERC-20 functions..."

# Check balanceOf
BALANCEOF_RESULT=$(cast call "$WETH9_ADDRESS" "balanceOf(address)" "0x0000000000000000000000000000000000000000" --rpc-url "$RPC_URL" 2>&1 || echo "ERROR")
if echo "$BALANCEOF_RESULT" | grep -qE "^(0x)?[0-9a-fA-F]+$"; then
    log_success "✓ balanceOf() function exists"
else
    log_warn "⚠ balanceOf() may not exist or has issues"
fi

# Check totalSupply
TOTALSUPPLY_RESULT=$(cast call "$WETH9_ADDRESS" "totalSupply()" --rpc-url "$RPC_URL" 2>&1 || echo "ERROR")
if echo "$TOTALSUPPLY_RESULT" | grep -qE "^(0x)?[0-9a-fA-F]+$"; then
    TOTALSUPPLY_ETH=$(echo "scale=18; $TOTALSUPPLY_RESULT / 1000000000000000000" | bc 2>/dev/null || echo "N/A")
    log_success "✓ totalSupply() function exists (Current: $TOTALSUPPLY_ETH WETH)"
else
    log_warn "⚠ totalSupply() may not exist or has issues"
fi

# Check decimals
DECIMALS_RESULT=$(cast call "$WETH9_ADDRESS" "decimals()" --rpc-url "$RPC_URL" 2>&1 || echo "ERROR")
if echo "$DECIMALS_RESULT" | grep -qE "^(0x)?[0-9a-fA-F]+$"; then
    DECIMALS=$(cast --to-dec "$DECIMALS_RESULT" 2>/dev/null || echo "N/A")
    if [ "$DECIMALS" = "18" ]; then
        log_success "✓ decimals() returns 18 (correct)"
    else
        log_warn "⚠ decimals() returns $DECIMALS (expected 18)"
    fi
else
    log_warn "⚠ decimals() may not exist (this is known issue with WETH9)"
fi

log_info ""

# Step 3: Check contract ETH balance vs total supply
log_info "Step 3: Verifying 1:1 backing..."
CONTRACT_ETH=$(cast balance "$WETH9_ADDRESS" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
TOTAL_SUPPLY=$(cast call "$WETH9_ADDRESS" "totalSupply()" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")

CONTRACT_ETH_ETH=$(echo "scale=18; $CONTRACT_ETH / 1000000000000000000" | bc 2>/dev/null || echo "0")
TOTAL_SUPPLY_ETH=$(echo "scale=18; $TOTAL_SUPPLY / 1000000000000000000" | bc 2>/dev/null || echo "0")

log_info "Contract ETH Balance: $CONTRACT_ETH_ETH ETH ($CONTRACT_ETH wei)"
log_info "WETH9 Total Supply: $TOTAL_SUPPLY_ETH WETH ($TOTAL_SUPPLY wei)"

if [ "$CONTRACT_ETH" = "$TOTAL_SUPPLY" ]; then
    log_success "✓ Contract balance matches total supply (1:1 backing verified)"
else
    log_error "✗ Contract balance does NOT match total supply!"
    log_error "  This indicates a potential issue with the contract implementation"
    DIFF=$(echo "$CONTRACT_ETH - $TOTAL_SUPPLY" | bc 2>/dev/null || echo "0")
    log_error "  Difference: $DIFF wei"
fi

log_info ""

# Step 4: Check deposit function signature
log_info "Step 4: Checking deposit() function..."
DEPOSIT_SIG="0xd0e30db0"  # deposit() function signature
if echo "$BYTECODE" | grep -q "$DEPOSIT_SIG"; then
    log_success "✓ deposit() function signature found in bytecode"
else
    log_warn "⚠ deposit() function signature not found (may use different encoding)"
fi

log_info ""

# Step 5: Analyze bytecode for potential issues
log_info "Step 5: Analyzing bytecode for modifications..."

# Check for common fee patterns (these are heuristics, not definitive)
# Look for patterns that might indicate fee deduction

# Save bytecode to temp file for analysis
TEMP_FILE=$(mktemp)
echo "$BYTECODE" > "$TEMP_FILE"

# Check bytecode size (standard WETH9 is typically around 2-3KB)
BYTECODE_SIZE_BYTES=$((BYTECODE_LENGTH / 2 - 1))  # Subtract 0x prefix
if [ "$BYTECODE_SIZE_BYTES" -lt 5000 ]; then
    log_info "Bytecode size: $BYTECODE_SIZE_BYTES bytes (reasonable for WETH9)"
else
    log_warn "Bytecode size: $BYTECODE_SIZE_BYTES bytes (larger than typical WETH9)"
fi

# Clean up
rm -f "$TEMP_FILE"

log_info ""

# Step 6: Summary and recommendations
log_info "========================================="
log_info "Summary"
log_info "========================================="
log_info ""

if [ "$CONTRACT_ETH" = "$TOTAL_SUPPLY" ]; then
    log_success "✓ Contract appears to maintain 1:1 backing"
    log_info ""
    log_info "Recommendations:"
    log_info "  1. Run verification script to test actual wrap ratio:"
    log_info "     ./scripts/verify-weth9-ratio.sh [private_key] 0.001"
    log_info "  2. If ratio test fails, contract may have hidden fees in deposit()"
    log_info "  3. Consider decompiling bytecode for detailed analysis"
else
    log_error "✗ Contract does NOT maintain 1:1 backing!"
    log_info ""
    log_info "This is a CRITICAL issue. The contract may:"
    log_info "  - Have fees deducted during deposit"
    log_info "  - Have modified deposit() implementation"
    log_info "  - Have accounting errors"
    log_info ""
    log_info "Immediate Actions Required:"
    log_info "  1. Decompile contract bytecode for analysis"
    log_info "  2. Compare with standard WETH9 implementation"
    log_info "  3. Test deposit() function with verification script"
    log_info "  4. Consider deploying standard WETH9 if contract is modified"
fi

log_info ""
log_info "For detailed bytecode analysis, use:"
log_info "  cast code $WETH9_ADDRESS --rpc-url $RPC_URL > weth9_bytecode.bin"
log_info "  Then use a decompiler (e.g., ethervm.io, panoramix.tools) to analyze"
