@lifi/perps-types 0.1.1-alpha.8 → 0.2.0-alpha.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 (206) hide show
  1. package/package.json +18 -4
  2. package/providers/lighter/package.json +5 -0
  3. package/src/_cjs/{market.js → action.js} +1 -1
  4. package/src/_cjs/action.js.map +1 -0
  5. package/src/_cjs/{trading.js → asset.js} +1 -1
  6. package/src/_cjs/asset.js.map +1 -0
  7. package/src/_cjs/enums.js +88 -15
  8. package/src/_cjs/enums.js.map +1 -1
  9. package/src/_cjs/index.js +3 -4
  10. package/src/_cjs/index.js.map +1 -1
  11. package/src/_cjs/providers/_shared/fillClassification.js +37 -0
  12. package/src/_cjs/providers/_shared/fillClassification.js.map +1 -0
  13. package/src/_cjs/providers/hyperliquid/assetId.js +11 -9
  14. package/src/_cjs/providers/hyperliquid/assetId.js.map +1 -1
  15. package/src/_cjs/providers/hyperliquid/mappers/_market.js +15 -0
  16. package/src/_cjs/providers/hyperliquid/mappers/_market.js.map +1 -0
  17. package/src/_cjs/providers/hyperliquid/mappers/activity.js +128 -0
  18. package/src/_cjs/providers/hyperliquid/mappers/activity.js.map +1 -0
  19. package/src/_cjs/providers/hyperliquid/mappers/{market.js → asset.js} +10 -9
  20. package/src/_cjs/providers/hyperliquid/mappers/asset.js.map +1 -0
  21. package/src/_cjs/providers/hyperliquid/mappers/fill.js +36 -0
  22. package/src/_cjs/providers/hyperliquid/mappers/fill.js.map +1 -0
  23. package/src/_cjs/providers/hyperliquid/mappers/index.js +12 -7
  24. package/src/_cjs/providers/hyperliquid/mappers/index.js.map +1 -1
  25. package/src/_cjs/providers/hyperliquid/mappers/order.js +86 -9
  26. package/src/_cjs/providers/hyperliquid/mappers/order.js.map +1 -1
  27. package/src/_cjs/providers/hyperliquid/mappers/position.js +8 -5
  28. package/src/_cjs/providers/hyperliquid/mappers/position.js.map +1 -1
  29. package/src/_cjs/providers/hyperliquid/mappers/shared.js +0 -10
  30. package/src/_cjs/providers/hyperliquid/mappers/shared.js.map +1 -1
  31. package/src/_cjs/providers/hyperliquid/types.js +18 -0
  32. package/src/_cjs/providers/hyperliquid/types.js.map +1 -1
  33. package/src/_cjs/{authorization.js → providers/lighter/apiTypes.js} +1 -1
  34. package/src/_cjs/providers/lighter/apiTypes.js.map +1 -0
  35. package/src/_cjs/providers/lighter/index.js +20 -0
  36. package/src/_cjs/providers/lighter/index.js.map +1 -0
  37. package/src/_cjs/providers/lighter/mappers/fill.js +34 -0
  38. package/src/_cjs/providers/lighter/mappers/fill.js.map +1 -0
  39. package/src/_cjs/providers/lighter/mappers/index.js +13 -0
  40. package/src/_cjs/providers/lighter/mappers/index.js.map +1 -0
  41. package/src/_cjs/providers/lighter/mappers/order.js +142 -0
  42. package/src/_cjs/providers/lighter/mappers/order.js.map +1 -0
  43. package/src/_cjs/providers/lighter/mappers/position.js +35 -0
  44. package/src/_cjs/providers/lighter/mappers/position.js.map +1 -0
  45. package/src/_cjs/providers/lighter/types.js +23 -0
  46. package/src/_cjs/providers/lighter/types.js.map +1 -0
  47. package/src/_cjs/{withdrawal.js → providers.js} +1 -1
  48. package/src/_cjs/providers.js.map +1 -0
  49. package/src/_esm/action.js +2 -0
  50. package/src/_esm/action.js.map +1 -0
  51. package/src/_esm/asset.js +2 -0
  52. package/src/_esm/asset.js.map +1 -0
  53. package/src/_esm/enums.js +97 -14
  54. package/src/_esm/enums.js.map +1 -1
  55. package/src/_esm/index.js +3 -4
  56. package/src/_esm/index.js.map +1 -1
  57. package/src/_esm/providers/_shared/fillClassification.js +43 -0
  58. package/src/_esm/providers/_shared/fillClassification.js.map +1 -0
  59. package/src/_esm/providers/hyperliquid/assetId.js +13 -11
  60. package/src/_esm/providers/hyperliquid/assetId.js.map +1 -1
  61. package/src/_esm/providers/hyperliquid/mappers/_market.js +22 -0
  62. package/src/_esm/providers/hyperliquid/mappers/_market.js.map +1 -0
  63. package/src/_esm/providers/hyperliquid/mappers/activity.js +138 -0
  64. package/src/_esm/providers/hyperliquid/mappers/activity.js.map +1 -0
  65. package/src/_esm/providers/hyperliquid/mappers/{market.js → asset.js} +8 -7
  66. package/src/_esm/providers/hyperliquid/mappers/asset.js.map +1 -0
  67. package/src/_esm/providers/hyperliquid/mappers/fill.js +32 -0
  68. package/src/_esm/providers/hyperliquid/mappers/fill.js.map +1 -0
  69. package/src/_esm/providers/hyperliquid/mappers/index.js +4 -4
  70. package/src/_esm/providers/hyperliquid/mappers/index.js.map +1 -1
  71. package/src/_esm/providers/hyperliquid/mappers/order.js +88 -8
  72. package/src/_esm/providers/hyperliquid/mappers/order.js.map +1 -1
  73. package/src/_esm/providers/hyperliquid/mappers/position.js +8 -5
  74. package/src/_esm/providers/hyperliquid/mappers/position.js.map +1 -1
  75. package/src/_esm/providers/hyperliquid/mappers/shared.js +1 -12
  76. package/src/_esm/providers/hyperliquid/mappers/shared.js.map +1 -1
  77. package/src/_esm/providers/hyperliquid/types.js +32 -2
  78. package/src/_esm/providers/hyperliquid/types.js.map +1 -1
  79. package/src/_esm/providers/lighter/apiTypes.js +5 -0
  80. package/src/_esm/providers/lighter/apiTypes.js.map +1 -0
  81. package/src/_esm/providers/lighter/index.js +4 -0
  82. package/src/_esm/providers/lighter/index.js.map +1 -0
  83. package/src/_esm/providers/lighter/mappers/fill.js +39 -0
  84. package/src/_esm/providers/lighter/mappers/fill.js.map +1 -0
  85. package/src/_esm/providers/lighter/mappers/index.js +4 -0
  86. package/src/_esm/providers/lighter/mappers/index.js.map +1 -0
  87. package/src/_esm/providers/lighter/mappers/order.js +162 -0
  88. package/src/_esm/providers/lighter/mappers/order.js.map +1 -0
  89. package/src/_esm/providers/lighter/mappers/position.js +38 -0
  90. package/src/_esm/providers/lighter/mappers/position.js.map +1 -0
  91. package/src/_esm/providers/lighter/types.js +26 -0
  92. package/src/_esm/providers/lighter/types.js.map +1 -0
  93. package/src/_esm/providers.js +2 -0
  94. package/src/_esm/providers.js.map +1 -0
  95. package/src/_types/account.d.ts +211 -18
  96. package/src/_types/account.d.ts.map +1 -1
  97. package/src/_types/action.d.ts +261 -0
  98. package/src/_types/action.d.ts.map +1 -0
  99. package/src/_types/{market.d.ts → asset.d.ts} +23 -34
  100. package/src/_types/asset.d.ts.map +1 -0
  101. package/src/_types/enums.d.ts +81 -5
  102. package/src/_types/enums.d.ts.map +1 -1
  103. package/src/_types/index.d.ts +3 -4
  104. package/src/_types/index.d.ts.map +1 -1
  105. package/src/_types/providers/_shared/fillClassification.d.ts +12 -0
  106. package/src/_types/providers/_shared/fillClassification.d.ts.map +1 -0
  107. package/src/_types/providers/hyperliquid/assetId.d.ts +8 -6
  108. package/src/_types/providers/hyperliquid/assetId.d.ts.map +1 -1
  109. package/src/_types/providers/hyperliquid/mappers/_market.d.ts +13 -0
  110. package/src/_types/providers/hyperliquid/mappers/_market.d.ts.map +1 -0
  111. package/src/_types/providers/hyperliquid/mappers/activity.d.ts +13 -0
  112. package/src/_types/providers/hyperliquid/mappers/activity.d.ts.map +1 -0
  113. package/src/_types/providers/hyperliquid/mappers/asset.d.ts +4 -0
  114. package/src/_types/providers/hyperliquid/mappers/asset.d.ts.map +1 -0
  115. package/src/_types/providers/hyperliquid/mappers/fill.d.ts +6 -0
  116. package/src/_types/providers/hyperliquid/mappers/fill.d.ts.map +1 -0
  117. package/src/_types/providers/hyperliquid/mappers/index.d.ts +4 -4
  118. package/src/_types/providers/hyperliquid/mappers/index.d.ts.map +1 -1
  119. package/src/_types/providers/hyperliquid/mappers/order.d.ts +15 -3
  120. package/src/_types/providers/hyperliquid/mappers/order.d.ts.map +1 -1
  121. package/src/_types/providers/hyperliquid/mappers/position.d.ts +1 -1
  122. package/src/_types/providers/hyperliquid/mappers/position.d.ts.map +1 -1
  123. package/src/_types/providers/hyperliquid/mappers/shared.d.ts +0 -6
  124. package/src/_types/providers/hyperliquid/mappers/shared.d.ts.map +1 -1
  125. package/src/_types/providers/hyperliquid/types.d.ts +112 -0
  126. package/src/_types/providers/hyperliquid/types.d.ts.map +1 -1
  127. package/src/_types/providers/lighter/apiTypes.d.ts +76 -0
  128. package/src/_types/providers/lighter/apiTypes.d.ts.map +1 -0
  129. package/src/_types/providers/lighter/index.d.ts +4 -0
  130. package/src/_types/providers/lighter/index.d.ts.map +1 -0
  131. package/src/_types/providers/lighter/mappers/fill.d.ts +9 -0
  132. package/src/_types/providers/lighter/mappers/fill.d.ts.map +1 -0
  133. package/src/_types/providers/lighter/mappers/index.d.ts +4 -0
  134. package/src/_types/providers/lighter/mappers/index.d.ts.map +1 -0
  135. package/src/_types/providers/lighter/mappers/order.d.ts +35 -0
  136. package/src/_types/providers/lighter/mappers/order.d.ts.map +1 -0
  137. package/src/_types/providers/lighter/mappers/position.d.ts +8 -0
  138. package/src/_types/providers/lighter/mappers/position.d.ts.map +1 -0
  139. package/src/_types/providers/lighter/types.d.ts +90 -0
  140. package/src/_types/providers/lighter/types.d.ts.map +1 -0
  141. package/src/_types/providers.d.ts +145 -0
  142. package/src/_types/providers.d.ts.map +1 -0
  143. package/src/_types/subscriptions.d.ts +23 -18
  144. package/src/_types/subscriptions.d.ts.map +1 -1
  145. package/src/_types/typedData.d.ts +13 -5
  146. package/src/_types/typedData.d.ts.map +1 -1
  147. package/src/account.ts +244 -18
  148. package/src/action.ts +324 -0
  149. package/src/asset.ts +88 -0
  150. package/src/enums.ts +92 -3
  151. package/src/index.ts +3 -4
  152. package/src/providers/_shared/fillClassification.ts +50 -0
  153. package/src/providers/hyperliquid/assetId.ts +18 -12
  154. package/src/providers/hyperliquid/mappers/_market.ts +21 -0
  155. package/src/providers/hyperliquid/mappers/activity.ts +169 -0
  156. package/src/providers/hyperliquid/mappers/{market.ts → asset.ts} +10 -12
  157. package/src/providers/hyperliquid/mappers/fill.ts +41 -0
  158. package/src/providers/hyperliquid/mappers/index.ts +10 -4
  159. package/src/providers/hyperliquid/mappers/order.ts +95 -14
  160. package/src/providers/hyperliquid/mappers/position.ts +8 -10
  161. package/src/providers/hyperliquid/mappers/shared.ts +0 -15
  162. package/src/providers/hyperliquid/types.ts +158 -0
  163. package/src/providers/lighter/apiTypes.ts +87 -0
  164. package/src/providers/lighter/index.ts +3 -0
  165. package/src/providers/lighter/mappers/fill.ts +57 -0
  166. package/src/providers/lighter/mappers/index.ts +8 -0
  167. package/src/providers/lighter/mappers/order.ts +184 -0
  168. package/src/providers/lighter/mappers/position.ts +46 -0
  169. package/src/providers/lighter/types.ts +111 -0
  170. package/src/providers.ts +153 -0
  171. package/src/subscriptions.ts +21 -20
  172. package/src/typedData.ts +15 -5
  173. package/src/_cjs/authorization.js.map +0 -1
  174. package/src/_cjs/market.js.map +0 -1
  175. package/src/_cjs/providers/hyperliquid/mappers/history.js +0 -22
  176. package/src/_cjs/providers/hyperliquid/mappers/history.js.map +0 -1
  177. package/src/_cjs/providers/hyperliquid/mappers/market.js.map +0 -1
  178. package/src/_cjs/trading.js.map +0 -1
  179. package/src/_cjs/withdrawal.js.map +0 -1
  180. package/src/_esm/authorization.js +0 -2
  181. package/src/_esm/authorization.js.map +0 -1
  182. package/src/_esm/market.js +0 -2
  183. package/src/_esm/market.js.map +0 -1
  184. package/src/_esm/providers/hyperliquid/mappers/history.js +0 -18
  185. package/src/_esm/providers/hyperliquid/mappers/history.js.map +0 -1
  186. package/src/_esm/providers/hyperliquid/mappers/market.js.map +0 -1
  187. package/src/_esm/trading.js +0 -2
  188. package/src/_esm/trading.js.map +0 -1
  189. package/src/_esm/withdrawal.js +0 -2
  190. package/src/_esm/withdrawal.js.map +0 -1
  191. package/src/_types/authorization.d.ts +0 -39
  192. package/src/_types/authorization.d.ts.map +0 -1
  193. package/src/_types/market.d.ts.map +0 -1
  194. package/src/_types/providers/hyperliquid/mappers/history.d.ts +0 -4
  195. package/src/_types/providers/hyperliquid/mappers/history.d.ts.map +0 -1
  196. package/src/_types/providers/hyperliquid/mappers/market.d.ts +0 -4
  197. package/src/_types/providers/hyperliquid/mappers/market.d.ts.map +0 -1
  198. package/src/_types/trading.d.ts +0 -80
  199. package/src/_types/trading.d.ts.map +0 -1
  200. package/src/_types/withdrawal.d.ts +0 -37
  201. package/src/_types/withdrawal.d.ts.map +0 -1
  202. package/src/authorization.ts +0 -46
  203. package/src/market.ts +0 -97
  204. package/src/providers/hyperliquid/mappers/history.ts +0 -25
  205. package/src/trading.ts +0 -97
  206. package/src/withdrawal.ts +0 -44
