@lifi/perps-sdk-provider-hyperliquid 5.1.0 → 6.0.1

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 (263) hide show
  1. package/dist/cjs/HyperliquidProvider.d.ts.map +1 -1
  2. package/dist/cjs/HyperliquidProvider.js +4 -0
  3. package/dist/cjs/HyperliquidProvider.js.map +1 -1
  4. package/dist/cjs/services/getAccount.d.ts.map +1 -1
  5. package/dist/cjs/services/getAccount.js +1 -1
  6. package/dist/cjs/services/getAccount.js.map +1 -1
  7. package/dist/cjs/services/getActivity.d.ts.map +1 -1
  8. package/dist/cjs/services/getActivity.js.map +1 -1
  9. package/dist/cjs/services/getFills.d.ts.map +1 -1
  10. package/dist/cjs/services/getFills.js.map +1 -1
  11. package/dist/cjs/services/getMarketSettings.d.ts +10 -0
  12. package/dist/cjs/services/getMarketSettings.d.ts.map +1 -0
  13. package/dist/cjs/services/getMarketSettings.js +26 -0
  14. package/dist/cjs/services/getMarketSettings.js.map +1 -0
  15. package/dist/cjs/services/getOrders.d.ts.map +1 -1
  16. package/dist/cjs/services/getOrders.js.map +1 -1
  17. package/dist/cjs/services/getPositions.d.ts.map +1 -1
  18. package/dist/cjs/services/getPositions.js +1 -1
  19. package/dist/cjs/services/getPositions.js.map +1 -1
  20. package/dist/cjs/signers/HyperliquidAgentStore.d.ts.map +1 -1
  21. package/dist/cjs/signers/HyperliquidAgentStore.js.map +1 -1
  22. package/dist/cjs/signers/signActions.d.ts.map +1 -1
  23. package/dist/cjs/signers/signActions.js.map +1 -1
  24. package/dist/cjs/types/account.d.ts +11 -0
  25. package/dist/cjs/types/account.d.ts.map +1 -1
  26. package/dist/cjs/types/account.js.map +1 -1
  27. package/dist/cjs/types/action.d.ts.map +1 -1
  28. package/dist/cjs/types/action.js.map +1 -1
  29. package/dist/cjs/types/asset.d.ts +1 -0
  30. package/dist/cjs/types/asset.d.ts.map +1 -1
  31. package/dist/cjs/types/fill.d.ts.map +1 -1
  32. package/dist/cjs/types/ledger.d.ts.map +1 -1
  33. package/dist/cjs/types/ledger.js.map +1 -1
  34. package/dist/cjs/types/order.d.ts.map +1 -1
  35. package/dist/cjs/types/ws.d.ts.map +1 -1
  36. package/dist/cjs/utils/assetId.d.ts.map +1 -1
  37. package/dist/cjs/utils/assetId.js.map +1 -1
  38. package/dist/cjs/utils/assetLogo.d.ts.map +1 -1
  39. package/dist/cjs/utils/assetLogo.js.map +1 -1
  40. package/dist/cjs/utils/decodeFastAssetCtxs.d.ts.map +1 -1
  41. package/dist/cjs/utils/decodeFastAssetCtxs.js.map +1 -1
  42. package/dist/cjs/utils/index.d.ts +1 -0
  43. package/dist/cjs/utils/index.d.ts.map +1 -1
  44. package/dist/cjs/utils/index.js +3 -1
  45. package/dist/cjs/utils/index.js.map +1 -1
  46. package/dist/cjs/utils/infoClient.d.ts.map +1 -1
  47. package/dist/cjs/utils/infoClient.js.map +1 -1
  48. package/dist/cjs/utils/mapActivity.d.ts.map +1 -1
  49. package/dist/cjs/utils/mapActivity.js +13 -10
  50. package/dist/cjs/utils/mapActivity.js.map +1 -1
  51. package/dist/cjs/utils/mapFill.d.ts.map +1 -1
  52. package/dist/cjs/utils/mapFill.js.map +1 -1
  53. package/dist/cjs/utils/mapMarket.d.ts.map +1 -1
  54. package/dist/cjs/utils/mapMarket.js +6 -1
  55. package/dist/cjs/utils/mapMarket.js.map +1 -1
  56. package/dist/cjs/utils/mapOrder.d.ts.map +1 -1
  57. package/dist/cjs/utils/mapOrder.js.map +1 -1
  58. package/dist/cjs/utils/mapPosition.d.ts +2 -2
  59. package/dist/cjs/utils/mapPosition.d.ts.map +1 -1
  60. package/dist/cjs/utils/mapPosition.js +17 -10
  61. package/dist/cjs/utils/mapPosition.js.map +1 -1
  62. package/dist/cjs/utils/spotBalance.d.ts.map +1 -1
  63. package/dist/cjs/utils/spotBalance.js.map +1 -1
  64. package/dist/cjs/utils/spotCollateral.d.ts.map +1 -1
  65. package/dist/cjs/utils/spotCollateral.js.map +1 -1
  66. package/dist/cjs/utils/transferMargin.d.ts +3 -0
  67. package/dist/cjs/utils/transferMargin.d.ts.map +1 -0
  68. package/dist/cjs/utils/transferMargin.js +40 -0
  69. package/dist/cjs/utils/transferMargin.js.map +1 -0
  70. package/dist/cjs/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  71. package/dist/cjs/websocket/HyperliquidWsProvider.js +3 -1
  72. package/dist/cjs/websocket/HyperliquidWsProvider.js.map +1 -1
  73. package/dist/esm/HyperliquidProvider.d.ts +14 -4
  74. package/dist/esm/HyperliquidProvider.d.ts.map +1 -1
  75. package/dist/esm/HyperliquidProvider.js +4 -0
  76. package/dist/esm/HyperliquidProvider.js.map +1 -1
  77. package/dist/esm/services/getAccount.d.ts.map +1 -1
  78. package/dist/esm/services/getAccount.js +2 -2
  79. package/dist/esm/services/getAccount.js.map +1 -1
  80. package/dist/esm/services/getActivity.d.ts +5 -1
  81. package/dist/esm/services/getActivity.d.ts.map +1 -1
  82. package/dist/esm/services/getActivity.js.map +1 -1
  83. package/dist/esm/services/getFills.d.ts +4 -3
  84. package/dist/esm/services/getFills.d.ts.map +1 -1
  85. package/dist/esm/services/getFills.js.map +1 -1
  86. package/dist/esm/services/getMarketSettings.d.ts +21 -0
  87. package/dist/esm/services/getMarketSettings.d.ts.map +1 -0
  88. package/dist/esm/services/getMarketSettings.js +29 -0
  89. package/dist/esm/services/getMarketSettings.js.map +1 -0
  90. package/dist/esm/services/getOrders.d.ts +2 -1
  91. package/dist/esm/services/getOrders.d.ts.map +1 -1
  92. package/dist/esm/services/getOrders.js.map +1 -1
  93. package/dist/esm/services/getPositions.d.ts +2 -2
  94. package/dist/esm/services/getPositions.d.ts.map +1 -1
  95. package/dist/esm/services/getPositions.js +2 -2
  96. package/dist/esm/services/getPositions.js.map +1 -1
  97. package/dist/esm/signers/HyperliquidAgentStore.d.ts +3 -3
  98. package/dist/esm/signers/HyperliquidAgentStore.d.ts.map +1 -1
  99. package/dist/esm/signers/HyperliquidAgentStore.js +1 -0
  100. package/dist/esm/signers/HyperliquidAgentStore.js.map +1 -1
  101. package/dist/esm/signers/signActions.d.ts +8 -6
  102. package/dist/esm/signers/signActions.d.ts.map +1 -1
  103. package/dist/esm/signers/signActions.js +8 -6
  104. package/dist/esm/signers/signActions.js.map +1 -1
  105. package/dist/esm/types/account.d.ts +68 -8
  106. package/dist/esm/types/account.d.ts.map +1 -1
  107. package/dist/esm/types/account.js.map +1 -1
  108. package/dist/esm/types/action.d.ts +28 -10
  109. package/dist/esm/types/action.d.ts.map +1 -1
  110. package/dist/esm/types/action.js +7 -7
  111. package/dist/esm/types/action.js.map +1 -1
  112. package/dist/esm/types/asset.d.ts +62 -15
  113. package/dist/esm/types/asset.d.ts.map +1 -1
  114. package/dist/esm/types/fill.d.ts +9 -3
  115. package/dist/esm/types/fill.d.ts.map +1 -1
  116. package/dist/esm/types/ledger.d.ts +24 -6
  117. package/dist/esm/types/ledger.d.ts.map +1 -1
  118. package/dist/esm/types/ledger.js.map +1 -1
  119. package/dist/esm/types/order.d.ts +19 -5
  120. package/dist/esm/types/order.d.ts.map +1 -1
  121. package/dist/esm/types/ws.d.ts +43 -14
  122. package/dist/esm/types/ws.d.ts.map +1 -1
  123. package/dist/esm/utils/assetId.d.ts +7 -7
  124. package/dist/esm/utils/assetId.d.ts.map +1 -1
  125. package/dist/esm/utils/assetId.js +7 -7
  126. package/dist/esm/utils/assetId.js.map +1 -1
  127. package/dist/esm/utils/assetLogo.d.ts +15 -13
  128. package/dist/esm/utils/assetLogo.d.ts.map +1 -1
  129. package/dist/esm/utils/assetLogo.js +15 -13
  130. package/dist/esm/utils/assetLogo.js.map +1 -1
  131. package/dist/esm/utils/decodeFastAssetCtxs.d.ts +7 -1
  132. package/dist/esm/utils/decodeFastAssetCtxs.d.ts.map +1 -1
  133. package/dist/esm/utils/decodeFastAssetCtxs.js +7 -1
  134. package/dist/esm/utils/decodeFastAssetCtxs.js.map +1 -1
  135. package/dist/esm/utils/index.d.ts +1 -0
  136. package/dist/esm/utils/index.d.ts.map +1 -1
  137. package/dist/esm/utils/index.js +1 -0
  138. package/dist/esm/utils/index.js.map +1 -1
  139. package/dist/esm/utils/infoClient.d.ts +6 -1
  140. package/dist/esm/utils/infoClient.d.ts.map +1 -1
  141. package/dist/esm/utils/infoClient.js.map +1 -1
  142. package/dist/esm/utils/mapActivity.d.ts +10 -1
  143. package/dist/esm/utils/mapActivity.d.ts.map +1 -1
  144. package/dist/esm/utils/mapActivity.js +23 -11
  145. package/dist/esm/utils/mapActivity.js.map +1 -1
  146. package/dist/esm/utils/mapFill.d.ts +1 -0
  147. package/dist/esm/utils/mapFill.d.ts.map +1 -1
  148. package/dist/esm/utils/mapFill.js +1 -0
  149. package/dist/esm/utils/mapFill.js.map +1 -1
  150. package/dist/esm/utils/mapMarket.d.ts.map +1 -1
  151. package/dist/esm/utils/mapMarket.js +8 -1
  152. package/dist/esm/utils/mapMarket.js.map +1 -1
  153. package/dist/esm/utils/mapOrder.d.ts +15 -4
  154. package/dist/esm/utils/mapOrder.d.ts.map +1 -1
  155. package/dist/esm/utils/mapOrder.js +15 -4
  156. package/dist/esm/utils/mapOrder.js.map +1 -1
  157. package/dist/esm/utils/mapPosition.d.ts +8 -3
  158. package/dist/esm/utils/mapPosition.d.ts.map +1 -1
  159. package/dist/esm/utils/mapPosition.js +20 -11
  160. package/dist/esm/utils/mapPosition.js.map +1 -1
  161. package/dist/esm/utils/spotBalance.d.ts +9 -10
  162. package/dist/esm/utils/spotBalance.d.ts.map +1 -1
  163. package/dist/esm/utils/spotBalance.js +9 -10
  164. package/dist/esm/utils/spotBalance.js.map +1 -1
  165. package/dist/esm/utils/spotCollateral.d.ts +9 -5
  166. package/dist/esm/utils/spotCollateral.d.ts.map +1 -1
  167. package/dist/esm/utils/spotCollateral.js +4 -4
  168. package/dist/esm/utils/spotCollateral.js.map +1 -1
  169. package/dist/esm/utils/transferMargin.d.ts +14 -0
  170. package/dist/esm/utils/transferMargin.d.ts.map +1 -0
  171. package/dist/esm/utils/transferMargin.js +45 -0
  172. package/dist/esm/utils/transferMargin.js.map +1 -0
  173. package/dist/esm/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  174. package/dist/esm/websocket/HyperliquidWsProvider.js +4 -2
  175. package/dist/esm/websocket/HyperliquidWsProvider.js.map +1 -1
  176. package/dist/types/HyperliquidProvider.d.ts +14 -4
  177. package/dist/types/HyperliquidProvider.d.ts.map +1 -1
  178. package/dist/types/services/getAccount.d.ts.map +1 -1
  179. package/dist/types/services/getActivity.d.ts +5 -1
  180. package/dist/types/services/getActivity.d.ts.map +1 -1
  181. package/dist/types/services/getFills.d.ts +4 -3
  182. package/dist/types/services/getFills.d.ts.map +1 -1
  183. package/dist/types/services/getMarketSettings.d.ts +21 -0
  184. package/dist/types/services/getMarketSettings.d.ts.map +1 -0
  185. package/dist/types/services/getOrders.d.ts +2 -1
  186. package/dist/types/services/getOrders.d.ts.map +1 -1
  187. package/dist/types/services/getPositions.d.ts +2 -2
  188. package/dist/types/services/getPositions.d.ts.map +1 -1
  189. package/dist/types/signers/HyperliquidAgentStore.d.ts +3 -3
  190. package/dist/types/signers/HyperliquidAgentStore.d.ts.map +1 -1
  191. package/dist/types/signers/signActions.d.ts +8 -6
  192. package/dist/types/signers/signActions.d.ts.map +1 -1
  193. package/dist/types/types/account.d.ts +68 -8
  194. package/dist/types/types/account.d.ts.map +1 -1
  195. package/dist/types/types/action.d.ts +28 -10
  196. package/dist/types/types/action.d.ts.map +1 -1
  197. package/dist/types/types/asset.d.ts +62 -15
  198. package/dist/types/types/asset.d.ts.map +1 -1
  199. package/dist/types/types/fill.d.ts +9 -3
  200. package/dist/types/types/fill.d.ts.map +1 -1
  201. package/dist/types/types/ledger.d.ts +24 -6
  202. package/dist/types/types/ledger.d.ts.map +1 -1
  203. package/dist/types/types/order.d.ts +19 -5
  204. package/dist/types/types/order.d.ts.map +1 -1
  205. package/dist/types/types/ws.d.ts +43 -14
  206. package/dist/types/types/ws.d.ts.map +1 -1
  207. package/dist/types/utils/assetId.d.ts +7 -7
  208. package/dist/types/utils/assetId.d.ts.map +1 -1
  209. package/dist/types/utils/assetLogo.d.ts +15 -13
  210. package/dist/types/utils/assetLogo.d.ts.map +1 -1
  211. package/dist/types/utils/decodeFastAssetCtxs.d.ts +7 -1
  212. package/dist/types/utils/decodeFastAssetCtxs.d.ts.map +1 -1
  213. package/dist/types/utils/index.d.ts +1 -0
  214. package/dist/types/utils/index.d.ts.map +1 -1
  215. package/dist/types/utils/infoClient.d.ts +6 -1
  216. package/dist/types/utils/infoClient.d.ts.map +1 -1
  217. package/dist/types/utils/mapActivity.d.ts +10 -1
  218. package/dist/types/utils/mapActivity.d.ts.map +1 -1
  219. package/dist/types/utils/mapFill.d.ts +1 -0
  220. package/dist/types/utils/mapFill.d.ts.map +1 -1
  221. package/dist/types/utils/mapMarket.d.ts.map +1 -1
  222. package/dist/types/utils/mapOrder.d.ts +15 -4
  223. package/dist/types/utils/mapOrder.d.ts.map +1 -1
  224. package/dist/types/utils/mapPosition.d.ts +8 -3
  225. package/dist/types/utils/mapPosition.d.ts.map +1 -1
  226. package/dist/types/utils/spotBalance.d.ts +9 -10
  227. package/dist/types/utils/spotBalance.d.ts.map +1 -1
  228. package/dist/types/utils/spotCollateral.d.ts +9 -5
  229. package/dist/types/utils/spotCollateral.d.ts.map +1 -1
  230. package/dist/types/utils/transferMargin.d.ts +14 -0
  231. package/dist/types/utils/transferMargin.d.ts.map +1 -0
  232. package/dist/types/websocket/HyperliquidWsProvider.d.ts.map +1 -1
  233. package/package.json +3 -3
  234. package/src/HyperliquidProvider.ts +30 -4
  235. package/src/services/getAccount.ts +7 -1
  236. package/src/services/getActivity.ts +5 -1
  237. package/src/services/getFills.ts +4 -3
  238. package/src/services/getMarketSettings.ts +56 -0
  239. package/src/services/getOrders.ts +2 -1
  240. package/src/services/getPositions.ts +13 -4
  241. package/src/signers/HyperliquidAgentStore.ts +3 -3
  242. package/src/signers/signActions.ts +8 -6
  243. package/src/types/account.ts +66 -8
  244. package/src/types/action.ts +28 -10
  245. package/src/types/asset.ts +62 -15
  246. package/src/types/fill.ts +9 -3
  247. package/src/types/ledger.ts +24 -6
  248. package/src/types/order.ts +19 -5
  249. package/src/types/ws.ts +43 -14
  250. package/src/utils/assetId.ts +7 -7
  251. package/src/utils/assetLogo.ts +15 -13
  252. package/src/utils/decodeFastAssetCtxs.ts +7 -1
  253. package/src/utils/index.ts +1 -0
  254. package/src/utils/infoClient.ts +6 -1
  255. package/src/utils/mapActivity.ts +23 -11
  256. package/src/utils/mapFill.ts +1 -0
  257. package/src/utils/mapMarket.ts +10 -1
  258. package/src/utils/mapOrder.ts +15 -4
  259. package/src/utils/mapPosition.ts +24 -15
  260. package/src/utils/spotBalance.ts +9 -10
  261. package/src/utils/spotCollateral.ts +9 -5
  262. package/src/utils/transferMargin.ts +65 -0
  263. package/src/websocket/HyperliquidWsProvider.ts +4 -1
