#!/usr/bin/env bash
# Monitor bridge transfer transactions and CCIP events
# Usage: ./monitor-bridge-transfers.sh [transaction_hash] [chain]

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_PROJECT="/home/intlc/projects/smom-dbis-138"

source "$SOURCE_PROJECT/.env" 2>/dev/null || true

RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
TX_HASH="${1:-}"
CHAIN="${2:-chain138}"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
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"; }

# Monitor specific transaction
monitor_transaction() {
    local tx_hash="$1"
    local rpc_url="$2"
    
    log_info "Monitoring transaction: $tx_hash"
    
    # Get transaction receipt
    local receipt=$(cast receipt "$tx_hash" --rpc-url "$rpc_url" 2>/dev/null || echo "")
    
    if [ -z "$receipt" ]; then
        log_warn "Transaction not found or pending..."
        return 1
    fi
    
    # Extract status
    local status=$(echo "$receipt" | grep -oP "status\s+\K\w+" || echo "unknown")
    local block=$(echo "$receipt" | grep -oP "blockNumber\s+\K\w+" || echo "unknown")
    
    if [ "$status" = "1" ] || [ "$status" = "0x1" ]; then
        log_success "✓ Transaction confirmed in block: $block"
        
        # Check for CCIP events
        log_info "Checking for CCIP events..."
        check_ccip_events "$tx_hash" "$rpc_url"
    else
        log_error "✗ Transaction failed"
    fi
}

# Check CCIP events
check_ccip_events() {
    local tx_hash="$1"
    local rpc_url="$2"
    
    WETH9_BRIDGE="${CCIPWETH9_BRIDGE_CHAIN138:-0x89dd12025bfCD38A168455A44B400e913ED33BE2}"
    WETH10_BRIDGE="${CCIPWETH10_BRIDGE_CHAIN138:-0xe0E93247376aa097dB308B92e6Ba36bA015535D0}"
    
    # Check for CrossChainTransferInitiated event
    local events=$(cast logs --from-block latest --to-block latest \
        --address "$WETH9_BRIDGE" \
        "CrossChainTransferInitiated(uint64,address,uint256)" \
        --rpc-url "$rpc_url" 2>/dev/null || echo "")
    
    if [ -n "$events" ]; then
        log_success "✓ CCIP transfer initiated"
        echo "$events"
    else
        log_warn "No CCIP events found"
    fi
}

# Monitor all recent bridge transfers
monitor_all_transfers() {
    log_info "Monitoring all recent bridge transfers..."
    
    WETH9_BRIDGE="${CCIPWETH9_BRIDGE_CHAIN138:-0x89dd12025bfCD38A168455A44B400e913ED33BE2}"
    WETH10_BRIDGE="${CCIPWETH10_BRIDGE_CHAIN138:-0xe0E93247376aa097dB308B92e6Ba36bA015535D0}"
    
    # Get recent events
    local recent_block=$(cast block-number --rpc-url "$RPC_URL")
    local from_block=$((recent_block - 100))
    
    log_info "Checking blocks $from_block to $recent_block..."
    
    # Check WETH9 bridge events
    cast logs --from-block "$from_block" --to-block latest \
        --address "$WETH9_BRIDGE" \
        "CrossChainTransferInitiated(uint64,address,uint256)" \
        --rpc-url "$RPC_URL" 2>/dev/null || log_warn "No WETH9 transfers found"
    
    # Check WETH10 bridge events
    cast logs --from-block "$from_block" --to-block latest \
        --address "$WETH10_BRIDGE" \
        "CrossChainTransferInitiated(uint64,address,uint256)" \
        --rpc-url "$RPC_URL" 2>/dev/null || log_warn "No WETH10 transfers found"
}

# Main execution
if [ -n "$TX_HASH" ]; then
    monitor_transaction "$TX_HASH" "$RPC_URL"
else
    monitor_all_transfers
fi

