@lifi/perps-sdk-provider-hyperliquid 0.0.0-preview-c3f47bd → 0.0.0-preview-2edcdb1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/HyperliquidProvider.d.ts.map +1 -1
- package/dist/cjs/HyperliquidProvider.js +2 -0
- package/dist/cjs/HyperliquidProvider.js.map +1 -1
- package/dist/cjs/accountConfig.d.ts.map +1 -1
- package/dist/cjs/accountConfig.js +1 -0
- package/dist/cjs/accountConfig.js.map +1 -1
- package/dist/cjs/accountSummary.d.ts.map +1 -1
- package/dist/cjs/accountSummary.js +5 -3
- package/dist/cjs/accountSummary.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/services/getAccount.d.ts.map +1 -1
- package/dist/cjs/services/getAccount.js +4 -4
- package/dist/cjs/services/getAccount.js.map +1 -1
- package/dist/cjs/services/getAccountExists.d.ts +8 -0
- package/dist/cjs/services/getAccountExists.d.ts.map +1 -0
- package/dist/cjs/services/getAccountExists.js +15 -0
- package/dist/cjs/services/getAccountExists.js.map +1 -0
- package/dist/cjs/types/account.d.ts +3 -0
- package/dist/cjs/types/account.d.ts.map +1 -1
- package/dist/cjs/types/account.js.map +1 -1
- package/dist/cjs/types/ws.d.ts +56 -1
- package/dist/cjs/types/ws.d.ts.map +1 -1
- package/dist/cjs/utils/decodeFastAssetCtxs.d.ts +1 -0
- package/dist/cjs/utils/decodeFastAssetCtxs.d.ts.map +1 -1
- package/dist/cjs/utils/decodeFastAssetCtxs.js +5 -1
- package/dist/cjs/utils/decodeFastAssetCtxs.js.map +1 -1
- package/dist/cjs/utils/index.d.ts +1 -1
- package/dist/cjs/utils/index.d.ts.map +1 -1
- package/dist/cjs/utils/index.js +2 -1
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/cjs/utils/mapOrder.d.ts.map +1 -1
- package/dist/cjs/utils/mapOrder.js +12 -5
- package/dist/cjs/utils/mapOrder.js.map +1 -1
- package/dist/cjs/websocket/HyperliquidWsProvider.d.ts +22 -0
- package/dist/cjs/websocket/HyperliquidWsProvider.d.ts.map +1 -1
- package/dist/cjs/websocket/HyperliquidWsProvider.js +430 -28
- package/dist/cjs/websocket/HyperliquidWsProvider.js.map +1 -1
- package/dist/esm/HyperliquidProvider.d.ts.map +1 -1
- package/dist/esm/HyperliquidProvider.js +2 -0
- package/dist/esm/HyperliquidProvider.js.map +1 -1
- package/dist/esm/accountConfig.d.ts.map +1 -1
- package/dist/esm/accountConfig.js +1 -0
- package/dist/esm/accountConfig.js.map +1 -1
- package/dist/esm/accountSummary.d.ts +2 -2
- package/dist/esm/accountSummary.d.ts.map +1 -1
- package/dist/esm/accountSummary.js +12 -9
- package/dist/esm/accountSummary.js.map +1 -1
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/services/getAccount.d.ts.map +1 -1
- package/dist/esm/services/getAccount.js +10 -7
- package/dist/esm/services/getAccount.js.map +1 -1
- package/dist/esm/services/getAccountExists.d.ts +22 -0
- package/dist/esm/services/getAccountExists.d.ts.map +1 -0
- package/dist/esm/services/getAccountExists.js +20 -0
- package/dist/esm/services/getAccountExists.js.map +1 -0
- package/dist/esm/types/account.d.ts +9 -0
- package/dist/esm/types/account.d.ts.map +1 -1
- package/dist/esm/types/account.js.map +1 -1
- package/dist/esm/types/ws.d.ts +75 -1
- package/dist/esm/types/ws.d.ts.map +1 -1
- package/dist/esm/utils/decodeFastAssetCtxs.d.ts +1 -0
- package/dist/esm/utils/decodeFastAssetCtxs.d.ts.map +1 -1
- package/dist/esm/utils/decodeFastAssetCtxs.js +9 -6
- package/dist/esm/utils/decodeFastAssetCtxs.js.map +1 -1
- package/dist/esm/utils/index.d.ts +1 -1
- package/dist/esm/utils/index.d.ts.map +1 -1
- package/dist/esm/utils/index.js +1 -1
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/esm/utils/mapOrder.d.ts +11 -1
- package/dist/esm/utils/mapOrder.d.ts.map +1 -1
- package/dist/esm/utils/mapOrder.js +23 -6
- package/dist/esm/utils/mapOrder.js.map +1 -1
- package/dist/esm/websocket/HyperliquidWsProvider.d.ts +27 -8
- package/dist/esm/websocket/HyperliquidWsProvider.d.ts.map +1 -1
- package/dist/esm/websocket/HyperliquidWsProvider.js +439 -47
- package/dist/esm/websocket/HyperliquidWsProvider.js.map +1 -1
- package/dist/types/HyperliquidProvider.d.ts.map +1 -1
- package/dist/types/accountConfig.d.ts.map +1 -1
- package/dist/types/accountSummary.d.ts +2 -2
- package/dist/types/accountSummary.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/services/getAccount.d.ts.map +1 -1
- package/dist/types/services/getAccountExists.d.ts +22 -0
- package/dist/types/services/getAccountExists.d.ts.map +1 -0
- package/dist/types/types/account.d.ts +9 -0
- package/dist/types/types/account.d.ts.map +1 -1
- package/dist/types/types/ws.d.ts +75 -1
- package/dist/types/types/ws.d.ts.map +1 -1
- package/dist/types/utils/decodeFastAssetCtxs.d.ts +1 -0
- package/dist/types/utils/decodeFastAssetCtxs.d.ts.map +1 -1
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/mapOrder.d.ts +11 -1
- package/dist/types/utils/mapOrder.d.ts.map +1 -1
- package/dist/types/websocket/HyperliquidWsProvider.d.ts +27 -8
- package/dist/types/websocket/HyperliquidWsProvider.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/HyperliquidProvider.ts +8 -0
- package/src/accountConfig.ts +1 -0
- package/src/accountSummary.ts +12 -9
- package/src/index.ts +2 -0
- package/src/services/getAccount.ts +10 -11
- package/src/services/getAccountExists.ts +40 -0
- package/src/types/account.ts +10 -0
- package/src/types/ws.ts +79 -1
- package/src/utils/decodeFastAssetCtxs.ts +10 -6
- package/src/utils/index.ts +4 -1
- package/src/utils/mapOrder.ts +23 -6
- package/src/websocket/HyperliquidWsProvider.ts +558 -49
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { getMarketRegistry, isActiveOrderStatus, ReconnectingWebSocket, resolveSubscribeQuote, WsProviderBase, wsLog, } from '@lifi/perps-sdk';
|
|
2
2
|
import { OrderSide, OrderType, } from '@lifi/perps-types';
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
|
|
3
|
+
import Big from 'big.js';
|
|
4
|
+
import { HYPERLIQUID_FEE_TIER_FALLBACK, SPOT_MARKET_ID } from '../constants.js';
|
|
5
|
+
import { decodeCompressedJson, decodeFastAssetCtxs, isOpenAssetPosition, isTriggerOrder, mapFill, mapMarketContext, mapOrderStatus, mapOrderType, mapPosition, priceStepToAggregation, spotAssetFromToken, spotBalance, spotPriceById, } from '../utils/index.js';
|
|
6
|
+
/** HL's compact `l2` snapshot carries 20 levels per side. */
|
|
6
7
|
const HL_L2_BOOK_MAX_LEVELS_PER_SIDE = 20;
|
|
7
8
|
/**
|
|
8
9
|
* `WsProviderFactory` constructor for Hyperliquid — pass to
|
|
@@ -33,14 +34,20 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
33
34
|
client;
|
|
34
35
|
registry;
|
|
35
36
|
perpCtxBySubDex = new Map();
|
|
37
|
+
spotCtxByMarketId = {};
|
|
38
|
+
orderbookKeysByMarketId = new Map();
|
|
39
|
+
latestOrderbookByMarketId = new Map();
|
|
36
40
|
// Latest mid/mark per `Market.id` from the high-frequency `fastAssetCtxs`
|
|
37
41
|
// feed (all dexes, incl. builder/sub-dex coins). Merged incrementally —
|
|
38
|
-
// frames carry only changed coins — and overlaid onto
|
|
39
|
-
//
|
|
42
|
+
// frames carry only changed coins — and overlaid onto asset contexts that
|
|
43
|
+
// carry oracle/funding/OI/metadata.
|
|
40
44
|
fastCtxByMarketId = {};
|
|
41
45
|
// `fastAssetCtxs` payloads are base64 + raw-DEFLATE; decode is async, so
|
|
42
46
|
// chain decodes to apply incremental frames in arrival order.
|
|
43
47
|
fastDecodeChain = Promise.resolve();
|
|
48
|
+
pacDecodeChain = Promise.resolve();
|
|
49
|
+
sacDecodeChain = Promise.resolve();
|
|
50
|
+
orderbookDecodeChain = Promise.resolve();
|
|
44
51
|
constructor(wsUrl, providerKey, client) {
|
|
45
52
|
super(new ReconnectingWebSocket(wsUrl, { pingPayload: '{"method":"ping"}' }), providerKey);
|
|
46
53
|
this.client = client;
|
|
@@ -61,9 +68,8 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
61
68
|
this.claimOrderUpdatesKey(this.toKey(sub));
|
|
62
69
|
}
|
|
63
70
|
await this.registry?.sync();
|
|
64
|
-
// Markets context aggregates
|
|
65
|
-
//
|
|
66
|
-
// feed, which supplies fast mid + mark for every market across all dexes.
|
|
71
|
+
// Markets context aggregates slower asset-context feeds with fast mid/mark
|
|
72
|
+
// ticks from `fastAssetCtxs`.
|
|
67
73
|
if (sub.channel === 'marketsContext') {
|
|
68
74
|
const entries = this.getMarketsContextSubEntries();
|
|
69
75
|
for (const { subKey, payload } of entries) {
|
|
@@ -76,20 +82,33 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
76
82
|
this.rws.send(JSON.stringify({ method: 'unsubscribe', subscription: payload }));
|
|
77
83
|
}
|
|
78
84
|
this.perpCtxBySubDex.clear();
|
|
85
|
+
this.spotCtxByMarketId = {};
|
|
79
86
|
this.fastCtxByMarketId = {};
|
|
80
87
|
};
|
|
81
88
|
}
|
|
82
89
|
// All other channels: single WS subscription per key
|
|
83
90
|
const key = this.toKey(sub);
|
|
91
|
+
if (sub.channel === 'orderbook') {
|
|
92
|
+
this.claimOrderbookKey(sub.marketId, key);
|
|
93
|
+
}
|
|
84
94
|
const payload = this.toHlPayload(sub);
|
|
85
95
|
await this.registerSub(key, payload);
|
|
96
|
+
if (sub.channel === 'orderbook') {
|
|
97
|
+
this.registerOrderbook(sub.marketId, key);
|
|
98
|
+
}
|
|
86
99
|
await this.rws.ready();
|
|
87
100
|
return () => {
|
|
88
101
|
this.unregisterSub(key);
|
|
89
102
|
if (this.orderUpdatesKey === key) {
|
|
90
103
|
this.orderUpdatesKey = undefined;
|
|
91
104
|
}
|
|
92
|
-
this.rws.send(JSON.stringify({
|
|
105
|
+
this.rws.send(JSON.stringify({
|
|
106
|
+
method: 'unsubscribe',
|
|
107
|
+
subscription: payload,
|
|
108
|
+
}));
|
|
109
|
+
if (sub.channel === 'orderbook') {
|
|
110
|
+
this.releaseOrderbook(sub.marketId, key);
|
|
111
|
+
}
|
|
93
112
|
};
|
|
94
113
|
}
|
|
95
114
|
/**
|
|
@@ -110,20 +129,62 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
110
129
|
}
|
|
111
130
|
this.orderUpdatesKey = key;
|
|
112
131
|
}
|
|
132
|
+
claimOrderbookKey(marketId, key) {
|
|
133
|
+
const activeKeys = this.orderbookKeysByMarketId.get(marketId);
|
|
134
|
+
if (activeKeys === undefined) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
for (const activeKey of [...activeKeys]) {
|
|
138
|
+
if (activeKey === key) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (!this.closeChannelIfIdle(activeKey)) {
|
|
142
|
+
throw new Error(`Hyperliquid supports one orderbook aggregation per market per ` +
|
|
143
|
+
`provider instance. Unsubscribe ${activeKey} before subscribing ${key}.`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
113
147
|
/**
|
|
114
|
-
* Sub-key + payload pairs for the markets-context aggregation:
|
|
115
|
-
* perp
|
|
148
|
+
* Sub-key + payload pairs for the markets-context aggregation: compressed
|
|
149
|
+
* all-perp (`pac`) and all-spot (`sac`) asset contexts plus the documented
|
|
116
150
|
* `fastAssetCtxs` feed (high-frequency mid + mark across every dex).
|
|
117
151
|
*/
|
|
118
152
|
getMarketsContextSubEntries() {
|
|
119
153
|
return [
|
|
120
|
-
{ subKey: '
|
|
154
|
+
{ subKey: 'pac', payload: { type: 'pac' } },
|
|
155
|
+
{ subKey: 'sac', payload: { type: 'sac' } },
|
|
121
156
|
{ subKey: 'fastAssetCtxs', payload: { type: 'fastAssetCtxs' } },
|
|
122
157
|
];
|
|
123
158
|
}
|
|
159
|
+
registerOrderbook(marketId, orderbookKey) {
|
|
160
|
+
let activeKeys = this.orderbookKeysByMarketId.get(marketId);
|
|
161
|
+
if (activeKeys === undefined) {
|
|
162
|
+
activeKeys = new Set();
|
|
163
|
+
this.orderbookKeysByMarketId.set(marketId, activeKeys);
|
|
164
|
+
}
|
|
165
|
+
activeKeys.add(orderbookKey);
|
|
166
|
+
}
|
|
167
|
+
releaseOrderbook(marketId, orderbookKey) {
|
|
168
|
+
const activeKeys = this.orderbookKeysByMarketId.get(marketId);
|
|
169
|
+
if (activeKeys === undefined) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
activeKeys.delete(orderbookKey);
|
|
173
|
+
if (activeKeys.size > 0) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
this.orderbookKeysByMarketId.delete(marketId);
|
|
177
|
+
}
|
|
124
178
|
onClose() {
|
|
125
179
|
this.perpCtxBySubDex.clear();
|
|
180
|
+
this.spotCtxByMarketId = {};
|
|
126
181
|
this.fastCtxByMarketId = {};
|
|
182
|
+
this.orderbookKeysByMarketId.clear();
|
|
183
|
+
this.latestOrderbookByMarketId.clear();
|
|
184
|
+
this.fastDecodeChain = Promise.resolve();
|
|
185
|
+
this.pacDecodeChain = Promise.resolve();
|
|
186
|
+
this.sacDecodeChain = Promise.resolve();
|
|
187
|
+
this.orderbookDecodeChain = Promise.resolve();
|
|
127
188
|
this.orderUpdatesKey = undefined;
|
|
128
189
|
}
|
|
129
190
|
sendSubscribe(payload) {
|
|
@@ -133,10 +194,14 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
133
194
|
switch (sub.channel) {
|
|
134
195
|
case 'marketsContext':
|
|
135
196
|
return 'marketsContext';
|
|
197
|
+
case 'marketContext':
|
|
198
|
+
return `marketContext:${sub.marketId}`;
|
|
136
199
|
case 'orderbook':
|
|
137
|
-
return `
|
|
200
|
+
return `l2:${sub.marketId}:${this.orderbookAggregationKey(sub.marketId, sub.priceStep)}`;
|
|
138
201
|
case 'candle':
|
|
139
202
|
return `candle:${sub.marketId}:${sub.interval}`;
|
|
203
|
+
case 'trades':
|
|
204
|
+
return `trades:${sub.marketId}`;
|
|
140
205
|
case 'orderUpdates':
|
|
141
206
|
return `orderUpdates:${sub.address.toLowerCase()}`;
|
|
142
207
|
case 'fills':
|
|
@@ -147,29 +212,52 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
147
212
|
return `spotState:${sub.address.toLowerCase()}`;
|
|
148
213
|
}
|
|
149
214
|
}
|
|
215
|
+
orderbookAggregation(marketId, priceStep) {
|
|
216
|
+
if (priceStep === undefined) {
|
|
217
|
+
return {};
|
|
218
|
+
}
|
|
219
|
+
return priceStepToAggregation(priceStep, this.orderbookReferencePrice(marketId));
|
|
220
|
+
}
|
|
221
|
+
orderbookAggregationKey(marketId, priceStep) {
|
|
222
|
+
const aggregation = this.orderbookAggregation(marketId, priceStep);
|
|
223
|
+
const nSigFigs = aggregation.nSigFigs ?? null;
|
|
224
|
+
const mantissa = aggregation.mantissa ?? null;
|
|
225
|
+
if (nSigFigs === null && mantissa === null) {
|
|
226
|
+
return 'full';
|
|
227
|
+
}
|
|
228
|
+
return `s:${nSigFigs}:m:${mantissa}`;
|
|
229
|
+
}
|
|
150
230
|
toHlPayload(sub) {
|
|
151
231
|
switch (sub.channel) {
|
|
152
232
|
case 'marketsContext':
|
|
153
233
|
// Handled via getMarketsContextSubEntries in openChannel; never reaches
|
|
154
234
|
// toHlPayload, but TS requires an exhaustive switch.
|
|
155
235
|
return { type: 'allDexsAssetCtxs' };
|
|
156
|
-
case '
|
|
236
|
+
case 'marketContext':
|
|
157
237
|
return {
|
|
158
|
-
type: '
|
|
238
|
+
type: 'activeAssetCtx',
|
|
159
239
|
coin: sub.marketId,
|
|
160
|
-
// HL's l2Book ignores any level-count field; it returns up to 20
|
|
161
|
-
// levels/side and controls granularity via nSigFigs (+ mantissa,
|
|
162
|
-
// valid only when nSigFigs === 5).
|
|
163
|
-
...(sub.priceStep !== undefined
|
|
164
|
-
? priceStepToAggregation(sub.priceStep, this.mergedMids().get(sub.marketId) ?? Number.NaN)
|
|
165
|
-
: {}),
|
|
166
240
|
};
|
|
241
|
+
case 'orderbook': {
|
|
242
|
+
const aggregation = this.orderbookAggregation(sub.marketId, sub.priceStep);
|
|
243
|
+
return {
|
|
244
|
+
type: 'l2',
|
|
245
|
+
c: sub.marketId,
|
|
246
|
+
s: aggregation.nSigFigs ?? null,
|
|
247
|
+
m: aggregation.mantissa ?? null,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
167
250
|
case 'candle':
|
|
168
251
|
return {
|
|
169
252
|
type: 'candle',
|
|
170
253
|
coin: sub.marketId,
|
|
171
254
|
interval: sub.interval,
|
|
172
255
|
};
|
|
256
|
+
case 'trades':
|
|
257
|
+
return {
|
|
258
|
+
type: 'trades',
|
|
259
|
+
coin: sub.marketId,
|
|
260
|
+
};
|
|
173
261
|
case 'orderUpdates':
|
|
174
262
|
return { type: 'orderUpdates', user: sub.address };
|
|
175
263
|
case 'fills':
|
|
@@ -207,15 +295,33 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
207
295
|
case 'allDexsAssetCtxs':
|
|
208
296
|
this.handleAllDexsAssetCtxs(msg.data);
|
|
209
297
|
break;
|
|
298
|
+
case 'pac':
|
|
299
|
+
this.handlePac(msg.data);
|
|
300
|
+
break;
|
|
301
|
+
case 'sac':
|
|
302
|
+
this.handleSac(msg.data);
|
|
303
|
+
break;
|
|
210
304
|
case 'fastAssetCtxs':
|
|
211
305
|
this.handleFastAssetCtxs(msg.data);
|
|
212
306
|
break;
|
|
307
|
+
case 'activeAssetCtx':
|
|
308
|
+
this.handleActiveAssetCtx(msg.data, raw);
|
|
309
|
+
break;
|
|
310
|
+
case 'activeSpotAssetCtx':
|
|
311
|
+
this.handleActiveSpotAssetCtx(msg.data);
|
|
312
|
+
break;
|
|
313
|
+
case 'l2':
|
|
314
|
+
this.handleL2(msg.data);
|
|
315
|
+
break;
|
|
213
316
|
case 'l2Book':
|
|
214
317
|
this.handleL2Book(msg.data);
|
|
215
318
|
break;
|
|
216
319
|
case 'candle':
|
|
217
320
|
this.handleCandle(msg.data);
|
|
218
321
|
break;
|
|
322
|
+
case 'trades':
|
|
323
|
+
this.handleTrades(msg.data);
|
|
324
|
+
break;
|
|
219
325
|
case 'orderUpdates':
|
|
220
326
|
this.handleOrderUpdates(msg.data);
|
|
221
327
|
break;
|
|
@@ -235,14 +341,65 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
235
341
|
}
|
|
236
342
|
}
|
|
237
343
|
handleAllDexsAssetCtxs(data) {
|
|
238
|
-
|
|
239
|
-
|
|
344
|
+
this.mergePerpAssetCtxEntries(allDexsAssetCtxEntries(data));
|
|
345
|
+
this.emitMarketsContext();
|
|
346
|
+
}
|
|
347
|
+
handlePac(base64) {
|
|
348
|
+
this.pacDecodeChain = this.pacDecodeChain
|
|
349
|
+
.then(async () => {
|
|
350
|
+
this.mergePerpAssetCtxEntries(await decodeCompressedJson(base64));
|
|
351
|
+
this.emitMarketsContext();
|
|
352
|
+
})
|
|
353
|
+
.catch((error) => wsLog.handlerFailure(this.providerKey, error));
|
|
354
|
+
}
|
|
355
|
+
mergePerpAssetCtxEntries(entries) {
|
|
356
|
+
for (const [dex, ctxs] of entries) {
|
|
357
|
+
const previous = this.perpCtxBySubDex.get(dex || 'default');
|
|
358
|
+
const byMarketId = {
|
|
359
|
+
...(previous ?? {}),
|
|
360
|
+
};
|
|
240
361
|
for (const ctx of ctxs) {
|
|
241
|
-
|
|
362
|
+
const marketId = ctx.coin;
|
|
363
|
+
if (marketId === undefined) {
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
const merged = mergePerpAssetCtx(previous?.[marketId], marketId, ctx);
|
|
367
|
+
if (merged !== undefined) {
|
|
368
|
+
byMarketId[marketId] = merged;
|
|
369
|
+
}
|
|
242
370
|
}
|
|
243
371
|
this.perpCtxBySubDex.set(dex || 'default', byMarketId);
|
|
244
372
|
}
|
|
245
|
-
|
|
373
|
+
}
|
|
374
|
+
handleSac(base64) {
|
|
375
|
+
this.sacDecodeChain = this.sacDecodeChain
|
|
376
|
+
.then(async () => {
|
|
377
|
+
const spotMarketIdBySacKey = this.spotMarketIdBySacKey();
|
|
378
|
+
const ctxs = await decodeCompressedJson(base64);
|
|
379
|
+
for (const [sacKey, ctx] of Object.entries(ctxs)) {
|
|
380
|
+
const marketId = spotMarketIdBySacKey.get(sacKey);
|
|
381
|
+
if (marketId === undefined) {
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
this.spotCtxByMarketId[marketId] = {
|
|
385
|
+
...this.spotCtxByMarketId[marketId],
|
|
386
|
+
...ctx,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
this.emitMarketsContext();
|
|
390
|
+
})
|
|
391
|
+
.catch((error) => wsLog.handlerFailure(this.providerKey, error));
|
|
392
|
+
}
|
|
393
|
+
spotMarketIdBySacKey() {
|
|
394
|
+
const map = new Map();
|
|
395
|
+
for (const market of this.registry?.markets ?? []) {
|
|
396
|
+
if (market.categoryId !== SPOT_MARKET_ID) {
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
map.set(market.id, market.id);
|
|
400
|
+
map.set(`${market.baseAsset.displaySymbol}/${market.quoteAsset.displaySymbol}`, market.id);
|
|
401
|
+
}
|
|
402
|
+
return map;
|
|
246
403
|
}
|
|
247
404
|
/**
|
|
248
405
|
* Decode a `fastAssetCtxs` frame (base64 + raw-DEFLATE) and merge its changed
|
|
@@ -266,11 +423,8 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
266
423
|
.catch((error) => wsLog.handlerFailure(this.providerKey, error));
|
|
267
424
|
}
|
|
268
425
|
/**
|
|
269
|
-
* Build the all-markets context map
|
|
270
|
-
*
|
|
271
|
-
* high-frequency `fastAssetCtxs` feed. A market present in `fastAssetCtxs` but
|
|
272
|
-
* without an asset-context frame yet (the aggregate feed is slow/idle) still
|
|
273
|
-
* gets a mid+mark entry, so price ticks without waiting on the aggregate.
|
|
426
|
+
* Build the all-markets context map from perp + spot asset contexts, with mid
|
|
427
|
+
* + mark overlaid from `fastAssetCtxs`.
|
|
274
428
|
*/
|
|
275
429
|
emitMarketsContext() {
|
|
276
430
|
const data = {};
|
|
@@ -285,6 +439,13 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
285
439
|
};
|
|
286
440
|
}
|
|
287
441
|
}
|
|
442
|
+
for (const [marketId, ctx] of Object.entries(this.spotCtxByMarketId)) {
|
|
443
|
+
const context = mapSpotMarketContext(marketId, ctx, this.fastCtxByMarketId[marketId]);
|
|
444
|
+
if (context === undefined) {
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
data[marketId] = context;
|
|
448
|
+
}
|
|
288
449
|
for (const [marketId, fast] of Object.entries(this.fastCtxByMarketId)) {
|
|
289
450
|
if (data[marketId] !== undefined) {
|
|
290
451
|
continue;
|
|
@@ -299,7 +460,28 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
299
460
|
}
|
|
300
461
|
this.emit('marketsContext', { channel: 'marketsContext', data });
|
|
301
462
|
}
|
|
302
|
-
|
|
463
|
+
handleActiveAssetCtx(data, raw) {
|
|
464
|
+
const ctx = activePerpAssetCtx(data.coin, data.ctx);
|
|
465
|
+
if (ctx === undefined) {
|
|
466
|
+
wsLog.parseFailure(this.providerKey, raw);
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
this.emit(`marketContext:${data.coin}`, {
|
|
470
|
+
channel: 'marketContext',
|
|
471
|
+
data: mapMarketContext(data.coin, ctx),
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
handleActiveSpotAssetCtx(data) {
|
|
475
|
+
const context = mapSpotMarketContext(data.coin, data.ctx);
|
|
476
|
+
if (context === undefined) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
this.emit(`marketContext:${data.coin}`, {
|
|
480
|
+
channel: 'marketContext',
|
|
481
|
+
data: context,
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
/** Latest mid per `Market.id` across asset-context and fast feeds. */
|
|
303
485
|
mergedMids() {
|
|
304
486
|
const map = new Map();
|
|
305
487
|
for (const byMarketId of this.perpCtxBySubDex.values()) {
|
|
@@ -308,6 +490,12 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
308
490
|
map.set(id, Number(mid));
|
|
309
491
|
}
|
|
310
492
|
}
|
|
493
|
+
for (const [id, ctx] of Object.entries(this.spotCtxByMarketId)) {
|
|
494
|
+
const mid = ctx.midPx ?? ctx.markPx;
|
|
495
|
+
if (mid != null) {
|
|
496
|
+
map.set(id, Number(mid));
|
|
497
|
+
}
|
|
498
|
+
}
|
|
311
499
|
for (const [id, fast] of Object.entries(this.fastCtxByMarketId)) {
|
|
312
500
|
const mid = fast.midPx ?? fast.markPx;
|
|
313
501
|
if (mid != null) {
|
|
@@ -316,21 +504,74 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
316
504
|
}
|
|
317
505
|
return map;
|
|
318
506
|
}
|
|
507
|
+
orderbookReferencePrice(marketId) {
|
|
508
|
+
const mid = this.mergedMids().get(marketId);
|
|
509
|
+
if (mid !== undefined) {
|
|
510
|
+
return mid;
|
|
511
|
+
}
|
|
512
|
+
const book = this.latestOrderbookByMarketId.get(marketId);
|
|
513
|
+
const bid = book?.bids[0]?.price;
|
|
514
|
+
const ask = book?.asks[0]?.price;
|
|
515
|
+
if (bid !== undefined && ask !== undefined) {
|
|
516
|
+
return (Number(bid) + Number(ask)) / 2;
|
|
517
|
+
}
|
|
518
|
+
return Number.NaN;
|
|
519
|
+
}
|
|
319
520
|
handleL2Book(data) {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
521
|
+
const book = {
|
|
522
|
+
provider: this.providerKey,
|
|
523
|
+
marketId: data.coin,
|
|
524
|
+
bids: data.levels[0]
|
|
525
|
+
.slice(0, HL_L2_BOOK_MAX_LEVELS_PER_SIDE)
|
|
526
|
+
.map((l) => ({ price: l.px, size: l.sz })),
|
|
527
|
+
asks: data.levels[1]
|
|
528
|
+
.slice(0, HL_L2_BOOK_MAX_LEVELS_PER_SIDE)
|
|
529
|
+
.map((l) => ({ price: l.px, size: l.sz })),
|
|
530
|
+
timestamp: data.time,
|
|
531
|
+
};
|
|
532
|
+
this.latestOrderbookByMarketId.set(data.coin, book);
|
|
533
|
+
this.emitOrderbook(data.coin, book);
|
|
534
|
+
}
|
|
535
|
+
handleL2(data) {
|
|
536
|
+
if (data.s !== undefined) {
|
|
537
|
+
this.handleL2Book(data.s);
|
|
538
|
+
}
|
|
539
|
+
if (data.u !== undefined) {
|
|
540
|
+
this.handleCompactL2Delta(data.u);
|
|
541
|
+
}
|
|
542
|
+
if (data.c === undefined) {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
const compressed = data.c;
|
|
546
|
+
this.orderbookDecodeChain = this.orderbookDecodeChain
|
|
547
|
+
.then(async () => {
|
|
548
|
+
this.handleCompactL2Delta(await decodeCompressedJson(compressed));
|
|
549
|
+
})
|
|
550
|
+
.catch((error) => wsLog.handlerFailure(this.providerKey, error));
|
|
551
|
+
}
|
|
552
|
+
handleCompactL2Delta(delta) {
|
|
553
|
+
const previous = this.latestOrderbookByMarketId.get(delta.c);
|
|
554
|
+
if (previous === undefined) {
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
const book = {
|
|
558
|
+
provider: this.providerKey,
|
|
559
|
+
marketId: delta.c,
|
|
560
|
+
bids: applyCompressedL2Side(previous.bids, delta.l[0], delta.r?.[0] ?? [], 'bid'),
|
|
561
|
+
asks: applyCompressedL2Side(previous.asks, delta.l[1], delta.r?.[1] ?? [], 'ask'),
|
|
562
|
+
timestamp: delta.t,
|
|
563
|
+
};
|
|
564
|
+
this.latestOrderbookByMarketId.set(delta.c, book);
|
|
565
|
+
this.emitOrderbook(delta.c, book);
|
|
566
|
+
}
|
|
567
|
+
emitOrderbook(marketId, book) {
|
|
568
|
+
const keys = this.orderbookKeysByMarketId.get(marketId);
|
|
569
|
+
if (keys === undefined) {
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
for (const key of keys) {
|
|
573
|
+
this.emit(key, { channel: 'orderbook', data: book });
|
|
574
|
+
}
|
|
334
575
|
}
|
|
335
576
|
handleCandle(data) {
|
|
336
577
|
this.emit(`candle:${data.s}:${data.i}`, {
|
|
@@ -345,6 +586,24 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
345
586
|
},
|
|
346
587
|
});
|
|
347
588
|
}
|
|
589
|
+
handleTrades(data) {
|
|
590
|
+
for (const trade of data) {
|
|
591
|
+
this.emit(`trades:${trade.coin}`, {
|
|
592
|
+
channel: 'trades',
|
|
593
|
+
data: [
|
|
594
|
+
{
|
|
595
|
+
provider: this.providerKey,
|
|
596
|
+
marketId: trade.coin,
|
|
597
|
+
price: trade.px,
|
|
598
|
+
size: trade.sz,
|
|
599
|
+
timestamp: trade.time,
|
|
600
|
+
side: trade.side === 'B' ? 'buy' : 'sell',
|
|
601
|
+
id: trade.tid !== undefined ? String(trade.tid) : trade.hash,
|
|
602
|
+
},
|
|
603
|
+
],
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
}
|
|
348
607
|
handleOrderUpdates(data) {
|
|
349
608
|
// Untagged frame: attributable only via the single-address invariant.
|
|
350
609
|
const key = this.orderUpdatesKey;
|
|
@@ -444,6 +703,132 @@ export class HyperliquidWsProvider extends WsProviderBase {
|
|
|
444
703
|
}
|
|
445
704
|
}
|
|
446
705
|
const isObject = (v) => typeof v === 'object' && v !== null;
|
|
706
|
+
const toMarketContextString = (value) => typeof value === 'string' || typeof value === 'number'
|
|
707
|
+
? String(value)
|
|
708
|
+
: undefined;
|
|
709
|
+
const toMarketCapString = (price, circulatingSupply) => {
|
|
710
|
+
const priceString = toMarketContextString(price);
|
|
711
|
+
const supplyString = toMarketContextString(circulatingSupply);
|
|
712
|
+
if (priceString === undefined || supplyString === undefined) {
|
|
713
|
+
return undefined;
|
|
714
|
+
}
|
|
715
|
+
try {
|
|
716
|
+
const parsedPrice = new Big(priceString);
|
|
717
|
+
const parsedSupply = new Big(supplyString);
|
|
718
|
+
if (parsedPrice.lte(0) || parsedSupply.lte(0)) {
|
|
719
|
+
return undefined;
|
|
720
|
+
}
|
|
721
|
+
return parsedPrice.times(parsedSupply).toFixed();
|
|
722
|
+
}
|
|
723
|
+
catch {
|
|
724
|
+
return undefined;
|
|
725
|
+
}
|
|
726
|
+
};
|
|
727
|
+
function mergePerpAssetCtx(previous, marketId, update) {
|
|
728
|
+
const funding = update.funding ?? previous?.funding;
|
|
729
|
+
const openInterest = update.openInterest ?? previous?.openInterest;
|
|
730
|
+
const dayNtlVlm = update.dayNtlVlm ?? previous?.dayNtlVlm;
|
|
731
|
+
const prevDayPx = update.prevDayPx ?? previous?.prevDayPx;
|
|
732
|
+
const markPx = update.markPx ?? previous?.markPx;
|
|
733
|
+
const midPx = 'midPx' in update ? update.midPx : previous?.midPx;
|
|
734
|
+
const oraclePx = update.oraclePx ?? previous?.oraclePx;
|
|
735
|
+
if (funding === undefined ||
|
|
736
|
+
openInterest === undefined ||
|
|
737
|
+
dayNtlVlm === undefined ||
|
|
738
|
+
prevDayPx === undefined ||
|
|
739
|
+
markPx === undefined ||
|
|
740
|
+
midPx === undefined ||
|
|
741
|
+
oraclePx === undefined) {
|
|
742
|
+
return undefined;
|
|
743
|
+
}
|
|
744
|
+
return {
|
|
745
|
+
coin: marketId,
|
|
746
|
+
funding,
|
|
747
|
+
openInterest,
|
|
748
|
+
dayNtlVlm,
|
|
749
|
+
prevDayPx,
|
|
750
|
+
markPx,
|
|
751
|
+
midPx,
|
|
752
|
+
oraclePx,
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
function allDexsAssetCtxEntries(data) {
|
|
756
|
+
return data.assetCtxs ?? data.ctxs ?? [];
|
|
757
|
+
}
|
|
758
|
+
function activePerpAssetCtx(marketId, ctx) {
|
|
759
|
+
const funding = toMarketContextString(ctx.funding);
|
|
760
|
+
const openInterest = toMarketContextString(ctx.openInterest);
|
|
761
|
+
const dayNtlVlm = toMarketContextString(ctx.dayNtlVlm);
|
|
762
|
+
const prevDayPx = toMarketContextString(ctx.prevDayPx);
|
|
763
|
+
const markPx = toMarketContextString(ctx.markPx);
|
|
764
|
+
const midPx = ctx.midPx === undefined || ctx.midPx === null
|
|
765
|
+
? null
|
|
766
|
+
: (toMarketContextString(ctx.midPx) ?? null);
|
|
767
|
+
const oraclePx = toMarketContextString(ctx.oraclePx);
|
|
768
|
+
if (funding === undefined ||
|
|
769
|
+
openInterest === undefined ||
|
|
770
|
+
dayNtlVlm === undefined ||
|
|
771
|
+
prevDayPx === undefined ||
|
|
772
|
+
markPx === undefined ||
|
|
773
|
+
oraclePx === undefined) {
|
|
774
|
+
return undefined;
|
|
775
|
+
}
|
|
776
|
+
return {
|
|
777
|
+
coin: marketId,
|
|
778
|
+
funding,
|
|
779
|
+
openInterest,
|
|
780
|
+
dayNtlVlm,
|
|
781
|
+
prevDayPx,
|
|
782
|
+
markPx,
|
|
783
|
+
midPx,
|
|
784
|
+
oraclePx,
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
function mapSpotMarketContext(marketId, ctx, fast) {
|
|
788
|
+
const midPrice = toMarketContextString(ctx.midPx ?? ctx.markPx);
|
|
789
|
+
const markPrice = toMarketContextString(ctx.markPx);
|
|
790
|
+
if (midPrice === undefined || markPrice === undefined) {
|
|
791
|
+
return undefined;
|
|
792
|
+
}
|
|
793
|
+
const emittedMarkPrice = fast?.markPx != null ? fast.markPx : markPrice;
|
|
794
|
+
return {
|
|
795
|
+
marketId,
|
|
796
|
+
midPrice: fast?.midPx != null ? fast.midPx : midPrice,
|
|
797
|
+
markPrice: emittedMarkPrice,
|
|
798
|
+
prevDayPrice: toMarketContextString(ctx.prevDayPx),
|
|
799
|
+
volume24h: toMarketContextString(ctx.dayNtlVlm),
|
|
800
|
+
marketCap: toMarketCapString(emittedMarkPrice, ctx.circulatingSupply),
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
function compactRemovalPrice(levels, value) {
|
|
804
|
+
if (typeof value === 'number') {
|
|
805
|
+
return levels[value]?.price;
|
|
806
|
+
}
|
|
807
|
+
return typeof value === 'string' ? value : value.p;
|
|
808
|
+
}
|
|
809
|
+
function applyCompressedL2Side(previous, updates, removals, side) {
|
|
810
|
+
const byPrice = new Map(previous.map((level) => [level.price, level.size]));
|
|
811
|
+
for (const removal of removals) {
|
|
812
|
+
const price = compactRemovalPrice(previous, removal);
|
|
813
|
+
if (price !== undefined) {
|
|
814
|
+
byPrice.delete(price);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
for (const update of updates) {
|
|
818
|
+
if (Number(update.s) === 0) {
|
|
819
|
+
byPrice.delete(update.p);
|
|
820
|
+
}
|
|
821
|
+
else {
|
|
822
|
+
byPrice.set(update.p, update.s);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
return [...byPrice.entries()]
|
|
826
|
+
.map(([price, size]) => ({ price, size }))
|
|
827
|
+
.sort((a, b) => side === 'bid'
|
|
828
|
+
? Number(b.price) - Number(a.price)
|
|
829
|
+
: Number(a.price) - Number(b.price))
|
|
830
|
+
.slice(0, HL_L2_BOOK_MAX_LEVELS_PER_SIDE);
|
|
831
|
+
}
|
|
447
832
|
/**
|
|
448
833
|
* Minimal presence/type check of the channel-discriminating and required
|
|
449
834
|
* fields the matching handler dereferences without its own guard. A frame
|
|
@@ -453,7 +838,7 @@ const isObject = (v) => typeof v === 'object' && v !== null;
|
|
|
453
838
|
*/
|
|
454
839
|
function isValidHlFrame(channel, data) {
|
|
455
840
|
// `fastAssetCtxs` carries a base64 string payload, not an object.
|
|
456
|
-
if (channel === 'fastAssetCtxs') {
|
|
841
|
+
if (channel === 'fastAssetCtxs' || channel === 'pac' || channel === 'sac') {
|
|
457
842
|
return typeof data === 'string';
|
|
458
843
|
}
|
|
459
844
|
if (!isObject(data)) {
|
|
@@ -461,15 +846,22 @@ function isValidHlFrame(channel, data) {
|
|
|
461
846
|
}
|
|
462
847
|
switch (channel) {
|
|
463
848
|
case 'allDexsAssetCtxs':
|
|
464
|
-
return Array.isArray(data.assetCtxs);
|
|
849
|
+
return Array.isArray(data.assetCtxs) || Array.isArray(data.ctxs);
|
|
850
|
+
case 'activeAssetCtx':
|
|
851
|
+
case 'activeSpotAssetCtx':
|
|
852
|
+
return typeof data.coin === 'string' && isObject(data.ctx);
|
|
465
853
|
case 'l2Book':
|
|
466
854
|
return (typeof data.coin === 'string' &&
|
|
467
855
|
Array.isArray(data.levels) &&
|
|
468
856
|
Array.isArray(data.levels[0]) &&
|
|
469
857
|
Array.isArray(data.levels[1]) &&
|
|
470
858
|
typeof data.time === 'number');
|
|
859
|
+
case 'l2':
|
|
860
|
+
return typeof data.c === 'string' || isObject(data.s) || isObject(data.u);
|
|
471
861
|
case 'candle':
|
|
472
862
|
return typeof data.s === 'string' && typeof data.i === 'string';
|
|
863
|
+
case 'trades':
|
|
864
|
+
return Array.isArray(data);
|
|
473
865
|
case 'orderUpdates':
|
|
474
866
|
return Array.isArray(data);
|
|
475
867
|
case 'userFills':
|