@lifi/perps-sdk-provider-hyperliquid 1.0.1 → 1.2.0

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.
Files changed (79) hide show
  1. package/dist/cjs/services/getAccount.js +1 -1
  2. package/dist/cjs/services/getAccount.js.map +1 -1
  3. package/dist/cjs/types/asset.d.ts +0 -1
  4. package/dist/cjs/types/asset.d.ts.map +1 -1
  5. package/dist/cjs/types/ws.d.ts +50 -4
  6. package/dist/cjs/types/ws.d.ts.map +1 -1
  7. package/dist/cjs/utils/decodeFastAssetCtxs.d.ts +4 -0
  8. package/dist/cjs/utils/decodeFastAssetCtxs.d.ts.map +1 -0
  9. package/dist/cjs/utils/decodeFastAssetCtxs.js +16 -0
  10. package/dist/cjs/utils/decodeFastAssetCtxs.js.map +1 -0
  11. package/dist/cjs/utils/index.d.ts +2 -1
  12. package/dist/cjs/utils/index.d.ts.map +1 -1
  13. package/dist/cjs/utils/index.js +6 -3
  14. package/dist/cjs/utils/index.js.map +1 -1
  15. package/dist/cjs/utils/mapMarketContext.d.ts +4 -0
  16. package/dist/cjs/utils/mapMarketContext.d.ts.map +1 -0
  17. package/dist/cjs/utils/mapMarketContext.js +23 -0
  18. package/dist/cjs/utils/mapMarketContext.js.map +1 -0
  19. package/dist/cjs/websocket/HyperliquidWsProvider.d.ts +21 -5
  20. package/dist/cjs/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  21. package/dist/cjs/websocket/HyperliquidWsProvider.js +291 -60
  22. package/dist/cjs/websocket/HyperliquidWsProvider.js.map +1 -1
  23. package/dist/esm/services/getAccount.js +2 -2
  24. package/dist/esm/services/getAccount.js.map +1 -1
  25. package/dist/esm/types/asset.d.ts +0 -2
  26. package/dist/esm/types/asset.d.ts.map +1 -1
  27. package/dist/esm/types/ws.d.ts +71 -5
  28. package/dist/esm/types/ws.d.ts.map +1 -1
  29. package/dist/esm/utils/decodeFastAssetCtxs.d.ts +12 -0
  30. package/dist/esm/utils/decodeFastAssetCtxs.d.ts.map +1 -0
  31. package/dist/esm/utils/decodeFastAssetCtxs.js +20 -0
  32. package/dist/esm/utils/decodeFastAssetCtxs.js.map +1 -0
  33. package/dist/esm/utils/index.d.ts +2 -1
  34. package/dist/esm/utils/index.d.ts.map +1 -1
  35. package/dist/esm/utils/index.js +2 -1
  36. package/dist/esm/utils/index.js.map +1 -1
  37. package/dist/esm/utils/mapMarket.d.ts +2 -2
  38. package/dist/esm/utils/mapMarket.js +2 -2
  39. package/dist/esm/utils/mapMarketContext.d.ts +10 -0
  40. package/dist/esm/utils/mapMarketContext.d.ts.map +1 -0
  41. package/dist/esm/utils/mapMarketContext.js +25 -0
  42. package/dist/esm/utils/mapMarketContext.js.map +1 -0
  43. package/dist/esm/websocket/HyperliquidWsProvider.d.ts +45 -13
  44. package/dist/esm/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  45. package/dist/esm/websocket/HyperliquidWsProvider.js +331 -76
  46. package/dist/esm/websocket/HyperliquidWsProvider.js.map +1 -1
  47. package/dist/types/types/asset.d.ts +0 -2
  48. package/dist/types/types/asset.d.ts.map +1 -1
  49. package/dist/types/types/ws.d.ts +71 -5
  50. package/dist/types/types/ws.d.ts.map +1 -1
  51. package/dist/types/utils/decodeFastAssetCtxs.d.ts +12 -0
  52. package/dist/types/utils/decodeFastAssetCtxs.d.ts.map +1 -0
  53. package/dist/types/utils/index.d.ts +2 -1
  54. package/dist/types/utils/index.d.ts.map +1 -1
  55. package/dist/types/utils/mapMarket.d.ts +2 -2
  56. package/dist/types/utils/mapMarketContext.d.ts +10 -0
  57. package/dist/types/utils/mapMarketContext.d.ts.map +1 -0
  58. package/dist/types/websocket/HyperliquidWsProvider.d.ts +45 -13
  59. package/dist/types/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  60. package/package.json +5 -5
  61. package/src/services/getAccount.ts +2 -2
  62. package/src/types/asset.ts +0 -3
  63. package/src/types/ws.ts +71 -5
  64. package/src/utils/decodeFastAssetCtxs.ts +24 -0
  65. package/src/utils/index.ts +5 -1
  66. package/src/utils/mapMarket.ts +2 -2
  67. package/src/utils/mapMarketContext.ts +33 -0
  68. package/src/websocket/HyperliquidWsProvider.ts +406 -86
  69. package/dist/cjs/utils/mapMarketPrice.d.ts +0 -4
  70. package/dist/cjs/utils/mapMarketPrice.d.ts.map +0 -1
  71. package/dist/cjs/utils/mapMarketPrice.js +0 -22
  72. package/dist/cjs/utils/mapMarketPrice.js.map +0 -1
  73. package/dist/esm/utils/mapMarketPrice.d.ts +0 -10
  74. package/dist/esm/utils/mapMarketPrice.d.ts.map +0 -1
  75. package/dist/esm/utils/mapMarketPrice.js +0 -24
  76. package/dist/esm/utils/mapMarketPrice.js.map +0 -1
  77. package/dist/types/utils/mapMarketPrice.d.ts +0 -10
  78. package/dist/types/utils/mapMarketPrice.d.ts.map +0 -1
  79. package/src/utils/mapMarketPrice.ts +0 -33
