import requests
import websockets
import json
import base64
import time
from websockets import connect

WS_URL = "wss://marga-e56m84-fast-mainnet.helius-rpc.com/"


class RaydiumDataCracker:
    def __init__(self):
        self.ws = None
        self.pools = {}
        # Different byte position combinations to test
        self.byte_positions = [
            (32, 40, 40, 48),  # Test 1: Original positions
            (64, 72, 72, 80),  # Test 2: AMM v4 reserves
            (176, 184, 184, 192),  # Test 3: Alternative layout
            (8, 16, 16, 24),  # Test 4: Header area
            (96, 104, 104, 112),  # Test 5: Middle section
            (128, 136, 136, 144),  # Test 6: Later section
            (144, 152, 152, 160),  # Test 7: Extended area
            (160, 168, 168, 176),  # Test 8: Further section
            (192, 200, 200, 208),  # Test 9: End section
            (48, 56, 56, 64),  # Test 10: Early middle
            (80, 88, 88, 96),  # Test 11: Quarter point
            (112, 120, 120, 128),  # Test 12: Mid section
            (24, 32, 32, 40),  # Test 13: Early section
            (40, 48, 48, 56),  # Test 14: Following early
            (56, 64, 64, 72),  # Test 15: Pre-reserves
            (88, 96, 96, 104),  # Test 16: Post-reserves
            (104, 112, 112, 120),  # Test 17: Mid-later
            (120, 128, 128, 136),  # Test 18: Three-quarter
            (136, 144, 144, 152),  # Test 19: Pre-end
            (168, 176, 176, 184),  # Test 20: Near-end
        ]

    async def connect(self):
        print("🔌 INITIATING RAYDIUM DATA CRACK")
        self.ws = await connect(WS_URL)
        subscribe_msg = {
            "jsonrpc": "2.0",
            "id": 1,
            "method": "programSubscribe",
            "params": [
                "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8",
                {"encoding": "base64", "commitment": "confirmed"},
            ],
        }
        await self.ws.send(json.dumps(subscribe_msg))
        print("✅ CONNECTION ESTABLISHED")

    def test_all_positions(self, raw_data):
        results = []
        for i, (base_start, base_end, quote_start, quote_end) in enumerate(
            self.byte_positions, 1
        ):
            try:
                base = int.from_bytes(raw_data[base_start:base_end], "little")
                quote = int.from_bytes(raw_data[quote_start:quote_end], "little")

                if base > 0 and quote > 0:
                    price = quote / base
                    volume = (quote * price) / 1e9
                    results.append(
                        {
                            "test_num": i,
                            "positions": f"Base: {base_start}-{base_end}, Quote: {quote_start}-{quote_end}",
                            "price": price,
                            "volume": volume,
                            "base": base,
                            "quote": quote,
                        }
                    )
            except Exception as e:
                continue
        return results

    def display_results(self, pool_address, results):
        print(f"\n💥 POOL: {pool_address}")
        print("TEST | BYTE POSITIONS | PRICE | VOLUME | BASE | QUOTE")
        print("-" * 80)

        for r in results:
            print(
                f"#{r['test_num']:2d} | "
                f"{r['positions']} | "
                f"${r['price']:.4f} | "
                f"${r['volume']:.2f}M | "
                f"{r['base']:.0f} | "
                f"{r['quote']:.0f}"
            )

    async def run(self):
        await self.connect()
        print("🚀 STARTING DATA ANALYSIS")

        while True:
            try:
                msg = await self.ws.recv()
                data = json.loads(msg)

                if "params" in data and "result" in data["params"]:
                    result = data["params"]["result"]["value"]
                    pool_address = result["pubkey"]
                    account_data = result["account"]["data"]

                    if isinstance(account_data, list) and len(account_data) > 0:
                        raw_data = base64.b64decode(account_data[0])
                        results = self.test_all_positions(raw_data)

                        if results:
                            self.display_results(pool_address, results)
                            print("\n🎯 VALID DATA STRUCTURES FOUND!")
                            print("Raw data preview:", raw_data[:32].hex())

                await asyncio.sleep(0.1)

            except Exception as e:
                print(f"🔄 Loop: {str(e)}")
                await asyncio.sleep(1)


if __name__ == "__main__":
    print("🔥 RAYDIUM DATA STRUCTURE ANALYZER v1.0")
    asyncio.run(RaydiumDataCracker().run())
 can u fix this script to work with my python 3.11.10 please