@@ -0,0 +1,184 @@
1
+ import {
2
+ OrderSide,
3
+ OrderStatus,
4
+ OrderType,
5
+ TimeInForce,
6
+ } from '../../../enums.js'
7
+ import type { OpenOrder, TriggerOrder } from '../../../account.js'
8
+ import type { Order } from '../../../action.js'
9
+ import type { LtOrder } from '../apiTypes.js'
10
+
11
+ // Lighter's `type` enum uses hyphens in the OpenAPI spec but earlier API
12
+ // versions emitted underscores. Tolerate both so we don't silently fall
13
+ // through to LIMIT for stop/take-profit orders.
14
+ const mapOrderType = (ltType: string): OrderType => {
15
+ const normalized = ltType.replace(/-/g, '_')
16
+ const map: Record<string, OrderType> = {
17
+ limit: OrderType.LIMIT,
18
+ market: OrderType.MARKET,
19
+ stop_loss: OrderType.STOP_MARKET,
20
+ stop_loss_limit: OrderType.STOP_LIMIT,
21
+ take_profit: OrderType.TAKE_PROFIT_MARKET,
22
+ take_profit_limit: OrderType.TAKE_PROFIT_LIMIT,
23
+ }
24
+ return map[normalized] ?? OrderType.LIMIT
25
+ }
26
+
27
+ const mapTimeInForce = (tif: string): TimeInForce | undefined => {
28
+ switch (tif.replace(/-/g, '_')) {
29
+ case 'good_till_time':
30
+ return TimeInForce.GTT
31
+ case 'immediate_or_cancel':
32
+ return TimeInForce.IOC
33
+ case 'post_only':
34
+ return TimeInForce.POST_ONLY
35
+ default:
36
+ return undefined
37
+ }
38
+ }
39
+
40
+ const mapOrderStatus = (status: string): OrderStatus => {
41
+ if (
42
+ status === 'open' ||
43
+ status === 'in-progress' ||
44
+ status === 'in_progress'
45
+ ) {
46
+ return OrderStatus.OPEN
47
+ }
48
+ if (status === 'pending') {
49
+ return OrderStatus.PENDING
50
+ }
51
+ if (status === 'filled') {
52
+ return OrderStatus.FILLED
53
+ }
54
+ if (status.startsWith('canceled')) {
55
+ return OrderStatus.CANCELLED
56
+ }
57
+ return OrderStatus.OPEN
58
+ }
59
+
60
+ /**
61
+ * Map a raw Lighter order status to a short English sentence describing
62
+ * *why* the order ended in a terminal non-FILLED state. Non-terminal
63
+ * statuses, plain `filled`, and unknown values return `undefined`.
64
+ */
65
+ export const mapStatusReason = (status: string): string | undefined => {
66
+ switch (status) {
67
+ case 'canceled':
68
+ return 'Order cancelled.'
69
+ case 'canceled-post-only':
70
+ return 'Order cancelled: post-only order would have crossed the book.'
71
+ case 'canceled-reduce-only':
72
+ return 'Order cancelled: would not reduce your position.'
73
+ case 'canceled-position-not-allowed':
74
+ return 'Order cancelled: position not allowed.'
75
+ case 'canceled-margin-not-allowed':
76
+ return 'Order cancelled: insufficient margin.'
77
+ case 'canceled-too-much-slippage':
78
+ return 'Order cancelled: slippage exceeded tolerance.'
79
+ case 'canceled-not-enough-liquidity':
80
+ return 'Order cancelled: not enough liquidity to fill.'
81
+ case 'canceled-self-trade':
82
+ return 'Order cancelled: would self-trade against your own resting order.'
83
+ case 'canceled-expired':
84
+ return 'Order expired.'
85
+ case 'canceled-oco':
86
+ return 'Order cancelled: sibling OCO order filled or cancelled first.'
87
+ case 'canceled-child':
88
+ return 'Order cancelled: parent order was cancelled.'
89
+ case 'canceled-liquidation':
90
+ return 'Order cancelled: account was liquidated.'
91
+ case 'canceled-invalid-balance':
92
+ return 'Order cancelled: invalid balance.'
93
+ default:
94
+ return undefined
95
+ }
96
+ }
97
+
98
+ /**
99
+ * True for order types Lighter exposes as TP/SL legs. Mirrors the
100
+ * Hyperliquid helper of the same name so the backend can split a raw
101
+ * Lighter order list into the same `openOrders` / `triggerOrders`
102
+ * buckets the SDK declares on `OrdersResponse`.
103
+ */
104
+ export const isTriggerType = (type: OrderType): boolean =>
105
+ type === OrderType.TAKE_PROFIT_MARKET ||
106
+ type === OrderType.TAKE_PROFIT_LIMIT ||
107
+ type === OrderType.STOP_MARKET ||
108
+ type === OrderType.STOP_LIMIT
109
+
110
+ /**
111
+ * Map a raw Lighter trigger order (stop/take-profit, market or limit) to
112
+ * the generic `TriggerOrder` shape. For market variants the `limitPrice`
113
+ * field is omitted; for limit variants `order.price` is the limit and
114
+ * `order.trigger_price` is the activation level.
115
+ */
116
+ export const mapTriggerOrder = (
117
+ order: LtOrder,
118
+ symbol: string
119
+ ): TriggerOrder => {
120
+ const type = mapOrderType(order.type)
121
+ const isLimit =
122
+ type === OrderType.TAKE_PROFIT_LIMIT || type === OrderType.STOP_LIMIT
123
+ return {
124
+ id: order.order_id,
125
+ asset: {
126
+ assetId: symbol,
127
+ market: 'lighter',
128
+ displaySymbol: symbol,
129
+ displayQuote: 'USDC',
130
+ },
131
+ type,
132
+ size: order.initial_base_amount,
133
+ triggerPrice: order.trigger_price,
134
+ ...(isLimit ? { limitPrice: order.price } : {}),
135
+ createdAt: new Date(order.created_at * 1000).toISOString(),
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Map a raw Lighter order to the generic OpenOrder type.
141
+ * @param symbol - Resolved symbol (market_index → symbol lookup)
142
+ */
143
+ export const mapOrder = (order: LtOrder, symbol: string): OpenOrder => ({
144
+ id: order.order_id,
145
+ asset: {
146
+ assetId: symbol,
147
+ market: 'lighter',
148
+ displaySymbol: symbol,
149
+ displayQuote: 'USDC',
150
+ },
151
+ side: order.is_ask ? OrderSide.SELL : OrderSide.BUY,
152
+ type: mapOrderType(order.type),
153
+ size: order.initial_base_amount,
154
+ price: order.price,
155
+ filledSize: order.filled_base_amount,
156
+ reduceOnly: order.reduce_only,
157
+ createdAt: new Date(order.created_at * 1000).toISOString(),
158
+ })
159
+
160
+ /**
161
+ * Map a raw Lighter order to the rich Order type — adds status, time-in-force
162
+ * and remaining/filled sizes on top of the OpenOrder fields.
163
+ */
164
+ export const mapOrderDetail = (order: LtOrder, symbol: string): Order => ({
165
+ orderId: order.order_id,
166
+ asset: {
167
+ assetId: symbol,
168
+ market: 'lighter',
169
+ displaySymbol: symbol,
170
+ displayQuote: 'USDC',
171
+ },
172
+ side: order.is_ask ? OrderSide.SELL : OrderSide.BUY,
173
+ type: mapOrderType(order.type),
174
+ price: order.price,
175
+ originalSize: order.initial_base_amount,
176
+ remainingSize: order.remaining_base_amount,
177
+ filledSize: order.filled_base_amount,
178
+ timeInForce: mapTimeInForce(order.time_in_force),
179
+ reduceOnly: order.reduce_only,
180
+ status: mapOrderStatus(order.status),
181
+ statusReason: mapStatusReason(order.status),
182
+ createdAt: new Date(order.created_at * 1000).toISOString(),
183
+ updatedAt: new Date(order.updated_at * 1000).toISOString(),
184
+ })
@@ -0,0 +1,46 @@
1
+ import { MarginMode, PositionSide } from '../../../enums.js'
2
+ import type { Position } from '../../../account.js'
3
+ import type { LtAccountPosition } from '../apiTypes.js'
4
+ import { LT_MARGIN_MODE_ISOLATED } from '../types.js'
5
+
6
+ /**
7
+ * Map a raw Lighter account position to the generic Position type.
8
+ * @param symbol Resolved symbol (market_id → symbol lookup, or `pos.symbol`).
9
+ */
10
+ export const mapPosition = (
11
+ pos: LtAccountPosition,
12
+ symbol: string
13
+ ): Position => {
14
+ const size = parseFloat(pos.position)
15
+ const isIsolated = pos.margin_mode === LT_MARGIN_MODE_ISOLATED
16
+
17
+ // `allocated_margin` is only populated for isolated positions (always "0"
18
+ // on cross accounts). For cross, derive margin as
19
+ // `position_value × initial_margin_fraction / 100` (IMF is in percent).
20
+ const positionValue = Math.abs(parseFloat(pos.position_value))
21
+ const imf = parseFloat(pos.initial_margin_fraction)
22
+ const marginUsed = isIsolated
23
+ ? pos.allocated_margin
24
+ : ((positionValue * imf) / 100).toString()
25
+
26
+ return {
27
+ asset: {
28
+ assetId: symbol,
29
+ market: 'lighter',
30
+ displaySymbol: symbol,
31
+ displayQuote: 'USDC',
32
+ },
33
+ side: pos.sign >= 0 ? PositionSide.LONG : PositionSide.SHORT,
34
+ size: Math.abs(size).toString(),
35
+ entryPrice: pos.avg_entry_price,
36
+ markPrice:
37
+ pos.position_value === '0' || size === 0
38
+ ? '0'
39
+ : (parseFloat(pos.position_value) / Math.abs(size)).toString(),
40
+ liquidationPrice: pos.liquidation_price,
41
+ unrealizedPnl: pos.unrealized_pnl,
42
+ leverage: imf > 0 ? Math.round(100 / imf) : 1,
43
+ marginUsed,
44
+ marginMode: isIsolated ? MarginMode.ISOLATED : MarginMode.CROSS,
45
+ }
46
+ }
@@ -0,0 +1,111 @@
1
+ // Lighter WASM signer parameter types — shapes of `wasmSignParams` per
2
+ // action. The backend populates these and the SDK passes them to the WASM
3
+ // signer alongside SDK-managed fields (nonce, account_index, api_key_index).
4
+
5
+ /** Backend-provided params for SignCreateOrder. */
6
+ export type LtCreateOrderWasmParams = {
7
+ market_index: number
8
+ client_order_index: number
9
+ base_amount: number
10
+ price: number
11
+ is_ask: number
12
+ order_type: number
13
+ time_in_force: number
14
+ reduce_only: boolean
15
+ trigger_price: number
16
+ order_expiry: number
17
+ integrator_account_index: number
18
+ integrator_taker_fee: number
19
+ integrator_maker_fee: number
20
+ }
21
+
22
+ /** Backend-provided params for SignCancelOrder. */
23
+ export type LtCancelOrderWasmParams = {
24
+ market_index: number
25
+ order_index: number
26
+ }
27
+
28
+ /** Backend-provided params for SignModifyOrder. */
29
+ export type LtModifyOrderWasmParams = {
30
+ market_index: number
31
+ order_index: number
32
+ base_amount: number
33
+ price: number
34
+ trigger_price: number
35
+ integrator_account_index: number
36
+ integrator_taker_fee: number
37
+ integrator_maker_fee: number
38
+ }
39
+
40
+ /** Backend-provided params for SignUpdateLeverage. */
41
+ export type LtUpdateLeverageWasmParams = {
42
+ market_index: number
43
+ fraction: number
44
+ margin_mode: number
45
+ }
46
+
47
+ /** Backend-provided params for SignUpdateMargin. */
48
+ export type LtUpdateMarginWasmParams = {
49
+ market_index: number
50
+ usdc_amount: number
51
+ direction: number
52
+ }
53
+
54
+ /** Backend-provided params for SignWithdraw. */
55
+ export type LtWithdrawWasmParams = {
56
+ asset_index: number
57
+ route_type: number
58
+ amount: number
59
+ }
60
+
61
+ /** Backend-provided params for SignCancelAllOrders. */
62
+ export type LtCancelAllOrdersWasmParams = {
63
+ /** 0=immediate (cancel GTC), 1=scheduled, 2=abort scheduled */
64
+ time_in_force: number
65
+ /** Unix timestamp in milliseconds. Use Date.now() for immediate cancels. */
66
+ timestamp_ms: number
67
+ }
68
+
69
+ /**
70
+ * Backend-provided params for SignChangePubKey (REGISTER_API_KEY).
71
+ *
72
+ * Hybrid signing: this action requires both a WASM blob and an L1 Ethereum
73
+ * signature (the user's wallet signs the ChangePubKey message). The SDK
74
+ * generates the keypair via the WASM signer and fills in `new_public_key`
75
+ * before invoking SignChangePubKey.
76
+ */
77
+ export type LtChangePubKeyWasmParams = {
78
+ /** API key slot index to register (0-255). SDK fills in the generated public key. */
79
+ api_key_index: number
80
+ nonce: number
81
+ }
82
+
83
+ // ---------------------------------------------------------------------------
84
+ // Lighter WASM signer constants
85
+ // ---------------------------------------------------------------------------
86
+
87
+ export const LT_ORDER_TYPE_LIMIT = 0
88
+ export const LT_ORDER_TYPE_MARKET = 1
89
+ export const LT_ORDER_TYPE_STOP_LOSS = 2
90
+ export const LT_ORDER_TYPE_STOP_LOSS_LIMIT = 3
91
+ export const LT_ORDER_TYPE_TAKE_PROFIT = 4
92
+ export const LT_ORDER_TYPE_TAKE_PROFIT_LIMIT = 5
93
+ export const LT_ORDER_TYPE_TWAP = 6
94
+
95
+ export const LT_TIME_IN_FORCE_IOC = 0
96
+ export const LT_TIME_IN_FORCE_GTC = 1
97
+ export const LT_TIME_IN_FORCE_POST_ONLY = 2
98
+
99
+ export const LT_MARGIN_MODE_CROSS = 0
100
+ export const LT_MARGIN_MODE_ISOLATED = 1
101
+
102
+ export const LT_MARGIN_DIRECTION_REMOVE = 0
103
+ export const LT_MARGIN_DIRECTION_ADD = 1
104
+
105
+ export const LT_ROUTE_PERP = 0
106
+ export const LT_ROUTE_SPOT = 1
107
+
108
+ export const LT_ASSET_ID_USDC = 3
109
+
110
+ export const LT_NIL_TRIGGER_PRICE = 0
111
+ export const LT_DEFAULT_ORDER_EXPIRY = -1
@@ -0,0 +1,153 @@
1
+ import type { ActionType, PerpsSigner, SigningMethod } from './enums.js'
2
+
3
+ export interface ActionDescriptor {
4
+ type: ActionType
5
+ signers: PerpsSigner[]
6
+ /**
7
+ * How the SDK must sign this specific action. Most actions match the
8
+ * provider's dominant pattern (Hyperliquid: EIP712, Lighter: WASM_BLOB),
9
+ * but on-chain bridge actions like DEPOSIT are EVM_TX regardless of
10
+ * provider — the SDK dispatches by this field rather than by
11
+ * `Provider.signingMethod`.
12
+ */
13
+ signingMethod: SigningMethod
14
+ }
15
+
16
+ /**
17
+ * One selectable value on a `Param` whose `values` are enumerated.
18
+ *
19
+ * `value` is the opaque per-provider identifier the SDK passes back to the
20
+ * backend in the corresponding action params (e.g. the string assigned to
21
+ * `mode` for `ACCOUNT_MODE`, or `tier` for `ACCOUNT_TYPE`).
22
+ * `label` is the user-facing string the widget renders.
23
+ */
24
+ export interface ParamOption {
25
+ value: string
26
+ label: string
27
+ }
28
+
29
+ /**
30
+ * A single parameter the widget must collect from the user before
31
+ * dispatching an action.
32
+ *
33
+ * `name` is the wire key the widget passes through to `execute()` — for the
34
+ * existing `ACCOUNT_MODE` action that's `'mode'`, for `ACCOUNT_TYPE` it's
35
+ * `'tier'`, etc. The widget builds the action's params object as
36
+ * `{ [param.name]: <selected value> }` straight from the descriptor at
37
+ * dispatch time; there is no per-ActionType translation layer.
38
+ *
39
+ * `type` describes the primitive shape of the value. Currently only
40
+ * `'string'` is admitted — numeric / boolean primitives are deferred until
41
+ * a real descriptor needs them.
42
+ *
43
+ * `values` is present iff the parameter has a fixed enumeration of
44
+ * admissible values (e.g. Hyperliquid abstraction modes, Lighter account
45
+ * tiers). When absent the widget renders a free-form input appropriate to
46
+ * the primitive `type`.
47
+ *
48
+ * `default` flags the suggested choice when `values` is provided. Must be
49
+ * one of the entries in `values` if specified; consumers should fall back
50
+ * to "no selection" when absent and let the user pick.
51
+ *
52
+ * `readOnly: true` means the widget must render the control disabled —
53
+ * typically used when the provider exposes the current value but does not
54
+ * allow the user to change it from this surface (e.g. Lighter account
55
+ * mode, which is set during onboarding and immutable thereafter).
56
+ */
57
+ export interface Param {
58
+ name: string
59
+ type: 'string'
60
+ values?: ParamOption[]
61
+ default?: ParamOption
62
+ readOnly?: boolean
63
+ }
64
+
65
+ /**
66
+ * Self-documenting descriptor for an account-level action the user may
67
+ * (setup) or must (options) interact with. Extends `ActionDescriptor`
68
+ * because each descriptor resolves to exactly one action the SDK
69
+ * dispatches when the user proceeds; the additional fields are the
70
+ * presentation and parameter-collection metadata the widget needs to
71
+ * render the action without hardcoded per-ActionType branches.
72
+ *
73
+ * `params` is the list of parameters the widget must collect from the
74
+ * user (zero-or-more); each entry tells the widget the wire key, the
75
+ * primitive type, optional enumeration of admissible values, and
76
+ * optional default. See `Param` for the field-level semantics.
77
+ *
78
+ * Categorisation of a descriptor as "setup" vs "options" is carried by
79
+ * which array on `Provider` it appears in (`Provider.setup` vs
80
+ * `Provider.options`), NOT by a field on the descriptor — by design.
81
+ * `ProviderSetup` and `ProviderOption` are type aliases for this same
82
+ * shape with no additional fields.
83
+ */
84
+ export interface ProviderActionDescriptor extends ActionDescriptor {
85
+ /** User-facing row title rendered in the setup / options modal. */
86
+ title: string
87
+ /** User-facing description rendered in the setup / options modal. */
88
+ description: string
89
+ /** Parameters the widget must collect before dispatching the action. */
90
+ params: Param[]
91
+ }
92
+
93
+ /**
94
+ * A descriptor that gates trading: the user MUST satisfy every entry on
95
+ * `Provider.setup` before they can place orders with this provider. The
96
+ * widget renders these in `<SetupModal />`, which auto-pops mid-session
97
+ * whenever setup becomes unsatisfied (e.g. Hyperliquid agent expiry).
98
+ *
99
+ * Type alias for `ProviderActionDescriptor` — no additional fields. The
100
+ * setup-vs-options distinction is the array, not a flag on the item.
101
+ */
102
+ export type ProviderSetup = ProviderActionDescriptor
103
+
104
+ /**
105
+ * A post-setup descriptor the user MAY interact with to tune provider
106
+ * behaviour (e.g. switching Hyperliquid abstraction modes, switching
107
+ * Lighter account tier). The widget renders these in `<OptionsModal />`
108
+ * behind a cog icon; they never gate trading.
109
+ *
110
+ * Type alias for `ProviderActionDescriptor` — no additional fields. The
111
+ * setup-vs-options distinction is the array, not a flag on the item.
112
+ */
113
+ export type ProviderOption = ProviderActionDescriptor
114
+
115
+ export interface ProviderMarketInfo {
116
+ id: string
117
+ quoteAsset: string | null
118
+ }
119
+
120
+ export interface Provider {
121
+ key: string
122
+ name: string
123
+ logoURI: string
124
+ signingMethod: SigningMethod
125
+ /** When false, the provider is announced but not yet selectable in clients. */
126
+ active: boolean
127
+ /**
128
+ * Mandatory account-setup descriptors. The user MUST satisfy every
129
+ * entry before trading. The widget renders these in `<SetupModal />`,
130
+ * which auto-pops whenever setup becomes unsatisfied. An empty array
131
+ * is valid and indicates the provider has no setup gates.
132
+ */
133
+ setup: ProviderSetup[]
134
+ /**
135
+ * Optional post-setup descriptors the user may tune (account mode,
136
+ * fee tier, etc.). Rendered in `<OptionsModal />` behind the cog icon;
137
+ * never gates trading. An empty array is valid.
138
+ */
139
+ options: ProviderOption[]
140
+ actions: ActionDescriptor[]
141
+ markets: ProviderMarketInfo[]
142
+ wsUrl?: string
143
+ /**
144
+ * Minimum deposit amount in USD that the provider's deposit path will
145
+ * accept. Absent means no minimum is advertised — clients should not
146
+ * gate the deposit UX on a value they don't have.
147
+ */
148
+ minDepositUsd?: number
149
+ }
150
+
151
+ export interface ProvidersResponse {
152
+ providers: Provider[]
153
+ }
@@ -1,12 +1,7 @@
1
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'
2
+ import type { Candle, OhlcvInterval, OrderbookResponse } from './asset.js'
3
+ import type { Fill, Position } from './account.js'
4
+ import type { Order } from './action.js'
10
5
 
11
6
  // --- Channels the user can subscribe to ---
12
7
 
@@ -14,18 +9,13 @@ export type PricesSubscription = { channel: 'prices'; dex: string }
14
9
  export type OrderbookSubscription = {
15
10
  channel: 'orderbook'
16
11
  dex: string
17
- symbol: string
12
+ assetId: string
18
13
  depth?: number
19
14
  }
20
- export type TradesSubscription = {
21
- channel: 'trades'
22
- dex: string
23
- symbol: string
24
- }
25
15
  export type CandleSubscription = {
26
16
  channel: 'candle'
27
17
  dex: string
28
- symbol: string
18
+ assetId: string
29
19
  interval: OhlcvInterval
30
20
  }
31
21
  export type OrderUpdatesSubscription = {
@@ -43,31 +33,42 @@ export type PositionsSubscription = {
43
33
  dex: string
44
34
  address: Address
45
35
  }
36
+ export type SpotBalancesSubscription = {
37
+ channel: 'spotBalances'
38
+ dex: string
39
+ address: Address
40
+ }
46
41
 
47
42
  export type Subscription =
48
43
  | PricesSubscription
49
44
  | OrderbookSubscription
50
- | TradesSubscription
51
45
  | CandleSubscription
52
46
  | OrderUpdatesSubscription
53
47
  | FillsSubscription
54
48
  | PositionsSubscription
49
+ | SpotBalancesSubscription
55
50
 
56
51
  // --- Events emitted to listeners ---
57
52
 
58
- export type PricesEvent = { channel: 'prices'; data: PricesResponse }
53
+ export interface SpotBalance {
54
+ coin: string
55
+ total: string
56
+ hold: string
57
+ }
58
+
59
+ export type PricesEvent = { channel: 'prices'; data: Record<string, string> }
59
60
  export type OrderbookEvent = { channel: 'orderbook'; data: OrderbookResponse }
60
- export type TradesEvent = { channel: 'trades'; data: HistoryItem[] }
61
61
  export type CandleEvent = { channel: 'candle'; data: Candle }
62
62
  export type OrderUpdatesEvent = { channel: 'orderUpdates'; data: Order[] }
63
- export type FillsEvent = { channel: 'fills'; data: HistoryItem[] }
63
+ export type FillsEvent = { channel: 'fills'; data: Fill[] }
64
64
  export type PositionsEvent = { channel: 'positions'; data: Position[] }
65
+ export type SpotBalancesEvent = { channel: 'spotBalances'; data: SpotBalance[] }
65
66
 
66
67
  export type SubscriptionEvent =
67
68
  | PricesEvent
68
69
  | OrderbookEvent
69
- | TradesEvent
70
70
  | CandleEvent
71
71
  | OrderUpdatesEvent
72
72
  | FillsEvent
73
73
  | PositionsEvent
74
+ | SpotBalancesEvent
package/src/typedData.ts CHANGED
@@ -1,12 +1,22 @@
1
- import type { TypedData, SignedTypedData } from '@lifi/types'
2
1
  import type { Address, Hex, TypedDataDomain, TypedDataParameter } from 'viem'
2
+ import type { HlPrimaryType } from './providers/hyperliquid/types.js'
3
3
 
4
- export type PerpsTypedData = Omit<TypedData, 'primaryType'> & {
5
- primaryType: string
4
+ /**
5
+ * All known EIP-712 primaryType values across supported providers.
6
+ * Extend this union when adding a new EIP-712 provider.
7
+ */
8
+ export type PerpsPrimaryType = HlPrimaryType
9
+
10
+ export type PerpsTypedData = {
11
+ domain: TypedDataDomain
12
+ types: Record<string, readonly TypedDataParameter[]>
13
+ primaryType: PerpsPrimaryType
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
+ message: Record<string, any>
6
16
  }
7
17
 
8
- export type PerpsSignedTypedData = Omit<SignedTypedData, 'primaryType'> & {
9
- primaryType: string
18
+ export type PerpsSignedTypedData = PerpsTypedData & {
19
+ signature: Hex
10
20
  }
11
21
 
12
22
  // Re-export viem primitives used across perps types
@@ -1 +0,0 @@
1
- {"version":3,"file":"authorization.js","sourceRoot":"","sources":["../authorization.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"market.js","sourceRoot":"","sources":["../market.ts"],"names":[],"mappings":""}
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mapHistoryItem = void 0;
4
- const enums_js_1 = require("../../../enums.js");
5
- const shared_js_1 = require("./shared.js");
6
- const mapHistoryItem = (fill, dexKey, assetIdLookup) => ({
7
- id: String(fill.tid),
8
- symbol: fill.coin,
9
- assetId: (0, shared_js_1.resolveAssetIdFromLookup)(assetIdLookup, fill.coin),
10
- dex: dexKey,
11
- side: fill.side === 'B' ? enums_js_1.OrderSide.BUY : enums_js_1.OrderSide.SELL,
12
- type: fill.dir?.includes('Limit') ? enums_js_1.OrderType.LIMIT : enums_js_1.OrderType.MARKET,
13
- size: fill.sz,
14
- price: fill.px,
15
- status: enums_js_1.HistoryItemStatus.FILLED,
16
- filledSize: fill.sz,
17
- fee: fill.fee,
18
- realizedPnl: fill.closedPnl === '0' ? null : fill.closedPnl,
19
- createdAt: new Date(fill.time).toISOString(),
20
- });
21
- exports.mapHistoryItem = mapHistoryItem;
22
- //# sourceMappingURL=history.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"history.js","sourceRoot":"","sources":["../../../../providers/hyperliquid/mappers/history.ts"],"names":[],"mappings":";;;AAAA,gDAA2E;AAI3E,2CAAsD;AAE/C,MAAM,cAAc,GAAG,CAC5B,IAAgB,EAChB,MAAc,EACd,aAAkC,EACrB,EAAE,CAAC,CAAC;IACjB,EAAE,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;IACpB,MAAM,EAAE,IAAI,CAAC,IAAI;IACjB,OAAO,EAAE,IAAA,oCAAwB,EAAC,aAAa,EAAE,IAAI,CAAC,IAAI,CAAC;IAC3D,GAAG,EAAE,MAAM;IACX,IAAI,EAAE,IAAI,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,oBAAS,CAAC,GAAG,CAAC,CAAC,CAAC,oBAAS,CAAC,IAAI;IACxD,IAAI,EAAE,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,oBAAS,CAAC,KAAK,CAAC,CAAC,CAAC,oBAAS,CAAC,MAAM;IACtE,IAAI,EAAE,IAAI,CAAC,EAAE;IACb,KAAK,EAAE,IAAI,CAAC,EAAE;IACd,MAAM,EAAE,4BAAiB,CAAC,MAAM;IAChC,UAAU,EAAE,IAAI,CAAC,EAAE;IACnB,GAAG,EAAE,IAAI,CAAC,GAAG;IACb,WAAW,EAAE,IAAI,CAAC,SAAS,KAAK,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS;IAC3D,SAAS,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE;CAC7C,CAAC,CAAA;AAlBW,QAAA,cAAc,kBAkBzB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"market.js","sourceRoot":"","sources":["../../../../providers/hyperliquid/mappers/market.ts"],"names":[],"mappings":";;;AACA,8CAAgD;AAGhD,MAAM,wBAAwB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAA;AAExC,MAAM,SAAS,GAAG,CACvB,QAAwB,EACxB,QAAoB,EACpB,QAAgB,EAChB,UAAkB,EAClB,MAAc,EACN,EAAE;IACV,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;IACtB,MAAM,eAAe,GACnB,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,wBAAwB,CAAC,GAAG,wBAAwB,CAAA;IAEtE,OAAO;QACL,MAAM,EAAE,QAAQ,CAAC,IAAI;QACrB,IAAI,EAAE,QAAQ,CAAC,IAAI;QACnB,OAAO,EAAE,qCAAqC,QAAQ,CAAC,IAAI,MAAM;QACjE,OAAO,EAAE,IAAA,6BAAgB,EAAC,QAAQ,EAAE,UAAU,CAAC;QAC/C,GAAG,EAAE,MAAM;QACX,UAAU,EAAE,QAAQ,CAAC,UAAU;QAC/B,WAAW,EAAE,QAAQ,CAAC,WAAW;QACjC,YAAY,EAAE,QAAQ,CAAC,YAAY,KAAK,IAAI;QAC5C,OAAO,EAAE;YACP,IAAI,EAAE,QAAQ,CAAC,OAAO;YACtB,eAAe;SAChB;QACD,YAAY,EAAE,QAAQ,CAAC,YAAY;QACnC,SAAS,EAAE,QAAQ,CAAC,SAAS;QAC7B,SAAS,EAAE,QAAQ,CAAC,MAAM;KAC3B,CAAA;AACH,CAAC,CAAA;AA5BY,QAAA,SAAS,aA4BrB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"trading.js","sourceRoot":"","sources":["../trading.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"withdrawal.js","sourceRoot":"","sources":["../withdrawal.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=authorization.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"authorization.js","sourceRoot":"","sources":["../authorization.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=market.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"market.js","sourceRoot":"","sources":["../market.ts"],"names":[],"mappings":""}