@nktkas/hyperliquid 0.13.1 → 0.14.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 (243) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +334 -107
  3. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  4. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  5. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.js +2 -0
  6. package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  7. package/esm/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  8. package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.js +1 -1
  9. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  10. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.js +2 -0
  12. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  13. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.js +1 -1
  14. package/{script/deps/jsr.io/@std/encoding/1.0.6 → esm/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  15. package/esm/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  16. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/hex.js +1 -1
  17. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  18. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  19. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +2 -0
  20. package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  21. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  22. package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.js +2 -2
  23. package/esm/mod.d.ts +4 -12
  24. package/esm/mod.d.ts.map +1 -1
  25. package/esm/mod.js +2 -2
  26. package/{script/src/transports → esm/src}/base.d.ts +10 -13
  27. package/esm/src/base.d.ts.map +1 -0
  28. package/esm/src/base.js +14 -0
  29. package/esm/src/clients/event.d.ts +59 -18
  30. package/esm/src/clients/event.d.ts.map +1 -1
  31. package/esm/src/clients/event.js +74 -18
  32. package/esm/src/clients/public.d.ts +348 -53
  33. package/esm/src/clients/public.d.ts.map +1 -1
  34. package/esm/src/clients/public.js +349 -50
  35. package/esm/src/clients/wallet.d.ts +345 -73
  36. package/esm/src/clients/wallet.d.ts.map +1 -1
  37. package/esm/src/clients/wallet.js +979 -229
  38. package/esm/src/signing.d.ts +135 -0
  39. package/esm/src/signing.d.ts.map +1 -0
  40. package/esm/src/signing.js +188 -0
  41. package/esm/src/transports/http/http_transport.d.ts +1 -1
  42. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  43. package/esm/src/transports/http/http_transport.js +1 -1
  44. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +64 -0
  45. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -0
  46. package/esm/src/transports/websocket/_hyperliquid_event_target.js +52 -0
  47. package/esm/src/transports/websocket/{reconnecting_websocket.d.ts → _reconnecting_websocket.d.ts} +26 -26
  48. package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -0
  49. package/esm/src/transports/websocket/{reconnecting_websocket.js → _reconnecting_websocket.js} +91 -76
  50. package/{script/src/transports/websocket/websocket_request_dispatcher.d.ts → esm/src/transports/websocket/_websocket_request_dispatcher.d.ts} +3 -7
  51. package/esm/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +1 -0
  52. package/esm/src/transports/websocket/{websocket_request_dispatcher.js → _websocket_request_dispatcher.js} +6 -10
  53. package/esm/src/transports/websocket/websocket_transport.d.ts +15 -21
  54. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  55. package/esm/src/transports/websocket/websocket_transport.js +55 -52
  56. package/esm/src/types/exchange/requests.d.ts +298 -79
  57. package/esm/src/types/exchange/requests.d.ts.map +1 -1
  58. package/esm/src/types/exchange/responses.d.ts +38 -38
  59. package/esm/src/types/exchange/responses.d.ts.map +1 -1
  60. package/esm/src/types/explorer/requests.d.ts +23 -10
  61. package/esm/src/types/explorer/requests.d.ts.map +1 -1
  62. package/esm/src/types/explorer/responses.d.ts +45 -3
  63. package/esm/src/types/explorer/responses.d.ts.map +1 -1
  64. package/esm/src/types/info/accounts.d.ts +279 -189
  65. package/esm/src/types/info/accounts.d.ts.map +1 -1
  66. package/esm/src/types/info/assets.d.ts +191 -185
  67. package/esm/src/types/info/assets.d.ts.map +1 -1
  68. package/esm/src/types/info/delegations.d.ts +117 -0
  69. package/esm/src/types/info/delegations.d.ts.map +1 -0
  70. package/esm/src/types/info/orders.d.ts +94 -94
  71. package/esm/src/types/info/orders.d.ts.map +1 -1
  72. package/esm/src/types/info/requests.d.ts +192 -64
  73. package/esm/src/types/info/requests.d.ts.map +1 -1
  74. package/esm/src/types/info/vaults.d.ts +42 -85
  75. package/esm/src/types/info/vaults.d.ts.map +1 -1
  76. package/esm/src/types/mod.d.ts +14 -0
  77. package/esm/src/types/mod.d.ts.map +1 -0
  78. package/esm/src/types/subscriptions/requests.d.ts +11 -1
  79. package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
  80. package/{script/src/types/subscriptions/common.d.ts → esm/src/types/subscriptions/responses.d.ts} +18 -3
  81. package/esm/src/types/subscriptions/responses.d.ts.map +1 -0
  82. package/package.json +20 -4
  83. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +13 -3
  84. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +23 -13
  85. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +55 -45
  86. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +97 -87
  87. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +14 -4
  88. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +288 -278
  89. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +238 -228
  90. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  91. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  92. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.js +13 -0
  93. package/script/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  94. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  95. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.js +45 -0
  96. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  97. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  98. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.js +13 -0
  99. package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  100. package/script/deps/jsr.io/@std/encoding/1.0.7/_validate_binary_like.js +39 -0
  101. package/{esm/deps/jsr.io/@std/encoding/1.0.6 → script/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  102. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  103. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.js +123 -0
  104. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  105. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  106. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +13 -0
  107. package/script/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  108. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  109. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +250 -0
  110. package/script/mod.d.ts +4 -12
  111. package/script/mod.d.ts.map +1 -1
  112. package/script/mod.js +21 -13
  113. package/{esm/src/transports → script/src}/base.d.ts +10 -13
  114. package/script/src/base.d.ts.map +1 -0
  115. package/script/src/base.js +29 -0
  116. package/script/src/clients/event.d.ts +59 -18
  117. package/script/src/clients/event.d.ts.map +1 -1
  118. package/script/src/clients/event.js +551 -485
  119. package/script/src/clients/public.d.ts +348 -53
  120. package/script/src/clients/public.d.ts.map +1 -1
  121. package/script/src/clients/public.js +1017 -708
  122. package/script/src/clients/wallet.d.ts +345 -73
  123. package/script/src/clients/wallet.d.ts.map +1 -1
  124. package/script/src/clients/wallet.js +1737 -977
  125. package/script/src/signing.d.ts +135 -0
  126. package/script/src/signing.d.ts.map +1 -0
  127. package/script/src/signing.js +203 -0
  128. package/script/src/transports/http/http_transport.d.ts +1 -1
  129. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  130. package/script/src/transports/http/http_transport.js +174 -164
  131. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +64 -0
  132. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -0
  133. package/script/src/transports/websocket/_hyperliquid_event_target.js +66 -0
  134. package/script/src/transports/websocket/{reconnecting_websocket.d.ts → _reconnecting_websocket.d.ts} +26 -26
  135. package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -0
  136. package/script/src/transports/websocket/_reconnecting_websocket.js +400 -0
  137. package/{esm/src/transports/websocket/websocket_request_dispatcher.d.ts → script/src/transports/websocket/_websocket_request_dispatcher.d.ts} +3 -7
  138. package/script/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +1 -0
  139. package/script/src/transports/websocket/_websocket_request_dispatcher.js +212 -0
  140. package/script/src/transports/websocket/websocket_transport.d.ts +15 -21
  141. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  142. package/script/src/transports/websocket/websocket_transport.js +235 -222
  143. package/script/src/types/exchange/requests.d.ts +298 -79
  144. package/script/src/types/exchange/requests.d.ts.map +1 -1
  145. package/script/src/types/exchange/requests.js +12 -2
  146. package/script/src/types/exchange/responses.d.ts +38 -38
  147. package/script/src/types/exchange/responses.d.ts.map +1 -1
  148. package/script/src/types/exchange/responses.js +12 -2
  149. package/script/src/types/explorer/requests.d.ts +23 -10
  150. package/script/src/types/explorer/requests.d.ts.map +1 -1
  151. package/script/src/types/explorer/requests.js +12 -2
  152. package/script/src/types/explorer/responses.d.ts +45 -3
  153. package/script/src/types/explorer/responses.d.ts.map +1 -1
  154. package/script/src/types/explorer/responses.js +12 -2
  155. package/script/src/types/info/accounts.d.ts +279 -189
  156. package/script/src/types/info/accounts.d.ts.map +1 -1
  157. package/script/src/types/info/accounts.js +12 -2
  158. package/script/src/types/info/assets.d.ts +191 -185
  159. package/script/src/types/info/assets.d.ts.map +1 -1
  160. package/script/src/types/info/assets.js +12 -2
  161. package/script/src/types/info/delegations.d.ts +117 -0
  162. package/script/src/types/info/delegations.d.ts.map +1 -0
  163. package/script/src/types/info/delegations.js +12 -0
  164. package/script/src/types/info/orders.d.ts +94 -94
  165. package/script/src/types/info/orders.d.ts.map +1 -1
  166. package/script/src/types/info/orders.js +12 -2
  167. package/script/src/types/info/requests.d.ts +192 -64
  168. package/script/src/types/info/requests.d.ts.map +1 -1
  169. package/script/src/types/info/requests.js +12 -2
  170. package/script/src/types/info/vaults.d.ts +42 -85
  171. package/script/src/types/info/vaults.d.ts.map +1 -1
  172. package/script/src/types/info/vaults.js +12 -2
  173. package/script/src/types/mod.d.ts +14 -0
  174. package/script/src/types/mod.d.ts.map +1 -0
  175. package/script/src/types/mod.js +12 -0
  176. package/script/src/types/subscriptions/requests.d.ts +11 -1
  177. package/script/src/types/subscriptions/requests.d.ts.map +1 -1
  178. package/script/src/types/subscriptions/requests.js +12 -2
  179. package/{esm/src/types/subscriptions/common.d.ts → script/src/types/subscriptions/responses.d.ts} +18 -3
  180. package/script/src/types/subscriptions/responses.d.ts.map +1 -0
  181. package/script/src/types/subscriptions/responses.js +12 -0
  182. package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  183. package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  184. package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  185. package/esm/src/transports/base.d.ts.map +0 -1
  186. package/esm/src/transports/base.js +0 -14
  187. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts +0 -66
  188. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +0 -1
  189. package/esm/src/transports/websocket/hyperliquid_event_target.js +0 -33
  190. package/esm/src/transports/websocket/reconnecting_websocket.d.ts.map +0 -1
  191. package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts.map +0 -1
  192. package/esm/src/types/common.d.ts +0 -3
  193. package/esm/src/types/common.d.ts.map +0 -1
  194. package/esm/src/types/exchange/common.d.ts +0 -36
  195. package/esm/src/types/exchange/common.d.ts.map +0 -1
  196. package/esm/src/types/explorer/common.d.ts +0 -37
  197. package/esm/src/types/explorer/common.d.ts.map +0 -1
  198. package/esm/src/types/subscriptions/common.d.ts.map +0 -1
  199. package/esm/src/types/subscriptions/common.js +0 -1
  200. package/esm/src/utils/key_sort.d.ts +0 -21
  201. package/esm/src/utils/key_sort.d.ts.map +0 -1
  202. package/esm/src/utils/key_sort.js +0 -124
  203. package/esm/src/utils/signing.d.ts +0 -109
  204. package/esm/src/utils/signing.d.ts.map +0 -1
  205. package/esm/src/utils/signing.js +0 -164
  206. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  207. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.js +0 -35
  208. package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +0 -29
  209. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  210. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.js +0 -113
  211. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  212. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.js +0 -240
  213. package/script/src/transports/base.d.ts.map +0 -1
  214. package/script/src/transports/base.js +0 -18
  215. package/script/src/transports/websocket/hyperliquid_event_target.d.ts +0 -66
  216. package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +0 -1
  217. package/script/src/transports/websocket/hyperliquid_event_target.js +0 -37
  218. package/script/src/transports/websocket/reconnecting_websocket.d.ts.map +0 -1
  219. package/script/src/transports/websocket/reconnecting_websocket.js +0 -374
  220. package/script/src/transports/websocket/websocket_request_dispatcher.d.ts.map +0 -1
  221. package/script/src/transports/websocket/websocket_request_dispatcher.js +0 -206
  222. package/script/src/types/common.d.ts +0 -3
  223. package/script/src/types/common.d.ts.map +0 -1
  224. package/script/src/types/common.js +0 -2
  225. package/script/src/types/exchange/common.d.ts +0 -36
  226. package/script/src/types/exchange/common.d.ts.map +0 -1
  227. package/script/src/types/exchange/common.js +0 -2
  228. package/script/src/types/explorer/common.d.ts +0 -37
  229. package/script/src/types/explorer/common.d.ts.map +0 -1
  230. package/script/src/types/explorer/common.js +0 -2
  231. package/script/src/types/subscriptions/common.d.ts.map +0 -1
  232. package/script/src/types/subscriptions/common.js +0 -2
  233. package/script/src/utils/key_sort.d.ts +0 -21
  234. package/script/src/utils/key_sort.d.ts.map +0 -1
  235. package/script/src/utils/key_sort.js +0 -127
  236. package/script/src/utils/signing.d.ts +0 -109
  237. package/script/src/utils/signing.d.ts.map +0 -1
  238. package/script/src/utils/signing.js +0 -172
  239. /package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
  240. /package/esm/src/types/{common.js → info/delegations.js} +0 -0
  241. /package/esm/src/types/{exchange/common.js → mod.js} +0 -0
  242. /package/esm/src/types/{explorer/common.js → subscriptions/responses.js} +0 -0
  243. /package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
@@ -1,17 +1,18 @@
1
- import { sorters } from "../utils/key_sort.js";
2
- import { signL1Action, signUserSignedAction, } from "../utils/signing.js";
3
- // ———————————————Errors———————————————
1
+ import { HyperliquidError } from "../base.js";
2
+ import { signL1Action, signUserSignedAction, } from "../signing.js";
3
+ // ——————————————— Errors ———————————————
4
4
  /** Error thrown when the API returns an error response. */
5
- export class ApiRequestError extends Error {
5
+ export class ApiRequestError extends HyperliquidError {
6
6
  constructor(response) {
7
7
  let message = "Cannot process API request";
8
8
  if (response.status === "err") {
9
+ // For ErrorResponse
9
10
  message += `: ${response.response}`;
10
11
  }
11
12
  else {
12
13
  if ("statuses" in response.response.data) {
13
- const errors = response.response.data.statuses
14
- .reduce((acc, status, index) => {
14
+ // For OrderResponse, CancelResponse
15
+ const errors = response.response.data.statuses.reduce((acc, status, index) => {
15
16
  if (typeof status === "object" && "error" in status) {
16
17
  acc.push(`Order ${index} failed: ${status.error}`);
17
18
  }
@@ -22,6 +23,7 @@ export class ApiRequestError extends Error {
22
23
  }
23
24
  }
24
25
  else {
26
+ // For TwapOrderResponse, TwapCancelResponse
25
27
  if (typeof response.response.data.status === "object" && "error" in response.response.data.status) {
26
28
  message += `: ${response.response.data.status.error}`;
27
29
  }
@@ -37,11 +39,11 @@ export class ApiRequestError extends Error {
37
39
  this.name = "ApiRequestError";
38
40
  }
39
41
  }
40
- // ———————————————Client———————————————
42
+ // ——————————————— Client ———————————————
41
43
  /**
42
44
  * Wallet client for interacting with the Hyperliquid API.
43
- * @typeParam T - The transport used to connect to the Hyperliquid API.
44
- * @typeParam W - The WalletClient/Account ([viem](https://viem.sh/docs/clients/wallet)) or Signer ([ethers.js](https://docs.ethers.io/v6/api/providers/#Signer)) used for signing transactions.
45
+ * @typeParam T The transport used to connect to the Hyperliquid API.
46
+ * @typeParam W The WalletClient/Account ([viem](https://viem.sh/docs/clients/wallet)) or Signer ([ethers.js](https://docs.ethers.io/v6/api/providers/#Signer)) used for signing transactions.
45
47
  */
46
48
  export class WalletClient {
47
49
  /**
@@ -59,7 +61,7 @@ export class WalletClient {
59
61
  * const client = new hl.WalletClient({ wallet, transport });
60
62
  * ```
61
63
  *
62
- * @example Private key via [ethers.js](https://docs.ethers.org/v6/api/wallet/#Wallet)
64
+ * @example Private key via [ethers.js](https://docs.ethers.org/v6/api/wallet/#Wallet) or [ethers.js v5](https://docs.ethers.org/v5/api/signer/#Wallet)
63
65
  * ```ts
64
66
  * import * as hl from "@nktkas/hyperliquid";
65
67
  * import { ethers } from "ethers";
@@ -74,14 +76,21 @@ export class WalletClient {
74
76
  * ```ts
75
77
  * import * as hl from "@nktkas/hyperliquid";
76
78
  * import { createWalletClient, custom } from "viem";
77
- * import { arbitrum } from "viem/chains";
78
79
  *
79
80
  * const [account] = await window.ethereum.request({ method: "eth_requestAccounts" });
80
- * const wallet = createWalletClient({ account, chain: arbitrum, transport: custom(window.ethereum) });
81
+ * const wallet = createWalletClient({ account, transport: custom(window.ethereum) });
81
82
  *
82
83
  * const transport = new hl.HttpTransport(); // or WebSocketTransport
83
84
  * const client = new hl.WalletClient({ wallet, transport });
84
85
  * ```
86
+ *
87
+ * @example External wallet (e.g. MetaMask) via `window.ethereum` directly
88
+ * ```ts
89
+ * import * as hl from "@nktkas/hyperliquid";
90
+ *
91
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
92
+ * const client = new hl.WalletClient({ wallet: window.ethereum, transport });
93
+ * ```
85
94
  */
86
95
  constructor(args) {
87
96
  /** The transport used to connect to the Hyperliquid API. */
@@ -91,7 +100,10 @@ export class WalletClient {
91
100
  writable: true,
92
101
  value: void 0
93
102
  });
94
- /** The WalletClient/Account ([viem](https://viem.sh/docs/clients/wallet)) or Signer ([ethers.js](https://docs.ethers.org/v6/api/providers/#Signer)) used for signing transactions. */
103
+ /**
104
+ * The [viem](https://viem.sh/docs/clients/wallet) or [ethers.js](https://docs.ethers.org/v6/api/providers/#Signer)
105
+ * used for signing transactions.
106
+ */
95
107
  Object.defineProperty(this, "wallet", {
96
108
  enumerable: true,
97
109
  configurable: true,
@@ -112,20 +124,31 @@ export class WalletClient {
112
124
  writable: true,
113
125
  value: void 0
114
126
  });
127
+ /**
128
+ * The network that will be used to sign transactions.
129
+ * Must match the network of the {@link wallet}.
130
+ */
131
+ Object.defineProperty(this, "signatureChainId", {
132
+ enumerable: true,
133
+ configurable: true,
134
+ writable: true,
135
+ value: void 0
136
+ });
115
137
  this.transport = args.transport;
116
138
  this.wallet = args.wallet;
117
139
  this.isTestnet = args.isTestnet ?? false;
118
140
  this.defaultVaultAddress = args.defaultVaultAddress;
141
+ this.signatureChainId = args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1");
119
142
  }
120
143
  // ———————————————Actions———————————————
121
144
  /**
122
145
  * Approve an agent to sign on behalf of the master or sub-accounts.
123
146
  * @param args - The parameters for the request.
124
147
  * @param signal - An optional abort signal
125
- * @returns {SuccessResponse} Successful response without specific data.
148
+ * @returns Successful response without specific data.
126
149
  * @throws {ApiRequestError} When the API returns an error response.
127
150
  *
128
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-an-api-wallet | Hyperliquid GitBook}
151
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-an-api-wallet
129
152
  * @example
130
153
  * ```ts
131
154
  * import * as hl from "@nktkas/hyperliquid";
@@ -139,29 +162,35 @@ export class WalletClient {
139
162
  * agentAddress: "0x...",
140
163
  * agentName: "agentName",
141
164
  * });
165
+ * ```
142
166
  */
143
167
  async approveAgent(args, signal) {
168
+ // Construct an action
144
169
  const action = {
145
170
  ...args,
146
171
  type: "approveAgent",
147
172
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
148
- signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
173
+ signatureChainId: this.signatureChainId,
149
174
  nonce: args.nonce ?? Date.now(),
150
175
  };
151
- const signature = await signUserSignedAction(this.wallet, action, {
152
- "HyperliquidTransaction:ApproveAgent": [
153
- { name: "hyperliquidChain", type: "string" },
154
- { name: "agentAddress", type: "address" },
155
- { name: "agentName", type: "string" },
156
- { name: "nonce", type: "uint64" },
157
- ],
158
- }, parseInt(action.signatureChainId, 16));
159
- const request = {
176
+ // Sign the action
177
+ const signature = await signUserSignedAction({
178
+ wallet: this.wallet,
160
179
  action,
161
- signature,
162
- nonce: action.nonce,
163
- };
180
+ types: {
181
+ "HyperliquidTransaction:ApproveAgent": [
182
+ { name: "hyperliquidChain", type: "string" },
183
+ { name: "agentAddress", type: "address" },
184
+ { name: "agentName", type: "string" },
185
+ { name: "nonce", type: "uint64" },
186
+ ],
187
+ },
188
+ chainId: parseInt(action.signatureChainId, 16),
189
+ });
190
+ // Send a request
191
+ const request = { action, signature, nonce: action.nonce };
164
192
  const response = await this.transport.request("action", request, signal);
193
+ // Validate a response
165
194
  this._validateResponse(response);
166
195
  return response;
167
196
  }
@@ -169,10 +198,10 @@ export class WalletClient {
169
198
  * Approve a max fee rate for a builder address.
170
199
  * @param args - The parameters for the request.
171
200
  * @param signal - An optional abort signal.
172
- * @returns {SuccessResponse} Successful response without specific data.
201
+ * @returns Successful response without specific data.
173
202
  * @throws {ApiRequestError} When the API returns an error response.
174
203
  *
175
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-a-builder-fee | Hyperliquid GitBook}
204
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-a-builder-fee
176
205
  * @example
177
206
  * ```ts
178
207
  * import * as hl from "@nktkas/hyperliquid";
@@ -186,29 +215,35 @@ export class WalletClient {
186
215
  * maxFeeRate: "0.01%",
187
216
  * builder: "0x...",
188
217
  * });
218
+ * ```
189
219
  */
190
220
  async approveBuilderFee(args, signal) {
221
+ // Construct an action
191
222
  const action = {
192
223
  ...args,
193
224
  type: "approveBuilderFee",
194
225
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
195
- signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
226
+ signatureChainId: this.signatureChainId,
196
227
  nonce: args.nonce ?? Date.now(),
197
228
  };
198
- const signature = await signUserSignedAction(this.wallet, action, {
199
- "HyperliquidTransaction:ApproveBuilderFee": [
200
- { name: "hyperliquidChain", type: "string" },
201
- { name: "maxFeeRate", type: "string" },
202
- { name: "builder", type: "address" },
203
- { name: "nonce", type: "uint64" },
204
- ],
205
- }, parseInt(action.signatureChainId, 16));
206
- const request = {
229
+ // Sign the action
230
+ const signature = await signUserSignedAction({
231
+ wallet: this.wallet,
207
232
  action,
208
- signature,
209
- nonce: action.nonce,
210
- };
233
+ types: {
234
+ "HyperliquidTransaction:ApproveBuilderFee": [
235
+ { name: "hyperliquidChain", type: "string" },
236
+ { name: "maxFeeRate", type: "string" },
237
+ { name: "builder", type: "address" },
238
+ { name: "nonce", type: "uint64" },
239
+ ],
240
+ },
241
+ chainId: parseInt(action.signatureChainId, 16),
242
+ });
243
+ // Send a request
244
+ const request = { action, signature, nonce: action.nonce };
211
245
  const response = await this.transport.request("action", request, signal);
246
+ // Validate a response
212
247
  this._validateResponse(response);
213
248
  return response;
214
249
  }
@@ -216,10 +251,10 @@ export class WalletClient {
216
251
  * Modify multiple orders.
217
252
  * @param args - The parameters for the request.
218
253
  * @param signal - An optional abort signal.
219
- * @returns {OrderResponseSuccess} Successful variant of {@link OrderResponse} without error statuses.
254
+ * @returns Successful variant of {@link OrderResponse} without error statuses.
220
255
  * @throws {ApiRequestError} When the API returns an error response.
221
256
  *
222
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders | Hyperliquid GitBook}
257
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders
223
258
  * @example
224
259
  * ```ts
225
260
  * import * as hl from "@nktkas/hyperliquid";
@@ -250,16 +285,53 @@ export class WalletClient {
250
285
  * ```
251
286
  */
252
287
  async batchModify(args, signal) {
288
+ // Destructure the parameters
253
289
  const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
254
- const sortedAction = sorters.batchModify({ type: "batchModify", ...actionArgs });
255
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
256
- const request = {
257
- action: sortedAction,
258
- signature,
290
+ // Construct an action
291
+ const action = {
292
+ type: "batchModify",
293
+ modifies: actionArgs.modifies.map((modify) => {
294
+ const sortedModify = {
295
+ oid: modify.oid,
296
+ order: {
297
+ a: modify.order.a,
298
+ b: modify.order.b,
299
+ p: modify.order.p,
300
+ s: modify.order.s,
301
+ r: modify.order.r,
302
+ t: "limit" in modify.order.t
303
+ ? {
304
+ limit: {
305
+ tif: modify.order.t.limit.tif,
306
+ },
307
+ }
308
+ : {
309
+ trigger: {
310
+ isMarket: modify.order.t.trigger.isMarket,
311
+ triggerPx: modify.order.t.trigger.triggerPx,
312
+ tpsl: modify.order.t.trigger.tpsl,
313
+ },
314
+ },
315
+ c: modify.order.c,
316
+ },
317
+ };
318
+ if (sortedModify.order.c === undefined)
319
+ delete sortedModify.order.c;
320
+ return sortedModify;
321
+ }),
322
+ };
323
+ // Sign the action
324
+ const signature = await signL1Action({
325
+ wallet: this.wallet,
326
+ action,
259
327
  nonce,
328
+ isTestnet: this.isTestnet,
260
329
  vaultAddress,
261
- };
330
+ });
331
+ // Send a request
332
+ const request = { action, signature, nonce, vaultAddress };
262
333
  const response = await this.transport.request("action", request, signal);
334
+ // Validate a response
263
335
  this._validateResponse(response);
264
336
  return response;
265
337
  }
@@ -267,10 +339,10 @@ export class WalletClient {
267
339
  * Cancel order(s).
268
340
  * @param args - The parameters for the request.
269
341
  * @param signal - An optional abort signal.
270
- * @returns {CancelResponseSuccess} Successful variant of {@link CancelResponse} without error statuses.
342
+ * @returns Successful variant of {@link CancelResponse} without error statuses.
271
343
  * @throws {ApiRequestError} When the API returns an error response.
272
344
  *
273
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s | Hyperliquid GitBook}
345
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
274
346
  * @example
275
347
  * ```ts
276
348
  * import * as hl from "@nktkas/hyperliquid";
@@ -289,27 +361,127 @@ export class WalletClient {
289
361
  * ```
290
362
  */
291
363
  async cancel(args, signal) {
364
+ // Destructure the parameters
292
365
  const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
293
- const sortedAction = sorters.cancel({ type: "cancel", ...actionArgs });
294
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
295
- const request = {
296
- action: sortedAction,
297
- signature,
366
+ // Construct an action
367
+ const action = {
368
+ type: "cancel",
369
+ cancels: actionArgs.cancels.map((cancel) => ({
370
+ a: cancel.a,
371
+ o: cancel.o,
372
+ })),
373
+ };
374
+ // Sign the action
375
+ const signature = await signL1Action({
376
+ wallet: this.wallet,
377
+ action,
298
378
  nonce,
379
+ isTestnet: this.isTestnet,
299
380
  vaultAddress,
381
+ });
382
+ // Send a request
383
+ const request = { action, signature, nonce, vaultAddress };
384
+ const response = await this.transport.request("action", request, signal);
385
+ // Validate a response
386
+ this._validateResponse(response);
387
+ return response;
388
+ }
389
+ /**
390
+ * Deposit into staking balance.
391
+ * @param args - The parameters for the request.
392
+ * @param signal - An optional abort signal.
393
+ * @returns Successful response without specific data.
394
+ * @throws {ApiRequestError} When the API returns an error response.
395
+ *
396
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-into-staking
397
+ * @example
398
+ * ```ts
399
+ * import * as hl from "@nktkas/hyperliquid";
400
+ * import { privateKeyToAccount } from "viem/accounts";
401
+ *
402
+ * const wallet = privateKeyToAccount("0x...");
403
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
404
+ * const client = new hl.WalletClient({ wallet, transport });
405
+ *
406
+ * const result = await client.cDeposit({ wei: 1 * 1e8 });
407
+ * ```
408
+ */
409
+ async cDeposit(args, signal) {
410
+ // Construct an action
411
+ const action = {
412
+ ...args,
413
+ type: "cDeposit",
414
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
415
+ signatureChainId: this.signatureChainId,
416
+ nonce: args.nonce ?? Date.now(),
300
417
  };
418
+ // Sign the action
419
+ const signature = await signUserSignedAction({
420
+ wallet: this.wallet,
421
+ action,
422
+ types: {
423
+ "HyperliquidTransaction:CDeposit": [
424
+ { name: "hyperliquidChain", type: "string" },
425
+ { name: "wei", type: "uint64" },
426
+ { name: "nonce", type: "uint64" },
427
+ ],
428
+ },
429
+ chainId: parseInt(action.signatureChainId, 16),
430
+ });
431
+ // Send a request
432
+ const request = { action, signature, nonce: action.nonce };
301
433
  const response = await this.transport.request("action", request, signal);
434
+ // Validate a response
302
435
  this._validateResponse(response);
303
436
  return response;
304
437
  }
305
438
  /**
306
- * Cancel order(s) by Client Order ID.
439
+ * Claim rewards from referral program.
307
440
  * @param args - The parameters for the request.
308
441
  * @param signal - An optional abort signal.
309
- * @returns {CancelResponseSuccess} Successful variant of {@link CancelResponse} without error statuses.
442
+ * @returns Successful response without specific data.
310
443
  * @throws {ApiRequestError} When the API returns an error response.
311
444
  *
312
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid | Hyperliquid GitBook}
445
+ * @see null - no documentation
446
+ * @example
447
+ * ```ts
448
+ * import * as hl from "@nktkas/hyperliquid";
449
+ * import { privateKeyToAccount } from "viem/accounts";
450
+ *
451
+ * const wallet = privateKeyToAccount("0x...");
452
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
453
+ * const client = new hl.WalletClient({ wallet, transport });
454
+ *
455
+ * const result = await client.claimRewards();
456
+ * ```
457
+ */
458
+ async claimRewards(args = {}, signal) {
459
+ // Destructure the parameters
460
+ const { nonce = Date.now() } = args;
461
+ // Construct an action
462
+ const sortedAction = { type: "claimRewards" };
463
+ // Sign the action
464
+ const signature = await signL1Action({
465
+ wallet: this.wallet,
466
+ action: sortedAction,
467
+ nonce,
468
+ isTestnet: this.isTestnet,
469
+ });
470
+ // Send a request
471
+ const request = { action: sortedAction, signature, nonce };
472
+ const response = await this.transport.request("action", request, signal);
473
+ // Validate a response
474
+ this._validateResponse(response);
475
+ return response;
476
+ }
477
+ /**
478
+ * Cancel order(s) by cloid.
479
+ * @param args - The parameters for the request.
480
+ * @param signal - An optional abort signal.
481
+ * @returns Successful variant of {@link CancelResponse} without error statuses.
482
+ * @throws {ApiRequestError} When the API returns an error response.
483
+ *
484
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid
313
485
  * @example
314
486
  * ```ts
315
487
  * import * as hl from "@nktkas/hyperliquid";
@@ -328,16 +500,119 @@ export class WalletClient {
328
500
  * ```
329
501
  */
330
502
  async cancelByCloid(args, signal) {
503
+ // Destructure the parameters
331
504
  const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
332
- const sortedAction = sorters.cancelByCloid({ type: "cancelByCloid", ...actionArgs });
333
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
334
- const request = {
335
- action: sortedAction,
336
- signature,
505
+ // Construct an action
506
+ const action = {
507
+ type: "cancelByCloid",
508
+ cancels: actionArgs.cancels.map((cancel) => ({
509
+ asset: cancel.asset,
510
+ cloid: cancel.cloid,
511
+ })),
512
+ };
513
+ // Sign the action
514
+ const signature = await signL1Action({
515
+ wallet: this.wallet,
516
+ action,
337
517
  nonce,
518
+ isTestnet: this.isTestnet,
338
519
  vaultAddress,
520
+ });
521
+ // Send a request
522
+ const request = { action, signature, nonce, vaultAddress };
523
+ const response = await this.transport.request("action", request, signal);
524
+ // Validate a response
525
+ this._validateResponse(response);
526
+ return response;
527
+ }
528
+ /**
529
+ * Withdraw from staking balance.
530
+ * @param args - The parameters for the request.
531
+ * @param signal - An optional abort signal.
532
+ * @returns Successful response without specific data.
533
+ * @throws {ApiRequestError} When the API returns an error response.
534
+ *
535
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#withdraw-from-staking
536
+ * @example
537
+ * ```ts
538
+ * import * as hl from "@nktkas/hyperliquid";
539
+ * import { privateKeyToAccount } from "viem/accounts";
540
+ *
541
+ * const wallet = privateKeyToAccount("0x...");
542
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
543
+ * const client = new hl.WalletClient({ wallet, transport });
544
+ *
545
+ * const result = await client.cWithdraw({ wei: 1 * 1e8 });
546
+ * ```
547
+ */
548
+ async cWithdraw(args, signal) {
549
+ // Construct an action
550
+ const action = {
551
+ ...args,
552
+ type: "cWithdraw",
553
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
554
+ signatureChainId: this.signatureChainId,
555
+ nonce: args.nonce ?? Date.now(),
339
556
  };
557
+ // Sign the action
558
+ const signature = await signUserSignedAction({
559
+ wallet: this.wallet,
560
+ action,
561
+ types: {
562
+ "HyperliquidTransaction:CWithdraw": [
563
+ { name: "hyperliquidChain", type: "string" },
564
+ { name: "wei", type: "uint64" },
565
+ { name: "nonce", type: "uint64" },
566
+ ],
567
+ },
568
+ chainId: parseInt(action.signatureChainId, 16),
569
+ });
570
+ // Send a request
571
+ const request = { action, signature, nonce: action.nonce };
340
572
  const response = await this.transport.request("action", request, signal);
573
+ // Validate a response
574
+ this._validateResponse(response);
575
+ return response;
576
+ }
577
+ /**
578
+ * Configure block type for EVM transactions.
579
+ * @param args - The parameters for the request.
580
+ * @param signal - An optional abort signal.
581
+ * @returns Response for creating a sub-account.
582
+ * @throws {ApiRequestError} When the API returns an error response.
583
+ *
584
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/evm/dual-block-architecture
585
+ * @example
586
+ * ```ts
587
+ * import * as hl from "@nktkas/hyperliquid";
588
+ * import { privateKeyToAccount } from "viem/accounts";
589
+ *
590
+ * const wallet = privateKeyToAccount("0x...");
591
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
592
+ * const client = new hl.WalletClient({ wallet, transport });
593
+ *
594
+ * const result = await client.evmUserModify({ usingBigBlocks: true });
595
+ * ```
596
+ */
597
+ async evmUserModify(args, signal) {
598
+ // Destructure the parameters
599
+ const { nonce = Date.now(), ...actionArgs } = args;
600
+ // Construct an action
601
+ const action = {
602
+ type: "evmUserModify",
603
+ usingBigBlocks: actionArgs.usingBigBlocks,
604
+ };
605
+ // Sign the action
606
+ const signature = await signL1Action({
607
+ wallet: this.wallet,
608
+ action,
609
+ nonce,
610
+ isTestnet: this.isTestnet,
611
+ });
612
+ // Send a request
613
+ const request = { action, signature, nonce };
614
+ const response = await this.transport.request("action", request, signal);
615
+ // Validate a response
341
616
  this._validateResponse(response);
342
617
  return response;
343
618
  }
@@ -345,9 +620,10 @@ export class WalletClient {
345
620
  * Create a sub-account.
346
621
  * @param args - The parameters for the request.
347
622
  * @param signal - An optional abort signal.
348
- * @returns {CreateSubAccountResponse} Response for creating a sub-account.
623
+ * @returns Response for creating a sub-account.
349
624
  * @throws {ApiRequestError} When the API returns an error response.
350
625
  *
626
+ * @see null - no documentation
351
627
  * @example
352
628
  * ```ts
353
629
  * import * as hl from "@nktkas/hyperliquid";
@@ -357,21 +633,28 @@ export class WalletClient {
357
633
  * const transport = new hl.HttpTransport(); // or WebSocketTransport
358
634
  * const client = new hl.WalletClient({ wallet, transport });
359
635
  *
360
- * const result = await client.createSubAccount({
361
- * name: "subAccountName",
362
- * });
636
+ * const result = await client.createSubAccount({ name: "subAccountName" });
363
637
  * ```
364
638
  */
365
639
  async createSubAccount(args, signal) {
640
+ // Destructure the parameters
366
641
  const { nonce = Date.now(), ...actionArgs } = args;
367
- const sortedAction = sorters.createSubAccount({ type: "createSubAccount", ...actionArgs });
368
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce);
369
- const request = {
370
- action: sortedAction,
371
- signature,
372
- nonce,
642
+ // Construct an action
643
+ const action = {
644
+ type: "createSubAccount",
645
+ name: actionArgs.name,
373
646
  };
647
+ // Sign the action
648
+ const signature = await signL1Action({
649
+ wallet: this.wallet,
650
+ action,
651
+ nonce,
652
+ isTestnet: this.isTestnet,
653
+ });
654
+ // Send a request
655
+ const request = { action, signature, nonce };
374
656
  const response = await this.transport.request("action", request, signal);
657
+ // Validate a response
375
658
  this._validateResponse(response);
376
659
  return response;
377
660
  }
@@ -379,10 +662,10 @@ export class WalletClient {
379
662
  * Modify an order.
380
663
  * @param args - The parameters for the request.
381
664
  * @param signal - An optional abort signal.
382
- * @returns {SuccessResponse} Successful response without specific data.
665
+ * @returns Successful response without specific data.
383
666
  * @throws {ApiRequestError} When the API returns an error response.
384
667
  *
385
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-an-order | Hyperliquid GitBook}
668
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-an-order
386
669
  * @example
387
670
  * ```ts
388
671
  * import * as hl from "@nktkas/hyperliquid";
@@ -411,16 +694,48 @@ export class WalletClient {
411
694
  * ```
412
695
  */
413
696
  async modify(args, signal) {
697
+ // Destructure the parameters
414
698
  const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
415
- const sortedAction = sorters.modify({ type: "modify", ...actionArgs });
416
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
417
- const request = {
418
- action: sortedAction,
419
- signature,
699
+ // Construct an action
700
+ const action = {
701
+ type: "modify",
702
+ oid: actionArgs.oid,
703
+ order: {
704
+ a: actionArgs.order.a,
705
+ b: actionArgs.order.b,
706
+ p: actionArgs.order.p,
707
+ s: actionArgs.order.s,
708
+ r: actionArgs.order.r,
709
+ t: "limit" in actionArgs.order.t
710
+ ? {
711
+ limit: {
712
+ tif: actionArgs.order.t.limit.tif,
713
+ },
714
+ }
715
+ : {
716
+ trigger: {
717
+ isMarket: actionArgs.order.t.trigger.isMarket,
718
+ triggerPx: actionArgs.order.t.trigger.triggerPx,
719
+ tpsl: actionArgs.order.t.trigger.tpsl,
720
+ },
721
+ },
722
+ c: actionArgs.order.c,
723
+ },
724
+ };
725
+ if (action.order.c === undefined)
726
+ delete action.order.c;
727
+ // Sign the action
728
+ const signature = await signL1Action({
729
+ wallet: this.wallet,
730
+ action,
420
731
  nonce,
732
+ isTestnet: this.isTestnet,
421
733
  vaultAddress,
422
- };
734
+ });
735
+ // Send a request
736
+ const request = { action, signature, nonce, vaultAddress };
423
737
  const response = await this.transport.request("action", request, signal);
738
+ // Validate a response
424
739
  this._validateResponse(response);
425
740
  return response;
426
741
  }
@@ -428,10 +743,10 @@ export class WalletClient {
428
743
  * Place an order(s).
429
744
  * @param args - The parameters for the request.
430
745
  * @param signal - An optional abort signal.
431
- * @returns {OrderResponseSuccess} Successful variant of {@link OrderResponse} without error statuses.
746
+ * @returns Successful variant of {@link OrderResponse} without error statuses.
432
747
  * @throws {ApiRequestError} When the API returns an error response.
433
748
  *
434
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order | Hyperliquid GitBook}
749
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order
435
750
  * @example
436
751
  * ```ts
437
752
  * import * as hl from "@nktkas/hyperliquid";
@@ -460,19 +775,59 @@ export class WalletClient {
460
775
  * ```
461
776
  */
462
777
  async order(args, signal) {
463
- const clonedArgs = structuredClone(args); // Clone to prevent mutation of original object
464
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = clonedArgs;
465
- if (actionArgs.builder)
466
- actionArgs.builder.b = actionArgs.builder.b.toLowerCase();
467
- const sortedAction = sorters.order({ type: "order", ...actionArgs });
468
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
469
- const request = {
470
- action: sortedAction,
471
- signature,
778
+ // Destructure the parameters
779
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
780
+ // Construct an action
781
+ const action = {
782
+ type: "order",
783
+ orders: actionArgs.orders.map((order) => {
784
+ const sortedOrder = {
785
+ a: order.a,
786
+ b: order.b,
787
+ p: order.p,
788
+ s: order.s,
789
+ r: order.r,
790
+ t: "limit" in order.t
791
+ ? {
792
+ limit: {
793
+ tif: order.t.limit.tif,
794
+ },
795
+ }
796
+ : {
797
+ trigger: {
798
+ isMarket: order.t.trigger.isMarket,
799
+ triggerPx: order.t.trigger.triggerPx,
800
+ tpsl: order.t.trigger.tpsl,
801
+ },
802
+ },
803
+ c: order.c,
804
+ };
805
+ if (order.c === undefined)
806
+ delete sortedOrder.c;
807
+ return sortedOrder;
808
+ }),
809
+ grouping: actionArgs.grouping,
810
+ builder: actionArgs.builder
811
+ ? {
812
+ b: actionArgs.builder.b.toLowerCase(),
813
+ f: actionArgs.builder.f,
814
+ }
815
+ : actionArgs.builder,
816
+ };
817
+ if (action.builder === undefined)
818
+ delete action.builder;
819
+ // Sign the action
820
+ const signature = await signL1Action({
821
+ wallet: this.wallet,
822
+ action,
472
823
  nonce,
824
+ isTestnet: this.isTestnet,
473
825
  vaultAddress,
474
- };
826
+ });
827
+ // Send a request
828
+ const request = { action, signature, nonce, vaultAddress };
475
829
  const response = await this.transport.request("action", request, signal);
830
+ // Validate a response
476
831
  this._validateResponse(response);
477
832
  return response;
478
833
  }
@@ -480,10 +835,10 @@ export class WalletClient {
480
835
  * Schedule a time to cancel all open orders.
481
836
  * @param args - The parameters for the request.
482
837
  * @param signal - An optional abort signal.
483
- * @returns {SuccessResponse} Successful response without specific data.
838
+ * @returns Successful response without specific data.
484
839
  * @throws {ApiRequestError} When the API returns an error response.
485
840
  *
486
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#schedule-cancel-dead-mans-switch | Hyperliquid GitBook}
841
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#schedule-cancel-dead-mans-switch
487
842
  * @example
488
843
  * ```ts
489
844
  * import * as hl from "@nktkas/hyperliquid";
@@ -493,22 +848,73 @@ export class WalletClient {
493
848
  * const transport = new hl.HttpTransport(); // or WebSocketTransport
494
849
  * const client = new hl.WalletClient({ wallet, transport });
495
850
  *
496
- * const result = await client.scheduleCancel({
497
- * time: Date.now() + 3600000, // Schedule cancellation 1 hour from now
498
- * });
851
+ * const result = await client.scheduleCancel({ time: Date.now() + 3600000 });
499
852
  * ```
500
853
  */
501
854
  async scheduleCancel(args = {}, signal) {
855
+ // Destructure the parameters
502
856
  const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
503
- const sortedAction = sorters.scheduleCancel({ type: "scheduleCancel", ...actionArgs });
504
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
505
- const request = {
506
- action: sortedAction,
507
- signature,
857
+ // Construct an action
858
+ const action = {
859
+ type: "scheduleCancel",
860
+ time: actionArgs.time,
861
+ };
862
+ if (action.time === undefined)
863
+ delete action.time;
864
+ // Sign the action
865
+ const signature = await signL1Action({
866
+ wallet: this.wallet,
867
+ action,
508
868
  nonce,
869
+ isTestnet: this.isTestnet,
509
870
  vaultAddress,
871
+ });
872
+ // Send a request
873
+ const request = { action, signature, nonce, vaultAddress };
874
+ const response = await this.transport.request("action", request, signal);
875
+ // Validate a response
876
+ this._validateResponse(response);
877
+ return response;
878
+ }
879
+ /**
880
+ * Set the display name in the leaderboard.
881
+ * @param args - The parameters for the request.
882
+ * @param signal - An optional abort signal.
883
+ * @returns Successful response without specific data.
884
+ * @throws {ApiRequestError} When the API returns an error response.
885
+ *
886
+ * @see null - no documentation
887
+ * @example
888
+ * ```ts
889
+ * import * as hl from "@nktkas/hyperliquid";
890
+ * import { privateKeyToAccount } from "viem/accounts";
891
+ *
892
+ * const wallet = privateKeyToAccount("0x...");
893
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
894
+ * const client = new hl.WalletClient({ wallet, transport });
895
+ *
896
+ * const result = await client.setDisplayName({ displayName: "My Name" });
897
+ * ```
898
+ */
899
+ async setDisplayName(args, signal) {
900
+ // Destructure the parameters
901
+ const { nonce = Date.now(), ...actionArgs } = args;
902
+ // Construct an action
903
+ const action = {
904
+ type: "setDisplayName",
905
+ displayName: actionArgs.displayName,
510
906
  };
907
+ // Sign the action
908
+ const signature = await signL1Action({
909
+ wallet: this.wallet,
910
+ action,
911
+ nonce,
912
+ isTestnet: this.isTestnet,
913
+ });
914
+ // Send a request
915
+ const request = { action, signature, nonce };
511
916
  const response = await this.transport.request("action", request, signal);
917
+ // Validate a response
512
918
  this._validateResponse(response);
513
919
  return response;
514
920
  }
@@ -516,9 +922,10 @@ export class WalletClient {
516
922
  * Set a referral code.
517
923
  * @param args - The parameters for the request.
518
924
  * @param signal - An optional abort signal.
519
- * @returns {SuccessResponse} Successful response without specific data.
925
+ * @returns Successful response without specific data.
520
926
  * @throws {ApiRequestError} When the API returns an error response.
521
927
  *
928
+ * @see null - no documentation
522
929
  * @example
523
930
  * ```ts
524
931
  * import * as hl from "@nktkas/hyperliquid";
@@ -528,21 +935,28 @@ export class WalletClient {
528
935
  * const transport = new hl.HttpTransport(); // or WebSocketTransport
529
936
  * const client = new hl.WalletClient({ wallet, transport });
530
937
  *
531
- * const result = await client.setReferrer({
532
- * code: "TEST",
533
- * });
938
+ * const result = await client.setReferrer({ code: "TEST" });
534
939
  * ```
535
940
  */
536
941
  async setReferrer(args, signal) {
942
+ // Destructure the parameters
537
943
  const { nonce = Date.now(), ...actionArgs } = args;
538
- const sortedAction = sorters.setReferrer({ type: "setReferrer", ...actionArgs });
539
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce);
540
- const request = {
541
- action: sortedAction,
542
- signature,
543
- nonce,
944
+ // Construct an action
945
+ const action = {
946
+ type: "setReferrer",
947
+ code: actionArgs.code,
544
948
  };
949
+ // Sign the action
950
+ const signature = await signL1Action({
951
+ wallet: this.wallet,
952
+ action,
953
+ nonce,
954
+ isTestnet: this.isTestnet,
955
+ });
956
+ // Send a request
957
+ const request = { action, signature, nonce };
545
958
  const response = await this.transport.request("action", request, signal);
959
+ // Validate a response
546
960
  this._validateResponse(response);
547
961
  return response;
548
962
  }
@@ -550,10 +964,10 @@ export class WalletClient {
550
964
  * Transfer a spot asset on L1 to another address.
551
965
  * @param args - The parameters for the request.
552
966
  * @param signal - An optional abort signal.
553
- * @returns {SuccessResponse} Successful response without specific data.
967
+ * @returns Successful response without specific data.
554
968
  * @throws {ApiRequestError} When the API returns an error response.
555
969
  *
556
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#l1-spot-transfer | Hyperliquid GitBook}
970
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#l1-spot-transfer
557
971
  * @example
558
972
  * ```ts
559
973
  * import * as hl from "@nktkas/hyperliquid";
@@ -571,38 +985,140 @@ export class WalletClient {
571
985
  * ```
572
986
  */
573
987
  async spotSend(args, signal) {
988
+ // Construct an action
574
989
  const action = {
575
990
  ...args,
576
991
  type: "spotSend",
577
992
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
578
- signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
993
+ signatureChainId: this.signatureChainId,
579
994
  time: args.time ?? Date.now(),
580
995
  };
581
- const signature = await signUserSignedAction(this.wallet, action, {
582
- "HyperliquidTransaction:SpotSend": [
583
- { name: "hyperliquidChain", type: "string" },
584
- { name: "destination", type: "string" },
585
- { name: "token", type: "string" },
586
- { name: "amount", type: "string" },
587
- { name: "time", type: "uint64" },
588
- ],
589
- }, parseInt(action.signatureChainId, 16));
590
- const request = {
996
+ // Sign the action
997
+ const signature = await signUserSignedAction({
998
+ wallet: this.wallet,
591
999
  action,
592
- signature,
593
- nonce: action.time,
1000
+ types: {
1001
+ "HyperliquidTransaction:SpotSend": [
1002
+ { name: "hyperliquidChain", type: "string" },
1003
+ { name: "destination", type: "string" },
1004
+ { name: "token", type: "string" },
1005
+ { name: "amount", type: "string" },
1006
+ { name: "time", type: "uint64" },
1007
+ ],
1008
+ },
1009
+ chainId: parseInt(action.signatureChainId, 16),
1010
+ });
1011
+ // Send a request
1012
+ const request = { action, signature, nonce: action.time };
1013
+ const response = await this.transport.request("action", request, signal);
1014
+ // Validate a response
1015
+ this._validateResponse(response);
1016
+ return response;
1017
+ }
1018
+ /**
1019
+ * Opt Out of Spot Dusting.
1020
+ * @param args - The parameters for the request.
1021
+ * @param signal - An optional abort signal.
1022
+ * @returns Successful response without specific data.
1023
+ * @throws {ApiRequestError} When the API returns an error response.
1024
+ *
1025
+ * @see null - no documentation
1026
+ * @example
1027
+ * ```ts
1028
+ * import * as hl from "@nktkas/hyperliquid";
1029
+ * import { privateKeyToAccount } from "viem/accounts";
1030
+ *
1031
+ * const wallet = privateKeyToAccount("0x...");
1032
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
1033
+ * const client = new hl.WalletClient({ wallet, transport });
1034
+ *
1035
+ * const result = await client.spotUser({
1036
+ * toggleSpotDusting: { optOut: false },
1037
+ * });
1038
+ * ```
1039
+ */
1040
+ async spotUser(args, signal) {
1041
+ // Destructure the parameters
1042
+ const { nonce = Date.now(), ...actionArgs } = args;
1043
+ // Construct an action
1044
+ const action = {
1045
+ type: "spotUser",
1046
+ toggleSpotDusting: {
1047
+ optOut: actionArgs.toggleSpotDusting.optOut,
1048
+ },
1049
+ };
1050
+ // Sign the action
1051
+ const signature = await signL1Action({
1052
+ wallet: this.wallet,
1053
+ action,
1054
+ nonce,
1055
+ isTestnet: this.isTestnet,
1056
+ });
1057
+ // Send a request
1058
+ const request = { action, signature, nonce };
1059
+ const response = await this.transport.request("action", request, signal);
1060
+ // Validate a response
1061
+ this._validateResponse(response);
1062
+ return response;
1063
+ }
1064
+ /**
1065
+ * Transfer between sub-accounts (spot).
1066
+ * @param args - The parameters for the request.
1067
+ * @param signal - An optional abort signal.
1068
+ * @returns Successful response without specific data.
1069
+ * @throws {ApiRequestError} When the API returns an error response.
1070
+ *
1071
+ * @see null - no documentation
1072
+ * @example
1073
+ * ```ts
1074
+ * import * as hl from "@nktkas/hyperliquid";
1075
+ * import { privateKeyToAccount } from "viem/accounts";
1076
+ *
1077
+ * const wallet = privateKeyToAccount("0x...");
1078
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
1079
+ * const client = new hl.WalletClient({ wallet, transport });
1080
+ *
1081
+ * const result = await client.subAccountSpotTransfer({
1082
+ * subAccountUser: "0x...",
1083
+ * isDeposit: true,
1084
+ * token: "USDC:0xeb62eee3685fc4c43992febcd9e75443",
1085
+ * amount: "1",
1086
+ * });
1087
+ * ```
1088
+ */
1089
+ async subAccountSpotTransfer(args, signal) {
1090
+ // Destructure the parameters
1091
+ const { nonce = Date.now(), ...actionArgs } = args;
1092
+ // Construct an action
1093
+ const action = {
1094
+ type: "subAccountSpotTransfer",
1095
+ subAccountUser: actionArgs.subAccountUser,
1096
+ isDeposit: actionArgs.isDeposit,
1097
+ token: actionArgs.token,
1098
+ amount: actionArgs.amount,
594
1099
  };
1100
+ // Sign the action
1101
+ const signature = await signL1Action({
1102
+ wallet: this.wallet,
1103
+ action,
1104
+ nonce,
1105
+ isTestnet: this.isTestnet,
1106
+ });
1107
+ // Send a request
1108
+ const request = { action, signature, nonce };
595
1109
  const response = await this.transport.request("action", request, signal);
1110
+ // Validate a response
596
1111
  this._validateResponse(response);
597
1112
  return response;
598
1113
  }
599
1114
  /**
600
- * Transfer between sub-accounts.
1115
+ * Transfer between sub-accounts (perpetual).
601
1116
  * @param args - The parameters for the request.
602
1117
  * @param signal - An optional abort signal.
603
- * @returns {SuccessResponse} Successful response without specific data.
1118
+ * @returns Successful response without specific data.
604
1119
  * @throws {ApiRequestError} When the API returns an error response.
605
1120
  *
1121
+ * @see null - no documentation
606
1122
  * @example
607
1123
  * ```ts
608
1124
  * import * as hl from "@nktkas/hyperliquid";
@@ -615,20 +1131,86 @@ export class WalletClient {
615
1131
  * const result = await client.subAccountTransfer({
616
1132
  * subAccountUser: "0x...",
617
1133
  * isDeposit: true,
618
- * usd: 1000000, // 1 USD in raw units (float amount * 1e6)
1134
+ * usd: 1 * 1e6,
619
1135
  * });
620
1136
  * ```
621
1137
  */
622
1138
  async subAccountTransfer(args, signal) {
1139
+ // Destructure the parameters
623
1140
  const { nonce = Date.now(), ...actionArgs } = args;
624
- const sortedAction = sorters.subAccountTransfer({ type: "subAccountTransfer", ...actionArgs });
625
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce);
626
- const request = {
627
- action: sortedAction,
628
- signature,
1141
+ // Construct an action
1142
+ const action = {
1143
+ type: "subAccountTransfer",
1144
+ subAccountUser: actionArgs.subAccountUser,
1145
+ isDeposit: actionArgs.isDeposit,
1146
+ usd: actionArgs.usd,
1147
+ };
1148
+ // Sign the action
1149
+ const signature = await signL1Action({
1150
+ wallet: this.wallet,
1151
+ action,
629
1152
  nonce,
1153
+ isTestnet: this.isTestnet,
1154
+ });
1155
+ // Send a request
1156
+ const request = { action, signature, nonce };
1157
+ const response = await this.transport.request("action", request, signal);
1158
+ // Validate a response
1159
+ this._validateResponse(response);
1160
+ return response;
1161
+ }
1162
+ /**
1163
+ * Delegate or undelegate stake from a validator.
1164
+ * @param args - The parameters for the request.
1165
+ * @param signal - An optional abort signal.
1166
+ * @returns Successful response without specific data.
1167
+ * @throws {ApiRequestError} When the API returns an error response.
1168
+ *
1169
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#delegate-or-undelegate-stake-from-validator
1170
+ * @example
1171
+ * ```ts
1172
+ * import * as hl from "@nktkas/hyperliquid";
1173
+ * import { privateKeyToAccount } from "viem/accounts";
1174
+ *
1175
+ * const wallet = privateKeyToAccount("0x...");
1176
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
1177
+ * const client = new hl.WalletClient({ wallet, transport });
1178
+ *
1179
+ * const result = await client.tokenDelegate({
1180
+ * validator: "0x...",
1181
+ * isUndelegate: true,
1182
+ * wei: 1 * 1e8,
1183
+ * });
1184
+ * ```
1185
+ */
1186
+ async tokenDelegate(args, signal) {
1187
+ // Construct an action
1188
+ const action = {
1189
+ ...args,
1190
+ type: "tokenDelegate",
1191
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1192
+ signatureChainId: this.signatureChainId,
1193
+ nonce: args.nonce ?? Date.now(),
630
1194
  };
1195
+ // Sign the action
1196
+ const signature = await signUserSignedAction({
1197
+ wallet: this.wallet,
1198
+ action,
1199
+ types: {
1200
+ "HyperliquidTransaction:TokenDelegate": [
1201
+ { name: "hyperliquidChain", type: "string" },
1202
+ { name: "validator", type: "address" },
1203
+ { name: "wei", type: "uint64" },
1204
+ { name: "isUndelegate", type: "bool" },
1205
+ { name: "nonce", type: "uint64" },
1206
+ ],
1207
+ },
1208
+ chainId: parseInt(action.signatureChainId, 16),
1209
+ });
1210
+ // Send a request
1211
+ const request = { action, signature, nonce: action.nonce };
631
1212
  const response = await this.transport.request("action", request, signal);
1213
+ // Validate a response
632
1214
  this._validateResponse(response);
633
1215
  return response;
634
1216
  }
@@ -636,10 +1218,10 @@ export class WalletClient {
636
1218
  * Cancel a TWAP order.
637
1219
  * @param args - The parameters for the request.
638
1220
  * @param signal - An optional abort signal.
639
- * @returns {TwapCancelResponseSuccess} Successful variant of {@link TwapCancelResponse} without error status.
1221
+ * @returns Successful variant of {@link TwapCancelResponse} without error status.
640
1222
  * @throws {ApiRequestError} When the API returns an error response.
641
1223
  *
642
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-a-twap-order | Hyperliquid GitBook}
1224
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-a-twap-order
643
1225
  * @example
644
1226
  * ```ts
645
1227
  * import * as hl from "@nktkas/hyperliquid";
@@ -656,16 +1238,26 @@ export class WalletClient {
656
1238
  * ```
657
1239
  */
658
1240
  async twapCancel(args, signal) {
1241
+ // Destructure the parameters
659
1242
  const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
660
- const sortedAction = sorters.twapCancel({ type: "twapCancel", ...actionArgs });
661
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
662
- const request = {
663
- action: sortedAction,
664
- signature,
1243
+ // Construct an action
1244
+ const action = {
1245
+ type: "twapCancel",
1246
+ a: actionArgs.a,
1247
+ t: actionArgs.t,
1248
+ };
1249
+ // Sign the action
1250
+ const signature = await signL1Action({
1251
+ wallet: this.wallet,
1252
+ action,
665
1253
  nonce,
1254
+ isTestnet: this.isTestnet,
666
1255
  vaultAddress,
667
- };
1256
+ });
1257
+ // Send a request
1258
+ const request = { action, signature, nonce, vaultAddress };
668
1259
  const response = await this.transport.request("action", request, signal);
1260
+ // Validate a response
669
1261
  this._validateResponse(response);
670
1262
  return response;
671
1263
  }
@@ -673,10 +1265,10 @@ export class WalletClient {
673
1265
  * Place a TWAP order.
674
1266
  * @param args - The parameters for the request.
675
1267
  * @param signal - An optional abort signal.
676
- * @returns {TwapOrderResponseSuccess} Successful variant of {@link TwapOrderResponse} without error status.
1268
+ * @returns Successful variant of {@link TwapOrderResponse} without error status.
677
1269
  * @throws {ApiRequestError} When the API returns an error response.
678
1270
  *
679
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-a-twap-order | Hyperliquid GitBook}
1271
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-a-twap-order
680
1272
  * @example
681
1273
  * ```ts
682
1274
  * import * as hl from "@nktkas/hyperliquid";
@@ -697,16 +1289,32 @@ export class WalletClient {
697
1289
  * ```
698
1290
  */
699
1291
  async twapOrder(args, signal) {
1292
+ // Destructure the parameters
700
1293
  const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
701
- const sortedAction = sorters.twapOrder({ type: "twapOrder", twap: actionArgs });
702
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
703
- const request = {
704
- action: sortedAction,
705
- signature,
1294
+ // Construct an action
1295
+ const action = {
1296
+ type: "twapOrder",
1297
+ twap: {
1298
+ a: actionArgs.a,
1299
+ b: actionArgs.b,
1300
+ s: actionArgs.s,
1301
+ r: actionArgs.r,
1302
+ m: actionArgs.m,
1303
+ t: actionArgs.t,
1304
+ },
1305
+ };
1306
+ // Sign the action
1307
+ const signature = await signL1Action({
1308
+ wallet: this.wallet,
1309
+ action,
706
1310
  nonce,
1311
+ isTestnet: this.isTestnet,
707
1312
  vaultAddress,
708
- };
1313
+ });
1314
+ // Send a request
1315
+ const request = { action, signature, nonce, vaultAddress };
709
1316
  const response = await this.transport.request("action", request, signal);
1317
+ // Validate a response
710
1318
  this._validateResponse(response);
711
1319
  return response;
712
1320
  }
@@ -714,10 +1322,10 @@ export class WalletClient {
714
1322
  * Update isolated margin for a position.
715
1323
  * @param args - The parameters for the request.
716
1324
  * @param signal - An optional abort signal.
717
- * @returns {SuccessResponse} Successful response without specific data.
1325
+ * @returns Successful response without specific data.
718
1326
  * @throws {ApiRequestError} When the API returns an error response.
719
1327
  *
720
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-isolated-margin | Hyperliquid GitBook}
1328
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-isolated-margin
721
1329
  * @example
722
1330
  * ```ts
723
1331
  * import * as hl from "@nktkas/hyperliquid";
@@ -730,21 +1338,32 @@ export class WalletClient {
730
1338
  * const result = await client.updateIsolatedMargin({
731
1339
  * asset: 0,
732
1340
  * isBuy: true, // Add to long position
733
- * ntli: 1000, // Add 1000 USD margin (integer only)
1341
+ * ntli: 1, // Add 1 USD margin (integer only)
734
1342
  * });
735
1343
  * ```
736
1344
  */
737
1345
  async updateIsolatedMargin(args, signal) {
1346
+ // Destructure the parameters
738
1347
  const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
739
- const sortedAction = sorters.updateIsolatedMargin({ type: "updateIsolatedMargin", ...actionArgs });
740
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
741
- const request = {
742
- action: sortedAction,
743
- signature,
1348
+ // Construct an action
1349
+ const action = {
1350
+ type: "updateIsolatedMargin",
1351
+ asset: actionArgs.asset,
1352
+ isBuy: actionArgs.isBuy,
1353
+ ntli: actionArgs.ntli,
1354
+ };
1355
+ // Sign the action
1356
+ const signature = await signL1Action({
1357
+ wallet: this.wallet,
1358
+ action,
744
1359
  nonce,
1360
+ isTestnet: this.isTestnet,
745
1361
  vaultAddress,
746
- };
1362
+ });
1363
+ // Send a request
1364
+ const request = { action, signature, nonce, vaultAddress };
747
1365
  const response = await this.transport.request("action", request, signal);
1366
+ // Validate a response
748
1367
  this._validateResponse(response);
749
1368
  return response;
750
1369
  }
@@ -752,10 +1371,10 @@ export class WalletClient {
752
1371
  * Update leverage for cross or isolated margin.
753
1372
  * @param args - The parameters for the request.
754
1373
  * @param signal - An optional abort signal.
755
- * @returns {SuccessResponse} Successful response without specific data.
1374
+ * @returns Successful response without specific data.
756
1375
  * @throws {ApiRequestError} When the API returns an error response.
757
1376
  *
758
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-leverage | Hyperliquid GitBook}
1377
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-leverage
759
1378
  * @example
760
1379
  * ```ts
761
1380
  * import * as hl from "@nktkas/hyperliquid";
@@ -773,16 +1392,27 @@ export class WalletClient {
773
1392
  * ```
774
1393
  */
775
1394
  async updateLeverage(args, signal) {
1395
+ // Destructure the parameters
776
1396
  const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
777
- const sortedAction = sorters.updateLeverage({ type: "updateLeverage", ...actionArgs });
778
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
779
- const request = {
780
- action: sortedAction,
781
- signature,
1397
+ // Construct an action
1398
+ const action = {
1399
+ type: "updateLeverage",
1400
+ asset: actionArgs.asset,
1401
+ isCross: actionArgs.isCross,
1402
+ leverage: actionArgs.leverage,
1403
+ };
1404
+ // Sign the action
1405
+ const signature = await signL1Action({
1406
+ wallet: this.wallet,
1407
+ action,
782
1408
  nonce,
1409
+ isTestnet: this.isTestnet,
783
1410
  vaultAddress,
784
- };
1411
+ });
1412
+ // Send a request
1413
+ const request = { action, signature, nonce, vaultAddress };
785
1414
  const response = await this.transport.request("action", request, signal);
1415
+ // Validate a response
786
1416
  this._validateResponse(response);
787
1417
  return response;
788
1418
  }
@@ -790,10 +1420,10 @@ export class WalletClient {
790
1420
  * Transfer funds between Spot and Perp accounts.
791
1421
  * @param args - The parameters for the request.
792
1422
  * @param signal - An optional abort signal.
793
- * @returns {SuccessResponse} Successful response without specific data.
1423
+ * @returns Successful response without specific data.
794
1424
  * @throws {ApiRequestError} When the API returns an error response.
795
1425
  *
796
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa | Hyperliquid GitBook}
1426
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
797
1427
  * @example
798
1428
  * ```ts
799
1429
  * import * as hl from "@nktkas/hyperliquid";
@@ -804,33 +1434,38 @@ export class WalletClient {
804
1434
  * const client = new hl.WalletClient({ wallet, transport });
805
1435
  *
806
1436
  * const result = await client.usdClassTransfer({
807
- * amount: "1000",
1437
+ * amount: "1",
808
1438
  * toPerp: true, // Transfer from Spot to Perp
809
1439
  * });
810
1440
  * ```
811
1441
  */
812
1442
  async usdClassTransfer(args, signal) {
1443
+ // Construct an action
813
1444
  const action = {
814
1445
  ...args,
815
1446
  type: "usdClassTransfer",
816
1447
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
817
- signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
1448
+ signatureChainId: this.signatureChainId,
818
1449
  nonce: args.nonce ?? Date.now(),
819
1450
  };
820
- const signature = await signUserSignedAction(this.wallet, action, {
821
- "HyperliquidTransaction:UsdClassTransfer": [
822
- { name: "hyperliquidChain", type: "string" },
823
- { name: "amount", type: "string" },
824
- { name: "toPerp", type: "bool" },
825
- { name: "nonce", type: "uint64" },
826
- ],
827
- }, parseInt(action.signatureChainId, 16));
828
- const request = {
1451
+ // Sign the action
1452
+ const signature = await signUserSignedAction({
1453
+ wallet: this.wallet,
829
1454
  action,
830
- signature,
831
- nonce: action.nonce,
832
- };
1455
+ types: {
1456
+ "HyperliquidTransaction:UsdClassTransfer": [
1457
+ { name: "hyperliquidChain", type: "string" },
1458
+ { name: "amount", type: "string" },
1459
+ { name: "toPerp", type: "bool" },
1460
+ { name: "nonce", type: "uint64" },
1461
+ ],
1462
+ },
1463
+ chainId: parseInt(action.signatureChainId, 16),
1464
+ });
1465
+ // Send a request
1466
+ const request = { action, signature, nonce: action.nonce };
833
1467
  const response = await this.transport.request("action", request, signal);
1468
+ // Validate a response
834
1469
  this._validateResponse(response);
835
1470
  return response;
836
1471
  }
@@ -838,10 +1473,10 @@ export class WalletClient {
838
1473
  * Transfer USDC on L1 to another address.
839
1474
  * @param args - The parameters for the request.
840
1475
  * @param signal - An optional abort signal.
841
- * @returns {SuccessResponse} Successful response without specific data.
1476
+ * @returns Successful response without specific data.
842
1477
  * @throws {ApiRequestError} When the API returns an error response.
843
1478
  *
844
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#l1-usdc-transfer | Hyperliquid GitBook}
1479
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#l1-usdc-transfer
845
1480
  * @example
846
1481
  * ```ts
847
1482
  * import * as hl from "@nktkas/hyperliquid";
@@ -853,43 +1488,142 @@ export class WalletClient {
853
1488
  *
854
1489
  * const result = await client.usdSend({
855
1490
  * destination: "0x...",
856
- * amount: "1000",
1491
+ * amount: "1",
857
1492
  * });
858
1493
  * ```
859
1494
  */
860
1495
  async usdSend(args, signal) {
1496
+ // Construct an action
861
1497
  const action = {
862
1498
  ...args,
863
1499
  type: "usdSend",
864
1500
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
865
- signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
1501
+ signatureChainId: this.signatureChainId,
866
1502
  time: args.time ?? Date.now(),
867
1503
  };
868
- const signature = await signUserSignedAction(this.wallet, action, {
869
- "HyperliquidTransaction:UsdSend": [
870
- { name: "hyperliquidChain", type: "string" },
871
- { name: "destination", type: "string" },
872
- { name: "amount", type: "string" },
873
- { name: "time", type: "uint64" },
874
- ],
875
- }, parseInt(action.signatureChainId, 16));
876
- const request = {
1504
+ // Sign the action
1505
+ const signature = await signUserSignedAction({
1506
+ wallet: this.wallet,
1507
+ action,
1508
+ types: {
1509
+ "HyperliquidTransaction:UsdSend": [
1510
+ { name: "hyperliquidChain", type: "string" },
1511
+ { name: "destination", type: "string" },
1512
+ { name: "amount", type: "string" },
1513
+ { name: "time", type: "uint64" },
1514
+ ],
1515
+ },
1516
+ chainId: parseInt(action.signatureChainId, 16),
1517
+ });
1518
+ // Send a request
1519
+ const request = { action, signature, nonce: action.time };
1520
+ const response = await this.transport.request("action", request, signal);
1521
+ // Validate a response
1522
+ this._validateResponse(response);
1523
+ return response;
1524
+ }
1525
+ /**
1526
+ * Distribute funds from a vault between followers.
1527
+ * @param args - The parameters for the request.
1528
+ * @param signal - An optional abort signal.
1529
+ * @returns Successful response without specific data.
1530
+ * @throws {ApiRequestError} When the API returns an error response.
1531
+ *
1532
+ * @see null - no documentation
1533
+ * @example
1534
+ * ```ts
1535
+ * import * as hl from "@nktkas/hyperliquid";
1536
+ * import { privateKeyToAccount } from "viem/accounts";
1537
+ *
1538
+ * const wallet = privateKeyToAccount("0x...");
1539
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
1540
+ * const client = new hl.WalletClient({ wallet, transport });
1541
+ *
1542
+ * const result = await client.vaultDistribute({
1543
+ * vaultAddress: "0x...",
1544
+ * usd: 10 * 1e6,
1545
+ * });
1546
+ * ```
1547
+ */
1548
+ async vaultDistribute(args, signal) {
1549
+ // Destructure the parameters
1550
+ const { nonce = Date.now(), ...actionArgs } = args;
1551
+ // Construct an action
1552
+ const action = {
1553
+ type: "vaultDistribute",
1554
+ vaultAddress: actionArgs.vaultAddress,
1555
+ usd: actionArgs.usd,
1556
+ };
1557
+ // Sign the action
1558
+ const signature = await signL1Action({
1559
+ wallet: this.wallet,
877
1560
  action,
878
- signature,
879
- nonce: action.time,
1561
+ nonce,
1562
+ isTestnet: this.isTestnet,
1563
+ });
1564
+ // Send a request
1565
+ const request = { action, signature, nonce };
1566
+ const response = await this.transport.request("action", request, signal);
1567
+ // Validate a response
1568
+ this._validateResponse(response);
1569
+ return response;
1570
+ }
1571
+ /**
1572
+ * Modify a vault's configuration.
1573
+ * @param args - The parameters for the request.
1574
+ * @param signal - An optional abort signal.
1575
+ * @returns Successful response without specific data.
1576
+ * @throws {ApiRequestError} When the API returns an error response.
1577
+ *
1578
+ * @see null - no documentation
1579
+ * @example
1580
+ * ```ts
1581
+ * import * as hl from "@nktkas/hyperliquid";
1582
+ * import { privateKeyToAccount } from "viem/accounts";
1583
+ *
1584
+ * const wallet = privateKeyToAccount("0x...");
1585
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
1586
+ * const client = new hl.WalletClient({ wallet, transport });
1587
+ *
1588
+ * const result = await client.vaultModify({
1589
+ * vaultAddress: "0x...",
1590
+ * allowDeposits: true,
1591
+ * alwaysCloseOnWithdraw: false,
1592
+ * });
1593
+ * ```
1594
+ */
1595
+ async vaultModify(args, signal) {
1596
+ // Destructure the parameters
1597
+ const { nonce = Date.now(), ...actionArgs } = args;
1598
+ // Construct an action
1599
+ const action = {
1600
+ type: "vaultModify",
1601
+ vaultAddress: actionArgs.vaultAddress,
1602
+ allowDeposits: actionArgs.allowDeposits,
1603
+ alwaysCloseOnWithdraw: actionArgs.alwaysCloseOnWithdraw,
880
1604
  };
1605
+ // Sign the action
1606
+ const signature = await signL1Action({
1607
+ wallet: this.wallet,
1608
+ action,
1609
+ nonce,
1610
+ isTestnet: this.isTestnet,
1611
+ });
1612
+ // Send a request
1613
+ const request = { action, signature, nonce };
881
1614
  const response = await this.transport.request("action", request, signal);
1615
+ // Validate a response
882
1616
  this._validateResponse(response);
883
1617
  return response;
884
1618
  }
885
1619
  /**
886
- * Transfer funds to/from a vault.
1620
+ * Add or remove funds from a vault.
887
1621
  * @param args - The parameters for the request.
888
1622
  * @param signal - An optional abort signal.
889
- * @returns {SuccessResponse} Successful response without specific data.
1623
+ * @returns Successful response without specific data.
890
1624
  * @throws {ApiRequestError} When the API returns an error response.
891
1625
  *
892
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-or-withdraw-from-a-vault | Hyperliquid GitBook}
1626
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-or-withdraw-from-a-vault
893
1627
  * @example
894
1628
  * ```ts
895
1629
  * import * as hl from "@nktkas/hyperliquid";
@@ -902,20 +1636,31 @@ export class WalletClient {
902
1636
  * const result = await client.vaultTransfer({
903
1637
  * vaultAddress: "0x...",
904
1638
  * isDeposit: true,
905
- * usd: 1000000, // 1 USD in raw units (float amount * 1e6)
1639
+ * usd: 10 * 1e6,
906
1640
  * });
907
1641
  * ```
908
1642
  */
909
1643
  async vaultTransfer(args, signal) {
1644
+ // Destructure the parameters
910
1645
  const { nonce = Date.now(), ...actionArgs } = args;
911
- const sortedAction = sorters.vaultTransfer({ type: "vaultTransfer", ...actionArgs });
912
- const signature = await signL1Action(this.wallet, this.isTestnet, sortedAction, nonce);
913
- const request = {
914
- action: sortedAction,
915
- signature,
916
- nonce,
1646
+ // Construct an action
1647
+ const action = {
1648
+ type: "vaultTransfer",
1649
+ vaultAddress: actionArgs.vaultAddress,
1650
+ isDeposit: actionArgs.isDeposit,
1651
+ usd: actionArgs.usd,
917
1652
  };
1653
+ // Sign the action
1654
+ const signature = await signL1Action({
1655
+ wallet: this.wallet,
1656
+ action,
1657
+ nonce,
1658
+ isTestnet: this.isTestnet,
1659
+ });
1660
+ // Send a request
1661
+ const request = { action, signature, nonce };
918
1662
  const response = await this.transport.request("action", request, signal);
1663
+ // Validate a response
919
1664
  this._validateResponse(response);
920
1665
  return response;
921
1666
  }
@@ -923,10 +1668,10 @@ export class WalletClient {
923
1668
  * Initiate a withdrawal request.
924
1669
  * @param args - The parameters for the request.
925
1670
  * @param signal - An optional abort signal.
926
- * @returns {SuccessResponse} Successful response without specific data.
1671
+ * @returns Successful response without specific data.
927
1672
  * @throws {ApiRequestError} When the API returns an error response.
928
1673
  *
929
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#initiate-a-withdrawal-request | Hyperliquid GitBook}
1674
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#initiate-a-withdrawal-request
930
1675
  * @example
931
1676
  * ```ts
932
1677
  * import * as hl from "@nktkas/hyperliquid";
@@ -938,32 +1683,37 @@ export class WalletClient {
938
1683
  *
939
1684
  * const result = await client.withdraw3({
940
1685
  * destination: "0x...",
941
- * amount: "1000",
1686
+ * amount: "1",
942
1687
  * });
943
1688
  * ```
944
1689
  */
945
1690
  async withdraw3(args, signal) {
1691
+ // Construct an action
946
1692
  const action = {
947
1693
  ...args,
948
1694
  type: "withdraw3",
949
1695
  hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
950
- signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
1696
+ signatureChainId: this.signatureChainId,
951
1697
  time: args.time ?? Date.now(),
952
1698
  };
953
- const signature = await signUserSignedAction(this.wallet, action, {
954
- "HyperliquidTransaction:Withdraw": [
955
- { name: "hyperliquidChain", type: "string" },
956
- { name: "destination", type: "string" },
957
- { name: "amount", type: "string" },
958
- { name: "time", type: "uint64" },
959
- ],
960
- }, parseInt(action.signatureChainId, 16));
961
- const request = {
1699
+ // Sign the action
1700
+ const signature = await signUserSignedAction({
1701
+ wallet: this.wallet,
962
1702
  action,
963
- signature,
964
- nonce: action.time,
965
- };
1703
+ types: {
1704
+ "HyperliquidTransaction:Withdraw": [
1705
+ { name: "hyperliquidChain", type: "string" },
1706
+ { name: "destination", type: "string" },
1707
+ { name: "amount", type: "string" },
1708
+ { name: "time", type: "uint64" },
1709
+ ],
1710
+ },
1711
+ chainId: parseInt(action.signatureChainId, 16),
1712
+ });
1713
+ // Send a request
1714
+ const request = { action, signature, nonce: action.time };
966
1715
  const response = await this.transport.request("action", request, signal);
1716
+ // Validate a response
967
1717
  this._validateResponse(response);
968
1718
  return response;
969
1719
  }