# -*- coding: utf-8 -*-

# PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
# https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code

import ccxt.async_support
from ccxt.async_support.base.ws.cache import ArrayCache
from ccxt.base.types import Any, Int, Liquidation, OrderBook, Str, Strings, Ticker, Tickers, Trade
from ccxt.async_support.base.ws.client import Client
from typing import List
from ccxt.base.precise import Precise


class lighter(ccxt.async_support.lighter):

    def describe(self) -> Any:
        return self.deep_extend(super(lighter, self).describe(), {
            'has': {
                'ws': True,
                'watchTicker': True,
                'watchMarkPrice': True,
                'watchMarkPrices': True,
                'watchTickers': True,
                'watchBidsAsks': False,
                'watchOrderBook': True,
                'watchTrades': True,
                'watchTradesForSymbols': False,
                'watchOrderBookForSymbols': False,
                'watchBalance': False,
                'watchLiquidations': True,
                'watchLiquidationsForSymbols': False,
                'watchMyLiquidations': False,
                'watchMyLiquidationsForSymbols': False,
                'watchOHLCV': False,
                'watchOHLCVForSymbols': False,
                'watchOrders': False,
                'watchMyTrades': True,
                'watchPositions': False,
                'watchFundingRate': False,
                'watchFundingRates': False,
                'unWatchOrderBook': True,
                'unWatchTicker': True,
                'unWatchTickers': True,
                'unWatchTrades': True,
                'unWatchMyTrades': True,
                'unWatchMarkPrice': True,
                'unWatchMarkPrices': True,
            },
            'urls': {
                'api': {
                    'ws': 'wss://mainnet.zklighter.elliot.ai/stream',
                },
                'test': {
                    'ws': 'wss://testnet.zklighter.elliot.ai/stream',
                },
            },
            'options': {},
        })

    def get_message_hash(self, unifiedChannel: str, symbol: Str = None, extra: Str = None):
        hash = unifiedChannel
        if symbol is not None:
            hash += '::' + symbol
        else:
            hash += 's'  # tickers, orderbooks, ohlcvs ...
        if extra is not None:
            hash += '::' + extra
        return hash

    async def subscribe_public(self, messageHash, params={}):
        url = self.urls['api']['ws']
        request: dict = {
            'type': 'subscribe',
        }
        subscription: dict = {
            'messageHash': messageHash,
            'params': params,
        }
        return await self.watch(url, messageHash, self.extend(request, params), messageHash, subscription)

    async def subscribe_public_multiple(self, messageHashes, params={}):
        url = self.urls['api']['ws']
        request: dict = {
            'type': 'subscribe',
        }
        subscription: dict = {
            'messageHashes': messageHashes,
            'params': params,
        }
        return await self.watch_multiple(url, messageHashes, self.extend(request, params), messageHashes, subscription)

    async def unsubscribe_public(self, messageHash, params={}):
        url = self.urls['api']['ws']
        request: dict = {
            'type': 'unsubscribe',
        }
        subscription: dict = {
            'messageHash': messageHash,
            'params': params,
        }
        return await self.watch(url, messageHash, self.extend(request, params), messageHash, subscription)

    def handle_delta(self, bookside, delta):
        price = self.safe_float(delta, 'price')
        amount = self.safe_float(delta, 'size')
        bookside.store(price, amount)

    def handle_deltas(self, bookside, deltas):
        for i in range(0, len(deltas)):
            self.handle_delta(bookside, deltas[i])

    def handle_order_book_message(self, client: Client, message, orderbook):
        data = self.safe_dict(message, 'order_book', {})
        self.handle_deltas(orderbook['asks'], self.safe_list(data, 'asks', []))
        self.handle_deltas(orderbook['bids'], self.safe_list(data, 'bids', []))
        orderbook['nonce'] = self.safe_integer(data, 'offset')
        timestamp = self.safe_integer(message, 'timestamp')
        orderbook['timestamp'] = timestamp
        orderbook['datetime'] = self.iso8601(timestamp)
        return orderbook

    def handle_order_book(self, client: Client, message):
        #
        # {
        #     "channel": "order_book:0",
        #     "offset": 11413309,
        #     "order_book": {
        #         "code": 0,
        #         "asks": [
        #             {
        #                 "price": "2979.64",
        #                 "size": "61.9487"
        #             }
        #         ],
        #         "bids": [
        #             {
        #                 "price": "2979.36",
        #                 "size": "0.0000"
        #             }
        #         ],
        #         "offset": 11413309,
        #         "nonce": 3107818665
        #     },
        #     "timestamp": 1763448665923,
        #     "type": "update/order_book"
        # }
        #
        data = self.safe_dict(message, 'order_book', {})
        channel = self.safe_string(message, 'channel', '')
        parts = channel.split(':')
        marketId = parts[1]
        market = self.safe_market(marketId)
        symbol = market['symbol']
        timestamp = self.safe_integer(message, 'timestamp')
        if not (symbol in self.orderbooks):
            self.orderbooks[symbol] = self.order_book()
        orderbook = self.orderbooks[symbol]
        type = self.safe_string(message, 'type', '')
        if type == 'subscribed/order_book':
            parsed = self.parse_order_book(data, symbol, timestamp, 'bids', 'asks', 'price', 'size')
            parsed['nonce'] = self.safe_integer(data, 'offset')
            orderbook.reset(parsed)
        elif type == 'update/order_book':
            self.handle_order_book_message(client, message, orderbook)
        messageHash = self.get_message_hash('orderbook', symbol)
        client.resolve(orderbook, messageHash)

    async def watch_order_book(self, symbol: str, limit: Int = None, params={}) -> OrderBook:
        """
        watches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://apidocs.lighter.xyz/docs/websocket-reference#order-book

        :param str symbol: unified symbol of the market to fetch the order book for
        :param int [limit]: the maximum amount of order book entries to return
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        """
        await self.load_markets()
        market = self.market(symbol)
        request: dict = {
            'channel': 'order_book/' + market['id'],
        }
        messageHash = self.get_message_hash('orderbook', symbol)
        orderbook = await self.subscribe_public(messageHash, self.extend(request, params))
        return orderbook.limit()

    async def un_watch_order_book(self, symbol: str, params={}) -> Any:
        """
        unWatches information on open orders with bid(buy) and ask(sell) prices, volumes and other data

        https://apidocs.lighter.xyz/docs/websocket-reference#order-book

        :param str symbol: unified symbol of the market
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: A dictionary of `order book structures <https://docs.ccxt.com/#/?id=order-book-structure>` indexed by market symbols
        """
        await self.load_markets()
        market = self.market(symbol)
        request: dict = {
            'channel': 'order_book/' + market['id'],
        }
        messageHash = self.get_message_hash('unsubscribe', symbol)
        return await self.unsubscribe_public(messageHash, self.extend(request, params))

    def handle_ticker(self, client: Client, message):
        #
        # watchTicker
        #     {
        #         "channel": "market_stats:0",
        #         "market_stats": {
        #             "market_id": 0,
        #             "index_price": "3015.56",
        #             "mark_price": "3013.91",
        #             "open_interest": "122736286.659423",
        #             "open_interest_limit": "72057594037927936.000000",
        #             "funding_clamp_small": "0.0500",
        #             "funding_clamp_big": "4.0000",
        #             "last_trade_price": "3013.13",
        #             "current_funding_rate": "0.0012",
        #             "funding_rate": "0.0012",
        #             "funding_timestamp": 1763532000004,
        #             "daily_base_token_volume": 643235.2763,
        #             "daily_quote_token_volume": 1983505435.673896,
        #             "daily_price_low": 2977.42,
        #             "daily_price_high": 3170.81,
        #             "daily_price_change": -0.3061987051035322
        #         },
        #         "type": "update/market_stats"
        #     }
        #
        # watchTickers
        # {
        #     "channel": "market_stats:all",
        #     "market_stats": {
        #         "96": {
        #             "market_id": 96,
        #             "index_price": "1.15901",
        #             "mark_price": "1.15954",
        #             "open_interest": "19392952.260530",
        #             "open_interest_limit": "50000000000000.000000",
        #             "funding_clamp_small": "0.0500",
        #             "funding_clamp_big": "4.0000",
        #             "last_trade_price": "1.15955",
        #             "current_funding_rate": "0.0000",
        #             "funding_rate": "0.0000",
        #             "funding_timestamp": 1763532000004,
        #             "daily_base_token_volume": 117634224.1,
        #             "daily_quote_token_volume": 136339744.383989,
        #             "daily_price_low": 1.15774,
        #             "daily_price_high": 1.16105,
        #             "daily_price_change": -0.004311757299805109
        #         }
        #     },
        #     "type": "update/market_stats"
        # }
        #
        data = self.safe_dict(message, 'market_stats', {})
        channel = self.safe_string(message, 'channel')
        if channel == 'market_stats:all':
            marketIds = list(data.keys())
            for i in range(0, len(marketIds)):
                marketId = marketIds[i]
                market = self.safe_market(marketId)
                symbol = market['symbol']
                ticker = self.parse_ticker(data[marketId], market)
                self.tickers[symbol] = ticker
                client.resolve(ticker, self.get_message_hash('ticker', symbol))
                client.resolve(ticker, self.get_message_hash('ticker'))
        else:
            marketId = self.safe_string(data, 'market_id')
            market = self.safe_market(marketId)
            symbol = market['symbol']
            ticker = self.parse_ticker(data, market)
            self.tickers[symbol] = ticker
            client.resolve(ticker, self.get_message_hash('ticker', symbol))

    async def watch_ticker(self, symbol: str, params={}) -> Ticker:
        """
        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market

        https://apidocs.lighter.xyz/docs/websocket-reference#market-stats

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        """
        await self.load_markets()
        market = self.market(symbol)
        request: dict = {
            'channel': 'market_stats/' + market['id'],
        }
        messageHash = self.get_message_hash('ticker', symbol)
        return await self.subscribe_public(messageHash, self.extend(request, params))

    async def un_watch_ticker(self, symbol: str, params={}) -> Any:
        """
        unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://apidocs.lighter.xyz/docs/websocket-reference#market-stats

        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        """
        await self.load_markets()
        market = self.market(symbol)
        request: dict = {
            'channel': 'market_stats/' + market['id'],
        }
        messageHash = self.get_message_hash('unsubscribe', symbol)
        return await self.unsubscribe_public(messageHash, self.extend(request, params))

    async def watch_tickers(self, symbols: Strings = None, params={}) -> Tickers:
        """

        https://apidocs.lighter.xyz/docs/websocket-reference#market-stats

        watches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list
        :param str[] [symbols]: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :param str [params.channel]: the channel to subscribe to, tickers by default. Can be tickers, sprd-tickers, index-tickers, block-tickers
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        """
        await self.load_markets()
        symbols = self.market_symbols(symbols)
        request: dict = {
            'channel': 'market_stats/all',
        }
        messageHashes = []
        if symbols is None or len(symbols) == 0:
            messageHashes.append(self.get_message_hash('ticker'))
        else:
            for i in range(0, len(symbols)):
                symbol = symbols[i]
                messageHashes.append(self.get_message_hash('ticker', symbol))
        newTicker = await self.subscribe_public_multiple(messageHashes, self.extend(request, params))
        if self.newUpdates:
            result = {}
            result[newTicker['symbol']] = newTicker
            return result
        return self.filter_by_array(self.tickers, 'symbol', symbols)

    async def un_watch_tickers(self, symbols: Strings = None, params={}) -> Any:
        """
        unWatches a price ticker, a statistical calculation with the information calculated over the past 24 hours for all markets of a specific list

        https://apidocs.lighter.xyz/docs/websocket-reference#market-stats

        :param str[] [symbols]: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        """
        await self.load_markets()
        request: dict = {
            'channel': 'market_stats/all',
        }
        messageHash = self.get_message_hash('unsubscribe')
        return await self.unsubscribe_public(messageHash, self.extend(request, params))

    async def watch_mark_price(self, symbol: str, params={}) -> Ticker:
        """

        https://apidocs.lighter.xyz/docs/websocket-reference#market-stats

        watches a mark price
        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        """
        return await self.watch_ticker(symbol, params)

    async def watch_mark_prices(self, symbols: Strings = None, params={}) -> Tickers:
        """

        https://apidocs.lighter.xyz/docs/websocket-reference#market-stats

        watches mark prices
        :param str[] [symbols]: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        """
        return await self.watch_tickers(symbols, params)

    async def un_watch_mark_price(self, symbol: str, params={}) -> Any:
        """

        https://apidocs.lighter.xyz/docs/websocket-reference#market-stats

        unWatches a mark price
        :param str symbol: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        """
        return await self.un_watch_ticker(symbol, params)

    async def un_watch_mark_prices(self, symbols: Strings = None, params={}) -> Any:
        """

        https://apidocs.lighter.xyz/docs/websocket-reference#market-stats

        unWatches mark prices
        :param str[] [symbols]: unified symbol of the market to fetch the ticker for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict: a `ticker structure <https://docs.ccxt.com/#/?id=ticker-structure>`
        """
        return await self.un_watch_tickers(symbols, params)

    def parse_ws_trade(self, trade, market=None):
        #
        #     {
        #         "trade_id": 526801155,
        #         "tx_hash": "1998d9df580acb7540aa141cc369d6ef926d003b3062196d2007bca15f978ab208e0caae4ac5872b",
        #         "type": "trade",
        #         "market_id": 0,
        #         "size": "0.0346",
        #         "price": "3028.85",
        #         "usd_amount": "104.798210",
        #         "ask_id": 281475673670566,
        #         "bid_id": 562949291740362,
        #         "ask_client_id": 76303170,
        #         "bid_client_id": 27601,
        #         "ask_account_id": 99349,
        #         "bid_account_id": 243008,
        #         "is_maker_ask": False,
        #         "block_height": 102322769,
        #         "timestamp": 1763623734215,
        #         "taker_position_size_before": "0.0346",
        #         "taker_entry_quote_before": "104.359926",
        #         "taker_initial_margin_fraction_before": 500,
        #         "taker_position_sign_changed": True,
        #         "maker_fee": 20,
        #         "maker_position_size_before": "2.1277",
        #         "maker_entry_quote_before": "6444.179555",
        #         "maker_initial_margin_fraction_before": 200
        #     }
        #
        timestamp = self.safe_integer(trade, 'timestamp')
        tradeId = self.safe_string(trade, 'trade_id')
        priceString = self.safe_string(trade, 'price')
        amountString = self.safe_string(trade, 'size')
        isMakerAsk = self.safe_bool(trade, 'is_maker_ask')
        side = 'sell' if (isMakerAsk is True) else 'buy'
        makerFeeRate = self.safe_string(market, 'maker_fee')
        maker = Precise.string_div(makerFeeRate, '100')
        feeAmount = Precise.string_mul(maker, makerFeeRate)
        return self.safe_trade({
            'info': trade,
            'id': tradeId,
            'order': None,
            'timestamp': timestamp,
            'datetime': self.iso8601(timestamp),
            'symbol': self.safe_symbol(None, market),
            'type': None,
            'side': side,
            'takerOrMaker': 'maker',
            'price': priceString,
            'amount': amountString,
            'cost': self.safe_string(trade, 'usd_amount'),
            'fee': {
                'cost': feeAmount,
                'currency': 'USDC',
            },
        }, market)

    def handle_trades(self, client: Client, message):
        #
        #     {
        #         "channel": "trade:0",
        #         "liquidation_trades": [],
        #         "nonce": 3159738569,
        #         "trades": [
        #             {
        #                 "trade_id": 526801155,
        #                 "tx_hash": "1998d9df580acb7540aa141cc369d6ef926d003b3062196d2007bca15f978ab208e0caae4ac5872b",
        #                 "type": "trade",
        #                 "market_id": 0,
        #                 "size": "0.0346",
        #                 "price": "3028.85",
        #                 "usd_amount": "104.798210",
        #                 "ask_id": 281475673670566,
        #                 "bid_id": 562949291740362,
        #                 "ask_client_id": 76303170,
        #                 "bid_client_id": 27601,
        #                 "ask_account_id": 99349,
        #                 "bid_account_id": 243008,
        #                 "is_maker_ask": False,
        #                 "block_height": 102322769,
        #                 "timestamp": 1763623734215,
        #                 "taker_position_size_before": "0.0346",
        #                 "taker_entry_quote_before": "104.359926",
        #                 "taker_initial_margin_fraction_before": 500,
        #                 "taker_position_sign_changed": True,
        #                 "maker_fee": 20,
        #                 "maker_position_size_before": "2.1277",
        #                 "maker_entry_quote_before": "6444.179555",
        #                 "maker_initial_margin_fraction_before": 200
        #             }
        #         ],
        #         "type": "subscribed/trade"
        #     }
        #
        liquidationData = self.safe_list(message, 'liquidation_trades', [])
        if len(liquidationData) > 0:
            self.handle_liquidation(client, message)
        data = self.safe_list(message, 'trades', [])
        channel = self.safe_string(message, 'channel', '')
        parts = channel.split(':')
        marketId = parts[1]
        market = self.safe_market(marketId)
        symbol = market['symbol']
        stored = self.safe_value(self.trades, symbol)
        if stored is None:
            limit = self.safe_integer(self.options, 'tradesLimit', 1000)
            stored = ArrayCache(limit)
            self.trades[symbol] = stored
        for i in range(0, len(data)):
            trade = self.parse_ws_trade(data[i], market)
            stored.append(trade)
        messageHash = self.get_message_hash('trade', symbol)
        client.resolve(stored, messageHash)

    async def watch_trades(self, symbol: str, since: Int = None, limit: Int = None, params={}) -> List[Trade]:
        """
        get the list of most recent trades for a particular symbol

        https://apidocs.lighter.xyz/docs/websocket-reference#trade

        :param str symbol: unified symbol of the market to fetch trades for
        :param int [since]: timestamp in ms of the earliest trade to fetch
        :param int [limit]: the maximum amount of trades to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        """
        await self.load_markets()
        market = self.market(symbol)
        request: dict = {
            'channel': 'trade/' + market['id'],
        }
        messageHash = self.get_message_hash('trade', market['symbol'])
        return await self.subscribe_public(messageHash, self.extend(request, params))

    async def un_watch_trades(self, symbol: str, params={}) -> Any:
        """
        unsubscribe from the trades channel

        https://apidocs.lighter.xyz/docs/websocket-reference#trade

        :param str symbol: unified symbol of the market to fetch trades for
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        """
        await self.load_markets()
        market = self.market(symbol)
        request: dict = {
            'channel': 'trade/' + market['id'],
        }
        messageHash = self.get_message_hash('unsubscribe', symbol)
        return await self.unsubscribe_public(messageHash, self.extend(request, params))

    def handle_my_trades(self, client: Client, message):
        #
        #     {
        #         "channel": "account_all_trades:723310",
        #         "trades": {
        #              13: [{
        #                  "trade_id": 526801155,
        #                  "tx_hash": "1998d9df580acb7540aa141cc369d6ef926d003b3062196d2007bca15f978ab208e0caae4ac5872b",
        #                  "type": "trade",
        #                  "market_id": 0,
        #                  "size": "0.0346",
        #                  "price": "3028.85",
        #                  "usd_amount": "104.798210",
        #                  "ask_id": 281475673670566,
        #                  "bid_id": 562949291740362,
        #                  "ask_client_id": 76303170,
        #                  "bid_client_id": 27601,
        #                  "ask_account_id": 99349,
        #                  "bid_account_id": 243008,
        #                  "is_maker_ask": False,
        #                  "block_height": 102322769,
        #                  "timestamp": 1763623734215,
        #                  "taker_position_size_before": "0.0346",
        #                  "taker_entry_quote_before": "104.359926",
        #                  "taker_initial_margin_fraction_before": 500,
        #                  "taker_position_sign_changed": True,
        #                  "maker_fee": 20,
        #                  "maker_position_size_before": "2.1277",
        #                  "maker_entry_quote_before": "6444.179555",
        #                  "maker_initial_margin_fraction_before": 200
        #              }]
        #         },
        #         "type": "update/account_all_trades"
        #     }
        #
        data = self.safe_dict(message, 'trades', {})
        marketIds = list(data.keys())
        idsLength = len(marketIds)
        if idsLength == 0:
            return False  # nothing to process
        if self.myTrades is None:
            limit = self.safe_integer(self.options, 'tradesLimit', 1000)
            self.myTrades = ArrayCache(limit)
        stored = self.myTrades
        messageHash = self.get_message_hash('myTrades')
        for i in range(0, len(marketIds)):
            marketId = marketIds[i]
            market = self.safe_market(marketId)
            trades = self.safe_list(data, marketId, [])
            for j in range(0, len(trades)):
                trade = self.parse_ws_trade(trades[j], market)
                stored.append(trade)
                symbol = trade['symbol']
                if symbol is not None:
                    symbolSpecificMessageHash = self.get_message_hash('myTrades', symbol)
                    client.resolve(stored, symbolSpecificMessageHash)
        client.resolve(stored, messageHash)
        return True

    async def watch_my_trades(self, symbol: Str = None, since: Int = None, limit: Int = None, params={}) -> List[Trade]:
        """
        subscribe to recent trades of an account.

        https://apidocs.lighter.xyz/docs/websocket-reference#account-all-trades

        :param str [symbol]: unified market symbol
        :param int [since]: timestamp in ms of the earliest trade to fetch
        :param int [limit]: the maximum amount of trades to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        """
        await self.load_markets()
        accountIndex = None
        accountIndex, params = await self.handleAccountIndex(params, 'watchMyTrades', 'accountIndex', 'account_index')
        messageHash = self.get_message_hash('myTrades')
        if symbol is not None:
            market = self.market(symbol)
            symbol = market['symbol']
            messageHash = self.get_message_hash('myTrades', symbol)
        request: dict = {
            'channel': 'account_all_trades/' + self.number_to_string(accountIndex),
        }
        trades = await self.subscribe_public(messageHash, self.extend(request, params))
        if self.newUpdates:
            limit = trades.getLimit(symbol, limit)
        return self.filter_by_symbol_since_limit(trades, symbol, since, limit, True)

    async def un_watch_my_trades(self, symbol: Str = None, params={}) -> Any:
        """
        unsubscribe from the account trades channel

        https://apidocs.lighter.xyz/docs/websocket-reference#account-all-trades

        :param str [symbol]: unified market symbol
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        """
        accountIndex
        accountIndex, params = await self.handleAccountIndex(params, 'unWatchMyTrades', 'accountIndex', 'account_index')
        messageHash = self.get_message_hash('unsubscribe', 'myTrades')
        if symbol is not None:
            await self.load_markets()
            market = self.market(symbol)
            symbol = market['symbol']
            messageHash = self.get_message_hash('unsubscribe', symbol)
        request: dict = {
            'channel': 'account_all_trades/' + accountIndex,
        }
        return await self.unsubscribe_public(messageHash, self.extend(request, params))

    def parse_ws_liquidation(self, liquidation, market=None):
        #
        #     {
        #         "trade_id": 526801155,
        #         "tx_hash": "1998d9df580acb7540aa141cc369d6ef926d003b3062196d2007bca15f978ab208e0caae4ac5872b",
        #         "type": "liquidation",
        #         "market_id": 0,
        #         "size": "0.0346",
        #         "price": "3028.85",
        #         "usd_amount": "104.798210",
        #         "ask_id": 281475673670566,
        #         "bid_id": 562949291740362,
        #         "ask_client_id": 76303170,
        #         "bid_client_id": 27601,
        #         "ask_account_id": 99349,
        #         "bid_account_id": 243008,
        #         "is_maker_ask": False,
        #         "block_height": 102322769,
        #         "timestamp": 1763623734215,
        #         "taker_position_size_before": "0.0346",
        #         "taker_entry_quote_before": "104.359926",
        #         "taker_initial_margin_fraction_before": 500,
        #         "taker_position_sign_changed": True,
        #         "maker_fee": 20,
        #         "maker_position_size_before": "2.1277",
        #         "maker_entry_quote_before": "6444.179555",
        #         "maker_initial_margin_fraction_before": 200
        #     }
        #
        timestamp = self.safe_integer(liquidation, 'timestamp')
        return self.safe_liquidation({
            'info': liquidation,
            'symbol': market['symbol'],
            'contracts': None,
            'contractSize': None,
            'price': self.safe_string(liquidation, 'price'),
            'side': self.safe_string(liquidation, 'size'),
            'baseValue': None,
            'quoteValue': None,
            'timestamp': timestamp,
            'datetime': self.iso8601(timestamp),
        })

    def handle_liquidation(self, client: Client, message):
        #
        #     {
        #         "channel": "trade:0",
        #         "liquidation_trades": [],
        #         "nonce": 3159738569,
        #         "trades": [
        #             {
        #                 "trade_id": 526801155,
        #                 "tx_hash": "1998d9df580acb7540aa141cc369d6ef926d003b3062196d2007bca15f978ab208e0caae4ac5872b",
        #                 "type": "trade",
        #                 "market_id": 0,
        #                 "size": "0.0346",
        #                 "price": "3028.85",
        #                 "usd_amount": "104.798210",
        #                 "ask_id": 281475673670566,
        #                 "bid_id": 562949291740362,
        #                 "ask_client_id": 76303170,
        #                 "bid_client_id": 27601,
        #                 "ask_account_id": 99349,
        #                 "bid_account_id": 243008,
        #                 "is_maker_ask": False,
        #                 "block_height": 102322769,
        #                 "timestamp": 1763623734215,
        #                 "taker_position_size_before": "0.0346",
        #                 "taker_entry_quote_before": "104.359926",
        #                 "taker_initial_margin_fraction_before": 500,
        #                 "taker_position_sign_changed": True,
        #                 "maker_fee": 20,
        #                 "maker_position_size_before": "2.1277",
        #                 "maker_entry_quote_before": "6444.179555",
        #                 "maker_initial_margin_fraction_before": 200
        #             }
        #         ],
        #         "type": "subscribed/trade"
        #     }
        #
        data = self.safe_list(message, 'liquidation_trades', [])
        channel = self.safe_string(message, 'channel', '')
        parts = channel.split(':')
        marketId = parts[1]
        market = self.safe_market(marketId)
        symbol = market['symbol']
        stored = self.safe_value(self.liquidations, symbol)
        if stored is None:
            limit = self.safe_integer(self.options, 'liquidationsLimit', 1000)
            self.liquidations = ArrayCache(limit)
            stored = self.liquidations
        for i in range(0, len(data)):
            liquidation = self.parse_ws_liquidation(data[i], market)
            stored.append(liquidation)
        messageHash = self.get_message_hash('liquidations', symbol)
        client.resolve(stored, messageHash)

    async def watch_liquidations(self, symbol: str, since: Int = None, limit: Int = None, params={}) -> List[Liquidation]:
        """
        watch the public liquidations of a trading pair

        https://apidocs.lighter.xyz/docs/websocket-reference#trade

        :param str symbol: unified symbol of the market to fetch trades for
        :param int [since]: timestamp in ms of the earliest trade to fetch
        :param int [limit]: the maximum amount of trades to fetch
        :param dict [params]: extra parameters specific to the exchange API endpoint
        :returns dict[]: a list of `trade structures <https://docs.ccxt.com/#/?id=public-trades>`
        """
        await self.load_markets()
        market = self.market(symbol)
        request: dict = {
            'channel': 'trade/' + market['id'],
        }
        messageHash = self.get_message_hash('liquidations', symbol)
        return await self.subscribe_public(messageHash, self.extend(request, params))

    def handle_error_message(self, client, message):
        #
        #     {
        #         "error": {
        #             "code": 30005,
        #             "message": "Invalid Channel:  (marketId)"
        #         }
        #     }
        #
        error = self.safe_dict(message, 'error')
        try:
            if error is not None:
                code = self.safe_string(message, 'code')
                if code is not None:
                    feedback = self.id + ' ' + self.json(message)
                    self.throw_exactly_matched_exception(self.exceptions['exact'], code, feedback)
        except Exception as e:
            client.reject(e)
        return True

    def handle_message(self, client: Client, message):
        if not self.handle_error_message(client, message):
            return
        type = self.safe_string(message, 'type', '')
        if type == 'ping':
            self.handle_ping(client, message)
            return
        channel = self.safe_string(message, 'channel', '')
        if channel.find('order_book:') >= 0:
            self.handle_order_book(client, message)
            return
        if channel.find('market_stats:') >= 0:
            self.handle_ticker(client, message)
            return
        if channel.find('trade:') >= 0:
            self.handle_trades(client, message)
            return
        if channel.find('account_all_trades:') >= 0:
            self.handle_my_trades(client, message)
            return
        if channel == '':
            self.handle_subscription_status(client, message)

    def handle_subscription_status(self, client: Client, message):
        #
        #     {
        #         "session_id": "8d354239-80e0-4b77-8763-87b6fef2f768",
        #         "type": "connected"
        #     }
        #
        #     {
        #         "type": "unsubscribed",
        #         "channel": "order_book:0"
        #     }
        #
        type = self.safe_string(message, 'type', '')
        id = self.safe_string(message, 'session_id')
        subscriptionsById = self.index_by(client.subscriptions, 'id')
        subscription = self.safe_dict(subscriptionsById, id, {})
        if type == 'unsubscribed':
            self.handle_un_subscription(client, subscription)
        return message

    def handle_un_subscription(self, client: Client, subscription: dict):
        messageHashes = self.safe_list(subscription, 'messageHashes', [])
        subMessageHashes = self.safe_list(subscription, 'subMessageHashes', [])
        for i in range(0, len(messageHashes)):
            unsubHash = messageHashes[i]
            subHash = subMessageHashes[i]
            self.clean_unsubscription(client, subHash, unsubHash)
        self.clean_cache(subscription)

    def handle_ping(self, client: Client, message):
        #
        #     {"type": "ping"}
        #
        self.spawn(self.pong, client, message)

    async def pong(self, client, message):
        request: dict = {
            'type': 'pong',
        }
        await client.send(request)
