@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.
Files changed (116) hide show
  1. package/dist/cjs/HyperliquidProvider.d.ts.map +1 -1
  2. package/dist/cjs/HyperliquidProvider.js +2 -0
  3. package/dist/cjs/HyperliquidProvider.js.map +1 -1
  4. package/dist/cjs/accountConfig.d.ts.map +1 -1
  5. package/dist/cjs/accountConfig.js +1 -0
  6. package/dist/cjs/accountConfig.js.map +1 -1
  7. package/dist/cjs/accountSummary.d.ts.map +1 -1
  8. package/dist/cjs/accountSummary.js +5 -3
  9. package/dist/cjs/accountSummary.js.map +1 -1
  10. package/dist/cjs/index.d.ts +2 -0
  11. package/dist/cjs/index.d.ts.map +1 -1
  12. package/dist/cjs/index.js +3 -1
  13. package/dist/cjs/index.js.map +1 -1
  14. package/dist/cjs/services/getAccount.d.ts.map +1 -1
  15. package/dist/cjs/services/getAccount.js +4 -4
  16. package/dist/cjs/services/getAccount.js.map +1 -1
  17. package/dist/cjs/services/getAccountExists.d.ts +8 -0
  18. package/dist/cjs/services/getAccountExists.d.ts.map +1 -0
  19. package/dist/cjs/services/getAccountExists.js +15 -0
  20. package/dist/cjs/services/getAccountExists.js.map +1 -0
  21. package/dist/cjs/types/account.d.ts +3 -0
  22. package/dist/cjs/types/account.d.ts.map +1 -1
  23. package/dist/cjs/types/account.js.map +1 -1
  24. package/dist/cjs/types/ws.d.ts +56 -1
  25. package/dist/cjs/types/ws.d.ts.map +1 -1
  26. package/dist/cjs/utils/decodeFastAssetCtxs.d.ts +1 -0
  27. package/dist/cjs/utils/decodeFastAssetCtxs.d.ts.map +1 -1
  28. package/dist/cjs/utils/decodeFastAssetCtxs.js +5 -1
  29. package/dist/cjs/utils/decodeFastAssetCtxs.js.map +1 -1
  30. package/dist/cjs/utils/index.d.ts +1 -1
  31. package/dist/cjs/utils/index.d.ts.map +1 -1
  32. package/dist/cjs/utils/index.js +2 -1
  33. package/dist/cjs/utils/index.js.map +1 -1
  34. package/dist/cjs/utils/mapOrder.d.ts.map +1 -1
  35. package/dist/cjs/utils/mapOrder.js +12 -5
  36. package/dist/cjs/utils/mapOrder.js.map +1 -1
  37. package/dist/cjs/websocket/HyperliquidWsProvider.d.ts +22 -0
  38. package/dist/cjs/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  39. package/dist/cjs/websocket/HyperliquidWsProvider.js +430 -28
  40. package/dist/cjs/websocket/HyperliquidWsProvider.js.map +1 -1
  41. package/dist/esm/HyperliquidProvider.d.ts.map +1 -1
  42. package/dist/esm/HyperliquidProvider.js +2 -0
  43. package/dist/esm/HyperliquidProvider.js.map +1 -1
  44. package/dist/esm/accountConfig.d.ts.map +1 -1
  45. package/dist/esm/accountConfig.js +1 -0
  46. package/dist/esm/accountConfig.js.map +1 -1
  47. package/dist/esm/accountSummary.d.ts +2 -2
  48. package/dist/esm/accountSummary.d.ts.map +1 -1
  49. package/dist/esm/accountSummary.js +12 -9
  50. package/dist/esm/accountSummary.js.map +1 -1
  51. package/dist/esm/index.d.ts +2 -0
  52. package/dist/esm/index.d.ts.map +1 -1
  53. package/dist/esm/index.js +1 -0
  54. package/dist/esm/index.js.map +1 -1
  55. package/dist/esm/services/getAccount.d.ts.map +1 -1
  56. package/dist/esm/services/getAccount.js +10 -7
  57. package/dist/esm/services/getAccount.js.map +1 -1
  58. package/dist/esm/services/getAccountExists.d.ts +22 -0
  59. package/dist/esm/services/getAccountExists.d.ts.map +1 -0
  60. package/dist/esm/services/getAccountExists.js +20 -0
  61. package/dist/esm/services/getAccountExists.js.map +1 -0
  62. package/dist/esm/types/account.d.ts +9 -0
  63. package/dist/esm/types/account.d.ts.map +1 -1
  64. package/dist/esm/types/account.js.map +1 -1
  65. package/dist/esm/types/ws.d.ts +75 -1
  66. package/dist/esm/types/ws.d.ts.map +1 -1
  67. package/dist/esm/utils/decodeFastAssetCtxs.d.ts +1 -0
  68. package/dist/esm/utils/decodeFastAssetCtxs.d.ts.map +1 -1
  69. package/dist/esm/utils/decodeFastAssetCtxs.js +9 -6
  70. package/dist/esm/utils/decodeFastAssetCtxs.js.map +1 -1
  71. package/dist/esm/utils/index.d.ts +1 -1
  72. package/dist/esm/utils/index.d.ts.map +1 -1
  73. package/dist/esm/utils/index.js +1 -1
  74. package/dist/esm/utils/index.js.map +1 -1
  75. package/dist/esm/utils/mapOrder.d.ts +11 -1
  76. package/dist/esm/utils/mapOrder.d.ts.map +1 -1
  77. package/dist/esm/utils/mapOrder.js +23 -6
  78. package/dist/esm/utils/mapOrder.js.map +1 -1
  79. package/dist/esm/websocket/HyperliquidWsProvider.d.ts +27 -8
  80. package/dist/esm/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  81. package/dist/esm/websocket/HyperliquidWsProvider.js +439 -47
  82. package/dist/esm/websocket/HyperliquidWsProvider.js.map +1 -1
  83. package/dist/types/HyperliquidProvider.d.ts.map +1 -1
  84. package/dist/types/accountConfig.d.ts.map +1 -1
  85. package/dist/types/accountSummary.d.ts +2 -2
  86. package/dist/types/accountSummary.d.ts.map +1 -1
  87. package/dist/types/index.d.ts +2 -0
  88. package/dist/types/index.d.ts.map +1 -1
  89. package/dist/types/services/getAccount.d.ts.map +1 -1
  90. package/dist/types/services/getAccountExists.d.ts +22 -0
  91. package/dist/types/services/getAccountExists.d.ts.map +1 -0
  92. package/dist/types/types/account.d.ts +9 -0
  93. package/dist/types/types/account.d.ts.map +1 -1
  94. package/dist/types/types/ws.d.ts +75 -1
  95. package/dist/types/types/ws.d.ts.map +1 -1
  96. package/dist/types/utils/decodeFastAssetCtxs.d.ts +1 -0
  97. package/dist/types/utils/decodeFastAssetCtxs.d.ts.map +1 -1
  98. package/dist/types/utils/index.d.ts +1 -1
  99. package/dist/types/utils/index.d.ts.map +1 -1
  100. package/dist/types/utils/mapOrder.d.ts +11 -1
  101. package/dist/types/utils/mapOrder.d.ts.map +1 -1
  102. package/dist/types/websocket/HyperliquidWsProvider.d.ts +27 -8
  103. package/dist/types/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  104. package/package.json +3 -3
  105. package/src/HyperliquidProvider.ts +8 -0
  106. package/src/accountConfig.ts +1 -0
  107. package/src/accountSummary.ts +12 -9
  108. package/src/index.ts +2 -0
  109. package/src/services/getAccount.ts +10 -11
  110. package/src/services/getAccountExists.ts +40 -0
  111. package/src/types/account.ts +10 -0
  112. package/src/types/ws.ts +79 -1
  113. package/src/utils/decodeFastAssetCtxs.ts +10 -6
  114. package/src/utils/index.ts +4 -1
  115. package/src/utils/mapOrder.ts +23 -6
  116. package/src/websocket/HyperliquidWsProvider.ts +558 -49
