@lifi/perps-types 0.1.1-alpha.0 → 0.1.1-alpha.2

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 (92) hide show
  1. package/package.json +8 -2
  2. package/providers/hyperliquid/package.json +5 -0
  3. package/src/_cjs/index.js +2 -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 +3 -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/_cjs/withdrawal.js +3 -0
  26. package/src/_cjs/withdrawal.js.map +1 -0
  27. package/src/_esm/index.js +2 -0
  28. package/src/_esm/index.js.map +1 -1
  29. package/src/_esm/providers/hyperliquid/assetId.js +23 -0
  30. package/src/_esm/providers/hyperliquid/assetId.js.map +1 -0
  31. package/src/_esm/providers/hyperliquid/index.js +4 -0
  32. package/src/_esm/providers/hyperliquid/index.js.map +1 -0
  33. package/src/_esm/providers/hyperliquid/mappers/history.js +18 -0
  34. package/src/_esm/providers/hyperliquid/mappers/history.js.map +1 -0
  35. package/src/_esm/providers/hyperliquid/mappers/index.js +6 -0
  36. package/src/_esm/providers/hyperliquid/mappers/index.js.map +1 -0
  37. package/src/_esm/providers/hyperliquid/mappers/market.js +24 -0
  38. package/src/_esm/providers/hyperliquid/mappers/market.js.map +1 -0
  39. package/src/_esm/providers/hyperliquid/mappers/order.js +72 -0
  40. package/src/_esm/providers/hyperliquid/mappers/order.js.map +1 -0
  41. package/src/_esm/providers/hyperliquid/mappers/position.js +25 -0
  42. package/src/_esm/providers/hyperliquid/mappers/position.js.map +1 -0
  43. package/src/_esm/providers/hyperliquid/mappers/shared.js +13 -0
  44. package/src/_esm/providers/hyperliquid/mappers/shared.js.map +1 -0
  45. package/src/_esm/providers/hyperliquid/types.js +5 -0
  46. package/src/_esm/providers/hyperliquid/types.js.map +1 -0
  47. package/src/_esm/subscriptions.js +2 -0
  48. package/src/_esm/subscriptions.js.map +1 -0
  49. package/src/_esm/withdrawal.js +2 -0
  50. package/src/_esm/withdrawal.js.map +1 -0
  51. package/src/_types/index.d.ts +2 -0
  52. package/src/_types/index.d.ts.map +1 -1
  53. package/src/_types/market.d.ts +1 -0
  54. package/src/_types/market.d.ts.map +1 -1
  55. package/src/_types/providers/hyperliquid/assetId.d.ts +12 -0
  56. package/src/_types/providers/hyperliquid/assetId.d.ts.map +1 -0
  57. package/src/_types/providers/hyperliquid/index.d.ts +4 -0
  58. package/src/_types/providers/hyperliquid/index.d.ts.map +1 -0
  59. package/src/_types/providers/hyperliquid/mappers/history.d.ts +4 -0
  60. package/src/_types/providers/hyperliquid/mappers/history.d.ts.map +1 -0
  61. package/src/_types/providers/hyperliquid/mappers/index.d.ts +6 -0
  62. package/src/_types/providers/hyperliquid/mappers/index.d.ts.map +1 -0
  63. package/src/_types/providers/hyperliquid/mappers/market.d.ts +4 -0
  64. package/src/_types/providers/hyperliquid/mappers/market.d.ts.map +1 -0
  65. package/src/_types/providers/hyperliquid/mappers/order.d.ts +6 -0
  66. package/src/_types/providers/hyperliquid/mappers/order.d.ts.map +1 -0
  67. package/src/_types/providers/hyperliquid/mappers/position.d.ts +4 -0
  68. package/src/_types/providers/hyperliquid/mappers/position.d.ts.map +1 -0
  69. package/src/_types/providers/hyperliquid/mappers/shared.d.ts +7 -0
  70. package/src/_types/providers/hyperliquid/mappers/shared.d.ts.map +1 -0
  71. package/src/_types/providers/hyperliquid/types.d.ts +177 -0
  72. package/src/_types/providers/hyperliquid/types.d.ts.map +1 -0
  73. package/src/_types/subscriptions.d.ts +70 -0
  74. package/src/_types/subscriptions.d.ts.map +1 -0
  75. package/src/_types/typedData.d.ts +2 -3
  76. package/src/_types/typedData.d.ts.map +1 -1
  77. package/src/_types/withdrawal.d.ts +37 -0
  78. package/src/_types/withdrawal.d.ts.map +1 -0
  79. package/src/index.ts +2 -0
  80. package/src/market.ts +1 -0
  81. package/src/providers/hyperliquid/assetId.ts +26 -0
  82. package/src/providers/hyperliquid/index.ts +3 -0
  83. package/src/providers/hyperliquid/mappers/history.ts +25 -0
  84. package/src/providers/hyperliquid/mappers/index.ts +5 -0
  85. package/src/providers/hyperliquid/mappers/market.ts +35 -0
  86. package/src/providers/hyperliquid/mappers/order.ts +89 -0
  87. package/src/providers/hyperliquid/mappers/position.ts +35 -0
  88. package/src/providers/hyperliquid/mappers/shared.ts +15 -0
  89. package/src/providers/hyperliquid/types.ts +200 -0
  90. package/src/subscriptions.ts +72 -0
  91. package/src/typedData.ts +2 -8
  92. package/src/withdrawal.ts +44 -0
