@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
|
@@ -14,27 +14,40 @@ import {
|
|
|
14
14
|
import {
|
|
15
15
|
type MarketContext,
|
|
16
16
|
type OpenOrder,
|
|
17
|
+
type OrderbookLevel,
|
|
18
|
+
type OrderbookResponse,
|
|
17
19
|
OrderSide,
|
|
18
20
|
OrderType,
|
|
19
21
|
type Subscription,
|
|
20
22
|
type TriggerOrder,
|
|
21
23
|
} from '@lifi/perps-types'
|
|
22
|
-
import
|
|
24
|
+
import Big from 'big.js'
|
|
25
|
+
import { HYPERLIQUID_FEE_TIER_FALLBACK, SPOT_MARKET_ID } from '../constants.js'
|
|
23
26
|
import type {
|
|
24
27
|
HlAssetPosition,
|
|
25
28
|
HlOrderDetail,
|
|
26
29
|
HlUserFill,
|
|
30
|
+
HlWsActiveAssetCtxData,
|
|
31
|
+
HlWsActiveSpotAssetCtxData,
|
|
27
32
|
HlWsAllDexsAssetCtxsData,
|
|
28
33
|
HlWsAllDexsClearinghouseStateData,
|
|
29
34
|
HlWsCandleData,
|
|
35
|
+
HlWsCompressedL2Data,
|
|
30
36
|
HlWsFastAssetCtx,
|
|
31
37
|
HlWsL2BookData,
|
|
38
|
+
HlWsL2Data,
|
|
32
39
|
HlWsMessage,
|
|
40
|
+
HlWsPacData,
|
|
33
41
|
HlWsPerpAssetCtx,
|
|
42
|
+
HlWsPerpAssetCtxPayload,
|
|
43
|
+
HlWsSacData,
|
|
44
|
+
HlWsSpotAssetCtx,
|
|
34
45
|
HlWsSpotStateData,
|
|
46
|
+
HlWsTrade,
|
|
35
47
|
HlWsUserFillsData,
|
|
36
48
|
} from '../types/index.js'
|
|
37
49
|
import {
|
|
50
|
+
decodeCompressedJson,
|
|
38
51
|
decodeFastAssetCtxs,
|
|
39
52
|
isOpenAssetPosition,
|
|
40
53
|
isTriggerOrder,
|
|
@@ -49,7 +62,7 @@ import {
|
|
|
49
62
|
spotPriceById,
|
|
50
63
|
} from '../utils/index.js'
|
|
51
64
|
|
|
52
|
-
/** HL's `
|
|
65
|
+
/** HL's compact `l2` snapshot carries 20 levels per side. */
|
|
53
66
|
const HL_L2_BOOK_MAX_LEVELS_PER_SIDE = 20
|
|
54
67
|
|
|
55
68
|
/**
|
|
@@ -85,14 +98,20 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
85
98
|
private readonly client: PerpsSDKClient | undefined
|
|
86
99
|
private readonly registry: MarketRegistry | undefined
|
|
87
100
|
private perpCtxBySubDex = new Map<string, Record<string, HlWsPerpAssetCtx>>()
|
|
101
|
+
private spotCtxByMarketId: Record<string, HlWsSpotAssetCtx> = {}
|
|
102
|
+
private orderbookKeysByMarketId = new Map<string, Set<string>>()
|
|
103
|
+
private latestOrderbookByMarketId = new Map<string, OrderbookResponse>()
|
|
88
104
|
// Latest mid/mark per `Market.id` from the high-frequency `fastAssetCtxs`
|
|
89
105
|
// feed (all dexes, incl. builder/sub-dex coins). Merged incrementally —
|
|
90
|
-
// frames carry only changed coins — and overlaid onto
|
|
91
|
-
//
|
|
106
|
+
// frames carry only changed coins — and overlaid onto asset contexts that
|
|
107
|
+
// carry oracle/funding/OI/metadata.
|
|
92
108
|
private fastCtxByMarketId: Record<string, HlWsFastAssetCtx> = {}
|
|
93
109
|
// `fastAssetCtxs` payloads are base64 + raw-DEFLATE; decode is async, so
|
|
94
110
|
// chain decodes to apply incremental frames in arrival order.
|
|
95
111
|
private fastDecodeChain: Promise<void> = Promise.resolve()
|
|
112
|
+
private pacDecodeChain: Promise<void> = Promise.resolve()
|
|
113
|
+
private sacDecodeChain: Promise<void> = Promise.resolve()
|
|
114
|
+
private orderbookDecodeChain: Promise<void> = Promise.resolve()
|
|
96
115
|
|
|
97
116
|
constructor(wsUrl: string, providerKey: string, client?: PerpsSDKClient) {
|
|
98
117
|
super(
|
|
@@ -133,9 +152,8 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
133
152
|
|
|
134
153
|
await this.registry?.sync()
|
|
135
154
|
|
|
136
|
-
// Markets context aggregates
|
|
137
|
-
//
|
|
138
|
-
// feed, which supplies fast mid + mark for every market across all dexes.
|
|
155
|
+
// Markets context aggregates slower asset-context feeds with fast mid/mark
|
|
156
|
+
// ticks from `fastAssetCtxs`.
|
|
139
157
|
if (sub.channel === 'marketsContext') {
|
|
140
158
|
const entries = this.getMarketsContextSubEntries()
|
|
141
159
|
|
|
@@ -153,14 +171,21 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
153
171
|
)
|
|
154
172
|
}
|
|
155
173
|
this.perpCtxBySubDex.clear()
|
|
174
|
+
this.spotCtxByMarketId = {}
|
|
156
175
|
this.fastCtxByMarketId = {}
|
|
157
176
|
}
|
|
158
177
|
}
|
|
159
178
|
|
|
160
179
|
// All other channels: single WS subscription per key
|
|
161
180
|
const key = this.toKey(sub)
|
|
181
|
+
if (sub.channel === 'orderbook') {
|
|
182
|
+
this.claimOrderbookKey(sub.marketId, key)
|
|
183
|
+
}
|
|
162
184
|
const payload = this.toHlPayload(sub)
|
|
163
185
|
await this.registerSub(key, payload)
|
|
186
|
+
if (sub.channel === 'orderbook') {
|
|
187
|
+
this.registerOrderbook(sub.marketId, key)
|
|
188
|
+
}
|
|
164
189
|
|
|
165
190
|
await this.rws.ready()
|
|
166
191
|
|
|
@@ -170,8 +195,14 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
170
195
|
this.orderUpdatesKey = undefined
|
|
171
196
|
}
|
|
172
197
|
this.rws.send(
|
|
173
|
-
JSON.stringify({
|
|
198
|
+
JSON.stringify({
|
|
199
|
+
method: 'unsubscribe',
|
|
200
|
+
subscription: payload,
|
|
201
|
+
})
|
|
174
202
|
)
|
|
203
|
+
if (sub.channel === 'orderbook') {
|
|
204
|
+
this.releaseOrderbook(sub.marketId, key)
|
|
205
|
+
}
|
|
175
206
|
}
|
|
176
207
|
}
|
|
177
208
|
|
|
@@ -198,9 +229,28 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
198
229
|
this.orderUpdatesKey = key
|
|
199
230
|
}
|
|
200
231
|
|
|
232
|
+
private claimOrderbookKey(marketId: string, key: string): void {
|
|
233
|
+
const activeKeys = this.orderbookKeysByMarketId.get(marketId)
|
|
234
|
+
if (activeKeys === undefined) {
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
for (const activeKey of [...activeKeys]) {
|
|
239
|
+
if (activeKey === key) {
|
|
240
|
+
continue
|
|
241
|
+
}
|
|
242
|
+
if (!this.closeChannelIfIdle(activeKey)) {
|
|
243
|
+
throw new Error(
|
|
244
|
+
`Hyperliquid supports one orderbook aggregation per market per ` +
|
|
245
|
+
`provider instance. Unsubscribe ${activeKey} before subscribing ${key}.`
|
|
246
|
+
)
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
201
251
|
/**
|
|
202
|
-
* Sub-key + payload pairs for the markets-context aggregation:
|
|
203
|
-
* perp
|
|
252
|
+
* Sub-key + payload pairs for the markets-context aggregation: compressed
|
|
253
|
+
* all-perp (`pac`) and all-spot (`sac`) asset contexts plus the documented
|
|
204
254
|
* `fastAssetCtxs` feed (high-frequency mid + mark across every dex).
|
|
205
255
|
*/
|
|
206
256
|
private getMarketsContextSubEntries(): Array<{
|
|
@@ -208,14 +258,46 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
208
258
|
payload: object
|
|
209
259
|
}> {
|
|
210
260
|
return [
|
|
211
|
-
{ subKey: '
|
|
261
|
+
{ subKey: 'pac', payload: { type: 'pac' } },
|
|
262
|
+
{ subKey: 'sac', payload: { type: 'sac' } },
|
|
212
263
|
{ subKey: 'fastAssetCtxs', payload: { type: 'fastAssetCtxs' } },
|
|
213
264
|
]
|
|
214
265
|
}
|
|
215
266
|
|
|
267
|
+
private registerOrderbook(marketId: string, orderbookKey: string): void {
|
|
268
|
+
let activeKeys = this.orderbookKeysByMarketId.get(marketId)
|
|
269
|
+
if (activeKeys === undefined) {
|
|
270
|
+
activeKeys = new Set()
|
|
271
|
+
this.orderbookKeysByMarketId.set(marketId, activeKeys)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
activeKeys.add(orderbookKey)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private releaseOrderbook(marketId: string, orderbookKey: string): void {
|
|
278
|
+
const activeKeys = this.orderbookKeysByMarketId.get(marketId)
|
|
279
|
+
if (activeKeys === undefined) {
|
|
280
|
+
return
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
activeKeys.delete(orderbookKey)
|
|
284
|
+
if (activeKeys.size > 0) {
|
|
285
|
+
return
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
this.orderbookKeysByMarketId.delete(marketId)
|
|
289
|
+
}
|
|
290
|
+
|
|
216
291
|
protected override onClose(): void {
|
|
217
292
|
this.perpCtxBySubDex.clear()
|
|
293
|
+
this.spotCtxByMarketId = {}
|
|
218
294
|
this.fastCtxByMarketId = {}
|
|
295
|
+
this.orderbookKeysByMarketId.clear()
|
|
296
|
+
this.latestOrderbookByMarketId.clear()
|
|
297
|
+
this.fastDecodeChain = Promise.resolve()
|
|
298
|
+
this.pacDecodeChain = Promise.resolve()
|
|
299
|
+
this.sacDecodeChain = Promise.resolve()
|
|
300
|
+
this.orderbookDecodeChain = Promise.resolve()
|
|
219
301
|
this.orderUpdatesKey = undefined
|
|
220
302
|
}
|
|
221
303
|
|
|
@@ -229,10 +311,17 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
229
311
|
switch (sub.channel) {
|
|
230
312
|
case 'marketsContext':
|
|
231
313
|
return 'marketsContext'
|
|
314
|
+
case 'marketContext':
|
|
315
|
+
return `marketContext:${sub.marketId}`
|
|
232
316
|
case 'orderbook':
|
|
233
|
-
return `
|
|
317
|
+
return `l2:${sub.marketId}:${this.orderbookAggregationKey(
|
|
318
|
+
sub.marketId,
|
|
319
|
+
sub.priceStep
|
|
320
|
+
)}`
|
|
234
321
|
case 'candle':
|
|
235
322
|
return `candle:${sub.marketId}:${sub.interval}`
|
|
323
|
+
case 'trades':
|
|
324
|
+
return `trades:${sub.marketId}`
|
|
236
325
|
case 'orderUpdates':
|
|
237
326
|
return `orderUpdates:${sub.address.toLowerCase()}`
|
|
238
327
|
case 'fills':
|
|
@@ -244,32 +333,66 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
244
333
|
}
|
|
245
334
|
}
|
|
246
335
|
|
|
336
|
+
private orderbookAggregation(
|
|
337
|
+
marketId: string,
|
|
338
|
+
priceStep: number | undefined
|
|
339
|
+
): { nSigFigs?: number; mantissa?: number } {
|
|
340
|
+
if (priceStep === undefined) {
|
|
341
|
+
return {}
|
|
342
|
+
}
|
|
343
|
+
return priceStepToAggregation(
|
|
344
|
+
priceStep,
|
|
345
|
+
this.orderbookReferencePrice(marketId)
|
|
346
|
+
)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
private orderbookAggregationKey(
|
|
350
|
+
marketId: string,
|
|
351
|
+
priceStep: number | undefined
|
|
352
|
+
): string {
|
|
353
|
+
const aggregation = this.orderbookAggregation(marketId, priceStep)
|
|
354
|
+
const nSigFigs = aggregation.nSigFigs ?? null
|
|
355
|
+
const mantissa = aggregation.mantissa ?? null
|
|
356
|
+
if (nSigFigs === null && mantissa === null) {
|
|
357
|
+
return 'full'
|
|
358
|
+
}
|
|
359
|
+
return `s:${nSigFigs}:m:${mantissa}`
|
|
360
|
+
}
|
|
361
|
+
|
|
247
362
|
private toHlPayload(sub: Subscription): object {
|
|
248
363
|
switch (sub.channel) {
|
|
249
364
|
case 'marketsContext':
|
|
250
365
|
// Handled via getMarketsContextSubEntries in openChannel; never reaches
|
|
251
366
|
// toHlPayload, but TS requires an exhaustive switch.
|
|
252
367
|
return { type: 'allDexsAssetCtxs' }
|
|
253
|
-
case '
|
|
368
|
+
case 'marketContext':
|
|
254
369
|
return {
|
|
255
|
-
type: '
|
|
370
|
+
type: 'activeAssetCtx',
|
|
256
371
|
coin: sub.marketId,
|
|
257
|
-
// HL's l2Book ignores any level-count field; it returns up to 20
|
|
258
|
-
// levels/side and controls granularity via nSigFigs (+ mantissa,
|
|
259
|
-
// valid only when nSigFigs === 5).
|
|
260
|
-
...(sub.priceStep !== undefined
|
|
261
|
-
? priceStepToAggregation(
|
|
262
|
-
sub.priceStep,
|
|
263
|
-
this.mergedMids().get(sub.marketId) ?? Number.NaN
|
|
264
|
-
)
|
|
265
|
-
: {}),
|
|
266
372
|
}
|
|
373
|
+
case 'orderbook': {
|
|
374
|
+
const aggregation = this.orderbookAggregation(
|
|
375
|
+
sub.marketId,
|
|
376
|
+
sub.priceStep
|
|
377
|
+
)
|
|
378
|
+
return {
|
|
379
|
+
type: 'l2',
|
|
380
|
+
c: sub.marketId,
|
|
381
|
+
s: aggregation.nSigFigs ?? null,
|
|
382
|
+
m: aggregation.mantissa ?? null,
|
|
383
|
+
}
|
|
384
|
+
}
|
|
267
385
|
case 'candle':
|
|
268
386
|
return {
|
|
269
387
|
type: 'candle',
|
|
270
388
|
coin: sub.marketId,
|
|
271
389
|
interval: sub.interval,
|
|
272
390
|
}
|
|
391
|
+
case 'trades':
|
|
392
|
+
return {
|
|
393
|
+
type: 'trades',
|
|
394
|
+
coin: sub.marketId,
|
|
395
|
+
}
|
|
273
396
|
case 'orderUpdates':
|
|
274
397
|
return { type: 'orderUpdates', user: sub.address }
|
|
275
398
|
case 'fills':
|
|
@@ -312,15 +435,33 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
312
435
|
case 'allDexsAssetCtxs':
|
|
313
436
|
this.handleAllDexsAssetCtxs(msg.data as HlWsAllDexsAssetCtxsData)
|
|
314
437
|
break
|
|
438
|
+
case 'pac':
|
|
439
|
+
this.handlePac(msg.data as string)
|
|
440
|
+
break
|
|
441
|
+
case 'sac':
|
|
442
|
+
this.handleSac(msg.data as string)
|
|
443
|
+
break
|
|
315
444
|
case 'fastAssetCtxs':
|
|
316
445
|
this.handleFastAssetCtxs(msg.data as string)
|
|
317
446
|
break
|
|
447
|
+
case 'activeAssetCtx':
|
|
448
|
+
this.handleActiveAssetCtx(msg.data as HlWsActiveAssetCtxData, raw)
|
|
449
|
+
break
|
|
450
|
+
case 'activeSpotAssetCtx':
|
|
451
|
+
this.handleActiveSpotAssetCtx(msg.data as HlWsActiveSpotAssetCtxData)
|
|
452
|
+
break
|
|
453
|
+
case 'l2':
|
|
454
|
+
this.handleL2(msg.data as HlWsL2Data)
|
|
455
|
+
break
|
|
318
456
|
case 'l2Book':
|
|
319
457
|
this.handleL2Book(msg.data as HlWsL2BookData)
|
|
320
458
|
break
|
|
321
459
|
case 'candle':
|
|
322
460
|
this.handleCandle(msg.data as HlWsCandleData)
|
|
323
461
|
break
|
|
462
|
+
case 'trades':
|
|
463
|
+
this.handleTrades(msg.data as HlWsTrade[])
|
|
464
|
+
break
|
|
324
465
|
case 'orderUpdates':
|
|
325
466
|
this.handleOrderUpdates(msg.data as HlOrderDetail[])
|
|
326
467
|
break
|
|
@@ -342,14 +483,76 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
342
483
|
}
|
|
343
484
|
|
|
344
485
|
private handleAllDexsAssetCtxs(data: HlWsAllDexsAssetCtxsData) {
|
|
345
|
-
|
|
346
|
-
|
|
486
|
+
this.mergePerpAssetCtxEntries(allDexsAssetCtxEntries(data))
|
|
487
|
+
this.emitMarketsContext()
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
private handlePac(base64: string) {
|
|
491
|
+
this.pacDecodeChain = this.pacDecodeChain
|
|
492
|
+
.then(async () => {
|
|
493
|
+
this.mergePerpAssetCtxEntries(
|
|
494
|
+
await decodeCompressedJson<HlWsPacData>(base64)
|
|
495
|
+
)
|
|
496
|
+
this.emitMarketsContext()
|
|
497
|
+
})
|
|
498
|
+
.catch((error) => wsLog.handlerFailure(this.providerKey, error))
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
private mergePerpAssetCtxEntries(
|
|
502
|
+
entries: [string, HlWsPerpAssetCtxPayload[]][]
|
|
503
|
+
) {
|
|
504
|
+
for (const [dex, ctxs] of entries) {
|
|
505
|
+
const previous = this.perpCtxBySubDex.get(dex || 'default')
|
|
506
|
+
const byMarketId: Record<string, HlWsPerpAssetCtx> = {
|
|
507
|
+
...(previous ?? {}),
|
|
508
|
+
}
|
|
347
509
|
for (const ctx of ctxs) {
|
|
348
|
-
|
|
510
|
+
const marketId = ctx.coin
|
|
511
|
+
if (marketId === undefined) {
|
|
512
|
+
continue
|
|
513
|
+
}
|
|
514
|
+
const merged = mergePerpAssetCtx(previous?.[marketId], marketId, ctx)
|
|
515
|
+
if (merged !== undefined) {
|
|
516
|
+
byMarketId[marketId] = merged
|
|
517
|
+
}
|
|
349
518
|
}
|
|
350
519
|
this.perpCtxBySubDex.set(dex || 'default', byMarketId)
|
|
351
520
|
}
|
|
352
|
-
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
private handleSac(base64: string) {
|
|
524
|
+
this.sacDecodeChain = this.sacDecodeChain
|
|
525
|
+
.then(async () => {
|
|
526
|
+
const spotMarketIdBySacKey = this.spotMarketIdBySacKey()
|
|
527
|
+
const ctxs = await decodeCompressedJson<HlWsSacData>(base64)
|
|
528
|
+
for (const [sacKey, ctx] of Object.entries(ctxs)) {
|
|
529
|
+
const marketId = spotMarketIdBySacKey.get(sacKey)
|
|
530
|
+
if (marketId === undefined) {
|
|
531
|
+
continue
|
|
532
|
+
}
|
|
533
|
+
this.spotCtxByMarketId[marketId] = {
|
|
534
|
+
...this.spotCtxByMarketId[marketId],
|
|
535
|
+
...ctx,
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
this.emitMarketsContext()
|
|
539
|
+
})
|
|
540
|
+
.catch((error) => wsLog.handlerFailure(this.providerKey, error))
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
private spotMarketIdBySacKey(): Map<string, string> {
|
|
544
|
+
const map = new Map<string, string>()
|
|
545
|
+
for (const market of this.registry?.markets ?? []) {
|
|
546
|
+
if (market.categoryId !== SPOT_MARKET_ID) {
|
|
547
|
+
continue
|
|
548
|
+
}
|
|
549
|
+
map.set(market.id, market.id)
|
|
550
|
+
map.set(
|
|
551
|
+
`${market.baseAsset.displaySymbol}/${market.quoteAsset.displaySymbol}`,
|
|
552
|
+
market.id
|
|
553
|
+
)
|
|
554
|
+
}
|
|
555
|
+
return map
|
|
353
556
|
}
|
|
354
557
|
|
|
355
558
|
/**
|
|
@@ -375,11 +578,8 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
375
578
|
}
|
|
376
579
|
|
|
377
580
|
/**
|
|
378
|
-
* Build the all-markets context map
|
|
379
|
-
*
|
|
380
|
-
* high-frequency `fastAssetCtxs` feed. A market present in `fastAssetCtxs` but
|
|
381
|
-
* without an asset-context frame yet (the aggregate feed is slow/idle) still
|
|
382
|
-
* gets a mid+mark entry, so price ticks without waiting on the aggregate.
|
|
581
|
+
* Build the all-markets context map from perp + spot asset contexts, with mid
|
|
582
|
+
* + mark overlaid from `fastAssetCtxs`.
|
|
383
583
|
*/
|
|
384
584
|
private emitMarketsContext() {
|
|
385
585
|
const data: Record<string, MarketContext> = {}
|
|
@@ -394,6 +594,17 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
394
594
|
}
|
|
395
595
|
}
|
|
396
596
|
}
|
|
597
|
+
for (const [marketId, ctx] of Object.entries(this.spotCtxByMarketId)) {
|
|
598
|
+
const context = mapSpotMarketContext(
|
|
599
|
+
marketId,
|
|
600
|
+
ctx,
|
|
601
|
+
this.fastCtxByMarketId[marketId]
|
|
602
|
+
)
|
|
603
|
+
if (context === undefined) {
|
|
604
|
+
continue
|
|
605
|
+
}
|
|
606
|
+
data[marketId] = context
|
|
607
|
+
}
|
|
397
608
|
for (const [marketId, fast] of Object.entries(this.fastCtxByMarketId)) {
|
|
398
609
|
if (data[marketId] !== undefined) {
|
|
399
610
|
continue
|
|
@@ -409,7 +620,30 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
409
620
|
this.emit('marketsContext', { channel: 'marketsContext', data })
|
|
410
621
|
}
|
|
411
622
|
|
|
412
|
-
|
|
623
|
+
private handleActiveAssetCtx(data: HlWsActiveAssetCtxData, raw: string) {
|
|
624
|
+
const ctx = activePerpAssetCtx(data.coin, data.ctx)
|
|
625
|
+
if (ctx === undefined) {
|
|
626
|
+
wsLog.parseFailure(this.providerKey, raw)
|
|
627
|
+
return
|
|
628
|
+
}
|
|
629
|
+
this.emit(`marketContext:${data.coin}`, {
|
|
630
|
+
channel: 'marketContext',
|
|
631
|
+
data: mapMarketContext(data.coin, ctx),
|
|
632
|
+
})
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
private handleActiveSpotAssetCtx(data: HlWsActiveSpotAssetCtxData) {
|
|
636
|
+
const context = mapSpotMarketContext(data.coin, data.ctx)
|
|
637
|
+
if (context === undefined) {
|
|
638
|
+
return
|
|
639
|
+
}
|
|
640
|
+
this.emit(`marketContext:${data.coin}`, {
|
|
641
|
+
channel: 'marketContext',
|
|
642
|
+
data: context,
|
|
643
|
+
})
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/** Latest mid per `Market.id` across asset-context and fast feeds. */
|
|
413
647
|
private mergedMids(): Map<string, number> {
|
|
414
648
|
const map = new Map<string, number>()
|
|
415
649
|
for (const byMarketId of this.perpCtxBySubDex.values()) {
|
|
@@ -418,6 +652,12 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
418
652
|
map.set(id, Number(mid))
|
|
419
653
|
}
|
|
420
654
|
}
|
|
655
|
+
for (const [id, ctx] of Object.entries(this.spotCtxByMarketId)) {
|
|
656
|
+
const mid = ctx.midPx ?? ctx.markPx
|
|
657
|
+
if (mid != null) {
|
|
658
|
+
map.set(id, Number(mid))
|
|
659
|
+
}
|
|
660
|
+
}
|
|
421
661
|
for (const [id, fast] of Object.entries(this.fastCtxByMarketId)) {
|
|
422
662
|
const mid = fast.midPx ?? fast.markPx
|
|
423
663
|
if (mid != null) {
|
|
@@ -427,21 +667,93 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
427
667
|
return map
|
|
428
668
|
}
|
|
429
669
|
|
|
670
|
+
private orderbookReferencePrice(marketId: string): number {
|
|
671
|
+
const mid = this.mergedMids().get(marketId)
|
|
672
|
+
if (mid !== undefined) {
|
|
673
|
+
return mid
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
const book = this.latestOrderbookByMarketId.get(marketId)
|
|
677
|
+
const bid = book?.bids[0]?.price
|
|
678
|
+
const ask = book?.asks[0]?.price
|
|
679
|
+
if (bid !== undefined && ask !== undefined) {
|
|
680
|
+
return (Number(bid) + Number(ask)) / 2
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
return Number.NaN
|
|
684
|
+
}
|
|
685
|
+
|
|
430
686
|
private handleL2Book(data: HlWsL2BookData) {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
687
|
+
const book = {
|
|
688
|
+
provider: this.providerKey,
|
|
689
|
+
marketId: data.coin,
|
|
690
|
+
bids: data.levels[0]
|
|
691
|
+
.slice(0, HL_L2_BOOK_MAX_LEVELS_PER_SIDE)
|
|
692
|
+
.map((l) => ({ price: l.px, size: l.sz })),
|
|
693
|
+
asks: data.levels[1]
|
|
694
|
+
.slice(0, HL_L2_BOOK_MAX_LEVELS_PER_SIDE)
|
|
695
|
+
.map((l) => ({ price: l.px, size: l.sz })),
|
|
696
|
+
timestamp: data.time,
|
|
697
|
+
}
|
|
698
|
+
this.latestOrderbookByMarketId.set(data.coin, book)
|
|
699
|
+
this.emitOrderbook(data.coin, book)
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
private handleL2(data: HlWsL2Data) {
|
|
703
|
+
if (data.s !== undefined) {
|
|
704
|
+
this.handleL2Book(data.s)
|
|
705
|
+
}
|
|
706
|
+
if (data.u !== undefined) {
|
|
707
|
+
this.handleCompactL2Delta(data.u)
|
|
708
|
+
}
|
|
709
|
+
if (data.c === undefined) {
|
|
710
|
+
return
|
|
711
|
+
}
|
|
712
|
+
const compressed = data.c
|
|
713
|
+
this.orderbookDecodeChain = this.orderbookDecodeChain
|
|
714
|
+
.then(async () => {
|
|
715
|
+
this.handleCompactL2Delta(
|
|
716
|
+
await decodeCompressedJson<HlWsCompressedL2Data>(compressed)
|
|
717
|
+
)
|
|
718
|
+
})
|
|
719
|
+
.catch((error) => wsLog.handlerFailure(this.providerKey, error))
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
private handleCompactL2Delta(delta: HlWsCompressedL2Data) {
|
|
723
|
+
const previous = this.latestOrderbookByMarketId.get(delta.c)
|
|
724
|
+
if (previous === undefined) {
|
|
725
|
+
return
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
const book: OrderbookResponse = {
|
|
729
|
+
provider: this.providerKey,
|
|
730
|
+
marketId: delta.c,
|
|
731
|
+
bids: applyCompressedL2Side(
|
|
732
|
+
previous.bids,
|
|
733
|
+
delta.l[0],
|
|
734
|
+
delta.r?.[0] ?? [],
|
|
735
|
+
'bid'
|
|
736
|
+
),
|
|
737
|
+
asks: applyCompressedL2Side(
|
|
738
|
+
previous.asks,
|
|
739
|
+
delta.l[1],
|
|
740
|
+
delta.r?.[1] ?? [],
|
|
741
|
+
'ask'
|
|
742
|
+
),
|
|
743
|
+
timestamp: delta.t,
|
|
744
|
+
}
|
|
745
|
+
this.latestOrderbookByMarketId.set(delta.c, book)
|
|
746
|
+
this.emitOrderbook(delta.c, book)
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
private emitOrderbook(marketId: string, book: OrderbookResponse) {
|
|
750
|
+
const keys = this.orderbookKeysByMarketId.get(marketId)
|
|
751
|
+
if (keys === undefined) {
|
|
752
|
+
return
|
|
753
|
+
}
|
|
754
|
+
for (const key of keys) {
|
|
755
|
+
this.emit(key, { channel: 'orderbook', data: book })
|
|
756
|
+
}
|
|
445
757
|
}
|
|
446
758
|
|
|
447
759
|
private handleCandle(data: HlWsCandleData) {
|
|
@@ -458,6 +770,25 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
458
770
|
})
|
|
459
771
|
}
|
|
460
772
|
|
|
773
|
+
private handleTrades(data: HlWsTrade[]) {
|
|
774
|
+
for (const trade of data) {
|
|
775
|
+
this.emit(`trades:${trade.coin}`, {
|
|
776
|
+
channel: 'trades',
|
|
777
|
+
data: [
|
|
778
|
+
{
|
|
779
|
+
provider: this.providerKey,
|
|
780
|
+
marketId: trade.coin,
|
|
781
|
+
price: trade.px,
|
|
782
|
+
size: trade.sz,
|
|
783
|
+
timestamp: trade.time,
|
|
784
|
+
side: trade.side === 'B' ? 'buy' : 'sell',
|
|
785
|
+
id: trade.tid !== undefined ? String(trade.tid) : trade.hash,
|
|
786
|
+
},
|
|
787
|
+
],
|
|
788
|
+
})
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
|
|
461
792
|
private handleOrderUpdates(data: HlOrderDetail[]) {
|
|
462
793
|
// Untagged frame: attributable only via the single-address invariant.
|
|
463
794
|
const key = this.orderUpdatesKey
|
|
@@ -570,6 +901,177 @@ export class HyperliquidWsProvider extends WsProviderBase<object> {
|
|
|
570
901
|
const isObject = (v: unknown): v is Record<string, unknown> =>
|
|
571
902
|
typeof v === 'object' && v !== null
|
|
572
903
|
|
|
904
|
+
const toMarketContextString = (value: unknown): string | undefined =>
|
|
905
|
+
typeof value === 'string' || typeof value === 'number'
|
|
906
|
+
? String(value)
|
|
907
|
+
: undefined
|
|
908
|
+
|
|
909
|
+
const toMarketCapString = (
|
|
910
|
+
price: unknown,
|
|
911
|
+
circulatingSupply: unknown
|
|
912
|
+
): string | undefined => {
|
|
913
|
+
const priceString = toMarketContextString(price)
|
|
914
|
+
const supplyString = toMarketContextString(circulatingSupply)
|
|
915
|
+
if (priceString === undefined || supplyString === undefined) {
|
|
916
|
+
return undefined
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
try {
|
|
920
|
+
const parsedPrice = new Big(priceString)
|
|
921
|
+
const parsedSupply = new Big(supplyString)
|
|
922
|
+
if (parsedPrice.lte(0) || parsedSupply.lte(0)) {
|
|
923
|
+
return undefined
|
|
924
|
+
}
|
|
925
|
+
return parsedPrice.times(parsedSupply).toFixed()
|
|
926
|
+
} catch {
|
|
927
|
+
return undefined
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
function mergePerpAssetCtx(
|
|
932
|
+
previous: HlWsPerpAssetCtx | undefined,
|
|
933
|
+
marketId: string,
|
|
934
|
+
update: HlWsPerpAssetCtxPayload
|
|
935
|
+
): HlWsPerpAssetCtx | undefined {
|
|
936
|
+
const funding = update.funding ?? previous?.funding
|
|
937
|
+
const openInterest = update.openInterest ?? previous?.openInterest
|
|
938
|
+
const dayNtlVlm = update.dayNtlVlm ?? previous?.dayNtlVlm
|
|
939
|
+
const prevDayPx = update.prevDayPx ?? previous?.prevDayPx
|
|
940
|
+
const markPx = update.markPx ?? previous?.markPx
|
|
941
|
+
const midPx = 'midPx' in update ? update.midPx : previous?.midPx
|
|
942
|
+
const oraclePx = update.oraclePx ?? previous?.oraclePx
|
|
943
|
+
|
|
944
|
+
if (
|
|
945
|
+
funding === undefined ||
|
|
946
|
+
openInterest === undefined ||
|
|
947
|
+
dayNtlVlm === undefined ||
|
|
948
|
+
prevDayPx === undefined ||
|
|
949
|
+
markPx === undefined ||
|
|
950
|
+
midPx === undefined ||
|
|
951
|
+
oraclePx === undefined
|
|
952
|
+
) {
|
|
953
|
+
return undefined
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
return {
|
|
957
|
+
coin: marketId,
|
|
958
|
+
funding,
|
|
959
|
+
openInterest,
|
|
960
|
+
dayNtlVlm,
|
|
961
|
+
prevDayPx,
|
|
962
|
+
markPx,
|
|
963
|
+
midPx,
|
|
964
|
+
oraclePx,
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
function allDexsAssetCtxEntries(
|
|
969
|
+
data: HlWsAllDexsAssetCtxsData
|
|
970
|
+
): [string, HlWsPerpAssetCtxPayload[]][] {
|
|
971
|
+
return data.assetCtxs ?? data.ctxs ?? []
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
function activePerpAssetCtx(
|
|
975
|
+
marketId: string,
|
|
976
|
+
ctx: HlWsActiveAssetCtxData['ctx']
|
|
977
|
+
): HlWsPerpAssetCtx | undefined {
|
|
978
|
+
const funding = toMarketContextString(ctx.funding)
|
|
979
|
+
const openInterest = toMarketContextString(ctx.openInterest)
|
|
980
|
+
const dayNtlVlm = toMarketContextString(ctx.dayNtlVlm)
|
|
981
|
+
const prevDayPx = toMarketContextString(ctx.prevDayPx)
|
|
982
|
+
const markPx = toMarketContextString(ctx.markPx)
|
|
983
|
+
const midPx =
|
|
984
|
+
ctx.midPx === undefined || ctx.midPx === null
|
|
985
|
+
? null
|
|
986
|
+
: (toMarketContextString(ctx.midPx) ?? null)
|
|
987
|
+
const oraclePx = toMarketContextString(ctx.oraclePx)
|
|
988
|
+
|
|
989
|
+
if (
|
|
990
|
+
funding === undefined ||
|
|
991
|
+
openInterest === undefined ||
|
|
992
|
+
dayNtlVlm === undefined ||
|
|
993
|
+
prevDayPx === undefined ||
|
|
994
|
+
markPx === undefined ||
|
|
995
|
+
oraclePx === undefined
|
|
996
|
+
) {
|
|
997
|
+
return undefined
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
return {
|
|
1001
|
+
coin: marketId,
|
|
1002
|
+
funding,
|
|
1003
|
+
openInterest,
|
|
1004
|
+
dayNtlVlm,
|
|
1005
|
+
prevDayPx,
|
|
1006
|
+
markPx,
|
|
1007
|
+
midPx,
|
|
1008
|
+
oraclePx,
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
function mapSpotMarketContext(
|
|
1013
|
+
marketId: string,
|
|
1014
|
+
ctx: HlWsSpotAssetCtx,
|
|
1015
|
+
fast?: HlWsFastAssetCtx
|
|
1016
|
+
): MarketContext | undefined {
|
|
1017
|
+
const midPrice = toMarketContextString(ctx.midPx ?? ctx.markPx)
|
|
1018
|
+
const markPrice = toMarketContextString(ctx.markPx)
|
|
1019
|
+
if (midPrice === undefined || markPrice === undefined) {
|
|
1020
|
+
return undefined
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
const emittedMarkPrice = fast?.markPx != null ? fast.markPx : markPrice
|
|
1024
|
+
return {
|
|
1025
|
+
marketId,
|
|
1026
|
+
midPrice: fast?.midPx != null ? fast.midPx : midPrice,
|
|
1027
|
+
markPrice: emittedMarkPrice,
|
|
1028
|
+
prevDayPrice: toMarketContextString(ctx.prevDayPx),
|
|
1029
|
+
volume24h: toMarketContextString(ctx.dayNtlVlm),
|
|
1030
|
+
marketCap: toMarketCapString(emittedMarkPrice, ctx.circulatingSupply),
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
function compactRemovalPrice(
|
|
1035
|
+
levels: OrderbookLevel[],
|
|
1036
|
+
value: number | string | { p: string }
|
|
1037
|
+
): string | undefined {
|
|
1038
|
+
if (typeof value === 'number') {
|
|
1039
|
+
return levels[value]?.price
|
|
1040
|
+
}
|
|
1041
|
+
return typeof value === 'string' ? value : value.p
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
function applyCompressedL2Side(
|
|
1045
|
+
previous: OrderbookLevel[],
|
|
1046
|
+
updates: HlWsCompressedL2Data['l'][number],
|
|
1047
|
+
removals: NonNullable<HlWsCompressedL2Data['r']>[number],
|
|
1048
|
+
side: 'bid' | 'ask'
|
|
1049
|
+
): OrderbookLevel[] {
|
|
1050
|
+
const byPrice = new Map(previous.map((level) => [level.price, level.size]))
|
|
1051
|
+
for (const removal of removals) {
|
|
1052
|
+
const price = compactRemovalPrice(previous, removal)
|
|
1053
|
+
if (price !== undefined) {
|
|
1054
|
+
byPrice.delete(price)
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
for (const update of updates) {
|
|
1058
|
+
if (Number(update.s) === 0) {
|
|
1059
|
+
byPrice.delete(update.p)
|
|
1060
|
+
} else {
|
|
1061
|
+
byPrice.set(update.p, update.s)
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
return [...byPrice.entries()]
|
|
1066
|
+
.map(([price, size]) => ({ price, size }))
|
|
1067
|
+
.sort((a, b) =>
|
|
1068
|
+
side === 'bid'
|
|
1069
|
+
? Number(b.price) - Number(a.price)
|
|
1070
|
+
: Number(a.price) - Number(b.price)
|
|
1071
|
+
)
|
|
1072
|
+
.slice(0, HL_L2_BOOK_MAX_LEVELS_PER_SIDE)
|
|
1073
|
+
}
|
|
1074
|
+
|
|
573
1075
|
/**
|
|
574
1076
|
* Minimal presence/type check of the channel-discriminating and required
|
|
575
1077
|
* fields the matching handler dereferences without its own guard. A frame
|
|
@@ -579,7 +1081,7 @@ const isObject = (v: unknown): v is Record<string, unknown> =>
|
|
|
579
1081
|
*/
|
|
580
1082
|
function isValidHlFrame(channel: string, data: unknown): boolean {
|
|
581
1083
|
// `fastAssetCtxs` carries a base64 string payload, not an object.
|
|
582
|
-
if (channel === 'fastAssetCtxs') {
|
|
1084
|
+
if (channel === 'fastAssetCtxs' || channel === 'pac' || channel === 'sac') {
|
|
583
1085
|
return typeof data === 'string'
|
|
584
1086
|
}
|
|
585
1087
|
if (!isObject(data)) {
|
|
@@ -587,7 +1089,10 @@ function isValidHlFrame(channel: string, data: unknown): boolean {
|
|
|
587
1089
|
}
|
|
588
1090
|
switch (channel) {
|
|
589
1091
|
case 'allDexsAssetCtxs':
|
|
590
|
-
return Array.isArray(data.assetCtxs)
|
|
1092
|
+
return Array.isArray(data.assetCtxs) || Array.isArray(data.ctxs)
|
|
1093
|
+
case 'activeAssetCtx':
|
|
1094
|
+
case 'activeSpotAssetCtx':
|
|
1095
|
+
return typeof data.coin === 'string' && isObject(data.ctx)
|
|
591
1096
|
case 'l2Book':
|
|
592
1097
|
return (
|
|
593
1098
|
typeof data.coin === 'string' &&
|
|
@@ -596,8 +1101,12 @@ function isValidHlFrame(channel: string, data: unknown): boolean {
|
|
|
596
1101
|
Array.isArray(data.levels[1]) &&
|
|
597
1102
|
typeof data.time === 'number'
|
|
598
1103
|
)
|
|
1104
|
+
case 'l2':
|
|
1105
|
+
return typeof data.c === 'string' || isObject(data.s) || isObject(data.u)
|
|
599
1106
|
case 'candle':
|
|
600
1107
|
return typeof data.s === 'string' && typeof data.i === 'string'
|
|
1108
|
+
case 'trades':
|
|
1109
|
+
return Array.isArray(data)
|
|
601
1110
|
case 'orderUpdates':
|
|
602
1111
|
return Array.isArray(data)
|
|
603
1112
|
case 'userFills':
|