WebSocket
This page will help you get started with zkLighter WebSocket server. You'll be up and running in a jiffy!
Connection
URL: wss://zk-perps-api.elliot.ai/stream
You can directly connect to the WebSocket server using wscat:
wscat -c 'wss://zk-perps-api.elliot.ai/stream'
Channels
Order Book
The order book channel sends the new ask and bid orders for the given market.
{
"type": "subscribe",
"channel": "order_book/{MARKET_INDEX}"
}
Example Subscription
{
"type": "subscribe",
"channel": "order_book/0"
}
Response Structure
{
"channel": "order_book:{MARKET_INDEX}",
"offset": INTEGER,
"order_book": {
"code": INTEGER,
"asks": [
{
"price": STRING,
"size": STRING
}
],
"bids": [
{
"price": STRING,
"size": STRING
}
],
"offset": INTEGER
},
"type": "update/order_book"
}
Example Response
{
"channel": "order_book:0",
"offset": 41692864,
"order_book": {
"code": 0,
"asks": [
{
"price": "3327.46",
"size": "29.0915"
}
],
"bids": [
{
"price": "3338.80",
"size": "10.2898"
}
],
"offset": 41692864
},
"type": "update/order_book"
}
Market Stats
The market stats channel sends the market stat data for the given market.
{
"type": "subscribe",
"channel": "market_stats/{MARKET_INDEX}"
}
or
{
"type": "subscribe",
"channel": "market_stats/all"
}
Example Subscription
{
"type": "subscribe",
"channel": "market_stats/0"
}
Response Structure
{
"channel": "market_stats:0",
"market_stats": {
"market_id": INTEGER,
"index_price": STRING,
"mark_price": STRING,
"last_trade_price": STRING,
"funding_rate": STRING,
"funding_timestamp": INTEGER,
"daily_base_token_volume": FLOAT,
"daily_quote_token_volume": FLOAT,
"daily_price_low": FLOAT,
"daily_price_high": FLOAT,
"daily_price_change": FLOAT
},
"type": "update/market_stats"
}
Example Response
{
"channel": "market_stats:0",
"market_stats": {
"market_id": 0,
"index_price": "3335.04",
"mark_price": "3335.09",
"last_trade_price": "3335.65",
"funding_rate": "0.0042",
"funding_timestamp": 1722337200000,
"daily_base_token_volume": 230206.48999999944,
"daily_quote_token_volume": 765295250.9804002,
"daily_price_low": 3265.13,
"daily_price_high": 3386.01,
"daily_price_change": -1.1562612047992835
},
"type": "update/market_stats"
}
Trade
The trade channel sends the new trade data for the given market.
{
"type": "subscribe",
"channel": "trade/{MARKET_INDEX}"
}
Example Subscription
{
"type": "subscribe",
"channel": "trade/0"
}
Response Structure
{
"channel": "trade:{MARKET_INDEX}",
"trades": [
{
"trade_id": INTEGER,
"tx_hash": STRING,
"market_id": INTEGER,
"size": STRING,
"price": STRING,
"ask_id": INTEGER,
"bid_id": INTEGER,
"maker_account_id": INTEGER,
"taker_account_id": INTEGER,
"is_maker_ask": BOOLEAN,
"block_height": INTEGER,
"timestamp": INTEGER
}
],
"type": "update/trade"
}
Example Response
{
"channel": "trade:0",
"trades": [
{
"trade_id": 14035051,
"tx_hash": "189068ebc6b5c7e5efda96f92842a2fafd280990692e56899a98de8c4a12a38c",
"market_id": 0,
"size": "0.1187",
"price": "3335.65",
"ask_id": 41720126,
"bid_id": 41720037,
"maker_account_id": 2304,
"taker_account_id": 21504,
"is_maker_ask": false,
"block_height": 2204468,
"timestamp": 1722339648
}
],
"type": "update/trade"
}
Account
The account channel sends account market data for the given market and account.
{
"type": "subscribe",
"channel": "account/{MARKET_ID}/{ACCOUNT_ID}"
}
Example Subscription
{
"type": "subscribe",
"channel": "account/0/1"
}
Response Structure
{
"channel": "account:{MARKET_ID}:{ACCOUNT_ID}",
"account": INTEGER,
"orders": [
{
"market_index": INTEGER,
"owner_account_index": INTEGER,
"initial_base_amount": STRING,
"remaining_base_amount": STRING,
"filled_base_amount": STRING,
"filled_quote_amount": STRING,
"price": STRING,
"base_size": INTEGER,
"base_price": INTEGER,
"nonce": INTEGER,
"is_ask": BOOLEAN,
"side": STRING,
"type": STRING,
"status": STRING,
"block_height": INTEGER,
"timestamp": INTEGER
}
],
"position": {
"market_id": INTEGER,
"name": STRING,
"symbol": STRING,
"sign": INTEGER,
"position": STRING,
"ask_order_size": STRING,
"bid_order_size": STRING,
"avg_entry_price": STRING,
"position_value": STRING,
"unrealized_pnl": STRING,
"realized_pnl": STRING
},
"trades": [
{
"trade_id": INTEGER,
"tx_hash": STRING,
"market_id": INTEGER,
"size": STRING,
"price": STRING,
"ask_id": INTEGER,
"bid_id": INTEGER,
"maker_account_id": INTEGER,
"taker_account_id": INTEGER,
"is_maker_ask": BOOLEAN,
"block_height": INTEGER,
"timestamp": INTEGER
}
],
"type": "update/account"
}
Example Response
{
"account": 10,
"channel": "account:0:10",
"orders": [
{
"market_index": 0,
"owner_account_index": 2560,
"initial_base_amount": "1.9856",
"remaining_base_amount": "0.0000",
"filled_base_amount": "1.9856",
"filled_quote_amount": "0.000000",
"price": "3336.28",
"base_size": 0,
"base_price": 333628,
"nonce": 1099496791960,
"is_ask": false,
"side": "",
"type": "limit",
"status": "canceled",
"block_height": 2206280,
"timestamp": 1722340825
}
],
"position": {
"market_id": 0,
"name": "Ethereum",
"symbol": "ETH",
"sign": 1,
"position": "44.8174",
"ask_order_size": "18.0519",
"bid_order_size": "12.7786",
"avg_entry_price": "3338.17",
"position_value": "149531.462404",
"unrealized_pnl": "-76.678174",
"realized_pnl": "0.000000"
},
"trades": [
{
"trade_id": 1096415,
"tx_hash": "16a1b19d55e74bf8eb9cdddcd895f3c12689db9ca2d630d56e8b1d3bceb31ae4",
"market_id": 1,
"size": "0.02000",
"price": "57093.9",
"ask_id": 3566270,
"bid_id": 3143818,
"maker_account_id": 10497,
"taker_account_id": 512,
"is_maker_ask": false,
"block_height": 296525,
"timestamp": 1720816104
}
],
"type": "update/account"
}
Account All
The account all channel sends specific account market data for all markets.
{
"type": "subscribe",
"channel": "account_all/{ACCOUNT_ID}"
}
Example Subscription
{
"type": "subscribe",
"channel": "account_all/1"
}
Response Structure
{
"account": INTEGER,
"channel": "account_all:{ACCOUNT_ID}",
"orders": {
"{MARKET_INDEX}": [
{
"market_index": INTEGER,
"owner_account_index": INTEGER,
"initial_base_amount": STRING,
"remaining_base_amount": STRING,
"filled_base_amount": STRING,
"filled_quote_amount": STRING,
"price": STRING,
"base_size": INTEGER,
"base_price": INTEGER,
"nonce": INTEGER,
"is_ask": BOOLEAN,
"side": STRING,
"type": STRING,
"status": STRING,
"block_height": INTEGER,
"timestamp": INTEGER
}
]
}
"positions": {
"{MARKET_INDEX}": {
"market_id": INTEGER,
"name": STRING,
"symbol": STRING,
"sign": INTEGER,
"position": STRING,
"ask_order_size": STRING,
"bid_order_size": STRING,
"avg_entry_price": STRING,
"position_value": STRING,
"unrealized_pnl": STRING,
"realized_pnl": STRING
}
},
"trades": {
"{MARKET_INDEX}": [
{
"trade_id": INTEGER,
"tx_hash": STRING,
"market_id": INTEGER,
"size": STRING,
"price": STRING,
"ask_id": INTEGER,
"bid_id": INTEGER,
"maker_account_id": INTEGER,
"taker_account_id": INTEGER,
"is_maker_ask": BOOLEAN,
"block_height": INTEGER,
"timestamp": INTEGER
}
]
},
"type": "update/account"
}
Example Response
{
"account": 10,
"channel": "account_all:10",
"orders": {
"0": [
{
"market_index": 0,
"owner_account_index": 2560,
"initial_base_amount": "1.4056",
"remaining_base_amount": "1.4056",
"filled_base_amount": "0.0000",
"filled_quote_amount": "0.000000",
"price": "3333.13",
"base_size": 14056,
"base_price": 333313,
"nonce": 1099496783128,
"is_ask": false,
"side": "",
"type": "limit",
"status": "open",
"block_height": 2207022,
"timestamp": 1722341315
}
]
},
"positions": {
"0": {
"market_id": 0,
"name": "Ethereum",
"symbol": "ETH",
"sign": 1,
"position": "33.1669",
"ask_order_size": "11.2480",
"bid_order_size": "7.1520",
"avg_entry_price": "3335.19",
"position_value": "110580.766283",
"unrealized_pnl": "-37.304904",
"realized_pnl": "0.000000"
}
},
"trades": {
"0": [
{
"trade_id": 14055277,
"tx_hash": "2329c30ebcd0bdcd8e2c37e64f11adc3027cf149820d3b81c56d6d789e3202fb",
"market_id": 0,
"size": "0.0597",
"price": "3334.39",
"ask_id": 41783711,
"bid_id": 41783756,
"maker_account_id": 2560,
"taker_account_id": 37376,
"is_maker_ask": true,
"block_height": 2206851,
"timestamp": 1722341203
}
]
},
"type": "update/account"
}
Sub Account All
This is almost the same with the Account All channel. But this channel sends data for only the given sub-account.
{
"type": "subscribe",
"channel": "sub_account_all/{ACCOUNT_ID}/{SUB_ACCOUNT_ID}"
}
Account Stats
The account stats channel sends account stats data for the specific account.
{
"type": "subscribe",
"channel": "user_stats/{ACCOUNT_ID}"
}
Example Subscription
{
"type": "subscribe",
"channel": "user_stats/0"
}
Response Structure
{
"channel": "user_stats:{ACCOUNT_ID}",
"stats": {
"portfolio_value": STRING,
"leverage": STRING,
"free_collateral": STRING,
"margin_usage": STRING,
"buying_power": STRING
},
"type": "update/user_stats"
}
Example Response
{
"channel": "user_stats:0",
"stats": {
"portfolio_value": "1073703028.513526",
"leverage": "0.01",
"free_collateral": "1073310019.616992",
"margin_usage": "0.04",
"buying_power": "0"
},
"type": "update/user_stats"
}
Transaction
The transaction channel sends all new transactions.
{
"type": "subscribe",
"channel": "transaction"
}
Response Structure
{
"channel": "transaction",
"txs": [
{
"hash": STRING,
"type": INTEGER,
"info": {
"AccountIndex": INTEGER,
"SubAccountIndex": INTEGER,
"OrderBookIndex": INTEGER,
"BaseAmount": INTEGER,
"Price": INTEGER,
"IsAsk": INTEGER,
"OrderType": INTEGER,
"ExpiredAt": INTEGER,
"Nonce": INTEGER,
"Sig": STRING
},
"event_info": STRING,
"status": INTEGER,
"transaction_index": INTEGER,
"l1_address": STRING,
"account_index": INTEGER,
"nonce": INTEGER,
"expire_at": INTEGER,
"block_height": INTEGER,
"created_at": INTEGER,
"verify_at": INTEGER,
"sequence_index": INTEGER
}
],
"type": "update/transaction"
}
Example Response
{
"channel": "transaction",
"txs": [
{
"hash": "15c84b2464b0f4db5a17aa8eb7c6df374908989dc3b23859934a290691c988ce",
"type": 7,
"info": "{\"AccountIndex\":15,\"SubAccountIndex\":0,\"OrderBookIndex\":0,\"BaseAmount\":22195,\"Price\":334254,\"IsAsk\":0,\"OrderType\":0,\"ExpiredAt\":1722347430333,\"Nonce\":1351648,\"Sig\":\"ju2vhaeBgdWrdcSlP7SaX4hMUFYo6hvZN6H7LKz4FR8CXLLEKgpM/CiNzC6B0eH69Gj5xD5XzWxq6vW1j7jMqA==\"}",
"event_info": "",
"status": 1,
"transaction_index": 0,
"l1_address": "0x08E72c9bEDEe6BD9283921ECaFe4BE18e697dfA6",
"account_index": 15,
"nonce": 1351648,
"expire_at": 1722347430333,
"block_height": -1,
"created_at": -62135596800,
"verify_at": 0,
"sequence_index": 70285565
}
],
"type": "update/transaction"
}
Executed Transaction
The structure is the same as with Transaction channel. But this channel sends only executed transactions.
{
"type": "subscribe",
"channel": "executed_transaction"
}
Updated 5 months ago