@lifi/perps-types 0.1.1-alpha.1 → 0.1.1-alpha.3

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 (82) hide show
  1. package/package.json +9 -2
  2. package/providers/hyperliquid/package.json +5 -0
  3. package/src/_cjs/index.js +1 -0
  4. package/src/_cjs/index.js.map +1 -1
  5. package/src/_cjs/providers/hyperliquid/assetId.js +19 -0
  6. package/src/_cjs/providers/hyperliquid/assetId.js.map +1 -0
  7. package/src/_cjs/providers/hyperliquid/index.js +20 -0
  8. package/src/_cjs/providers/hyperliquid/index.js.map +1 -0
  9. package/src/_cjs/providers/hyperliquid/mappers/history.js +22 -0
  10. package/src/_cjs/providers/hyperliquid/mappers/history.js.map +1 -0
  11. package/src/_cjs/providers/hyperliquid/mappers/index.js +15 -0
  12. package/src/_cjs/providers/hyperliquid/mappers/index.js.map +1 -0
  13. package/src/_cjs/providers/hyperliquid/mappers/market.js +28 -0
  14. package/src/_cjs/providers/hyperliquid/mappers/market.js.map +1 -0
  15. package/src/_cjs/providers/hyperliquid/mappers/order.js +77 -0
  16. package/src/_cjs/providers/hyperliquid/mappers/order.js.map +1 -0
  17. package/src/_cjs/providers/hyperliquid/mappers/position.js +29 -0
  18. package/src/_cjs/providers/hyperliquid/mappers/position.js.map +1 -0
  19. package/src/_cjs/providers/hyperliquid/mappers/shared.js +12 -0
  20. package/src/_cjs/providers/hyperliquid/mappers/shared.js.map +1 -0
  21. package/src/_cjs/providers/hyperliquid/types.js +181 -0
  22. package/src/_cjs/providers/hyperliquid/types.js.map +1 -0
  23. package/src/_cjs/subscriptions.js +3 -0
  24. package/src/_cjs/subscriptions.js.map +1 -0
  25. package/src/_esm/index.js +1 -0
  26. package/src/_esm/index.js.map +1 -1
  27. package/src/_esm/providers/hyperliquid/assetId.js +23 -0
  28. package/src/_esm/providers/hyperliquid/assetId.js.map +1 -0
  29. package/src/_esm/providers/hyperliquid/index.js +4 -0
  30. package/src/_esm/providers/hyperliquid/index.js.map +1 -0
  31. package/src/_esm/providers/hyperliquid/mappers/history.js +18 -0
  32. package/src/_esm/providers/hyperliquid/mappers/history.js.map +1 -0
  33. package/src/_esm/providers/hyperliquid/mappers/index.js +6 -0
  34. package/src/_esm/providers/hyperliquid/mappers/index.js.map +1 -0
  35. package/src/_esm/providers/hyperliquid/mappers/market.js +24 -0
  36. package/src/_esm/providers/hyperliquid/mappers/market.js.map +1 -0
  37. package/src/_esm/providers/hyperliquid/mappers/order.js +72 -0
  38. package/src/_esm/providers/hyperliquid/mappers/order.js.map +1 -0
  39. package/src/_esm/providers/hyperliquid/mappers/position.js +25 -0
  40. package/src/_esm/providers/hyperliquid/mappers/position.js.map +1 -0
  41. package/src/_esm/providers/hyperliquid/mappers/shared.js +13 -0
  42. package/src/_esm/providers/hyperliquid/mappers/shared.js.map +1 -0
  43. package/src/_esm/providers/hyperliquid/types.js +203 -0
  44. package/src/_esm/providers/hyperliquid/types.js.map +1 -0
  45. package/src/_esm/subscriptions.js +2 -0
  46. package/src/_esm/subscriptions.js.map +1 -0
  47. package/src/_types/index.d.ts +1 -0
  48. package/src/_types/index.d.ts.map +1 -1
  49. package/src/_types/market.d.ts +1 -0
  50. package/src/_types/market.d.ts.map +1 -1
  51. package/src/_types/providers/hyperliquid/assetId.d.ts +12 -0
  52. package/src/_types/providers/hyperliquid/assetId.d.ts.map +1 -0
  53. package/src/_types/providers/hyperliquid/index.d.ts +4 -0
  54. package/src/_types/providers/hyperliquid/index.d.ts.map +1 -0
  55. package/src/_types/providers/hyperliquid/mappers/history.d.ts +4 -0
  56. package/src/_types/providers/hyperliquid/mappers/history.d.ts.map +1 -0
  57. package/src/_types/providers/hyperliquid/mappers/index.d.ts +6 -0
  58. package/src/_types/providers/hyperliquid/mappers/index.d.ts.map +1 -0
  59. package/src/_types/providers/hyperliquid/mappers/market.d.ts +4 -0
  60. package/src/_types/providers/hyperliquid/mappers/market.d.ts.map +1 -0
  61. package/src/_types/providers/hyperliquid/mappers/order.d.ts +6 -0
  62. package/src/_types/providers/hyperliquid/mappers/order.d.ts.map +1 -0
  63. package/src/_types/providers/hyperliquid/mappers/position.d.ts +4 -0
  64. package/src/_types/providers/hyperliquid/mappers/position.d.ts.map +1 -0
  65. package/src/_types/providers/hyperliquid/mappers/shared.d.ts +7 -0
  66. package/src/_types/providers/hyperliquid/mappers/shared.d.ts.map +1 -0
  67. package/src/_types/providers/hyperliquid/types.d.ts +340 -0
  68. package/src/_types/providers/hyperliquid/types.d.ts.map +1 -0
  69. package/src/_types/subscriptions.d.ts +70 -0
  70. package/src/_types/subscriptions.d.ts.map +1 -0
  71. package/src/index.ts +1 -0
  72. package/src/market.ts +1 -0
  73. package/src/providers/hyperliquid/assetId.ts +26 -0
  74. package/src/providers/hyperliquid/index.ts +3 -0
  75. package/src/providers/hyperliquid/mappers/history.ts +25 -0
  76. package/src/providers/hyperliquid/mappers/index.ts +5 -0
  77. package/src/providers/hyperliquid/mappers/market.ts +35 -0
  78. package/src/providers/hyperliquid/mappers/order.ts +89 -0
  79. package/src/providers/hyperliquid/mappers/position.ts +35 -0
  80. package/src/providers/hyperliquid/mappers/shared.ts +15 -0
  81. package/src/providers/hyperliquid/types.ts +290 -0
  82. package/src/subscriptions.ts +72 -0