@@ -11,6 +11,7 @@ import {
11
11
  type ProviderGetAccountParams,
12
12
  type ProviderGetActivityParams,
13
13
  type ProviderGetFillsParams,
14
+ type ProviderGetMarketSettingsParams,
14
15
  type ProviderGetOrderParams,
15
16
  type ProviderGetOrdersParams,
16
17
  type ProviderGetPositionsParams,
@@ -30,6 +31,7 @@ import {
30
31
  type ActivitiesResponse,
31
32
  type FillsResponse,
32
33
  type Market,
34
+ type MarketSettings,
33
35
  type Order,
34
36
  type OrdersResponse,
35
37
  PerpsErrorCode,
@@ -56,6 +58,7 @@ import { getAccount } from './services/getAccount.js'
56
58
  import { getAccountExists } from './services/getAccountExists.js'
57
59
  import { getActivity } from './services/getActivity.js'
58
60
  import { getFills } from './services/getFills.js'
61
+ import { getMarketSettings } from './services/getMarketSettings.js'
59
62
  import { getOrder } from './services/getOrder.js'
60
63
  import { getOrders } from './services/getOrders.js'
61
64
  import { getPositions } from './services/getPositions.js'
@@ -68,6 +71,7 @@ import type { HlExtraAgents } from './types/index.js'
68
71
  import { hlInfoOptions, infoRequest } from './utils/infoClient.js'
69
72
  import { calculateLiquidationPrice } from './utils/liquidation.js'
70
73
  import { formatOrderPrice, formatOrderSize } from './utils/orderFormatting.js'
74
+ import { positionMarginConstraints } from './utils/transferMargin.js'
71
75
 
72
76
  /**
73
77
  * Options for {@link hyperliquidProvider}.
@@ -97,15 +101,25 @@ export interface HyperliquidProviderOptions {
97
101
  * persistence, and revocation. The base {@link PerpsProviderPlugin} contract
98
102
  * stays provider-agnostic — this extension is opt-in for callers that
99
103
  * explicitly type against it (e.g. to surface a "revoke agent" affordance).
104
+ * @public
100
105
  */
101
106
  export interface HyperliquidPerpsProvider extends PerpsProviderPlugin {
102
- /** Resolve the agent wallet address, throwing if none has been created. */
107
+ /**
108
+ * Resolve the stored agent wallet address for a user.
109
+ * @throws {PerpsError} With `AgentNotFound` when no agent has been created.
110
+ */
103
111
  getAgentAddress(address: Address): Promise<Address>
104
- /** Whether an agent keypair exists for the user address. */
112
+ /** Return whether a persisted agent keypair exists for the user address. */
105
113
  hasAgent(address: Address): Promise<boolean>
106
- /** Remove the user's agent keypair (revoke local authorization). */
114
+ /**
115
+ * Remove the user's persisted agent keypair and local authorization.
116
+ * This does not submit an on-chain `APPROVE_AGENT` revocation.
117
+ */
107
118
  removeAgent(address: Address): Promise<void>
108
- /** Import an existing agent keypair for the user address. */
119
+ /**
120
+ * Validate, persist, and return an existing agent private key for a user.
121
+ * The returned private key is stored by the configured {@link StorageAdapter}.
122
+ */
109
123
  importAgent(address: Address, privateKey: Hex): Promise<HyperliquidAgent>
110
124
  }
111
125
 
@@ -288,6 +302,16 @@ export function hyperliquidProvider(
288
302
  opts
289
303
  ),
290
304
 
305
+ getMarketSettings: (
306
+ params: ProviderGetMarketSettingsParams,
307
+ opts?: SDKRequestOptions
308
+ ): Promise<MarketSettings | undefined> =>
309
+ getMarketSettings(
310
+ contextRef.require(),
311
+ { address: params.address, market: params.market },
312
+ opts
313
+ ),
314
+
291
315
  getOrders: (
292
316
  params: ProviderGetOrdersParams,
293
317
  opts?: SDKRequestOptions
@@ -383,6 +407,8 @@ export function hyperliquidProvider(
383
407
  market.maxLeverage
384
408
  ),
385
409
 
410
+ positionMarginConstraints,
411
+
386
412
  projectConfig: (
387
413
  config: AccountConfig,
388
414
  setup: ProviderAction[],
@@ -4,6 +4,7 @@ import {
4
4
  type ProviderGetAccountParams,
5
5
  type SDKRequestOptions,
6
6
  stringToFloat,
7
+ toPerpsMarketDisplay,
7
8
  } from '@lifi/perps-sdk'
8
9
  import type {
9
10
  AccountResponse,
@@ -214,7 +215,12 @@ export const getAccount = async (
214
215
  const positions: Position[] = stateResults.flatMap((state) =>
215
216
  state.assetPositions
216
217
  .filter(isOpenAssetPosition)
217
- .map((ap) => mapPosition(ap, registry.require(ap.position.coin)))
218
+ .map((ap) =>
219
+ mapPosition(
220
+ ap,
221
+ toPerpsMarketDisplay(registry.require(ap.position.coin))
222
+ )
223
+ )
218
224
  )
219
225
 
220
226
  const stateByDex = new Map<string, HlClearinghouseState>()
@@ -31,11 +31,15 @@ import {
31
31
  */
32
32
  export interface GetActivityParams {
33
33
  address: Address
34
+ /** Maximum items returned; defaults to 50 and is capped at 200. */
34
35
  limit?: number
35
- /** `cursor` is a ms-since-epoch upper bound items with `timestamp < cursor` are returned. */
36
+ /** Millisecond timestamp cursor; rows strictly older than it are returned. */
36
37
  cursor?: string
38
+ /** Inclusive lower bound in milliseconds since epoch; defaults to 90 days ago. */
37
39
  startTime?: number
40
+ /** Inclusive upper bound in milliseconds since epoch. */
38
41
  endTime?: number
42
+ /** Optional normalized activity-type filter applied after mapping. */
39
43
  type?: ActivityType[]
40
44
  }
41
45
 
@@ -22,12 +22,13 @@ import { hlInfoOptions, infoRequest } from '../utils/infoClient.js'
22
22
  */
23
23
  export interface GetFillsParams {
24
24
  address: Address
25
+ /** Maximum items returned; defaults to 50 and is capped at 200. */
25
26
  limit?: number
26
- /** Opaque pagination cursor from a previous page's `pagination.cursor`. */
27
+ /** Opaque cursor returned in the previous page's `pagination.cursor`. */
27
28
  cursor?: string
28
- /** Inclusive lower bound in ms-since-epoch switches to the `userFillsByTime` endpoint. */
29
+ /** Inclusive lower bound in milliseconds; selects `userFillsByTime`. */
29
30
  startTime?: number
30
- /** Inclusive upper bound in ms-since-epoch switches to the `userFillsByTime` endpoint. */
31
+ /** Inclusive upper bound in milliseconds; selects `userFillsByTime`. */
31
32
  endTime?: number
32
33
  }
33
34
 
@@ -0,0 +1,56 @@
1
+ import type { SDKRequestOptions } from '@lifi/perps-sdk'
2
+ import {
3
+ MarginMode,
4
+ type MarketRef,
5
+ type MarketSettings,
6
+ } from '@lifi/perps-types'
7
+ import type { Address } from 'viem'
8
+ import { SPOT_MARKET_ID } from '../constants.js'
9
+ import type { HyperliquidContext } from '../context.js'
10
+ import type { HlActiveAssetData } from '../types/index.js'
11
+ import { hlInfoOptions, infoRequest } from '../utils/infoClient.js'
12
+
13
+ /**
14
+ * Parameters for {@link getMarketSettings}.
15
+ *
16
+ * @public
17
+ */
18
+ export interface GetMarketSettingsParams {
19
+ address: Address
20
+ market: MarketRef
21
+ }
22
+
23
+ /**
24
+ * The user's venue-stored margin mode and leverage for one perps market,
25
+ * read from `activeAssetData` — present whether or not a position is open.
26
+ * @throws {PerpsError} On Hyperliquid REST error, network, or parsing failures.
27
+ * @public
28
+ */
29
+ export const getMarketSettings = async (
30
+ { client, apiUrl }: HyperliquidContext,
31
+ params: GetMarketSettingsParams,
32
+ options?: SDKRequestOptions
33
+ ): Promise<MarketSettings | undefined> => {
34
+ // Spot markets carry no leverage state on the venue.
35
+ if (params.market.categoryId === SPOT_MARKET_ID) {
36
+ return undefined
37
+ }
38
+ const data = await infoRequest<HlActiveAssetData>(
39
+ apiUrl,
40
+ {
41
+ type: 'activeAssetData',
42
+ user: params.address,
43
+ coin: params.market.marketId,
44
+ },
45
+ hlInfoOptions(client, options)
46
+ )
47
+ const leverage = data.leverage
48
+ if (!leverage) {
49
+ return undefined
50
+ }
51
+ return {
52
+ marginMode:
53
+ leverage.type === 'isolated' ? MarginMode.ISOLATED : MarginMode.CROSS,
54
+ leverage: leverage.value,
55
+ }
56
+ }
@@ -22,8 +22,9 @@ import { hlInfoOptions, infoRequest } from '../utils/infoClient.js'
22
22
  */
23
23
  export interface GetOrdersParams {
24
24
  address: Address
25
- /** Optional filter opaque `Market.id`. */
25
+ /** Optional filter using the normalized opaque `Market.id`. */
26
26
  marketId?: string
27
+ /** Page-size hint; Hyperliquid returns all open orders in one response. */
27
28
  limit?: number
28
29
  }
29
30
 
@@ -1,4 +1,8 @@
1
- import { getMarketRegistry, type SDKRequestOptions } from '@lifi/perps-sdk'
1
+ import {
2
+ getMarketRegistry,
3
+ type SDKRequestOptions,
4
+ toPerpsMarketDisplay,
5
+ } from '@lifi/perps-sdk'
2
6
  import type { PositionsResponse } from '@lifi/perps-types'
3
7
  import type { Address } from 'viem'
4
8
  import { PROVIDER_KEY } from '../constants.js'
@@ -18,9 +22,9 @@ import { hlInfoOptions, infoRequest } from '../utils/infoClient.js'
18
22
  */
19
23
  export interface GetPositionsParams {
20
24
  address: Address
21
- /** Filter to a single opaque `Market.id` (e.g. `'BTC'`, `'xyz:PURR'`). */
25
+ /** Optional filter using the normalized opaque `Market.id`. */
22
26
  marketId?: string
23
- /** Page size hint surfaced on the response. Hyperliquid returns all open positions in one call, so pagination is never required. */
27
+ /** Page-size hint; Hyperliquid returns all open positions in one response. */
24
28
  limit?: number
25
29
  }
26
30
 
@@ -58,7 +62,12 @@ export const getPositions = async (
58
62
  let positions = stateResults.flatMap((state) =>
59
63
  state.assetPositions
60
64
  .filter(isOpenAssetPosition)
61
- .map((ap) => mapPosition(ap, registry.require(ap.position.coin)))
65
+ .map((ap) =>
66
+ mapPosition(
67
+ ap,
68
+ toPerpsMarketDisplay(registry.require(ap.position.coin))
69
+ )
70
+ )
62
71
  )
63
72
 
64
73
  if (params.marketId !== undefined) {
@@ -15,12 +15,11 @@ const STORAGE_PREFIX = 'lifi-perps-agent'
15
15
  /**
16
16
  * Hyperliquid agent keypair — an EVM keypair the user approves (via
17
17
  * `APPROVE_AGENT`) to sign trading actions on their behalf, so each order
18
- * does not require a wallet prompt.
18
+ * does not require a wallet prompt. `privateKey` is a 32-byte EVM hex key.
19
+ * @public
19
20
  */
20
21
  export interface HyperliquidAgent {
21
- /** Agent wallet address. */
22
22
  address: Address
23
- /** Agent private key. */
24
23
  privateKey: Hex
25
24
  }
26
25
 
@@ -60,6 +59,7 @@ const isHyperliquidAgent = (value: unknown): value is HyperliquidAgent => {
60
59
  * fully compromised page — a same-origin script can still drive this store to
61
60
  * decrypt. Blast radius is limited to agent trading: fund withdrawal still
62
61
  * requires L1 `APPROVE_AGENT` consent that the agent key alone cannot grant.
62
+ * @public
63
63
  */
64
64
  export class HyperliquidAgentStore {
65
65
  private storage: StorageAdapter
@@ -15,12 +15,14 @@ import type { Address, Hex } from 'viem'
15
15
  import type { HyperliquidAgentStore } from './HyperliquidAgentStore.js'
16
16
 
17
17
  /**
18
- * Sign a batch of EIP-712 action steps. Hyperliquid owns both EIP712 arms and
19
- * picks WHO signs from the descriptor's `signers` (forwarded on the context):
20
- * - `AGENT` the user's Hyperliquid agent keypair (provisioned + approved
21
- * during setup; a missing agent throws).
22
- * - `USER` → the end-user's wallet, read from `ctx.userWallet`.
23
- * Hyperliquid declares no WASM_BLOB / EVM_TX actions, so those schemes reject.
18
+ * Sign a batch of Hyperliquid EIP-712 action steps. `AGENT` descriptors use
19
+ * the persisted per-user agent keypair; `USER` descriptors use
20
+ * `ctx.userWallet`. Other signing methods are rejected because Hyperliquid
21
+ * declares no WASM_BLOB or EVM_TX action schemes.
22
+ *
23
+ * @throws {PerpsError} When the method is unsupported, the required agent is
24
+ * missing, or a user-signed action has no wallet.
25
+ * @public
24
26
  */
25
27
  export async function hyperliquidSignActions(
26
28
  agentStore: HyperliquidAgentStore,
@@ -1,26 +1,52 @@
1
1
  // Account-level types returned by Hyperliquid `/info`.
2
2
 
3
- /** @public */
3
+ /**
4
+ * Perpetual position payload from Hyperliquid `clearinghouseState`.
5
+ *
6
+ * Numeric values are decimal strings in quote-asset units except `szi`,
7
+ * which is signed size: positive for long and negative for short. `coin` is
8
+ * the Hyperliquid wire market identifier; `leverage.type` is typically
9
+ * `'cross'` or `'isolated'`.
10
+ * @public
11
+ */
4
12
  export type HlPosition = {
13
+ /** Hyperliquid wire market identifier. */
5
14
  coin: string
15
+ /** Signed position size; positive is long and negative is short. */
6
16
  szi: string
17
+ /** Average entry price as a decimal string. */
7
18
  entryPx: string
19
+ /** Position notional value as a decimal string. */
8
20
  positionValue: string
21
+ /** Estimated liquidation price; may be an empty string when unavailable. */
9
22
  liquidationPx: string
23
+ /** Unrealized PnL in quote-asset units. */
10
24
  unrealizedPnl: string
25
+ /** Margin currently assigned to this position. */
11
26
  marginUsed: string
27
+ /** Leverage mode and numeric leverage multiplier. */
12
28
  leverage: {
13
29
  type: string
14
30
  value: number
15
31
  }
16
32
  }
17
33
 
18
- /** @public */
34
+ /**
35
+ * Wrapper for a position in the `assetPositions` array of a clearinghouse
36
+ * state response.
37
+ * @public
38
+ */
19
39
  export type HlAssetPosition = {
20
40
  position: HlPosition
21
41
  }
22
42
 
23
- /** @public */
43
+ /**
44
+ * Perpetual clearinghouse account state returned by Hyperliquid `/info`.
45
+ * `assetPositions` includes zero-size rows that callers may discard. Monetary
46
+ * fields are decimal strings in the account's quote asset; `marginSummary`
47
+ * covers all positions while `crossMarginSummary` covers cross margin only.
48
+ * @public
49
+ */
24
50
  export type HlClearinghouseState = {
25
51
  assetPositions: HlAssetPosition[]
26
52
  marginSummary: {
@@ -33,7 +59,12 @@ export type HlClearinghouseState = {
33
59
  }
34
60
  }
35
61
 
36
- /** @public */
62
+ /**
63
+ * One spot balance from Hyperliquid `spotClearinghouseState`.
64
+ * `token` is the numeric spot token index, and amounts are decimal strings in
65
+ * the token's native units.
66
+ * @public
67
+ */
37
68
  export type HlSpotBalance = {
38
69
  coin: string
39
70
  token: number
@@ -42,19 +73,31 @@ export type HlSpotBalance = {
42
73
  entryNtl: string
43
74
  }
44
75
 
45
- /** @public */
76
+ /**
77
+ * Spot clearinghouse state returned by Hyperliquid `/info`.
78
+ * @public
79
+ */
46
80
  export type HlSpotClearinghouseState = {
47
81
  balances: HlSpotBalance[]
48
82
  }
49
83
 
50
- /** @public */
84
+ /**
85
+ * User fee rates returned by Hyperliquid `userFees`.
86
+ * Rates are decimal fractions represented as strings, not percentage values
87
+ * (for example, `'0.00045'` is 0.045%).
88
+ * @public
89
+ */
51
90
  export type HlUserFees = {
52
91
  userAddRate: string
53
92
  userCrossRate: string
54
93
  activeReferralDiscount: string
55
94
  }
56
95
 
57
- /** @public */
96
+ /**
97
+ * Entries returned by Hyperliquid `extraAgents`. The endpoint may add fields
98
+ * over time, so the provider retains each entry as an open record.
99
+ * @public
100
+ */
58
101
  export type HlExtraAgents = Record<string, unknown>[]
59
102
 
60
103
  /**
@@ -67,6 +110,21 @@ export type HlPreTransferCheck = {
67
110
  userExists: boolean
68
111
  }
69
112
 
113
+ /**
114
+ * `activeAssetData` info response: the user's live per-asset trading state.
115
+ * `leverage` reflects the venue-stored setting whether or not a position is
116
+ * open on the asset.
117
+ * @public
118
+ */
119
+ export type HlActiveAssetData = {
120
+ user: string
121
+ coin: string
122
+ leverage: { type: 'cross' | 'isolated'; value: number }
123
+ maxTradeSzs: [string, string]
124
+ availableToTrade: [string, string]
125
+ markPx: string
126
+ }
127
+
70
128
  /**
71
129
  * Possible values returned by the `userAbstraction` info endpoint.
72
130
  * `null` means abstraction has never been set; `'default'`/`'disabled'` are
@@ -81,6 +139,6 @@ export const HlAbstractionMode = {
81
139
  DEX_ABSTRACTION: 'dexAbstraction',
82
140
  } as const
83
141
 
84
- /** @public */
142
+ /** Union of the exact `userAbstraction` wire values accepted by Hyperliquid. @public */
85
143
  export type HlAbstractionMode =
86
144
  (typeof HlAbstractionMode)[keyof typeof HlAbstractionMode]
@@ -1,6 +1,13 @@
1
1
  // Hyperliquid `/exchange` request/response shapes + EIP-712 primary types.
2
2
 
3
- /** @public */
3
+ /**
4
+ * EIP-712 `/exchange` request sent to Hyperliquid.
5
+ *
6
+ * `action` is the provider-specific action object; signature fields are the
7
+ * hex `r`/`s` values and recovery byte `v`; `nonce` is milliseconds since the
8
+ * Unix epoch. `vaultAddress` is included only when submitting for a vault.
9
+ * @public
10
+ */
4
11
  export type HlExchangeRequest = {
5
12
  action: Record<string, unknown>
6
13
  signature: {
@@ -12,7 +19,14 @@ export type HlExchangeRequest = {
12
19
  vaultAddress?: string | null
13
20
  }
14
21
 
15
- /** @public */
22
+ /**
23
+ * Response envelope returned by Hyperliquid `/exchange`.
24
+ *
25
+ * A successful response may contain per-order statuses such as `filled`,
26
+ * `resting`, or `error`; the provider keeps the upstream status strings and
27
+ * numeric order IDs unchanged.
28
+ * @public
29
+ */
16
30
  export type HlExchangeResponse = {
17
31
  status: string
18
32
  response?:
@@ -34,28 +48,32 @@ export type HlExchangeResponse = {
34
48
  }
35
49
  }
36
50
 
37
- /** @public */
51
+ /** EIP-712 primary type for user approval of a Hyperliquid agent wallet. @public */
38
52
  export const HL_PRIMARY_TYPE_APPROVE_AGENT =
39
53
  'HyperliquidTransaction:ApproveAgent' as const
40
- /** @public */
54
+ /** EIP-712 primary type for user approval of an integrator builder fee. @public */
41
55
  export const HL_PRIMARY_TYPE_APPROVE_BUILDER_FEE =
42
56
  'HyperliquidTransaction:ApproveBuilderFee' as const
43
- /** @public */
57
+ /** EIP-712 primary type for changing the user's account abstraction mode. @public */
44
58
  export const HL_PRIMARY_TYPE_USER_SET_ABSTRACTION =
45
59
  'HyperliquidTransaction:UserSetAbstraction' as const
46
- /** @public */
60
+ /** EIP-712 primary type for an agent wallet changing abstraction mode. @public */
47
61
  export const HL_PRIMARY_TYPE_AGENT_SET_ABSTRACTION =
48
62
  'HyperliquidTransaction:AgentSetAbstraction' as const
49
- /** @public */
63
+ /** EIP-712 primary type for withdrawing assets from Hyperliquid. @public */
50
64
  export const HL_PRIMARY_TYPE_WITHDRAW =
51
65
  'HyperliquidTransaction:Withdraw' as const
52
- /** @public */
66
+ /** EIP-712 primary type for sending assets between Hyperliquid accounts or DEXes. @public */
53
67
  export const HL_PRIMARY_TYPE_SEND_ASSET =
54
68
  'HyperliquidTransaction:SendAsset' as const
55
- /** @public */
69
+ /** EIP-712 primary type used by the agent wallet authorization payload. @public */
56
70
  export const HL_PRIMARY_TYPE_AGENT = 'Agent' as const
57
71
 
58
- /** @public */
72
+ /**
73
+ * Union of EIP-712 primary type names supported by Hyperliquid signing actions.
74
+ * Values are the exact strings required in the typed-data domain.
75
+ * @public
76
+ */
59
77
  export type HlPrimaryType =
60
78
  | typeof HL_PRIMARY_TYPE_APPROVE_AGENT
61
79
  | typeof HL_PRIMARY_TYPE_APPROVE_BUILDER_FEE
@@ -1,20 +1,34 @@
1
1
  // Asset / market metadata returned by Hyperliquid `/info`.
2
2
 
3
- /** @public */
3
+ /**
4
+ * Perpetual market metadata from Hyperliquid `meta.universe`.
5
+ * `szDecimals` is the lot-size precision; `maxLeverage` is the venue cap.
6
+ * @public
7
+ */
4
8
  export type HlUniverseItem = {
5
9
  name: string
6
10
  szDecimals: number
7
11
  maxLeverage: number
8
12
  onlyIsolated?: boolean
13
+ /** Current replacement for deprecated `onlyIsolated`. */
14
+ marginMode?: 'strictIsolated' | 'noCross'
9
15
  isDelisted?: boolean
10
16
  }
11
17
 
12
- /** @public */
18
+ /**
19
+ * Perpetual metadata envelope returned by the Hyperliquid `meta` info query.
20
+ * @public
21
+ */
13
22
  export type HlMeta = {
14
23
  universe: HlUniverseItem[]
15
24
  }
16
25
 
17
- /** @public */
26
+ /**
27
+ * Live perpetual market context from Hyperliquid `metaAndAssetCtxs`.
28
+ * Prices, funding, open interest, and volume are decimal strings; `funding`
29
+ * is a rate and `dayNtlVlm` is 24-hour notional volume.
30
+ * @public
31
+ */
18
32
  export type HlAssetCtx = {
19
33
  funding: string
20
34
  openInterest: string
@@ -23,13 +37,21 @@ export type HlAssetCtx = {
23
37
  markPx: string
24
38
  }
25
39
 
26
- /** @public */
40
+ /**
41
+ * Tuple returned by `metaAndAssetCtxs`: static universe metadata followed by
42
+ * one live context per universe entry.
43
+ * @public
44
+ */
27
45
  export type HlMetaAndAssetCtxs = [HlMeta, HlAssetCtx[]]
28
46
 
29
- /** @public */
47
+ /** Alias for the perpetual universe array in {@link HlMeta}. @public */
30
48
  export type HlUniverse = HlMeta['universe']
31
49
 
32
- /** @public */
50
+ /**
51
+ * OHLCV candle from Hyperliquid's candle feed. `t` is the opening timestamp
52
+ * in milliseconds; prices and volume are decimal strings.
53
+ * @public
54
+ */
33
55
  export type HlCandle = {
34
56
  t: number
35
57
  o: string
@@ -39,26 +61,43 @@ export type HlCandle = {
39
61
  v: string
40
62
  }
41
63
 
42
- /** @public */
64
+ /** Snapshot array of {@link HlCandle} values, ordered by the upstream feed. @public */
43
65
  export type HlCandleSnapshot = HlCandle[]
44
66
 
45
- /** @public */
67
+ /**
68
+ * One L2 order-book level. `px` is price, `sz` is aggregate size, and `n` is
69
+ * the number of orders at that price; numeric values are wire strings except
70
+ * for the order count.
71
+ * @public
72
+ */
46
73
  export type HlLevel = {
47
74
  px: string
48
75
  sz: string
49
76
  n: number
50
77
  }
51
78
 
52
- /** @public */
79
+ /**
80
+ * L2 order-book snapshot with bid levels at index 0 and ask levels at index 1.
81
+ * `time` is the snapshot timestamp in milliseconds.
82
+ * @public
83
+ */
53
84
  export type HlL2Book = {
54
85
  levels: [HlLevel[], HlLevel[]]
55
86
  time: number
56
87
  }
57
88
 
58
- /** @public */
89
+ /**
90
+ * Perpetual DEX descriptors from Hyperliquid `perpDexs`; `null` represents the
91
+ * main DEX and named entries represent HIP-3 sub-DEXes.
92
+ * @public
93
+ */
59
94
  export type HlPerpDexs = (null | { name: string })[]
60
95
 
61
- /** @public */
96
+ /**
97
+ * Spot token metadata. `index` is the numeric token index used by spot
98
+ * clearinghouse balances; `tokenId` is the venue's wire identifier.
99
+ * @public
100
+ */
62
101
  export type HlSpotToken = {
63
102
  name: string
64
103
  index: number
@@ -66,7 +105,11 @@ export type HlSpotToken = {
66
105
  szDecimals: number
67
106
  }
68
107
 
69
- /** @public */
108
+ /**
109
+ * Spot universe entry describing a trading pair. `tokens` contains the two
110
+ * numeric token indexes used by the pair.
111
+ * @public
112
+ */
70
113
  export type HlSpotUniverseEntry = {
71
114
  name: string
72
115
  tokens: [number, number]
@@ -74,13 +117,17 @@ export type HlSpotUniverseEntry = {
74
117
  isCanonical: boolean
75
118
  }
76
119
 
77
- /** @public */
120
+ /** Spot metadata envelope containing token definitions and trading pairs. @public */
78
121
  export type HlSpotMeta = {
79
122
  tokens: HlSpotToken[]
80
123
  universe: HlSpotUniverseEntry[]
81
124
  }
82
125
 
83
- /** @public */
126
+ /**
127
+ * Live spot token context. Prices and 24-hour volume are decimal strings;
128
+ * `midPx` is `null` when the venue has no midpoint for the pair.
129
+ * @public
130
+ */
84
131
  export type HlSpotAssetCtx = {
85
132
  coin: string
86
133
  prevDayPx: string
@@ -89,5 +136,5 @@ export type HlSpotAssetCtx = {
89
136
  midPx: string | null
90
137
  }
91
138
 
92
- /** @public */
139
+ /** Tuple returned by the spot metadata/context info query. @public */
93
140
  export type HlSpotMetaAndAssetCtxs = [HlSpotMeta, HlSpotAssetCtx[]]
package/src/types/fill.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  // Fill shapes returned by Hyperliquid `/info`.
2
2
 
3
- /** @public */
3
+ /**
4
+ * One user trade fill returned by Hyperliquid `userFills` or
5
+ * `userFillsByTime`. Prices, size, fee, PnL, and starting position are decimal
6
+ * strings; `time` is milliseconds since the Unix epoch and `tid`/`oid` are
7
+ * numeric trade and order IDs.
8
+ * @public
9
+ */
4
10
  export type HlUserFill = {
5
11
  tid: number
6
12
  oid: number
@@ -17,8 +23,8 @@ export type HlUserFill = {
17
23
  startPosition: string
18
24
  }
19
25
 
20
- /** @public */
26
+ /** All fills returned by the unbounded `userFills` query. @public */
21
27
  export type HlUserFills = HlUserFill[]
22
28
 
23
- /** @public */
29
+ /** Fills returned by the time-bounded `userFillsByTime` query. @public */
24
30
  export type HlUserFillsByTime = HlUserFill[]