@@ -28,7 +28,7 @@ import {
28
28
  spotPriceById,
29
29
  } from '../utils/index.js'
30
30
  import { hlInfoOptions, infoRequest } from '../utils/infoClient.js'
31
- import { mapPosition } from '../utils/mapPosition.js'
31
+ import { isOpenAssetPosition, mapPosition } from '../utils/mapPosition.js'
32
32
 
33
33
  /**
34
34
  * Parameters for {@link getAccount}.
@@ -37,16 +37,14 @@ import { mapPosition } from '../utils/mapPosition.js'
37
37
  */
38
38
  export type GetAccountParams = ProviderGetAccountParams
39
39
 
40
+ // `marginSummary` covers the whole account (cross AND isolated positions);
41
+ // `crossMarginSummary` is the cross-only subset and would drop isolated
42
+ // equity/margin.
40
43
  const getAccountValue = (state: HlClearinghouseState): number =>
41
- Number.parseFloat(
42
- state.crossMarginSummary.accountValue || state.marginSummary.accountValue
43
- )
44
+ Number.parseFloat(state.marginSummary.accountValue)
44
45
 
45
46
  const getTotalMarginUsed = (state: HlClearinghouseState): number =>
46
- Number.parseFloat(
47
- state.crossMarginSummary.totalMarginUsed ||
48
- state.marginSummary.totalMarginUsed
49
- )
47
+ Number.parseFloat(state.marginSummary.totalMarginUsed)
50
48
 