@@ -0,0 +1,89 @@
1
+ import {
2
+ OrderSide,
3
+ OrderStatus,
4
+ OrderType,
5
+ TimeInForce,
6
+ } from '../../../enums.js'
7
+ import type { OpenOrder } from '../../../account.js'
8
+ import type { Order } from '../../../trading.js'
9
+ import type { HlFrontendOpenOrder, HlOrderDetail } from '../types.js'
10
+
11
+ import { resolveAssetIdFromLookup } from './shared.js'
12
+
13
+ export const mapOpenOrder = (
14
+ o: HlFrontendOpenOrder,
15
+ dexKey: string,
16
+ assetIdLookup: Map<string, number>
17
+ ): OpenOrder => ({
18
+ id: String(o.oid),
19
+ symbol: o.coin,
20
+ assetId: resolveAssetIdFromLookup(assetIdLookup, o.coin),
21
+ dex: dexKey,
22
+ side: o.side === 'B' ? OrderSide.BUY : OrderSide.SELL,
23
+ type: o.orderType === 'Limit' ? OrderType.LIMIT : OrderType.MARKET,
24
+ size: o.sz,
25
+ price: o.limitPx,
26
+ filledSize: o.origSz
27
+ ? (parseFloat(o.origSz) - parseFloat(o.sz)).toString()
28
+ : '0',
29
+ reduceOnly: o.reduceOnly ?? false,
30
+ createdAt: new Date(o.timestamp).toISOString(),
31
+ })
32
+
33
+ const mapOrderStatus = (status: string): OrderStatus => {
34
+ switch (status) {
35
+ case 'open':
36
+ case 'resting':
37
+ return OrderStatus.OPEN
38
+ case 'filled':
39
+ return OrderStatus.FILLED
40
+ case 'canceled':
41
+ case 'cancelled':
42
+ return OrderStatus.CANCELLED
43
+ case 'rejected':
44
+ return OrderStatus.REJECTED
45
+ case 'triggered':
46
+ return OrderStatus.TRIGGERED
47
+ case 'marginCanceled':
48
+ return OrderStatus.CANCELLED
49
+ default:
50
+ return OrderStatus.PENDING
51
+ }
52
+ }
53
+
54
+ const mapTimeInForce = (tif: string | undefined): TimeInForce | undefined => {
55
+ switch (tif) {
56
+ case 'Gtc':
57
+ return TimeInForce.GTC
58
+ case 'Ioc':
59
+ return TimeInForce.IOC
60
+ case 'Alo':
61
+ return TimeInForce.POST_ONLY
62
+ default:
63
+ return undefined
64
+ }
65
+ }
66
+
67
+ export const mapOrder = (detail: HlOrderDetail): Order => {
68
+ const o = detail.order
69
+ const filled = parseFloat(o.origSz) - parseFloat(o.sz)
70
+
71
+ return {
72
+ orderId: String(o.oid),
73
+ clientOrderId: o.cloid ?? undefined,
74
+ symbol: o.coin,
75
+ side: o.side === 'B' ? OrderSide.BUY : OrderSide.SELL,
76
+ type: o.orderType === 'Limit' ? OrderType.LIMIT : OrderType.MARKET,
77
+ price: o.limitPx,
78
+ originalSize: o.origSz,
79
+ remainingSize: o.sz,
80
+ filledSize: filled.toString(),
81
+ timeInForce: mapTimeInForce(o.tif ?? undefined),
82
+ reduceOnly: o.reduceOnly ?? undefined,
83
+ isTrigger: o.triggerCondition !== undefined && o.triggerCondition !== 'N/A',
84
+ triggerPrice: o.triggerPx ?? undefined,
85
+ status: mapOrderStatus(detail.status),
86
+ createdAt: new Date(o.timestamp).toISOString(),
87
+ updatedAt: new Date(detail.statusTimestamp).toISOString(),
88
+ }
89
+ }
@@ -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,200 @@
1
+ // ---------------------------------------------------------------------------
2
+ // Hand-written Hyperliquid /info response types
3
+ // ---------------------------------------------------------------------------
4
+
5
+ // -- metaAndAssetCtxs -------------------------------------------------------
6
+
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[]]
25
+
26
+ export type HlUniverse = HlMeta['universe']
27
+
28
+ // -- allMids ----------------------------------------------------------------
29
+
30
+ export type HlAllMids = Record<string, string>
31
+
32
+ // -- candleSnapshot ---------------------------------------------------------
33
+
34
+ export type HlCandle = {
35
+ t: number
36
+ o: string
37
+ h: string
38
+ l: string
39
+ c: string
40
+ v: string
41
+ }
42
+
43
+ export type HlCandleSnapshot = HlCandle[]
44
+
45
+ // -- l2Book -----------------------------------------------------------------
46
+
47
+ export type HlLevel = { px: string; sz: string; n: number }
48
+
49
+ export type HlL2Book = {
50
+ levels: [HlLevel[], HlLevel[]]
51
+ time: number
52
+ }
53
+
54
+ // -- clearinghouseState -----------------------------------------------------
55
+
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
+ }
74
+
75
+ // -- spotClearinghouseState -------------------------------------------------
76
+
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
+ }
88
+
89
+ // -- userFees ---------------------------------------------------------------
90
+
91
+ export type HlUserFees = {
92
+ userAddRate: string
93
+ userCrossRate: string
94
+ activeReferralDiscount: string
95
+ }
96
+
97
+ // -- frontendOpenOrders -----------------------------------------------------
98
+
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[]
112
+
113
+ // -- extraAgents ------------------------------------------------------------
114
+
115
+ export type HlExtraAgents = Array<Record<string, unknown>>
116
+
117
+ // -- userFills / userFillsByTime --------------------------------------------
118
+
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[]
134
+
135
+ // -- orderStatus ------------------------------------------------------------
136
+
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' }
165
+
166
+ // -- perpDexs ---------------------------------------------------------------
167
+
168
+ export type HlPerpDexs = Array<null | { name: string }>
169
+
170
+ // ---------------------------------------------------------------------------
171
+ // Exchange request types
172
+ // ---------------------------------------------------------------------------
173
+
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
+ }
@@ -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
package/src/typedData.ts CHANGED
@@ -1,18 +1,12 @@
1
1
  import type { TypedData, SignedTypedData } from '@lifi/types'
