@lifi/perps-types 0.1.1-alpha.9 → 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 +81 -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 +80 -8
  18. package/src/_cjs/providers/hyperliquid/mappers/activity.js.map +1 -1
  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 +9 -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 +90 -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 +92 -13
  64. package/src/_esm/providers/hyperliquid/mappers/activity.js.map +1 -1
  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 +3 -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 +178 -23
  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 +76 -6
  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 +7 -7
  112. package/src/_types/providers/hyperliquid/mappers/activity.d.ts.map +1 -1
  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 +3 -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 +90 -2
  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 +193 -22
  148. package/src/action.ts +324 -0
  149. package/src/asset.ts +88 -0
  150. package/src/enums.ts +85 -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 +98 -13
  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 +9 -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 +128 -4
  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
package/src/account.ts CHANGED
@@ -1,7 +1,11 @@
1
1
  import type { Address } from './typedData.js'
2
+ import type { AssetDisplay } from './asset.js'
2
3
  import type {
4
+ ActionType,
3
5
  ActivityType,
4
- HistoryItemStatus,
6
+ FillClassification,
7
+ FillStatus,
8
+ LiquidityRole,
5
9
  MarginMode,
6
10
  OrderSide,
7
11
  OrderType,
@@ -14,9 +18,7 @@ export interface FeeTier {
14
18
  }
15
19
 
16
20
  export interface Position {
17
- symbol: string
18
- assetId: number
19
- dex: string
21
+ asset: AssetDisplay
20
22
  side: PositionSide
21
23
  size: string
22
24
  entryPrice: string
@@ -30,15 +32,14 @@ export interface Position {
30
32
 
31
33
  export interface OpenOrder {
32
34
  id: string
33
- symbol: string
34
- assetId: number
35
- dex: string
35
+ asset: AssetDisplay
36
36
  side: OrderSide
37
37
  type: OrderType
38
38
  size: string
39
39
  price: string
40
40
  filledSize: string
41
41
  reduceOnly: boolean
42
+ label?: string
42
43
  createdAt: string
43
44
  }
44
45
 
@@ -48,30 +49,61 @@ export interface Balance {
48
49
  }
49
50
 
50
51
  export interface AccountResponse {
51
- dex: string
52
+ provider: string
52
53
  address: Address
53
- balances: Balance[]
54
+ balances: Record<string, Balance[]>
54
55
  marginUsed: string
55
56
  unrealizedPnl: string
56
57
  feeTier: FeeTier
58
+ /**
59
+ * Per-provider account state, strongly typed and discriminated on
60
+ * `config.provider`. Consumers narrow with `config.provider === 'hyperliquid'`
61
+ * etc. to get access to the provider-specific fields. There is no untyped
62
+ * escape hatch — fields specific to a future provider belong on a new
63
+ * variant of `AccountConfig`, not on a generic `Record<string, unknown>`.
64
+ */
65
+ config: AccountConfig
66
+ }
67
+
68
+ export interface TriggerOrder {
69
+ id: string
70
+ asset: AssetDisplay
71
+ type: OrderType
72
+ size: string
73
+ triggerPrice: string
74
+ limitPrice?: string
75
+ label?: string
76
+ createdAt: string
77
+ }
78
+
79
+ export interface PositionsResponse {
80
+ provider: string
57
81
  positions: Position[]
82
+ pagination: Pagination
83
+ }
84
+
85
+ export interface OrdersResponse {
86
+ provider: string
58
87
  openOrders: OpenOrder[]
59
- config: Record<string, unknown>
88
+ triggerOrders: TriggerOrder[]
89
+ pagination: Pagination
60
90
  }
61
91
 
62
- export interface HistoryItem {
92
+ export interface Fill {
63
93
  id: string
64
- symbol: string
65
- assetId: number
66
- dex: string
94
+ orderId: string
95
+ asset: AssetDisplay
67
96
  side: OrderSide
68
97
  type: OrderType
69
98
  size: string
70
99
  price: string
71
- status: HistoryItemStatus
100
+ status: FillStatus
101
+ liquidity: LiquidityRole
72
102
  filledSize?: string
73
103
  fee?: string
74
104
  realizedPnl?: string | null
105
+ startPosition?: string
106
+ classification: FillClassification
75
107
  createdAt: string
76
108
  }
77
109
 
@@ -82,9 +114,9 @@ export interface Pagination {
82
114
  nextUrl?: string
83
115
  }
84
116
 
85
- export interface HistoryResponse {
86
- dex: string
87
- items: HistoryItem[]
117
+ export interface FillsResponse {
118
+ provider: string
119
+ items: Fill[]
88
120
  pagination: Pagination
89
121
  }
90
122
 
@@ -94,7 +126,7 @@ export interface HistoryResponse {
94
126
 
95
127
  export interface BaseActivity {
96
128
  id: string
97
- dex: string
129
+ provider: string
98
130
  timestamp: string
99
131
  }
100
132
 
@@ -110,7 +142,7 @@ export interface WithdrawalActivity extends BaseActivity {
110
142
  }
111
143
 
112
144
  export interface LiquidatedPosition {
113
- symbol: string
145
+ asset: AssetDisplay
114
146
  size: string
115
147
  }
116
148
 
@@ -124,20 +156,159 @@ export interface LiquidationActivity extends BaseActivity {
124
156
 
125
157
  export interface FundingActivity extends BaseActivity {
126
158
  type: ActivityType.FUNDING
127
- symbol: string
159
+ asset: AssetDisplay
128
160
  amount: string
129
161
  positionSize: string
130
162
  fundingRate: string
131
163
  }
132
164
 
165
+ /**
166
+ * Internal transfer between two accounts on the same provider (e.g. Lighter
167
+ * `/api/v1/transfer/history`, Hyperliquid `spotTransfer` ledger entry).
168
+ * Direction is relative to the queried account.
169
+ *
170
+ * The counterparty is identified by either an account index (integer L2
171
+ * account identifier — Lighter) or a wallet address (Hyperliquid, where
172
+ * accounts ARE addresses). The type is a discriminated union over these two
173
+ * shapes: at least one of `counterpartyAccountIndex` / `counterpartyAddress`
174
+ * MUST be present, and either may appear alone or alongside the other.
175
+ *
176
+ * Consumers that render counterparties should prefer `counterpartyAccountIndex`
177
+ * when present (it's the canonical handle on index-based providers) and fall
178
+ * back to a truncated `counterpartyAddress` otherwise.
179
+ */
180
+ export type TransferActivity = BaseActivity & {
181
+ type: ActivityType.TRANSFER
182
+ direction: 'IN' | 'OUT'
183
+ asset: string
184
+ amount: string
185
+ meta?: Record<string, unknown>
186
+ } & (
187
+ | { counterpartyAccountIndex: number; counterpartyAddress?: string }
188
+ | { counterpartyAccountIndex?: number; counterpartyAddress: string }
189
+ )
190
+
133
191
  export type ActivityItem =
134
192
  | DepositActivity
135
193
  | WithdrawalActivity
136
194
  | LiquidationActivity
137
195
  | FundingActivity
196
+ | TransferActivity
138
197
 
139
198
  export interface ActivitiesResponse {
140
- dex: string
199
+ provider: string
141
200
  items: ActivityItem[]
142
201
  pagination: Pagination
143
202
  }
203
+
204
+ // ---------------------------------------------------------------------------
205
+ // Account configuration state — typed `AccountResponse.config`
206
+ // ---------------------------------------------------------------------------
207
+
208
+ /**
209
+ * Hyperliquid record describing one authorised agent wallet on the
210
+ * `userExtraAgents` response. Shape mirrors what the Hyperliquid info API
211
+ * returns; the backend forwards the entries verbatim. The widget renders the
212
+ * count / expiry on the `APPROVE_AGENT` setup descriptor.
213
+ */
214
+ export type HyperliquidAgent = Record<string, unknown>
215
+
216
+ /**
217
+ * Hyperliquid builder-fee approval state. Surfaced by the backend after
218
+ * comparing the user's `maxBuilderFee` against the configured LI.FI builder
219
+ * fee for this dex. The widget uses `approved: false` to badge
220
+ * `APPROVE_BUILDER_FEE` as outstanding.
221
+ */
222
+ export interface HyperliquidBuilderFeeApproval {
223
+ builderAddress: string
224
+ /** Maximum fee rate the user must approve, in basis points as a string. */
225
+ maxFeeRate: string
226
+ approved: boolean
227
+ }
228
+
229
+ /**
230
+ * Hyperliquid-specific account configuration state.
231
+ *
232
+ * `abstractionMode: null` means abstraction has never been set; consumers
233
+ * should fall back to the descriptor's `default` `ParamOption`.
234
+ *
235
+ * `builderFeeApproval` is absent when no builder is configured for this
236
+ * provider; when present, `approved` reports whether the user has signed the
237
+ * maximum builder fee.
238
+ */
239
+ export interface HyperliquidAccountConfig {
240
+ provider: 'hyperliquid'
241
+ abstractionMode: string | null
242
+ agents: HyperliquidAgent[]
243
+ builderFeeApproval?: HyperliquidBuilderFeeApproval
244
+ }
245
+
246
+ /**
247
+ * Lighter-specific account configuration state.
248
+ *
249
+ * `accountIndex` is the L2 integer account identifier.
250
+ *
251
+ * `apiKeyRegistered` reports whether a key is currently live in the
252
+ * `apiKeyIndex` slot; the `REGISTER_API_KEY` setup descriptor gates trading
253
+ * on this.
254
+ *
255
+ * `accountType` is the raw integer fee/latency tier from Lighter's
256
+ * `/api/v1/account.account_type` — decoding to a human label is left to the
257
+ * consumer.
258
+ *
259
+ * `readOnlyTokenApproved` mirrors `apiKeyRegistered`'s shape but for the
260
+ * long-lived read-only token. When `true`, `readOnlyTokenExpiry` and
261
+ * `readOnlyTokenScope` MUST be present; when `false`, both MUST be absent.
262
+ * The `APPROVE_READ_ONLY_TOKEN` setup descriptor gates query-only access on
263
+ * this flag.
264
+ */
265
+ export interface LighterAccountConfig {
266
+ provider: 'lighter'
267
+ accountIndex: number
268
+ apiKeyIndex: number
269
+ apiKeyRegistered: boolean
270
+ accountType: number
271
+ readOnlyTokenApproved: boolean
272
+ /** Unix seconds. Present iff `readOnlyTokenApproved === true`. */
273
+ readOnlyTokenExpiry?: number
274
+ /** Lighter's documented `single | all` scope. Present iff `readOnlyTokenApproved === true`. */
275
+ readOnlyTokenScope?: 'single' | 'all'
276
+ }
277
+
278
+ /**
279
+ * Discriminated union of per-provider account configuration state.
280
+ * Narrow with `config.provider === '<key>'` to access provider-specific
281
+ * fields. Adding a new provider means adding a new variant, NOT widening
282
+ * to a record-keyed shape.
283
+ */
284
+ export type AccountConfig = HyperliquidAccountConfig | LighterAccountConfig
285
+
286
+ /**
287
+ * The current value of a single descriptor parameter, as projected by the
288
+ * SDK from the typed `AccountConfig` for widget consumption.
289
+ *
290
+ * `name` matches `Param.name` on the descriptor that produced it; `value`
291
+ * is the current state in the primitive shape the descriptor declared.
292
+ * `null` indicates "no current value" (the user has not made a selection
293
+ * yet, or the provider has not surfaced one), and the widget should fall
294
+ * back to the descriptor's `default` `ParamOption` (when present) or
295
+ * render the control with no highlight.
296
+ */
297
+ export interface AccountConfigValue {
298
+ name: string
299
+ value: string | number | boolean | null
300
+ }
301
+
302
+ /**
303
+ * SDK projection of `AccountConfig` against a single descriptor — one
304
+ * `AccountConfigSetting` per descriptor on `Provider.setup` /
305
+ * `Provider.options`. `values` carries the current state for each `Param`
306
+ * the descriptor declared.
307
+ *
308
+ * Consumers must NOT read `AccountConfig` directly — narrow through this
309
+ * projection so a new provider variant doesn't require widget changes.
310
+ */
311
+ export interface AccountConfigSetting {
312
+ type: ActionType
313
+ values: AccountConfigValue[]
314
+ }
package/src/action.ts ADDED
@@ -0,0 +1,324 @@
1
+ import type { MarginMode, OrderType } from './enums.js'
2
+ import {
3
+ type ActionType,
4
+ type OrderSide,
5
+ type OrderStatus,
6
+ type TimeInForce,
7
+ type TriggerCondition,
8
+ } from './enums.js'
9
+ import type { Address, Hex, PerpsTypedData } from './typedData.js'
10
+ import type { AssetIdentity, AssetDisplay } from './asset.js'
11
+
12
+ // ActionStep variants (create → sign → execute). The SDK picks the variant
13
+ // from the provider's `signingMethod` on `/providers`:
14
+ // Eip712 — EIP-712 typed data + ECDSA signature
15
+ // WasmBlob — WASM signer output ({txType, txInfo, txHash})
16
+ // EvmTx — plain EVM transaction
17
+ export interface Eip712ActionStep {
18
+ action: ActionType
19
+ typedData: PerpsTypedData
20
+ }
21
+
22
+ export interface WasmBlobActionStep {
23
+ action: ActionType
24
+ wasmSignParams: Record<string, unknown>
25
+ }
26
+
27
+ export interface EvmTxActionStep {
28
+ action: ActionType
29
+ txParams: Record<string, unknown>
30
+ }
31
+
32
+ export type ActionStep = Eip712ActionStep | WasmBlobActionStep | EvmTxActionStep
33
+
34
+ export interface Eip712SignedActionStep {
35
+ action: ActionType
36
+ typedData: PerpsTypedData
37
+ signature: Hex
38
+ }
39
+
40
+ export interface WasmBlobSignedActionStep {
41
+ action: ActionType
42
+ wasmSignParams: Record<string, unknown>
43
+ signedTx: {
44
+ txType: number
45
+ txInfo: string
46
+ txHash: string
47
+ }
48
+ }
49
+
50
+ export interface EvmTxSignedActionStep {
51
+ action: ActionType
52
+ txParams: Record<string, unknown>
53
+ txHash: string
54
+ }
55
+
56
+ export type SignedActionStep =
57
+ | Eip712SignedActionStep
58
+ | WasmBlobSignedActionStep
59
+ | EvmTxSignedActionStep
60
+
61
+ export type ActionResult =
62
+ | {
63
+ action: ActionType
64
+ success: true
65
+ orderId?: string
66
+ }
67
+ | {
68
+ action: ActionType
69
+ success: false
70
+ error: string
71
+ }
72
+
73
+ // ---------------------------------------------------------------------------
74
+ // Shared data types
75
+ // ---------------------------------------------------------------------------
76
+
77
+ export interface TriggerOrderInput {
78
+ triggerPrice: string
79
+ limitPrice?: string
80
+ }
81
+
82
+ export interface ModifyOrderInput {
83
+ id: string
84
+ price?: string
85
+ size?: string
86
+ triggerPrice?: string
87
+ limitPrice?: string
88
+ }
89
+
90
+ export interface Order {
91
+ orderId: string
92
+ asset: AssetDisplay
93
+ side: OrderSide
94
+ type: OrderType
95
+ price?: string
96
+ originalSize: string
97
+ remainingSize: string
98
+ filledSize: string
99
+ timeInForce?: TimeInForce
100
+ expiresAt?: string
101
+ reduceOnly?: boolean
102
+ isTrigger?: boolean
103
+ triggerPrice?: string
104
+ triggerCondition?: TriggerCondition
105
+ status: OrderStatus
106
+ /**
107
+ * Human-readable English sentence describing *why* the order ended in a
108
+ * terminal non-FILLED state (e.g. slippage exceeded, reduce-only would
109
+ * not reduce, expiry). Populated only when the provider exposes a
110
+ * specific cancellation/rejection reason; `undefined` for non-terminal
111
+ * statuses, plain `filled`, and bare cancels/rejects with no actionable
112
+ * detail.
113
+ */
114
+ statusReason?: string
115
+ averagePrice?: string
116
+ createdAt: string
117
+ updatedAt: string
118
+ }
119
+
120
+ // ---------------------------------------------------------------------------
121
+ // Per-action param types
122
+ // ---------------------------------------------------------------------------
123
+
124
+ export interface PlaceOrderParams {
125
+ asset: AssetIdentity
126
+ side: OrderSide
127
+ type?: OrderType
128
+ size: string
129
+ price?: string
130
+ leverage?: number
131
+ /**
132
+ * Margin mode to use for the position. When omitted, the backend falls back
133
+ * to the provider's default (currently CROSS for both Hyperliquid and
134
+ * Lighter). Callers should set this explicitly when they need ISOLATED.
135
+ */
136
+ marginMode?: MarginMode
137
+ reduceOnly?: boolean
138
+ timeInForce?: TimeInForce
139
+ expiresAt?: string
140
+ takeProfit?: TriggerOrderInput
141
+ stopLoss?: TriggerOrderInput
142
+ }
143
+
144
+ export interface PlaceTriggerOrderParams {
145
+ asset: AssetIdentity
146
+ side: OrderSide
147
+ takeProfit?: TriggerOrderInput
148
+ stopLoss?: TriggerOrderInput
149
+ }
150
+
151
+ export interface CancelOrderParams {
152
+ ids: string[]
153
+ }
154
+
155
+ export interface ModifyOrderParams {
156
+ modifications: ModifyOrderInput[]
157
+ }
158
+
159
+ export interface UpdateLeverageParams {
160
+ asset: AssetIdentity
161
+ leverage: number
162
+ /**
163
+ * Margin mode to use for the position. When omitted, the backend falls back
164
+ * to the provider's default (currently CROSS for both Hyperliquid and
165
+ * Lighter). Callers should set this explicitly when they need ISOLATED.
166
+ */
167
+ marginMode?: MarginMode
168
+ }
169
+
170
+ export interface UpdatePositionMarginParams {
171
+ asset: AssetIdentity
172
+ action: 'add' | 'remove'
173
+ amount: string
174
+ }
175
+
176
+ export interface WithdrawalParams {
177
+ destination: Address
178
+ amount: string
179
+ }
180
+
181
+ export interface DepositParams {
182
+ /** Amount of the token to deposit (human-readable, e.g. "100.5"). */
183
+ amount: string
184
+ /** ERC-20 token address on the source chain. */
185
+ tokenAddress: Address
186
+ /** Chain ID of the source chain (e.g. 1 for Ethereum, 42161 for Arbitrum). */
187
+ chainId: number
188
+ }
189
+
190
+ export interface ApproveAgentParams {
191
+ agentAddress: string
192
+ agentTtlMs?: number
193
+ }
194
+
195
+ /**
196
+ * Params for `ActionType.ACCOUNT_MODE` — switch the account's operating
197
+ * mode (e.g. HL abstraction variant, Lighter UTA / Simple). The string
198
+ * is opaque per-provider; the authoritative enumeration of valid values
199
+ * lives on the descriptor's `Param.values` array (see
200
+ * `ProviderActionDescriptor` in `providers.ts`). The SDK and backend are
201
+ * responsible for validating `mode` against that array — `@lifi/perps-types`
202
+ * intentionally does not encode the per-provider value list here, so a
203
+ * provider can add a new mode without a types release.
204
+ */
205
+ export interface AccountModeParams {
206
+ mode: string
207
+ }
208
+
209
+ /**
210
+ * Params for `ActionType.ACCOUNT_TYPE` — switch the account's fee/latency
211
+ * tier (e.g. Lighter standard / premium). Providers without tiering omit
212
+ * the action entirely; the descriptor's `Param.values` array enumerates
213
+ * the valid tiers for providers that do support it.
214
+ */
215
+ export interface AccountTypeParams {
216
+ tier: string
217
+ }
218
+
219
+ export interface SendAssetParams {
220
+ collateral: string
221
+ sourceDex: string
222
+ destinationDex: string
223
+ amount: string
224
+ }
225
+
226
+ // ---------------------------------------------------------------------------
227
+ // ActionParamsMap — compile-time type resolution for SDK
228
+ // ---------------------------------------------------------------------------
229
+
230
+ export interface CancelAllOrdersParams {
231
+ /** 0=immediate (cancel GTC), 1=scheduled, 2=abort scheduled */
232
+ timeInForce: number
233
+ /** Unix timestamp in milliseconds (required for scheduled cancels) */
234
+ timestampMs?: number
235
+ }
236
+
237
+ export interface RegisterApiKeyParams {
238
+ /** The API key slot index to register (0-255). Reusing a fixed slot overwrites the old key. */
239
+ apiKeyIndex: number
240
+ }
241
+
242
+ /**
243
+ * Params for `ActionType.APPROVE_READ_ONLY_TOKEN` — mint a long-lived
244
+ * read-only token Lighter recognises for query-only access (positions,
245
+ * fills, balances). The token format on the wire is
246
+ * `ro:{accountIndex}:{scope}:{expiry}:{rand}`; this type carries only the
247
+ * inputs needed to construct that token, not the token itself.
248
+ *
249
+ * `expirySeconds` is the absolute unix-seconds expiry. Lighter constrains
250
+ * the lifetime to a minimum of 1 day and a maximum of 10 years from now;
251
+ * the SDK and backend are responsible for enforcing those bounds.
252
+ *
253
+ * `scope` mirrors Lighter's documented `single | all` literal:
254
+ * - `'single'` — token authorises read access to one account only.
255
+ * - `'all'` — token authorises read access to every account owned by the
256
+ * signer. Default for the perps stack today.
257
+ */
258
+ export interface ApproveReadOnlyTokenParams {
259
+ accountIndex: number
260
+ expirySeconds: number
261
+ scope: 'single' | 'all'
262
+ }
263
+
264
+ export interface ActionParamsMap {
265
+ [ActionType.APPROVE_AGENT]: ApproveAgentParams
266
+ [ActionType.APPROVE_BUILDER_FEE]: Record<string, never>
267
+ [ActionType.ACCOUNT_MODE]: AccountModeParams
268
+ [ActionType.ACCOUNT_TYPE]: AccountTypeParams
269
+ [ActionType.SEND_ASSET]: SendAssetParams
270
+ [ActionType.WITHDRAWAL]: WithdrawalParams
271
+ [ActionType.TRANSFER]: Record<string, never>
272
+ [ActionType.PLACE_ORDER]: PlaceOrderParams
273
+ [ActionType.PLACE_TRIGGER_ORDER]: PlaceTriggerOrderParams
274
+ [ActionType.CANCEL_ORDER]: CancelOrderParams
275
+ [ActionType.CANCEL_ALL_ORDERS]: CancelAllOrdersParams
276
+ [ActionType.MODIFY_ORDER]: ModifyOrderParams
277
+ [ActionType.UPDATE_LEVERAGE]: UpdateLeverageParams
278
+ [ActionType.UPDATE_POSITION_MARGIN]: UpdatePositionMarginParams
279
+ [ActionType.REGISTER_API_KEY]: RegisterApiKeyParams
280
+ [ActionType.APPROVE_READ_ONLY_TOKEN]: ApproveReadOnlyTokenParams
281
+ [ActionType.DEPOSIT]: DepositParams
282
+ }
283
+
284
+ // ---------------------------------------------------------------------------
285
+ // Request / Response types
286
+ // ---------------------------------------------------------------------------
287
+
288
+ /**
289
+ * Distributed discriminated request shape: selecting an `action` literal
290
+ * narrows `params` to exactly the matching entry in `ActionParamsMap`.
291
+ * Mapped over `ActionType` so a new action automatically yields a new branch.
292
+ */
293
+ export type CreateActionRequest = {
294
+ [K in ActionType]: {
295
+ provider: string
296
+ address: Address
297
+ signerAddress?: Address
298
+ action: K
299
+ params: ActionParamsMap[K]
300
+ }
301
+ }[ActionType]
302
+
303
+ export interface CreateActionResponse {
304
+ actions: ActionStep[]
305
+ }
306
+
307
+ /**
308
+ * Distributed discriminated execute-request shape: narrowing on `action`
309
+ * pins the action kind to a single `ActionType`; `actions` is the
310
+ * already-discriminated `SignedActionStep[]` union above.
311
+ */
312
+ export type ExecuteActionRequest = {
313
+ [K in ActionType]: {
314
+ provider: string
315
+ address: Address
316
+ signerAddress?: Address
317
+ action: K
318
+ actions: SignedActionStep[]
319
+ }
320
+ }[ActionType]
321
+
322
+ export interface ExecuteActionResponse {
323
+ results: ActionResult[]
324
+ }
package/src/asset.ts ADDED
@@ -0,0 +1,88 @@
1
+ export interface FundingInfo {
2
+ rate: string
3
+ nextFundingTime: number
4
+ }
5
+
6
+ export interface AssetIdentity {
7
+ assetId: string // provider's canonical identity: "BTC", "xyz:PURR", "@142"
8
+ market: string // market category from /providers.markets: "hyperliquid", "xyz", "spot"
9
+ }
10
+
11
+ export interface Asset extends AssetIdentity {
12
+ displaySymbol: string // UI base name: "BTC", "PURR"; spot keeps full pair: "PURR/USDC"
13
+ displayQuote: string | null // quote asset for perps: "USDC", "USDH"; null for spot
14
+ displayName?: string // future: "Bitcoin", "Ethereum"
15
+ logoURI: string
16
+ szDecimals: number
17
+ maxLeverage: number
18
+ onlyIsolated: boolean
19
+ isMarginCollateral?: boolean
20
+ funding: FundingInfo
21
+ openInterest?: string
22
+ volume24h?: string
23
+ prevDayPrice?: string
24
+ markPrice: string
25
+ }
26
+
27
+ export type AssetDisplay = Pick<
28
+ Asset,
29
+ 'assetId' | 'market' | 'displaySymbol' | 'displayQuote'
30
+ >
31
+
32
+ export interface AssetPrice {
33
+ assetId: string
34
+ price: string
35
+ }
36
+
37
+ export interface AssetsResponse {
38
+ assets: Asset[]
39
+ }
40
+
41
+ export interface PricesResponse {
42
+ prices: AssetPrice[]
43
+ }
44
+
45
+ export interface Candle {
46
+ t: number
47
+ o: string
48
+ h: string
49
+ l: string
50
+ c: string
51
+ v: string
52
+ }
53
+
54
+ export interface OhlcvResponse {
55
+ provider: string
56
+ assetId: string
57
+ interval: string
58
+ candles: Candle[]
59
+ }
60
+
61
+ export type OhlcvInterval =
62
+ | '1m'
63
+ | '3m'
64
+ | '5m'
65
+ | '15m'
66
+ | '30m'
67
+ | '1h'
68
+ | '2h'
69
+ | '4h'
70
+ | '8h'
71
+ | '12h'
72
+ | '1d'
73
+ | '3d'
74
+ | '1w'
75
+ | '1M'
76
+
77
+ export interface OrderbookLevel {
78
+ price: string
79
+ size: string
80
+ }
81
+
82
+ export interface OrderbookResponse {
83
+ provider: string
84
+ assetId: string
85
+ bids: OrderbookLevel[]
86
+ asks: OrderbookLevel[]
87
+ timestamp: number
88
+ }