51
49
  const getMarginUsed = (
52
50
  abstraction: HlAbstractionMode | null,
@@ -113,8 +111,8 @@ const buildBalances = (
113
111
 
114
112
  // Unified/portfolio modes hold everything in spot — per-dex equity would
115
113
  // double-count. Only disabled/dexAbstraction carry separate venue collateral.
116
- // `accountValue` is reported net of locked margin, i.e. the free collateral;
117
- // the locked portion is carried by the positions' `marginUsed`.
114
+ // `accountValue` is the dex's TOTAL equity: locked margin and unrealized
115
+ // PnL are already included, so summaries must not add them on top.
118
116
  if (!isUnifiedMode(abstraction)) {
119
117
  for (const [dex, state] of stateByDex) {
120
118
  const categoryId = dex || PROVIDER_KEY
@@ -218,7 +216,7 @@ export const getAccount = async (
218
216
 
219
217
  const positions: Position[] = stateResults.flatMap((state) =>
220
218
  state.assetPositions
221
- .filter((ap) => Number.parseFloat(ap.position.szi) !== 0)
219
+ .filter(isOpenAssetPosition)
222
220
  .map((ap) => mapPosition(ap, registry.require(ap.position.coin)))
223
221
  )
224
222
 
@@ -252,6 +250,7 @@ export const getAccount = async (
252
250
  address: params.address,
253
251
  balances,
254
252
  collateralBalances,
253
+ positions,
255
254
  marginUsed: getMarginUsed(abstractionResult, positions, stateByDex),
256
255
  unrealizedPnl: totalUnrealizedPnl.toString(),
257
256
  feeTier: {
@@ -0,0 +1,40 @@
1
+ import type { SDKRequestOptions } from '@lifi/perps-sdk'
2
+ import { type Address, zeroAddress } from 'viem'
3
+ import type { HyperliquidContext } from '../context.js'
4
+ import type { HlPreTransferCheck } from '../types/index.js'
5
+ import { hlInfoOptions, infoRequest } from '../utils/infoClient.js'
6
+
7
+ /**
8
+ * Parameters for {@link getAccountExists}.
9
+ *
10
+ * @public
11
+ */
12
+ export interface GetAccountExistsParams {
13
+ address: Address
14
+ }
15
+
16
+ /**
17
+ * Resolve whether a Hyperliquid Core account exists for `address` via a
18
+ * `preTransferCheck` info query. An account exists only after its first
19
+ * deposit (which pays the one-time creation fee), so this is the deposit-first
20
+ * gate for setup. `userExists` is independent of the `source` field, so a zero
21
+ * address is used.
22
+ * @throws {PerpsError} On Hyperliquid REST error, network, or parsing failures.
23
+ * @public
24
+ */
25
+ export const getAccountExists = async (
26
+ { client, apiUrl }: HyperliquidContext,
27
+ params: GetAccountExistsParams,
28
+ options?: SDKRequestOptions
29
+ ): Promise<boolean> => {
30
+ const { userExists } = await infoRequest<HlPreTransferCheck>(
31
+ apiUrl,
32
+ {
33
+ type: 'preTransferCheck',
34
+ user: params.address,
35
+ source: zeroAddress,
36
+ },
37
+ hlInfoOptions(client, options)
38
+ )
39
+ return userExists
40
+ }
@@ -57,6 +57,16 @@ export type HlUserFees = {
57
57
  /** @public */
58
58
  export type HlExtraAgents = Record<string, unknown>[]
59
59
 
60
+ /**
61
+ * Response of the `preTransferCheck` info query. `userExists` is `false` until
62
+ * the account's first deposit (which pays the one-time creation `fee`); only
63
+ * `userExists` is read here.
64
+ * @public
65
+ */
66
+ export type HlPreTransferCheck = {
67
+ userExists: boolean
68
+ }
69
+
60
70
  /**
61
71
  * Possible values returned by the `userAbstraction` info endpoint.
62
72
  * `null` means abstraction has never been set (standard mode).
package/src/types/ws.ts CHANGED
@@ -4,6 +4,8 @@ import type { HlClearinghouseState, HlSpotBalance } from './account.js'
4
4
  import type { HlCandle, HlL2Book } from './asset.js'
5
5
  import type { HlUserFill } from './fill.js'
6
6
 
7
+ type HlWsNumberString = string | number
8
+
7
9
  /**
8
10
  * Perp asset context as carried on the `allDexsAssetCtxs` WS feed. Extends the
9
11
  * REST `/info` `HlAssetCtx` shape with the wire-only `coin` tag, the order-book
@@ -21,15 +23,27 @@ export type HlWsPerpAssetCtx = {
21
23
  oraclePx: string
22
24
  }
23
25
 
26
+ /** @public */
27
+ export type HlWsPerpAssetCtxPayload = Partial<HlWsPerpAssetCtx>
28
+
24
29
  /**
25
30
  * `allDexsAssetCtxs` carries one `[dex, ctxs]` pair per perp sub-DEX, mirroring
26
31
  * `allDexsClearinghouseState`. Spot contexts arrive separately.
27
32
  * @public
28
33
  */
29
34
  export type HlWsAllDexsAssetCtxsData = {
30
- assetCtxs: [string, HlWsPerpAssetCtx[]][]
35
+ assetCtxs?: [string, HlWsPerpAssetCtx[]][]
36
+ ctxs?: [string, HlWsPerpAssetCtxPayload[]][]
31
37
  }
32
38
 
39
+ /**
40
+ * Compressed perp asset-context feed. Snapshot/update frames carry one
41
+ * `[dex, ctxs]` pair per perp sub-DEX. Updates can omit unchanged fields inside
42
+ * each context.
43
+ * @public
44
+ */
45
+ export type HlWsPacData = [string, HlWsPerpAssetCtxPayload[]][]
46
+
33
47
  /** @public */
34
48
  export type HlWsMessage = {
35
49
  channel: string
@@ -47,9 +61,62 @@ export type HlWsFastAssetCtx = {
47
61
  midPx?: string | null
48
62
  }
49
63
 
64
+ /** @public */
65
+ export type HlWsSpotAssetCtx = {
66
+ dayNtlVlm?: HlWsNumberString
67
+ prevDayPx?: HlWsNumberString
68
+ markPx?: HlWsNumberString
69
+ midPx?: HlWsNumberString | null
70
+ dayBaseVlm?: HlWsNumberString
71
+ circulatingSupply?: HlWsNumberString
72
+ }
73
+
74
+ /** @public */
75
+ export type HlWsActiveAssetCtxData = {
76
+ coin: string
77
+ ctx: Partial<Record<keyof HlWsPerpAssetCtx, HlWsNumberString | null>>
78
+ }
79
+
80
+ /** @public */
81
+ export type HlWsActiveSpotAssetCtxData = {
82
+ coin: string
83
+ ctx: HlWsSpotAssetCtx
84
+ }
85
+
86
+ /**
87
+ * Compressed spot asset-context feed. The first frame is a broad snapshot; later
88
+ * frames only carry changed fields.
89
+ * @public
90
+ */
91
+ export type HlWsSacData = Record<string, HlWsSpotAssetCtx>
92
+
50
93
  /** @public */
51
94
  export type HlWsL2BookData = HlL2Book & { coin: string }
52
95
 
96
+ /** @public */
97
+ export type HlWsL2Data = {
98
+ s?: HlL2Book & { coin: string }
99
+ c?: string
100
+ u?: HlWsCompressedL2Data
101
+ }
102
+
103
+ /** @public */
104
+ export type HlWsCompressedL2Level = {
105
+ p: string
106
+ s: string
107
+ }
108
+
109
+ /** @public */
110
+ export type HlWsCompressedL2Data = {
111
+ c: string
112
+ t: number
113
+ l: [HlWsCompressedL2Level[], HlWsCompressedL2Level[]]
114
+ r?: [
115
+ Array<number | string | { p: string }>,
116
+ Array<number | string | { p: string }>,
117
+ ]
118
+ }
119
+
53
120
  /** @public */
54
121
  export type HlWsCandleData = HlCandle & {
55
122
  T: number
@@ -58,6 +125,17 @@ export type HlWsCandleData = HlCandle & {
58
125
  n: number
59
126
  }
60
127
 
128
+ /** @public */
129
+ export type HlWsTrade = {
130
+ coin: string
131
+ side: string
132
+ px: string
133
+ sz: string
134
+ time: number
135
+ tid?: number
136
+ hash?: string
137
+ }
138
+
61
139
  /** @public */
62
140
  export type HlWsUserFillsData = {
63
141
  isSnapshot: boolean
@@ -1,5 +1,14 @@
1
1
  import type { HlWsFastAssetCtx } from '../types/index.js'
2
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
+
3
12
  /**
4
13
  * Decode a Hyperliquid `fastAssetCtxs` frame. HL sends the payload as a
5
14
  * base64-encoded, raw-DEFLATE-compressed (RFC 1951 — no zlib/gzip wrapper) JSON
@@ -11,10 +20,5 @@ import type { HlWsFastAssetCtx } from '../types/index.js'
11
20
  export async function decodeFastAssetCtxs(
12
21
  base64: string
13
22
  ): Promise<Record<string, HlWsFastAssetCtx>> {
14
- const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0))
15
- const stream = new Blob([bytes])
16
- .stream()
17
- .pipeThrough(new DecompressionStream('deflate-raw'))
18
- const text = await new Response(stream).text()
19
- return JSON.parse(text)
23
+ return decodeCompressedJson<Record<string, HlWsFastAssetCtx>>(base64)
20
24
  }
@@ -3,7 +3,10 @@
3
3
  // (e.g. `./assetId.js`, `./mapFill.js`) directly.
4
4
 
5
5
  export { assetIsSpot, calculateAssetId, getProviderIndex } from './assetId.js'
6
- export { decodeFastAssetCtxs } from './decodeFastAssetCtxs.js'
6
+ export {
7
+ decodeCompressedJson,
8
+ decodeFastAssetCtxs,
9
+ } from './decodeFastAssetCtxs.js'
7
10
  export type { InfoRequestOptions } from './infoClient.js'
8
11
  export { hlInfoOptions, infoRequest } from './infoClient.js'
9
12
  export {
@@ -122,7 +122,17 @@ export const mapTriggerOrder = (
122
122
  }
123
123
  }
124
124
 
125
- /** @public */
125
+ /**
126
+ * Map a raw Hyperliquid order status string to the SDK's OrderStatus enum.
127
+ *
128
+ * HL's terminal statuses are mostly compound strings ending in `Canceled`
129
+ * or `Rejected` (e.g. `siblingFilledCanceled`, `tickRejected`); classify
130
+ * those by suffix so a status HL adds later still lands in the correct
131
+ * terminal bucket instead of silently falling through to PENDING, which
132
+ * {@link isActiveOrderStatus} treats as active. `scheduledCancel` is the
133
+ * one documented cancel status without the `Canceled` suffix.
134
+ * @public
135
+ */
126
136
  export const mapOrderStatus = (status: string): OrderStatus => {
127
137
  switch (status) {
128
138
  case 'open':
@@ -132,14 +142,19 @@ export const mapOrderStatus = (status: string): OrderStatus => {
132
142
  return OrderStatus.FILLED
133
143
  case 'canceled':
134
144
  case 'cancelled':
145
+ case 'scheduledCancel':
135
146
  return OrderStatus.CANCELLED
136
147
  case 'rejected':
137
148
  return OrderStatus.REJECTED
138
149
  case 'triggered':
139
150
  return OrderStatus.TRIGGERED
140
- case 'marginCanceled':
141
- return OrderStatus.CANCELLED
142
151
  default:
152
+ if (status.endsWith('Canceled') || status.endsWith('Cancelled')) {
153
+ return OrderStatus.CANCELLED
154
+ }
155
+ if (status.endsWith('Rejected')) {
156
+ return OrderStatus.REJECTED
157
+ }
143
158
  return OrderStatus.PENDING
144
159
  }
145
160
  }
@@ -153,7 +168,7 @@ export const mapOrderStatus = (status: string): OrderStatus => {
153
168
  */
154
169
  export const mapStatusReason = (status: string): string | undefined => {
155
170
  switch (status) {
156
- case 'iocCanceled':
171
+ case 'iocCancelRejected':
157
172
  return 'Order cancelled: not enough liquidity to fill immediately.'
158
173
  case 'reduceOnlyCanceled':
159
174
  return 'Order cancelled: would not reduce your position.'
@@ -165,12 +180,14 @@ export const mapStatusReason = (status: string): string | undefined => {
165
180
  return 'Order cancelled: sibling OCO order filled first.'
166
181
  case 'selfTradeCanceled':
167
182
  return 'Order cancelled: would self-trade against your own resting order.'
183
+ case 'scheduledCancel':
184
+ return "Order cancelled: dead man's switch triggered."
168
185
  case 'tickRejected':
169
186
  return 'Order rejected: price did not match the tick size.'
170
187
  case 'minTradeNtlRejected':
171
188
  return 'Order rejected: notional value below the minimum trade size.'
172
- case 'delistedRejected':
173
- return 'Order rejected: market has been delisted.'
189
+ case 'delistedCanceled':
190
+ return 'Order cancelled: market has been delisted.'
174
191
  default:
175
192
  return undefined
176
193
  }