@@ -1,21 +1,77 @@
1
1
  import type { HlClearinghouseState, HlSpotBalance } from './account.js';
2
- import type { HlAllMids, HlCandle, HlL2Book } from './asset.js';
2
+ import type { HlCandle, HlL2Book } from './asset.js';
3
3
  import type { HlUserFill } from './fill.js';
4
+ /**
5
+ * Perp asset context as carried on the `allDexsAssetCtxs` WS feed. Extends the
6
+ * REST `/info` `HlAssetCtx` shape with the wire-only `coin` tag, the order-book
7
+ * `midPx` (null when the book is empty), and `oraclePx`.
8
+ * @public
9
+ */
10
+ export type HlWsPerpAssetCtx = {
11
+ coin: string;
12
+ funding: string;
13
+ openInterest: string;
14
+ dayNtlVlm: string;
15
+ prevDayPx: string;
16
+ markPx: string;
17
+ midPx: string | null;
18
+ oraclePx: string;
19
+ };
20
+ /**
21
+ * `allDexsAssetCtxs` carries one `[dex, ctxs]` pair per perp sub-DEX, mirroring
22
+ * `allDexsClearinghouseState`. Spot contexts arrive separately.
23
+ * @public
24
+ */
25
+ export type HlWsAllDexsAssetCtxsData = {
26
+ assetCtxs: [string, HlWsPerpAssetCtx[]][];
27
+ };
4
28
  /** @public */
5
29
  export type HlWsMessage = {
6
30
  channel: string;
7
31
  data: unknown;
8
32
  };