@@ -0,0 +1,35 @@
1
+ import { MarginMode, PositionSide } from '../../../enums.js'
2
+ import type { Position } from '../../../account.js'
3
+ import type { HlAssetPosition } from '../types.js'
4
+
5
+ import { resolveAssetIdFromLookup } from './shared.js'
6
+
7
+ export const mapPosition = (
8
+ ap: HlAssetPosition,
9
+ dexKey: string,
10
+ assetIdLookup: Map<string, number>
11
+ ): Position => {
12
+ const pos = ap.position
13
+ const szi = parseFloat(pos.szi)
14
+
15
+ return {
16
+ symbol: pos.coin,
17
+ assetId: resolveAssetIdFromLookup(assetIdLookup, pos.coin),
18
+ dex: dexKey,
19
+ side: szi >= 0 ? PositionSide.LONG : PositionSide.SHORT,
20
+ size: Math.abs(szi).toString(),
21
+ entryPrice: pos.entryPx ?? '0',
22
+ markPrice:
23
+ pos.positionValue && szi !== 0
24
+ ? (parseFloat(pos.positionValue) / Math.abs(szi)).toString()
25
+ : '0',
26
+ liquidationPrice: pos.liquidationPx ?? '0',
27
+ unrealizedPnl: pos.unrealizedPnl,
28
+ leverage: ap.position.leverage.value,
29
+ marginUsed: pos.marginUsed,
30
+ marginMode:
31
+ ap.position.leverage.type === 'cross'
32
+ ? MarginMode.CROSS
33
+ : MarginMode.ISOLATED,
34
+ }
35
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Look up an asset ID by symbol, throwing if the symbol is unknown.
3
+ * Asset ID 0 is a valid Hyperliquid asset (BTC-PERP), so a fallback
4
+ * to 0 would silently corrupt data.
5
+ */
6
+ export const resolveAssetIdFromLookup = (
7
+ lookup: Map<string, number>,
8
+ symbol: string
9
+ ): number => {
10
+ const id = lookup.get(symbol)
11
+ if (id === undefined) {
12
+ throw new Error(`Unknown asset symbol: ${symbol}`)
13
+ }
14
+ return id
15
+ }
@@ -0,0 +1,290 @@
1
+ import { Type, type Static } from '@sinclair/typebox'
2
+
3
+ // ---------------------------------------------------------------------------
4
+ // Hyperliquid /info response schemas (TypeBox)
5
+ //
6
+ // Each schema produces both a JSON Schema object (for AJV validation) and a
7
+ // TypeScript type via Static<>. The derived types are structurally identical
8
+ // to the hand-written ones they replace.
9
+ //
10
+ // All schemas use additionalProperties: true (TypeBox default) so that new
11
+ // fields added by Hyperliquid pass through without breaking validation.
12
+ // ---------------------------------------------------------------------------
13
+
14
+ // -- metaAndAssetCtxs -------------------------------------------------------
15
+
16
+ export const HlUniverseItemSchema = Type.Object({
17
+ name: Type.String(),
18
+ szDecimals: Type.Number(),
19
+ maxLeverage: Type.Number(),
20
+ onlyIsolated: Type.Boolean(),
21
+ isDelisted: Type.Boolean(),
22
+ })
23
+ export type HlUniverseItem = Static<typeof HlUniverseItemSchema>
24
+
25
+ export const HlMetaSchema = Type.Object({
26
+ universe: Type.Array(HlUniverseItemSchema),
27
+ })
28
+ export type HlMeta = Static<typeof HlMetaSchema>
29
+
30
+ export const HlAssetCtxSchema = Type.Object({
31
+ funding: Type.String(),
32
+ openInterest: Type.String(),
33
+ dayNtlVlm: Type.String(),
34
+ markPx: Type.String(),
35
+ })
36
+ export type HlAssetCtx = Static<typeof HlAssetCtxSchema>
37
+
38
+ export const HlMetaAndAssetCtxsSchema = Type.Tuple([
39
+ HlMetaSchema,
40
+ Type.Array(HlAssetCtxSchema),
41
+ ])
42
+ export type HlMetaAndAssetCtxs = Static<typeof HlMetaAndAssetCtxsSchema>
43
+
44
+ export type HlUniverse = HlMeta['universe']
45
+
46
+ // -- allMids ----------------------------------------------------------------
47
+
48
+ export const HlAllMidsSchema = Type.Record(Type.String(), Type.String())
49
+ export type HlAllMids = Static<typeof HlAllMidsSchema>
50
+
51
+ // -- candleSnapshot ---------------------------------------------------------
52
+
53
+ export const HlCandleSchema = Type.Object({
54
+ t: Type.Number(),
55
+ o: Type.String(),
56
+ h: Type.String(),
57
+ l: Type.String(),
58
+ c: Type.String(),
59
+ v: Type.String(),
60
+ })
61
+ export type HlCandle = Static<typeof HlCandleSchema>
62
+
63
+ export const HlCandleSnapshotSchema = Type.Array(HlCandleSchema)
64
+ export type HlCandleSnapshot = Static<typeof HlCandleSnapshotSchema>
65
+
66
+ // -- l2Book -----------------------------------------------------------------
67
+
68
+ export const HlLevelSchema = Type.Object({
69
+ px: Type.String(),
70
+ sz: Type.String(),
71
+ n: Type.Number(),
72
+ })
73
+ export type HlLevel = Static<typeof HlLevelSchema>
74
+
75
+ export const HlL2BookSchema = Type.Object({
76
+ levels: Type.Tuple([Type.Array(HlLevelSchema), Type.Array(HlLevelSchema)]),
77
+ time: Type.Number(),
78
+ })
79
+ export type HlL2Book = Static<typeof HlL2BookSchema>
80
+
81
+ // -- clearinghouseState -----------------------------------------------------
82
+
83
+ export const HlPositionSchema = Type.Object({
84
+ coin: Type.String(),
85
+ szi: Type.String(),
86
+ entryPx: Type.String(),
87
+ positionValue: Type.String(),
88
+ liquidationPx: Type.String(),
89
+ unrealizedPnl: Type.String(),
90
+ marginUsed: Type.String(),
91
+ leverage: Type.Object({
92
+ type: Type.String(),
93
+ value: Type.Number(),
94
+ }),
95
+ })
96
+ export type HlPosition = Static<typeof HlPositionSchema>
97
+
98
+ export const HlAssetPositionSchema = Type.Object({
99
+ position: HlPositionSchema,
100
+ })
101
+ export type HlAssetPosition = Static<typeof HlAssetPositionSchema>
102
+
103
+ export const HlClearinghouseStateSchema = Type.Object({
104
+ assetPositions: Type.Array(HlAssetPositionSchema),
105
+ marginSummary: Type.Object({
106
+ accountValue: Type.String(),
107
+ totalMarginUsed: Type.String(),
108
+ }),
109
+ crossMarginSummary: Type.Object({
110
+ accountValue: Type.String(),
111
+ totalMarginUsed: Type.String(),
112
+ }),
113
+ })
114
+ export type HlClearinghouseState = Static<typeof HlClearinghouseStateSchema>
115
+
116
+ // -- spotClearinghouseState -------------------------------------------------
117
+
118
+ export const HlSpotBalanceSchema = Type.Object({
119
+ coin: Type.String(),
120
+ token: Type.Number(),
121
+ total: Type.String(),
122
+ hold: Type.String(),
123
+ entryNtl: Type.String(),
124
+ })
125
+ export type HlSpotBalance = Static<typeof HlSpotBalanceSchema>
126
+
127
+ export const HlSpotClearinghouseStateSchema = Type.Object({
128
+ balances: Type.Array(HlSpotBalanceSchema),
129
+ })
130
+ export type HlSpotClearinghouseState = Static<
131
+ typeof HlSpotClearinghouseStateSchema
132
+ >
133
+
134
+ // -- userFees ---------------------------------------------------------------
135
+
136
+ export const HlUserFeesSchema = Type.Object({
137
+ userAddRate: Type.String(),
138
+ userCrossRate: Type.String(),
139
+ activeReferralDiscount: Type.String(),
140
+ })
141
+ export type HlUserFees = Static<typeof HlUserFeesSchema>
142
+
143
+ // -- frontendOpenOrders -----------------------------------------------------
144
+
145
+ export const HlFrontendOpenOrderSchema = Type.Object({
146
+ oid: Type.Number(),
147
+ coin: Type.String(),
148
+ side: Type.String(),
149
+ sz: Type.String(),
150
+ limitPx: Type.String(),
151
+ orderType: Type.String(),
152
+ origSz: Type.String(),
153
+ reduceOnly: Type.Boolean(),
154
+ timestamp: Type.Number(),
155
+ })
156
+ export type HlFrontendOpenOrder = Static<typeof HlFrontendOpenOrderSchema>
157
+
158
+ export const HlFrontendOpenOrdersSchema = Type.Array(
159
+ HlFrontendOpenOrderSchema
160
+ )
161
+ export type HlFrontendOpenOrders = Static<typeof HlFrontendOpenOrdersSchema>
162
+
163
+ // -- extraAgents ------------------------------------------------------------
164
+
165
+ export const HlExtraAgentsSchema = Type.Array(
166
+ Type.Record(Type.String(), Type.Unknown())
167
+ )
168
+ export type HlExtraAgents = Static<typeof HlExtraAgentsSchema>
169
+
170
+ // -- userFills / userFillsByTime --------------------------------------------
171
+
172
+ export const HlUserFillSchema = Type.Object({
173
+ tid: Type.Number(),
174
+ coin: Type.String(),
175
+ side: Type.String(),
176
+ sz: Type.String(),
177
+ px: Type.String(),
178
+ dir: Type.String(),
179
+ fee: Type.String(),
180
+ closedPnl: Type.String(),
181
+ time: Type.Number(),
182
+ })
183
+ export type HlUserFill = Static<typeof HlUserFillSchema>
184
+
185
+ export const HlUserFillsSchema = Type.Array(HlUserFillSchema)
186
+ export type HlUserFills = Static<typeof HlUserFillsSchema>
187
+
188
+ export const HlUserFillsByTimeSchema = Type.Array(HlUserFillSchema)
189
+ export type HlUserFillsByTime = Static<typeof HlUserFillsByTimeSchema>
190
+
191
+ // -- orderStatus ------------------------------------------------------------
192
+
193
+ export const HlOrderDetailSchema = Type.Object({
194
+ order: Type.Object({
195
+ oid: Type.Number(),
196
+ coin: Type.String(),
197
+ side: Type.String(),
198
+ sz: Type.String(),
199
+ limitPx: Type.String(),
200
+ orderType: Type.String(),
201
+ origSz: Type.String(),
202
+ reduceOnly: Type.Boolean(),
203
+ timestamp: Type.Number(),
204
+ tif: Type.Union([Type.String(), Type.Null()]),
205
+ cloid: Type.Union([Type.String(), Type.Null()]),
206
+ triggerCondition: Type.String(),
207
+ triggerPx: Type.Union([Type.String(), Type.Null()]),
208
+ }),
209
+ status: Type.String(),
210
+ statusTimestamp: Type.Number(),
211
+ })
212
+ export type HlOrderDetail = Static<typeof HlOrderDetailSchema>
213
+
214
+ export const HlOrderStatusFoundSchema = Type.Object({
215
+ status: Type.Literal('order'),
216
+ order: HlOrderDetailSchema,
217
+ })
218
+ export type HlOrderStatusFound = Static<typeof HlOrderStatusFoundSchema>
219
+
220
+ export const HlOrderStatusResponseSchema = Type.Union([
221
+ HlOrderStatusFoundSchema,
222
+ Type.Object({ status: Type.Literal('unknownOid') }),
223
+ ])
224
+ export type HlOrderStatusResponse = Static<
225
+ typeof HlOrderStatusResponseSchema
226
+ >
227
+
228
+ // -- perpDexs ---------------------------------------------------------------
229
+
230
+ export const HlPerpDexsSchema = Type.Array(
231
+ Type.Union([Type.Null(), Type.Object({ name: Type.String() })])
232
+ )
233
+ export type HlPerpDexs = Static<typeof HlPerpDexsSchema>
234
+
235
+ // ---------------------------------------------------------------------------
236
+ // Exchange request / response schemas
237
+ // ---------------------------------------------------------------------------
238
+
239
+ export const HlExchangeRequestSchema = Type.Object({
240
+ action: Type.Record(Type.String(), Type.Unknown()),
241
+ signature: Type.Object({
242
+ r: Type.String(),
243
+ s: Type.String(),
244
+ v: Type.Number(),
245
+ }),
246
+ nonce: Type.Number(),
247
+ vaultAddress: Type.Optional(Type.Union([Type.String(), Type.Null()])),
248
+ })
249
+ export type HlExchangeRequest = Static<typeof HlExchangeRequestSchema>
250
+
251
+ export const HlExchangeResponseSchema = Type.Object({
252
+ status: Type.String(),
253
+ response: Type.Optional(
254
+ Type.Union([
255
+ Type.String(),
256
+ Type.Object({
257
+ type: Type.String(),
258
+ data: Type.Optional(
259
+ Type.Object({
260
+ statuses: Type.Optional(
261
+ Type.Array(
262
+ Type.Union([
263
+ Type.String(),
264
+ Type.Object({
265
+ filled: Type.Object({
266
+ totalSz: Type.String(),
267
+ avgPx: Type.String(),
268
+ oid: Type.Number(),
269
+ }),
270
+ }),
271
+ Type.Object({ resting: Type.Object({ oid: Type.Number() }) }),
272
+ Type.Object({
273
+ waitingForFill: Type.Object({ oid: Type.Number() }),
274
+ }),
275
+ Type.Object({
276
+ waitingForTrigger: Type.Object({ oid: Type.Number() }),
277
+ }),
278
+ Type.Object({ success: Type.Literal(true) }),
279
+ Type.Object({ error: Type.String() }),
280
+ ])
281
+ )
282
+ ),
283
+ status: Type.Optional(Type.Unknown()),
284
+ })
285
+ ),
286
+ }),
287
+ ])
288
+ ),
289
+ })
290
+ export type HlExchangeResponse = Static<typeof HlExchangeResponseSchema>
@@ -0,0 +1,72 @@
1
+ import type { Address } from './typedData.js'
2
+ import type {
3
+ Candle,
4
+ OhlcvInterval,
5
+ OrderbookResponse,
6
+ PricesResponse,
7
+ } from './market.js'
8
+ import type { HistoryItem, Position } from './account.js'
9
+ import type { Order } from './trading.js'
10
+
11
+ // --- Channels the user can subscribe to ---
12
+
13
+ export type PricesSubscription = { channel: 'prices'; dex: string }
14
+ export type OrderbookSubscription = {
15
+ channel: 'orderbook'
16
+ dex: string
17
+ symbol: string
18
+ }
19
+ export type TradesSubscription = {
20
+ channel: 'trades'
21
+ dex: string
22
+ symbol: string
23
+ }
24
+ export type CandleSubscription = {
25
+ channel: 'candle'
26
+ dex: string
27
+ symbol: string
28
+ interval: OhlcvInterval
29
+ }
30
+ export type OrderUpdatesSubscription = {
31
+ channel: 'orderUpdates'
32
+ dex: string
33
+ address: Address
34
+ }
35
+ export type FillsSubscription = {
36
+ channel: 'fills'
37
+ dex: string
38
+ address: Address
39
+ }
40
+ export type PositionsSubscription = {
41
+ channel: 'positions'
42
+ dex: string
43
+ address: Address
44
+ }
45
+
46
+ export type Subscription =
47
+ | PricesSubscription
48
+ | OrderbookSubscription
49
+ | TradesSubscription
50
+ | CandleSubscription
51
+ | OrderUpdatesSubscription
52
+ | FillsSubscription
53
+ | PositionsSubscription
54
+
55
+ // --- Events emitted to listeners ---
56
+
57
+ export type PricesEvent = { channel: 'prices'; data: PricesResponse }
58
+ export type OrderbookEvent = { channel: 'orderbook'; data: OrderbookResponse }
59
+ export type TradesEvent = { channel: 'trades'; data: HistoryItem[] }
60
+ export type CandleEvent = { channel: 'candle'; data: Candle }
61
+ export type OrderUpdatesEvent = { channel: 'orderUpdates'; data: Order[] }
62
+ export type FillsEvent = { channel: 'fills'; data: HistoryItem[] }
63
+ export type PositionsEvent = { channel: 'positions'; data: Position[] }
64
+
65
+ export type SubscriptionEvent =
66
+ | PricesEvent
67
+ | OrderbookEvent
68
+ | TradesEvent
69
+ | CandleEvent
70
+ | OrderUpdatesEvent
71
+ | FillsEvent
72
+ | PositionsEvent