Files
beast-trader/dashboard/venv/lib/python3.12/site-packages/ccxt/pro/lighter.py

1187 lines
51 KiB
Python

# -*- 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, Balances, Int, Liquidation, Order, 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': True,
'watchLiquidations': True,
'watchLiquidationsForSymbols': False,
'watchMyLiquidations': False,
'watchMyLiquidationsForSymbols': False,
'watchOHLCV': False,
'watchOHLCVForSymbols': False,
'watchOrders': True,
'watchMyTrades': True,
'watchPositions': False,
'watchFundingRate': False,
'watchFundingRates': False,
'unWatchOrderBook': True,
'unWatchTicker': True,
'unWatchTickers': True,
'unWatchTrades': True,
'unWatchMyTrades': True,
'unWatchMarkPrice': True,
'unWatchMarkPrices': True,
'unWatchOrders': 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(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)
async def subscribe_private(self, messageHash, params={}):
await self.preLoadLighterLibrary()
params['auth'] = self.createAuth(params)
return await self.subscribe_public(messageHash, params)
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(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(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 = []
symbolsLength = 0
if symbols is not None:
symbolsLength = len(symbols)
if symbolsLength == 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(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 = 'buy' if isMakerAsk else 'sell'
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': 'taker',
'price': priceString,
'amount': amountString,
'cost': self.safe_string(trade, 'usd_amount'),
'fee': None,
}, 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', [])
liquidationDataLength = len(liquidationData)
if liquidationDataLength > 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
dataLength = len(data)
for i in range(0, dataLength):
iReversed = dataLength - 1 - i
trade = self.parse_ws_trade(data[iReversed], 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'])
trades = await self.subscribe_public(messageHash, self.extend(request, params))
return self.filter_by_since_limit(trades, since, limit, 'timestamp', True)
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(messageHash, self.extend(request, params))
def parse_ws_order_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')
costString = self.safe_string(trade, 'usd_amount')
isMakerAsk = self.safe_bool(trade, 'is_maker_ask')
side = 'buy' if isMakerAsk else 'sell'
accountIndex = self.safe_integer(trade, 'accountIndex')
order = None
takerOrMaker = None
if accountIndex is not None:
if self.safe_integer(trade, 'bid_account_id') == accountIndex:
order = self.safe_string(trade, 'bid_id')
takerOrMaker = 'taker' if isMakerAsk else 'maker'
elif self.safe_integer(trade, 'ask_account_id') == accountIndex:
order = self.safe_string(trade, 'ask_id')
takerOrMaker = 'maker' if isMakerAsk else 'taker'
fee = None
if takerOrMaker is not None:
feeRateRaw = self.safe_string(trade, 'maker_fee') if (takerOrMaker == 'maker') else self.safe_string(trade, 'taker_fee')
feeRate = Precise.string_div(feeRateRaw, '1000000') if (feeRateRaw is not None) else '0'
feeAmount = Precise.string_mul(costString, feeRate)
fee = {
'cost': feeAmount,
'currency': 'USDC',
'rate': feeRate,
}
return self.safe_trade({
'info': trade,
'id': tradeId,
'order': order,
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
'symbol': self.safe_symbol(None, market),
'type': None,
'side': side,
'takerOrMaker': takerOrMaker,
'price': priceString,
'amount': amountString,
'cost': costString,
'fee': fee,
}, market)
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"
# }
#
channel = self.safe_string(message, 'channel', '')
parts = channel.split(':')
accountIndex = parts[1]
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, [])
tradesLength = len(trades)
for j in range(0, tradesLength):
jReversed = tradesLength - 1 - j
tradeRaw = trades[jReversed]
tradeRaw['accountIndex'] = accountIndex
trade = self.parse_ws_order_trade(tradeRaw, 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 = None
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(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')
isMakerAsk = self.safe_bool(liquidation, 'is_maker_ask')
side = 'buy' if isMakerAsk else 'sell'
contracts = self.safe_string(liquidation, 'size')
contractSize = self.safe_string(market, 'contractSize')
price = self.safe_string(liquidation, 'price')
baseValue = Precise.string_mul(contracts, contractSize)
quoteValue = Precise.string_mul(baseValue, price)
return self.safe_liquidation({
'info': liquidation,
'symbol': market['symbol'],
'contracts': contracts,
'contractSize': contractSize,
'price': price,
'side': side,
'baseValue': baseValue,
'quoteValue': quoteValue,
'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
dataLength = len(data)
for i in range(0, dataLength):
iReversed = dataLength - 1 - i
liquidation = self.parse_ws_liquidation(data[iReversed], 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))
async def watch_balance(self, params={}) -> Balances:
"""
watch balance and get the amount of funds available for trading or funds locked in orders
https://apidocs.lighter.xyz/docs/websocket-reference#account-all-assets
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.type]: 'spot' or 'swap', default is 'swap'
:returns dict: a `balance structure <https://docs.ccxt.com/?id=balance-structure>`
"""
await self.load_markets()
defaultType = self.safe_string_2(self.options, 'watchBalance', 'defaultType', 'spot')
type = None
type, params = self.handle_param_string(params, 'type', defaultType)
accountIndex = None
accountIndex, params = await self.handleAccountIndex(params, 'watchBalance', 'accountIndex', 'account_index')
messageHash = self.get_message_hash('balances', None, type)
request = {}
if type == 'spot':
request['channel'] = 'account_all_assets/' + self.number_to_string(accountIndex)
return await self.subscribe_private(messageHash, self.extend(request, params))
else:
request['channel'] = 'user_stats/' + self.number_to_string(accountIndex)
return await self.subscribe_public(messageHash, self.extend(request, params))
def handle_balance(self, client: Client, message):
#
# spot balance
# {
# "assets": {
# "1": {
# "symbol": "ETH",
# "asset_id": 1,
# "balance": "7.1072",
# "locked_balance": "0.0000"
# },
# "3": {
# "symbol": "USDC",
# "asset_id": 3,
# "balance": "6343.581906",
# "locked_balance": "297.000000"
# }
# },
# "channel": "account_all_assets:1234",
# "timestamp": 1773158679717,
# "type": "update/account_all_assets"
# }
#
# swap balance
# {
# "channel": "user_stats:10",
# "stats": {
# "collateral": "5000.00",
# "portfolio_value": "15000.00",
# "leverage": "3.0",
# "available_balance": "2000.00",
# "margin_usage": "0.80",
# "buying_power": "4000.00",
# "account_trading_mode": 1,
# "cross_stats":{
# "collateral":"0.000000",
# "portfolio_value":"0.000000",
# "leverage":"0.00",
# "available_balance":"0.000000",
# "margin_usage":"0.00",
# "buying_power":"0"
# },
# "total_stats":{
# "collateral":"0.000000",
# "portfolio_value":"0.000000",
# "leverage":"0.00",
# "available_balance":"0.000000",
# "margin_usage":"0.00",
# "buying_power":"0"
# }
# },
# "timestamp": 1773158679717,
# "type": "update/user_stats"
# }
#
channel = self.safe_string(message, 'channel', '')
type = 'spot'
if channel.find('user_stats:') >= 0:
type = 'swap'
balance = self.safe_dict(self.balance, type, {})
if type == 'spot':
assets = self.safe_dict(message, 'assets', {})
assetIds = list(assets.keys())
for i in range(0, len(assetIds)):
assetId = assetIds[i]
asset = assets[assetId]
codeId = self.safe_string(asset, 'symbol')
code = self.safe_currency_code(codeId)
account = self.account()
account['used'] = self.safe_string(asset, 'locked_balance')
account['total'] = self.safe_string(asset, 'balance')
balance[code] = account
else:
stats = self.safe_dict(message, 'stats', {})
account = self.account()
account['free'] = self.safe_string(stats, 'available_balance')
account['total'] = self.safe_string(stats, 'collateral')
account['info'] = stats
balance['USDC'] = account
timestamp = self.safe_integer(message, 'timestamp')
balance['timestamp'] = timestamp
balance['datetime'] = self.iso8601(timestamp)
self.balance[type] = self.safe_balance(balance)
messageHash = self.get_message_hash('balances', None, type)
client.resolve(self.balance[type], messageHash)
return True
async def watch_orders(self, symbol: Str = None, since: Int = None, limit: Int = None, params={}) -> List[Order]:
"""
watches information on multiple orders made by the user
https://apidocs.lighter.xyz/docs/websocket-reference#account-all-orders
:param str symbol: unified market symbol of the market orders were made in
:param int [since]: the earliest time in ms to fetch orders for
:param int [limit]: the maximum number of order structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
"""
await self.load_markets()
accountIndex = None
accountIndex, params = await self.handleAccountIndex(params, 'watchOrders', 'accountIndex', 'account_index')
messageHash = None
request = {}
if symbol is not None:
market = self.market(symbol)
messageHash = self.get_message_hash('orders', market['symbol'])
request['channel'] = 'account_orders/' + market['id'] + '/' + self.number_to_string(accountIndex)
else:
messageHash = self.get_message_hash('orders')
request['channel'] = 'account_all_orders/' + self.number_to_string(accountIndex)
orders = await self.subscribe_private(messageHash, self.extend(request, params))
if self.newUpdates:
limit = orders.getLimit(symbol, limit)
return self.filter_by_symbol_since_limit(orders, symbol, since, limit, True)
async def un_watch_orders(self, symbol: Str = None, params={}) -> Any:
"""
unWatches information on multiple orders made by the user
https://apidocs.lighter.xyz/docs/websocket-reference#account-all-orders
:param str symbol: unified market symbol of the market orders were made in
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
"""
await self.load_markets()
accountIndex = None
accountIndex, params = await self.handleAccountIndex(params, 'watchOrders', 'accountIndex', 'account_index')
messageHash = None
request = {}
if symbol is not None:
market = self.market(symbol)
messageHash = self.get_message_hash('orders', market['symbol'])
request['channel'] = 'account_orders/' + market['id'] + '/' + self.number_to_string(accountIndex)
else:
messageHash = self.get_message_hash('orders')
request['channel'] = 'account_all_orders/' + self.number_to_string(accountIndex)
return await self.unsubscribe(messageHash, self.extend(request, params))
def handle_orders(self, client: Client, message):
#
# {
# "account": {ACCOUNT_INDEX},
# "channel": "account_orders:{MARKET_INDEX}",
# "nonce": INTEGER,
# "orders": {
# "{MARKET_INDEX}": [Order] # the only present market index will be the one provided
# },
# "type": "update/account_orders"
# }
#
# {
# "channel": "account_all_orders:{ACCOUNT_ID}",
# "orders": {
# "{MARKET_INDEX}": [Order]
# },
# "type": "update/account_all_orders"
# }
#
data = self.safe_dict(message, 'orders', {})
marketIds = list(data.keys())
idsLength = len(marketIds)
if idsLength == 0:
return False # nothing to process
if self.orders is None:
limit = self.safe_integer(self.options, 'ordersLimit', 1000)
self.orders = ArrayCache(limit)
stored = self.orders
messageHash = self.get_message_hash('orders')
for i in range(0, len(marketIds)):
marketId = marketIds[i]
market = self.safe_market(marketId)
orders = self.safe_list(data, marketId, [])
for j in range(0, len(orders)):
order = self.parse_order(orders[j], market)
stored.append(order)
symbol = order['symbol']
if symbol is not None:
symbolSpecificMessageHash = self.get_message_hash('orders', symbol)
client.resolve(stored, symbolSpecificMessageHash)
client.resolve(stored, messageHash)
return True
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(error, '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.find('account_all_assets:') >= 0:
self.handle_balance(client, message)
return
if channel.find('user_stats:') >= 0:
self.handle_balance(client, message)
return
if channel.find('account_orders:') >= 0:
self.handle_orders(client, message)
return
if channel.find('account_all_orders:') >= 0:
self.handle_orders(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)