2
2
  import type { Address, Hex, TypedDataDomain, TypedDataParameter } from 'viem'
3
3
 
4
- export type PerpsTypedDataPrimaryType =
5
- | 'HyperliquidTransaction:ApproveAgent'
6
- | 'HyperliquidTransaction:ApproveBuilderFee'
7
- | 'HyperliquidTransaction:UserSetAbstraction'
8
- | 'Agent'
9
-
10
4
  export type PerpsTypedData = Omit<TypedData, 'primaryType'> & {
11
- primaryType: PerpsTypedDataPrimaryType
5
+ primaryType: string
12
6
  }
13
7
 
14
8
  export type PerpsSignedTypedData = Omit<SignedTypedData, 'primaryType'> & {
15
- primaryType: PerpsTypedDataPrimaryType
9
+ primaryType: string
16
10
  }
17
11
 
18
12
  // Re-export viem primitives used across perps types
@@ -0,0 +1,44 @@
1
+ import type { Address, Hex, PerpsTypedData } from './typedData.js'
2
+
3
+ export interface WithdrawalInput {
4
+ destination: Address
5
+ amount: string // e.g., "100.5" — units are $1 USDC
6
+ }
7
+
8
+ export interface CreateWithdrawalRequest {
9
+ dex: string
10
+ address: Address
11
+ withdrawal: WithdrawalInput
12
+ }
13
+
14
+ export interface WithdrawalAction {
15
+ action: string // "Withdraw"
16
+ description?: string
17
+ typedData: PerpsTypedData
18
+ }
19
+
20
+ export interface CreateWithdrawalResponse {
21
+ action: WithdrawalAction
22
+ }
23
+
24
+ export interface SignedWithdrawal {
25
+ action: string
26
+ typedData: PerpsTypedData
27
+ signature: Hex
28
+ }
29
+
30
+ export interface SubmitWithdrawalRequest {
31
+ dex: string
32
+ address: Address
33
+ action: SignedWithdrawal
34
+ }
35
+
36
+ export interface WithdrawalResult {
37
+ action: string
38
+ success: boolean
39
+ error?: string
40
+ }
41
+
42
+ export interface SubmitWithdrawalResponse {
43
+ result: WithdrawalResult
44
+ }