#!/usr/bin/env python3
"""
Check what selector was used in our failed close transaction
"""
from web3 import Web3

BSC_RPC = "https://bsc-dataseed.binance.org/"

w3 = Web3(Web3.HTTPProvider(BSC_RPC))

print("="*80)
print("🔍 ANALYZING OUR FAILED CLOSE TRANSACTION")
print("="*80)

# Our failed close transaction
close_tx_hash = "0xc99d631551bd64da5aac15fbb211ecf52f4ff3f085a92bbc6ae744efc6656db5"

print(f"\n📊 Transaction: {close_tx_hash}")

tx = w3.eth.get_transaction(close_tx_hash)
receipt = w3.eth.get_transaction_receipt(close_tx_hash)

print(f"\n   Status: {receipt['status']} (0=FAILED, 1=SUCCESS)")
print(f"   Gas used: {receipt['gasUsed']}")
print(f"   From: {tx['from']}")
print(f"   To: {tx['to']}")

print(f"\n   Input data (first 74 bytes):")
print(f"   {tx['input'][:74]}")

# Extract function selector (first 4 bytes after 0x)
selector = tx['input'][:10]
print(f"\n   Function selector: {selector}")

# Extract the parameter (next 32 bytes = trade_hash)
if len(tx['input']) >= 74:
    param = tx['input'][10:74]
    print(f"   Parameter (trade_hash): 0x{param}")

# Compare with expected
from eth_utils import keccak
expected_selector = '0x' + keccak(text="closeTrade(bytes32)").hex()[:8]
print(f"\n   Expected selector for closeTrade(bytes32): {expected_selector}")

if selector == expected_selector:
    print(f"   ✅ Selector is CORRECT")
else:
    print(f"   ❌ Selector is WRONG!")

print("\n" + "="*80)
