@lifi/perps-types 0.1.1-alpha.2 → 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.
@@ -1,200 +1,290 @@
1
+ import { Type, type Static } from '@sinclair/typebox'
2
+
1
3
  // ---------------------------------------------------------------------------
2
- // Hand-written Hyperliquid /info response types
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.
3
12
  // ---------------------------------------------------------------------------
4
13
 
5
14
  // -- metaAndAssetCtxs -------------------------------------------------------
6
15
 
7
- export type HlUniverseItem = {
8
- name: string
9
- szDecimals: number
10
- maxLeverage: number
11
- onlyIsolated: boolean
12
- isDelisted: boolean
13
- }
14
-
15
- export type HlMeta = { universe: HlUniverseItem[] }
16
-
17
- export type HlAssetCtx = {
18
- funding: string
19
- openInterest: string
20
- dayNtlVlm: string
21
- markPx: string
22
- }
23
-
24
- export type HlMetaAndAssetCtxs = [HlMeta, HlAssetCtx[]]
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>
25
43
 
26
44
  export type HlUniverse = HlMeta['universe']
27
45
 
28
46
  // -- allMids ----------------------------------------------------------------
29
47
 
30
- export type HlAllMids = Record<string, string>
48
+ export const HlAllMidsSchema = Type.Record(Type.String(), Type.String())
49
+ export type HlAllMids = Static<typeof HlAllMidsSchema>
31
50
 
32
51
  // -- candleSnapshot ---------------------------------------------------------
33
52
 
34
- export type HlCandle = {
35
- t: number
36
- o: string
37
- h: string
38
- l: string
39
- c: string
40
- v: string
41
- }
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>
42
62
 
43
- export type HlCandleSnapshot = HlCandle[]
63
+ export const HlCandleSnapshotSchema = Type.Array(HlCandleSchema)
64
+ export type HlCandleSnapshot = Static<typeof HlCandleSnapshotSchema>
44
65
 
45
66
  // -- l2Book -----------------------------------------------------------------
46
67
 
47
- export type HlLevel = { px: string; sz: string; n: number }
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>
48
74
 
49
- export type HlL2Book = {
50
- levels: [HlLevel[], HlLevel[]]
51
- time: number
52
- }
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>
53
80
 
54
81
  // -- clearinghouseState -----------------------------------------------------
55
82
 
56
- export type HlPosition = {
57
- coin: string
58
- szi: string
59
- entryPx: string
60
- positionValue: string
61
- liquidationPx: string
62
- unrealizedPnl: string
63
- marginUsed: string
64
- leverage: { type: string; value: number }
65
- }
66
-
67
- export type HlAssetPosition = { position: HlPosition }
68
-
69
- export type HlClearinghouseState = {
70
- assetPositions: HlAssetPosition[]
71
- marginSummary: { accountValue: string; totalMarginUsed: string }
72
- crossMarginSummary: { accountValue: string; totalMarginUsed: string }
73
- }
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>
74
115
 
75
116
  // -- spotClearinghouseState -------------------------------------------------
76
117
 
77
- export type HlSpotBalance = {
78
- coin: string
79
- token: number
80
- total: string
81
- hold: string
82
- entryNtl: string
83
- }
84
-
85
- export type HlSpotClearinghouseState = {
86
- balances: HlSpotBalance[]
87
- }
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
+ >
88
133
 
89
134
  // -- userFees ---------------------------------------------------------------
90
135
 
91
- export type HlUserFees = {
92
- userAddRate: string
93
- userCrossRate: string
94
- activeReferralDiscount: string
95
- }
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>
96
142
 
97
143
  // -- frontendOpenOrders -----------------------------------------------------
98
144
 
99
- export type HlFrontendOpenOrder = {
100
- oid: number
101
- coin: string
102
- side: string
103
- sz: string
104
- limitPx: string
105
- orderType: string
106
- origSz: string
107
- reduceOnly: boolean
108
- timestamp: number
109
- }
110
-
111
- export type HlFrontendOpenOrders = HlFrontendOpenOrder[]
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>
112
162
 
113
163
  // -- extraAgents ------------------------------------------------------------
114
164
 
115
- export type HlExtraAgents = Array<Record<string, unknown>>
165
+ export const HlExtraAgentsSchema = Type.Array(
166
+ Type.Record(Type.String(), Type.Unknown())
167
+ )
168
+ export type HlExtraAgents = Static<typeof HlExtraAgentsSchema>
116
169
 
117
170
  // -- userFills / userFillsByTime --------------------------------------------
118
171
 
119
- export type HlUserFill = {
120
- tid: number
121
- coin: string
122
- side: string
123
- sz: string
124
- px: string
125
- dir: string
126
- fee: string
127
- closedPnl: string
128
- time: number
129
- }
130
-
131
- export type HlUserFills = HlUserFill[]
132
-
133
- export type HlUserFillsByTime = HlUserFill[]
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>
134
190
 
135
191
  // -- orderStatus ------------------------------------------------------------
136
192
 
137
- export type HlOrderStatusFound = {
138
- status: 'order'
139
- order: HlOrderDetail
140
- }
141
-
142
- export type HlOrderDetail = {
143
- order: {
144
- oid: number
145
- coin: string
146
- side: string
147
- sz: string
148
- limitPx: string
149
- orderType: string
150
- origSz: string
151
- reduceOnly: boolean
152
- timestamp: number
153
- tif: string | null
154
- cloid: string | null
155
- triggerCondition: string
156
- triggerPx: string | null
157
- }
158
- status: string
159
- statusTimestamp: number
160
- }
161
-
162
- export type HlOrderStatusResponse =
163
- | HlOrderStatusFound
164
- | { status: 'unknownOid' }
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
+ >
165
227
 
166
228
  // -- perpDexs ---------------------------------------------------------------
167
229
 
168
- export type HlPerpDexs = Array<null | { name: string }>
230
+ export const HlPerpDexsSchema = Type.Array(
231
+ Type.Union([Type.Null(), Type.Object({ name: Type.String() })])
232
+ )
233
+ export type HlPerpDexs = Static<typeof HlPerpDexsSchema>
169
234
 
170
235
  // ---------------------------------------------------------------------------
171
- // Exchange request types
236
+ // Exchange request / response schemas
172
237
  // ---------------------------------------------------------------------------
173
238
 
174
- export type HlExchangeRequest = {
175
- action: Record<string, unknown>
176
- signature: { r: string; s: string; v: number }
177
- nonce: number
178
- vaultAddress?: string | null
179
- }
180
-
181
- export type HlExchangeResponse = {
182
- status: string
183
- response?:
184
- | string
185
- | {
186
- type: string
187
- data?: {
188
- statuses?: Array<
189
- | string
190
- | { filled: { totalSz: string; avgPx: string; oid: number } }
191
- | { resting: { oid: number } }
192
- | { waitingForFill: { oid: number } }
193
- | { waitingForTrigger: { oid: number } }
194
- | { success: true }
195
- | { error: string }
196
- >
197
- status?: unknown
198
- }
199
- }
200
- }
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>