9
- /** @public */
10
- export type HlWsAllMidsData = {
11
- mids: HlAllMids;
12
- dex?: string;
33
+ /**
34
+ * Per-coin entry from the compressed `fastAssetCtxs` feed (mark + mid only,
35
+ * keyed by coin across all dexes). `midPx` is null when the book is empty;
36
+ * fields are omitted from incremental frames when unchanged.
37
+ * @public
38
+ */
39
+ export type HlWsFastAssetCtx = {
40
+ markPx?: string;
41
+ midPx?: string | null;
13
42
  };
14
43
  /** @public */
15
44
  export type HlWsL2BookData = HlL2Book & {
16
45
  coin: string;
17
46
  };
18
47
  /** @public */
48
+ export type HlWsL2Data = {
49
+ s?: HlL2Book & {
50
+ coin: string;
51
+ };
52
+ c?: string;
53
+ u?: HlWsCompressedL2Data;
54
+ };
55
+ /** @public */
56
+ export type HlWsCompressedL2Level = {
57
+ p: string;
58
+ s: string;
59
+ };
60
+ /** @public */
61
+ export type HlWsCompressedL2Data = {
62
+ c: string;
63
+ t: number;
64
+ l: [HlWsCompressedL2Level[], HlWsCompressedL2Level[]];
65
+ r?: [
66
+ Array<number | string | {
67
+ p: string;
68
+ }>,
69
+ Array<number | string | {
70
+ p: string;
71
+ }>
72
+ ];
73
+ };
74
+ /** @public */
19
75
  export type HlWsCandleData = HlCandle & {
20
76
  T: number;
21
77
  s: string;
@@ -23,6 +79,16 @@ export type HlWsCandleData = HlCandle & {
23
79
  n: number;
24
80
  };
25
81
  /** @public */
82
+ export type HlWsTrade = {
83
+ coin: string;
84
+ side: string;
85
+ px: string;
86
+ sz: string;
87
+ time: number;
88
+ tid?: number;
89
+ hash?: string;
90
+ };
91
+ /** @public */
26
92
  export type HlWsUserFillsData = {
27
93
  isSnapshot: boolean;
28
94
  user: string;
@@ -1 +1 @@
1
- {"version":3,"file":"ws.d.ts","sourceRoot":"","sources":["../../../src/types/ws.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,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9E,CAAA;AAED,cAAc;AACd,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE;QAAE,QAAQ,EAAE,aAAa,EAAE,CAAA;KAAE,CAAA;CACzC,CAAA"}
@@ -0,0 +1,12 @@
1
+ import type { HlWsFastAssetCtx } from '../types/index.js';
2
+ export declare function decodeCompressedJson<T>(base64: string): Promise<T>;
3
+ /**
4
+ * Decode a Hyperliquid `fastAssetCtxs` frame. HL sends the payload as a
5
+ * base64-encoded, raw-DEFLATE-compressed (RFC 1951 — no zlib/gzip wrapper) JSON
6
+ * object keyed by coin. Decoded via the web-standard `DecompressionStream`
7
+ * (Node 18+, modern browsers), so no dependency is needed.
8
+ *
9
+ * @throws If the input is not valid base64 / raw-DEFLATE / JSON.
10
+ */
11
+ export declare function decodeFastAssetCtxs(base64: string): Promise<Record<string, HlWsFastAssetCtx>>;
12
+ //# sourceMappingURL=decodeFastAssetCtxs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decodeFastAssetCtxs.d.ts","sourceRoot":"","sources":["../../../src/utils/decodeFastAssetCtxs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAEzD,wBAAsB,oBAAoB,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAOxE;AAED;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CACvC,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC,CAE3C"}
@@ -1,11 +1,12 @@
1
1
  export { assetIsSpot, calculateAssetId, getProviderIndex } from './assetId.js';
2
+ export { decodeCompressedJson, decodeFastAssetCtxs, } from './decodeFastAssetCtxs.js';
2
3
  export type { InfoRequestOptions } from './infoClient.js';
3
4
  export { hlInfoOptions, infoRequest } from './infoClient.js';
4
5
  export { calculateLiquidationPrice, calculateMaintenanceMarginRate, } from './liquidation.js';
5
6
  export { mapFundingActivity, mapLedgerEntry } from './mapActivity.js';
6
7
  export { classifyFillFromPosition, mapFill } from './mapFill.js';
7
8
  export { mapMarket } from './mapMarket.js';
8
- export { mapMarketPrice } from './mapMarketPrice.js';
9
+ export { mapMarketContext } from './mapMarketContext.js';
9
10
  export { isTriggerOrder, isTriggerType, mapOpenOrder, mapOrder, mapOrderStatus, mapOrderType, mapStatusReason, mapTriggerOrder, } from './mapOrder.js';
10
11
  export { isOpenAssetPosition, mapPosition } from './mapPosition.js';
11
12
  export { coinAsset, perpsDexNames } from './marketDisplay.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAC9E,YAAY,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AACzD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC5D,OAAO,EACL,yBAAyB,EACzB,8BAA8B,GAC/B,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACrE,OAAO,EAAE,wBAAwB,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACpD,OAAO,EACL,cAAc,EACd,aAAa,EACb,YAAY,EACZ,QAAQ,EACR,cAAc,EACd,YAAY,EACZ,eAAe,EACf,eAAe,GAChB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AACnE,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAC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O,EACL,kBAAkB,EAClB,WAAW,EACX,aAAa,GACd,MAAM,kBAAkB,CAAA"}
@@ -2,8 +2,8 @@ import type { PerpsMarket } from '@lifi/perps-types';
2
2
  import type { HlUniverseItem } from '../types/index.js';
3
3
  /**
4
4
  * Map a Hyperliquid universe entry to a {@link PerpsMarket}'s static instrument
5
- * metadata. Live mark/stats fields live on the `/prices` item — see
6
- * {@link mapMarketPrice}. `categoryId` is the `Provider.categories[].id` of the
5
+ * metadata. Live mark/stats fields live on the {@link MarketContext} — see
6
+ * {@link mapMarketContext}. `categoryId` is the `Provider.categories[].id` of the
7
7
  * dex the entry was fetched from — known to the caller at fetch time, never
8
8
  * re-derived from the coin string.
9
9
  * @public
@@ -0,0 +1,10 @@
1
+ import type { MarketContext } from '@lifi/perps-types';
2
+ import type { HlWsPerpAssetCtx } from '../types/index.js';
3
+ /**
4
+ * Map a Hyperliquid perp asset context from the `allDexsAssetCtxs` feed to a
5
+ * {@link MarketContext}. `midPx` is null when the book is empty; mid then falls
6
+ * back to the mark so the context always carries a usable mid.
7
+ * @public
8
+ */
9
+ export declare const mapMarketContext: (marketId: string, ctx: HlWsPerpAssetCtx) => MarketContext;
10
+ //# sourceMappingURL=mapMarketContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mapMarketContext.d.ts","sourceRoot":"","sources":["../../../src/utils/mapMarketContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAIzD;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,GAC3B,UAAU,MAAM,EAChB,KAAK,gBAAgB,KACpB,aAkBF,CAAA"}
@@ -4,17 +4,16 @@ import { type Subscription } from '@lifi/perps-types';
4
4
  * `WsProviderFactory` constructor for Hyperliquid — pass to
5
5
  * `new PerpsWsClient(client, { wsProviders: { hyperliquid: hyperliquidWsProvider() } })`.
6
6
  *
7
- * Derives the active sub-DEX set from the `markets` list `PerpsWsClient`
8
- * passes in (the provider's own key and `'spot'` are excluded — they
9
- * aren't sub-DEX subscriptions on the HL wire). Higher-order shape mirrors
10
- * `lighterWsProvider(options)` so the two factories register identically.
7
+ * Higher-order shape mirrors `lighterWsProvider(options)` so the two factories
8
+ * register identically.
11
9
  *
12
10
  * @public
13
11
  */
14
12
  export declare const hyperliquidWsProvider: () => WsProviderFactory;
15
13
  /**
16
- * Hyperliquid realtime {@link WsProvider}: multiplexes prices, positions,
17
- * orders, fills and spot balances over a single {@link ReconnectingWebSocket}.
14
+ * Hyperliquid realtime {@link WsProvider}: multiplexes markets context,
15
+ * positions, orders, fills and spot balances over a single
16
+ * {@link ReconnectingWebSocket}.
18
17
  * Construct via {@link hyperliquidWsProvider}.
19
18
  *
20
19
  * `orderUpdates` supports a single address per provider instance: HL delivers
@@ -27,11 +26,15 @@ export declare const hyperliquidWsProvider: () => WsProviderFactory;
27
26
  */
28
27
  export declare class HyperliquidWsProvider extends WsProviderBase<object> {
29
28
  private orderUpdatesKey;
30
- private readonly subDexes;
31
29
  private readonly client;
32
30
  private readonly registry;
33
- private midsBySubDex;
34
- constructor(wsUrl: string, providerKey: string, subDexes: string[], client?: PerpsSDKClient);
31
+ private perpCtxBySubDex;
32
+ private orderbookKeysByMarketId;
33
+ private latestOrderbookByMarketId;
34
+ private fastCtxByMarketId;
35
+ private fastDecodeChain;
36
+ private orderbookDecodeChain;
37
+ constructor(wsUrl: string, providerKey: string, client?: PerpsSDKClient);
35
38
  subscribeQuote(params: ProviderGetQuoteParams, onQuote: QuoteListener): Promise<() => void>;
36
39
  protected openChannel(sub: Subscription): Promise<() => void>;
37
40
  /**
@@ -41,18 +44,47 @@ export declare class HyperliquidWsProvider extends WsProviderBase<object> {
41
44
  * so a wallet switch needn't wait out the teardown linger; a live one throws.
42
45
  */
43
46
  private claimOrderUpdatesKey;
44
- /** Build sub-key + payload pairs for each sub-dex allMids subscription. */
45
- private getPriceSubEntries;
47
+ private claimOrderbookKey;
48
+ /**
49
+ * Sub-key + payload pairs for the markets-context aggregation: the all-dexs
50
+ * perp asset-context feed (oracle/funding/OI/metadata) plus the compressed
51
+ * `fastAssetCtxs` feed (high-frequency mid + mark across every dex).
52
+ */
53
+ private getMarketsContextSubEntries;
54
+ private registerOrderbook;
55
+ private releaseOrderbook;
46
56
  protected onClose(): void;
47
57
  protected sendSubscribe(payload: object): void;
48
58
  protected toKey(sub: Subscription): string;
59
+ private orderbookAggregation;
60
+ private orderbookAggregationKey;
49
61
  private toHlPayload;
50
62
  protected handleMessage(raw: string): void;
51
- private handleAllMids;
52
- /** Latest mid per `Market.id`, merged across sub-dex allMids frames. */
63
+ private handleAllDexsAssetCtxs;
64
+ /**
65
+ * Decode a `fastAssetCtxs` frame (base64 + raw-DEFLATE) and merge its changed
66
+ * coins into the per-market fast-context store. Frames are incremental, so a
67
+ * field absent from this frame keeps its prior value. Decodes are chained to
68
+ * preserve arrival order across the async boundary.
69
+ */
70
+ private handleFastAssetCtxs;
71
+ /**
72
+ * Build the all-markets context map: every perp asset context across sub-dexs
73
+ * (oracle/funding/OI/metadata), with mid + mark overlaid from the
74
+ * high-frequency `fastAssetCtxs` feed. A market present in `fastAssetCtxs` but
75
+ * without an asset-context frame yet (the aggregate feed is slow/idle) still
76
+ * gets a mid+mark entry, so price ticks without waiting on the aggregate.
77
+ */
78
+ private emitMarketsContext;
79
+ /** Latest mid per `Market.id` across the perp ctx feed and `fastAssetCtxs`. */
53
80
  private mergedMids;
81
+ private orderbookReferencePrice;
54
82
  private handleL2Book;
83
+ private handleL2;
84
+ private handleCompactL2Delta;
85
+ private emitOrderbook;
55
86
  private handleCandle;
87
+ private handleTrades;
56
88
  private handleOrderUpdates;
57
89
  private handleUserFills;
58
90
  private handleAllDexsClearinghouseState;
@@ -1 +1 @@
1
- {"version":3,"file":"HyperliquidWsProvider.d.ts","sourceRoot":"","sources":["../../../src/websocket/HyperliquidWsProvider.ts"],"names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IAmBzB;;;;OAIG;IACH,OAAO,CAAC,2BAA2B;IAUnC,OAAO,CAAC,iBAAiB;IAUzB,OAAO,CAAC,gBAAgB;cAcL,OAAO,IAAI,IAAI;IAQlC,SAAS,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM9C,SAAS,CAAC,KAAK,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM;IAwB1C,OAAO,CAAC,oBAAoB;IAa5B,OAAO,CAAC,uBAAuB;IAa/B,OAAO,CAAC,WAAW;IAwCnB,SAAS,CAAC,aAAa,CAAC,GAAG,EAAE,MAAM;IAkEnC,OAAO,CAAC,sBAAsB;IAW9B;;;;;OAKG;IACH,OAAO,CAAC,mBAAmB;IAgB3B;;;;;;OAMG;IACH,OAAO,CAAC,kBAAkB;IA4B1B,+EAA+E;IAC/E,OAAO,CAAC,UAAU;IAiBlB,OAAO,CAAC,uBAAuB;IAgB/B,OAAO,CAAC,YAAY;IAgBpB,OAAO,CAAC,QAAQ;IAoBhB,OAAO,CAAC,oBAAoB;IA2B5B,OAAO,CAAC,aAAa;IAUrB,OAAO,CAAC,YAAY;IAcpB,OAAO,CAAC,YAAY;IAmBpB,OAAO,CAAC,kBAAkB;IA0D1B,OAAO,CAAC,eAAe;IAWvB,OAAO,CAAC,+BAA+B;IAkBvC,OAAO,CAAC,eAAe;CAaxB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifi/perps-sdk-provider-hyperliquid",
3
- "version": "1.0.1",
3
+ "version": "1.2.0",
4
4
  "description": "Hyperliquid provider plugin for @lifi/perps-sdk",
5
5
  "homepage": "https://github.com/lifinance/perps-sdk",
6
6
  "bugs": {
@@ -11,7 +11,7 @@
11
11
  "url": "https://github.com/lifinance/perps-sdk.git"
12
12
  },
13
13
  "license": "Apache-2.0",
14
- "author": "LI.FI <dev@li.fi>",
14
+ "author": "Aaron Boyd <aaron@li.finance>",
15
15
  "type": "module",
16
16
  "main": "./dist/cjs/index.js",
17
17
  "module": "./dist/esm/index.js",
@@ -47,11 +47,11 @@
47
47
  ],
48
48
  "dependencies": {
49
49
  "big.js": "^7.0.1",
50
- "@lifi/perps-types": "1.0.1"
50
+ "viem": "^2.48.8",
51
+ "@lifi/perps-types": "^1.2.0"
51
52
  },
52
53
  "peerDependencies": {
53
- "viem": "^2.48.8",
54
- "@lifi/perps-sdk": "1.0.1"
54
+ "@lifi/perps-sdk": "^1.2.0"
55
55
  },
56
56
  "publishConfig": {
57
57
  "access": "public"
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  getMarketRegistry,
3
- getPrices,
3
+ getMarketsContext,
4
4
  type ProviderGetAccountParams,
5
5
  type SDKRequestOptions,
6
6
  stringToFloat,
@@ -208,7 +208,7 @@ export const getAccount = async (
208
208
  )
209
209
  )
210
210
  ),
211
- getPrices(client, { provider: PROVIDER_KEY }, options),
211
+ getMarketsContext(client, { provider: PROVIDER_KEY }, options),
212
212
  ])
213
213
 
214
214
  const priceById = spotPriceById(
@@ -29,9 +29,6 @@ export type HlMetaAndAssetCtxs = [HlMeta, HlAssetCtx[]]
29
29
  /** @public */
30
30
  export type HlUniverse = HlMeta['universe']
31
31
 
32
- /** @public */
33
- export type HlAllMids = Record<string, string>
34
-
35
32
  /** @public */
36
33
  export type HlCandle = {
37
34
  t: number
package/src/types/ws.ts CHANGED
@@ -1,24 +1,79 @@
1
1
  // Hyperliquid WebSocket incoming message types.
2
2
 
3
3
  import type { HlClearinghouseState, HlSpotBalance } from './account.js'
4
- import type { HlAllMids, HlCandle, HlL2Book } from './asset.js'
4
+ import type { HlCandle, HlL2Book } from './asset.js'
5
5
  import type { HlUserFill } from './fill.js'
6
6
 
7
+ /**
8
+ * Perp asset context as carried on the `allDexsAssetCtxs` WS feed. Extends the
9
+ * REST `/info` `HlAssetCtx` shape with the wire-only `coin` tag, the order-book
10
+ * `midPx` (null when the book is empty), and `oraclePx`.
11
+ * @public
12
+ */
13
+ export type HlWsPerpAssetCtx = {
14
+ coin: string
15
+ funding: string
16
+ openInterest: string
17
+ dayNtlVlm: string
18
+ prevDayPx: string
19
+ markPx: string
20
+ midPx: string | null
21
+ oraclePx: string
22
+ }
23
+
24
+ /**
25
+ * `allDexsAssetCtxs` carries one `[dex, ctxs]` pair per perp sub-DEX, mirroring
26
+ * `allDexsClearinghouseState`. Spot contexts arrive separately.
27
+ * @public
28
+ */
29
+ export type HlWsAllDexsAssetCtxsData = {
30
+ assetCtxs: [string, HlWsPerpAssetCtx[]][]
31
+ }
32
+
7
33
  /** @public */
8
34
  export type HlWsMessage = {
9
35
  channel: string
10
36
  data: unknown
11
37
  }
12
38
 
13
- /** @public */
14
- export type HlWsAllMidsData = {
15
- mids: HlAllMids
16
- dex?: string
39
+ /**
40
+ * Per-coin entry from the compressed `fastAssetCtxs` feed (mark + mid only,
41
+ * keyed by coin across all dexes). `midPx` is null when the book is empty;
42
+ * fields are omitted from incremental frames when unchanged.
43
+ * @public
44
+ */
45
+ export type HlWsFastAssetCtx = {
46
+ markPx?: string
47
+ midPx?: string | null
17
48
  }
18
49
 
19
50
  /** @public */
20
51
  export type HlWsL2BookData = HlL2Book & { coin: string }
21
52
 
53
+ /** @public */
54
+ export type HlWsL2Data = {
55
+ s?: HlL2Book & { coin: string }
56
+ c?: string
57
+ u?: HlWsCompressedL2Data
58
+ }
59
+
60
+ /** @public */
61
+ export type HlWsCompressedL2Level = {
62
+ p: string
63
+ s: string
64
+ }
65
+
66
+ /** @public */
67
+ export type HlWsCompressedL2Data = {
68
+ c: string
69
+ t: number
70
+ l: [HlWsCompressedL2Level[], HlWsCompressedL2Level[]]
71
+ r?: [
72
+ Array<number | string | { p: string }>,
73
+ Array<number | string | { p: string }>,
74
+ ]
75
+ }
76
+
22
77
  /** @public */
23
78
  export type HlWsCandleData = HlCandle & {
24
79
  T: number
@@ -27,6 +82,17 @@ export type HlWsCandleData = HlCandle & {
27
82
  n: number
28
83
  }
29
84
 
85
+ /** @public */
86
+ export type HlWsTrade = {
87
+ coin: string
88
+ side: string
89
+ px: string
90
+ sz: string
91
+ time: number
92
+ tid?: number
93
+ hash?: string
94
+ }
95
+
30
96
  /** @public */
31
97
  export type HlWsUserFillsData = {
32
98
  isSnapshot: boolean
@@ -0,0 +1,24 @@
1
+ import type { HlWsFastAssetCtx } from '../types/index.js'
2
+
3
+ export async function decodeCompressedJson<T>(base64: string): Promise<T> {
4
+ const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0))
5
+ const stream = new Blob([bytes])
6
+ .stream()
7
+ .pipeThrough(new DecompressionStream('deflate-raw'))
8
+ const text = await new Response(stream).text()
9
+ return JSON.parse(text) as T
10
+ }
11
+
12
+ /**
13
+ * Decode a Hyperliquid `fastAssetCtxs` frame. HL sends the payload as a
14
+ * base64-encoded, raw-DEFLATE-compressed (RFC 1951 — no zlib/gzip wrapper) JSON
15
+ * object keyed by coin. Decoded via the web-standard `DecompressionStream`
16
+ * (Node 18+, modern browsers), so no dependency is needed.
17
+ *
18
+ * @throws If the input is not valid base64 / raw-DEFLATE / JSON.
19
+ */
20
+ export async function decodeFastAssetCtxs(
21
+ base64: string
22
+ ): Promise<Record<string, HlWsFastAssetCtx>> {
23
+ return decodeCompressedJson<Record<string, HlWsFastAssetCtx>>(base64)
24
+ }
@@ -3,6 +3,10 @@
3
3
  // (e.g. `./assetId.js`, `./mapFill.js`) directly.
4
4
 
5
5
  export { assetIsSpot, calculateAssetId, getProviderIndex } from './assetId.js'
6
+ export {
7
+ decodeCompressedJson,
8
+ decodeFastAssetCtxs,
9
+ } from './decodeFastAssetCtxs.js'
6
10
  export type { InfoRequestOptions } from './infoClient.js'
7
11
  export { hlInfoOptions, infoRequest } from './infoClient.js'
8
12
  export {
@@ -12,7 +16,7 @@ export {
12
16
  export { mapFundingActivity, mapLedgerEntry } from './mapActivity.js'
13
17
  export { classifyFillFromPosition, mapFill } from './mapFill.js'
14
18
  export { mapMarket } from './mapMarket.js'
15
- export { mapMarketPrice } from './mapMarketPrice.js'
19
+ export { mapMarketContext } from './mapMarketContext.js'
16
20
  export {
17
21
  isTriggerOrder,
18
22
  isTriggerType,
@@ -10,8 +10,8 @@ const QUOTE_SYMBOL = 'USDC'
10
10
 
11
11
  /**
12
12
  * Map a Hyperliquid universe entry to a {@link PerpsMarket}'s static instrument
13
- * metadata. Live mark/stats fields live on the `/prices` item — see
14
- * {@link mapMarketPrice}. `categoryId` is the `Provider.categories[].id` of the
13
+ * metadata. Live mark/stats fields live on the {@link MarketContext} — see
14
+ * {@link mapMarketContext}. `categoryId` is the `Provider.categories[].id` of the
15
15
  * dex the entry was fetched from — known to the caller at fetch time, never
16
16
  * re-derived from the coin string.
17
17
  * @public
@@ -0,0 +1,33 @@
1
+ import type { MarketContext } from '@lifi/perps-types'
2
+ import type { HlWsPerpAssetCtx } from '../types/index.js'
3
+
4
+ const NEXT_FUNDING_INTERVAL_MS = 60 * 60 * 1000 // 1 hour
5
+
6
+ /**
7
+ * Map a Hyperliquid perp asset context from the `allDexsAssetCtxs` feed to a
8
+ * {@link MarketContext}. `midPx` is null when the book is empty; mid then falls
9
+ * back to the mark so the context always carries a usable mid.
10
+ * @public
11
+ */
12
+ export const mapMarketContext = (
13
+ marketId: string,
14
+ ctx: HlWsPerpAssetCtx
15
+ ): MarketContext => {
16
+ const now = Date.now()
17
+ const nextFundingTime =
18
+ Math.ceil(now / NEXT_FUNDING_INTERVAL_MS) * NEXT_FUNDING_INTERVAL_MS
19
+
20
+ return {
21
+ marketId,
22
+ midPrice: ctx.midPx ?? ctx.markPx,
23
+ markPrice: ctx.markPx,
24
+ oraclePrice: ctx.oraclePx,
25
+ prevDayPrice: ctx.prevDayPx,
26
+ volume24h: ctx.dayNtlVlm,
27
+ openInterest: ctx.openInterest,
28
+ funding: {
29
+ rate: ctx.funding,
30
+ nextFundingTime,
31
+ },
32
+ }
33
+ }