@nktkas/hyperliquid 0.22.1 → 0.23.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 (230) hide show
  1. package/README.md +125 -70
  2. package/esm/mod.d.ts +1 -1
  3. package/esm/mod.d.ts.map +1 -1
  4. package/esm/src/base.d.ts +4 -1
  5. package/esm/src/base.d.ts.map +1 -1
  6. package/esm/src/base.js +2 -2
  7. package/esm/src/clients/exchange.d.ts +420 -368
  8. package/esm/src/clients/exchange.d.ts.map +1 -1
  9. package/esm/src/clients/exchange.js +721 -551
  10. package/esm/src/clients/info.d.ts +137 -41
  11. package/esm/src/clients/info.d.ts.map +1 -1
  12. package/esm/src/clients/info.js +124 -28
  13. package/esm/src/clients/multiSign.d.ts +406 -484
  14. package/esm/src/clients/multiSign.d.ts.map +1 -1
  15. package/esm/src/clients/multiSign.js +639 -734
  16. package/esm/src/clients/subscription.d.ts +38 -3
  17. package/esm/src/clients/subscription.d.ts.map +1 -1
  18. package/esm/src/clients/subscription.js +36 -3
  19. package/esm/src/signing/_signTypedData/ethers.d.ts +33 -0
  20. package/esm/src/signing/_signTypedData/ethers.d.ts.map +1 -0
  21. package/esm/src/signing/_signTypedData/ethers.js +12 -0
  22. package/esm/src/signing/_signTypedData/mod.d.ts +35 -0
  23. package/esm/src/signing/_signTypedData/mod.d.ts.map +1 -0
  24. package/esm/src/signing/_signTypedData/mod.js +59 -0
  25. package/esm/src/signing/_signTypedData/private_key.d.ts +26 -0
  26. package/esm/src/signing/_signTypedData/private_key.d.ts.map +1 -0
  27. package/esm/src/signing/_signTypedData/private_key.js +144 -0
  28. package/esm/src/signing/_signTypedData/viem.d.ts +23 -0
  29. package/esm/src/signing/_signTypedData/viem.d.ts.map +1 -0
  30. package/esm/src/signing/_signTypedData/viem.js +6 -0
  31. package/esm/src/signing/_signTypedData/window.d.ts +29 -0
  32. package/esm/src/signing/_signTypedData/window.d.ts.map +1 -0
  33. package/esm/src/signing/_signTypedData/window.js +30 -0
  34. package/esm/src/signing/_sorter.d.ts +128 -0
  35. package/esm/src/signing/_sorter.d.ts.map +1 -0
  36. package/esm/src/{signing.js → signing/_sorter.js} +36 -449
  37. package/esm/src/signing/mod.d.ts +272 -0
  38. package/esm/src/signing/mod.d.ts.map +1 -0
  39. package/esm/src/signing/mod.js +306 -0
  40. package/esm/src/transports/base.d.ts +1 -1
  41. package/esm/src/transports/base.d.ts.map +1 -1
  42. package/esm/src/transports/base.js +2 -2
  43. package/esm/src/transports/http/http_transport.d.ts +9 -15
  44. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  45. package/esm/src/transports/http/http_transport.js +64 -59
  46. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +1 -1
  47. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
  48. package/esm/src/transports/websocket/_hyperliquid_event_target.js +1 -1
  49. package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
  50. package/esm/src/transports/websocket/_reconnecting_websocket.js +17 -4
  51. package/esm/src/transports/websocket/_websocket_async_request.d.ts +0 -9
  52. package/esm/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
  53. package/esm/src/transports/websocket/_websocket_async_request.js +14 -20
  54. package/esm/src/transports/websocket/websocket_transport.d.ts +8 -4
  55. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  56. package/esm/src/transports/websocket/websocket_transport.js +83 -54
  57. package/esm/src/types/exchange/requests.d.ts +27 -51
  58. package/esm/src/types/exchange/requests.d.ts.map +1 -1
  59. package/esm/src/types/explorer/requests.d.ts +5 -8
  60. package/esm/src/types/explorer/requests.d.ts.map +1 -1
  61. package/esm/src/types/info/accounts.d.ts +5 -5
  62. package/esm/src/types/info/accounts.d.ts.map +1 -1
  63. package/esm/src/types/info/delegations.d.ts +1 -1
  64. package/esm/src/types/info/delegations.d.ts.map +1 -1
  65. package/esm/src/types/info/orders.d.ts +1 -1
  66. package/esm/src/types/info/orders.d.ts.map +1 -1
  67. package/esm/src/types/info/requests.d.ts +49 -96
  68. package/esm/src/types/info/requests.d.ts.map +1 -1
  69. package/esm/src/types/subscriptions/requests.d.ts +72 -18
  70. package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
  71. package/esm/src/types/subscriptions/responses.d.ts +2 -0
  72. package/esm/src/types/subscriptions/responses.d.ts.map +1 -1
  73. package/package.json +12 -8
  74. package/script/mod.d.ts +1 -1
  75. package/script/mod.d.ts.map +1 -1
  76. package/script/mod.js +13 -23
  77. package/script/src/base.d.ts +4 -1
  78. package/script/src/base.d.ts.map +1 -1
  79. package/script/src/base.js +10 -20
  80. package/script/src/clients/exchange.d.ts +420 -368
  81. package/script/src/clients/exchange.d.ts.map +1 -1
  82. package/script/src/clients/exchange.js +2000 -1840
  83. package/script/src/clients/info.d.ts +137 -41
  84. package/script/src/clients/info.d.ts.map +1 -1
  85. package/script/src/clients/info.js +1296 -1210
  86. package/script/src/clients/multiSign.d.ts +406 -484
  87. package/script/src/clients/multiSign.d.ts.map +1 -1
  88. package/script/src/clients/multiSign.js +2043 -2148
  89. package/script/src/clients/subscription.d.ts +38 -3
  90. package/script/src/clients/subscription.d.ts.map +1 -1
  91. package/script/src/clients/subscription.js +568 -545
  92. package/script/src/signing/_signTypedData/ethers.d.ts +33 -0
  93. package/script/src/signing/_signTypedData/ethers.d.ts.map +1 -0
  94. package/script/src/signing/_signTypedData/ethers.js +16 -0
  95. package/script/src/signing/_signTypedData/mod.d.ts +35 -0
  96. package/script/src/signing/_signTypedData/mod.d.ts.map +1 -0
  97. package/script/src/signing/_signTypedData/mod.js +67 -0
  98. package/script/src/signing/_signTypedData/private_key.d.ts +26 -0
  99. package/script/src/signing/_signTypedData/private_key.d.ts.map +1 -0
  100. package/script/src/signing/_signTypedData/private_key.js +148 -0
  101. package/script/src/signing/_signTypedData/viem.d.ts +23 -0
  102. package/script/src/signing/_signTypedData/viem.d.ts.map +1 -0
  103. package/script/src/signing/_signTypedData/viem.js +9 -0
  104. package/script/src/signing/_signTypedData/window.d.ts +29 -0
  105. package/script/src/signing/_signTypedData/window.d.ts.map +1 -0
  106. package/script/src/signing/_signTypedData/window.js +34 -0
  107. package/script/src/signing/_sorter.d.ts +128 -0
  108. package/script/src/signing/_sorter.d.ts.map +1 -0
  109. package/script/src/signing/_sorter.js +698 -0
  110. package/script/src/signing/mod.d.ts +272 -0
  111. package/script/src/signing/mod.d.ts.map +1 -0
  112. package/script/src/signing/mod.js +331 -0
  113. package/script/src/transports/base.d.ts +1 -1
  114. package/script/src/transports/base.d.ts.map +1 -1
  115. package/script/src/transports/base.js +11 -21
  116. package/script/src/transports/http/http_transport.d.ts +9 -15
  117. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  118. package/script/src/transports/http/http_transport.js +97 -102
  119. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +1 -1
  120. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
  121. package/script/src/transports/websocket/_hyperliquid_event_target.js +51 -61
  122. package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
  123. package/script/src/transports/websocket/_reconnecting_websocket.js +271 -268
  124. package/script/src/transports/websocket/_websocket_async_request.d.ts +0 -9
  125. package/script/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
  126. package/script/src/transports/websocket/_websocket_async_request.js +157 -174
  127. package/script/src/transports/websocket/websocket_transport.d.ts +8 -4
  128. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  129. package/script/src/transports/websocket/websocket_transport.js +209 -189
  130. package/script/src/types/exchange/requests.d.ts +27 -51
  131. package/script/src/types/exchange/requests.d.ts.map +1 -1
  132. package/script/src/types/exchange/requests.js +2 -12
  133. package/script/src/types/exchange/responses.js +2 -12
  134. package/script/src/types/explorer/requests.d.ts +5 -8
  135. package/script/src/types/explorer/requests.d.ts.map +1 -1
  136. package/script/src/types/explorer/requests.js +2 -12
  137. package/script/src/types/explorer/responses.js +2 -12
  138. package/script/src/types/info/accounts.d.ts +5 -5
  139. package/script/src/types/info/accounts.d.ts.map +1 -1
  140. package/script/src/types/info/accounts.js +2 -12
  141. package/script/src/types/info/assets.js +2 -12
  142. package/script/src/types/info/delegations.d.ts +1 -1
  143. package/script/src/types/info/delegations.d.ts.map +1 -1
  144. package/script/src/types/info/delegations.js +2 -12
  145. package/script/src/types/info/markets.js +2 -12
  146. package/script/src/types/info/orders.d.ts +1 -1
  147. package/script/src/types/info/orders.d.ts.map +1 -1
  148. package/script/src/types/info/orders.js +2 -12
  149. package/script/src/types/info/requests.d.ts +49 -96
  150. package/script/src/types/info/requests.d.ts.map +1 -1
  151. package/script/src/types/info/requests.js +2 -12
  152. package/script/src/types/info/vaults.js +2 -12
  153. package/script/src/types/mod.js +2 -12
  154. package/script/src/types/subscriptions/requests.d.ts +72 -18
  155. package/script/src/types/subscriptions/requests.d.ts.map +1 -1
  156. package/script/src/types/subscriptions/requests.js +2 -12
  157. package/script/src/types/subscriptions/responses.d.ts +2 -0
  158. package/script/src/types/subscriptions/responses.d.ts.map +1 -1
  159. package/script/src/types/subscriptions/responses.js +2 -12
  160. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +0 -2
  161. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +0 -1
  162. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +0 -1
  163. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +0 -95
  164. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +0 -1
  165. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +0 -10
  166. package/esm/deps/jsr.io/@std/async/1.0.13/delay.d.ts +0 -43
  167. package/esm/deps/jsr.io/@std/async/1.0.13/delay.d.ts.map +0 -1
  168. package/esm/deps/jsr.io/@std/async/1.0.13/delay.js +0 -63
  169. package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts +0 -9
  170. package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts.map +0 -1
  171. package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.js +0 -2
  172. package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts +0 -21
  173. package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts.map +0 -1
  174. package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.js +0 -32
  175. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
  176. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
  177. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -51
  178. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
  179. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
  180. package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -13
  181. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
  182. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
  183. package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -2
  184. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
  185. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
  186. package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -87
  187. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +0 -9
  188. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +0 -1
  189. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +0 -2
  190. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts +0 -39
  191. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +0 -1
  192. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.js +0 -237
  193. package/esm/src/signing.d.ts +0 -463
  194. package/esm/src/signing.d.ts.map +0 -1
  195. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +0 -2
  196. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +0 -1
  197. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +0 -27
  198. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +0 -95
  199. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +0 -1
  200. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +0 -24
  201. package/script/deps/jsr.io/@std/async/1.0.13/delay.d.ts +0 -43
  202. package/script/deps/jsr.io/@std/async/1.0.13/delay.d.ts.map +0 -1
  203. package/script/deps/jsr.io/@std/async/1.0.13/delay.js +0 -76
  204. package/script/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts +0 -9
  205. package/script/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts.map +0 -1
  206. package/script/deps/jsr.io/@std/bytes/1.0.6/_types.js +0 -13
  207. package/script/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts +0 -21
  208. package/script/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts.map +0 -1
  209. package/script/deps/jsr.io/@std/bytes/1.0.6/concat.js +0 -45
  210. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
  211. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
  212. package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -67
  213. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
  214. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
  215. package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -26
  216. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
  217. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
  218. package/script/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -13
  219. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
  220. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
  221. package/script/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -101
  222. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +0 -9
  223. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +0 -1
  224. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +0 -13
  225. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts +0 -39
  226. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +0 -1
  227. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +0 -250
  228. package/script/src/signing.d.ts +0 -463
  229. package/script/src/signing.d.ts.map +0 -1
  230. package/script/src/signing.js +0 -1129
@@ -1,1861 +1,2021 @@
1
- (function (factory) {
2
- if (typeof module === "object" && typeof module.exports === "object") {
3
- var v = factory(require, exports);
4
- if (v !== undefined) module.exports = v;
5
- }
6
- else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "../base.js", "../signing.js"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.ExchangeClient = exports.ApiRequestError = void 0;
13
- const base_js_1 = require("../base.js");
14
- const signing_js_1 = require("../signing.js");
15
- /** Error thrown when the API returns an error response. */
16
- class ApiRequestError extends base_js_1.HyperliquidError {
17
- response;
18
- constructor(response) {
19
- let message;
20
- if (response.status === "err") {
21
- // ErrorResponse
22
- message = response.response;
23
- }
24
- else {
25
- if ("statuses" in response.response.data) {
26
- // OrderResponse | CancelResponse
27
- const errors = response.response.data.statuses.reduce((acc, status, index) => {
28
- if (typeof status === "object" && "error" in status) {
29
- acc.push(`Order ${index}: ${status.error}`);
30
- }
31
- return acc;
32
- }, []);
33
- if (errors.length > 0) {
34
- message = errors.join(", ");
35
- }
36
- }
37
- else {
38
- // TwapOrderResponse | TwapCancelResponse
39
- if (typeof response.response.data.status === "object" && "error" in response.response.data.status) {
40
- message = response.response.data.status.error;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExchangeClient = exports.ApiRequestError = void 0;
4
+ const base_js_1 = require("../base.js");
5
+ const mod_js_1 = require("../signing/mod.js");
6
+ /** Error thrown when the API returns an error response. */
7
+ class ApiRequestError extends base_js_1.HyperliquidError {
8
+ response;
9
+ constructor(response) {
10
+ let message;
11
+ if (response.status === "err") {
12
+ // ErrorResponse
13
+ message = response.response;
14
+ }
15
+ else {
16
+ if ("statuses" in response.response.data) {
17
+ // OrderResponse | CancelResponse
18
+ const errors = response.response.data.statuses.reduce((acc, status, index) => {
19
+ if (typeof status === "object" && "error" in status) {
20
+ acc.push(`Order ${index}: ${status.error}`);
41
21
  }
22
+ return acc;
23
+ }, []);
24
+ if (errors.length > 0) {
25
+ message = errors.join(", ");
42
26
  }
43
27
  }
44
- super(message || "An unknown error occurred while processing an API request. See `response` for more details.");
45
- this.response = response;
46
- this.name = "ApiRequestError";
47
- }
48
- }
49
- exports.ApiRequestError = ApiRequestError;
50
- /** Nonce manager for generating unique nonces for signing transactions. */
51
- class NonceManager {
52
- /** The last nonce used for signing transactions. */
53
- lastNonce = 0;
54
- /**
55
- * Gets the next nonce for signing transactions.
56
- * @returns The next nonce.
57
- */
58
- getNonce() {
59
- let nonce = Date.now();
60
- if (nonce <= this.lastNonce) {
61
- nonce = ++this.lastNonce;
62
- }
63
28
  else {
64
- this.lastNonce = nonce;
29
+ // TwapOrderResponse | TwapCancelResponse
30
+ if (typeof response.response.data.status === "object" && "error" in response.response.data.status) {
31
+ message = response.response.data.status.error;
32
+ }
65
33
  }
66
- return nonce;
67
34
  }
35
+ super(message || "An unknown error occurred while processing an API request. See `response` for more details.");
36
+ this.response = response;
37
+ this.name = "ApiRequestError";
68
38
  }
39
+ }
40
+ exports.ApiRequestError = ApiRequestError;
41
+ /** Nonce manager for generating unique nonces for signing transactions. */
42
+ class NonceManager {
43
+ /** The last nonce used for signing transactions. */
44
+ lastNonce = 0;
69
45
  /**
70
- * Exchange client for interacting with the Hyperliquid API.
71
- * @typeParam T The transport used to connect to the Hyperliquid API.
72
- * @typeParam W The wallet used for signing transactions.
46
+ * Gets the next nonce for signing transactions.
47
+ * @returns The next nonce.
73
48
  */
74
- class ExchangeClient {
75
- transport;
76
- wallet;
77
- isTestnet;
78
- defaultVaultAddress;
79
- defaultExpiresAfter;
80
- signatureChainId;
81
- nonceManager;
82
- /**
83
- * Initialises a new instance.
84
- * @param args - The parameters for the client.
85
- *
86
- * @example Private key via [viem](https://viem.sh/docs/clients/wallet#local-accounts-private-key-mnemonic-etc)
87
- * ```ts
88
- * import * as hl from "@nktkas/hyperliquid";
89
- * import { privateKeyToAccount } from "viem/accounts";
90
- *
91
- * const wallet = privateKeyToAccount("0x...");
92
- *
93
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
94
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
95
- * ```
96
- *
97
- * @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)
98
- * ```ts
99
- * import * as hl from "@nktkas/hyperliquid";
100
- * import { ethers } from "ethers";
101
- *
102
- * const wallet = new ethers.Wallet("0x...");
103
- *
104
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
105
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
106
- * ```
107
- *
108
- * @example External wallet (e.g. MetaMask) via [viem](https://viem.sh/docs/clients/wallet#optional-hoist-the-account)
109
- * ```ts
110
- * import * as hl from "@nktkas/hyperliquid";
111
- * import { createWalletClient, custom } from "viem";
112
- *
113
- * const [account] = await window.ethereum.request({ method: "eth_requestAccounts" });
114
- * const wallet = createWalletClient({ account, transport: custom(window.ethereum) });
115
- *
116
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
117
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
118
- * ```
119
- *
120
- * @example External wallet (e.g. MetaMask) via `window.ethereum` directly
121
- * ```ts
122
- * import * as hl from "@nktkas/hyperliquid";
123
- *
124
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
125
- * const exchClient = new hl.ExchangeClient({ wallet: window.ethereum, transport });
126
- * ```
127
- */
128
- constructor(args) {
129
- this.transport = args.transport;
130
- this.wallet = args.wallet;
131
- this.isTestnet = args.isTestnet ?? false;
132
- this.defaultVaultAddress = args.defaultVaultAddress;
133
- this.defaultExpiresAfter = args.defaultExpiresAfter;
134
- this.signatureChainId = args.signatureChainId ?? this._guessSignatureChainId;
135
- this.nonceManager = args.nonceManager ?? new NonceManager().getNonce;
136
- }
137
- /**
138
- * Approve an agent to sign on behalf of the master account.
139
- * @param args - The parameters for the request.
140
- * @param signal - An optional abort signal
141
- * @returns Successful response without specific data.
142
- * @throws {ApiRequestError} When the API returns an error response.
143
- *
144
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-an-api-wallet
145
- * @example
146
- * ```ts
147
- * import * as hl from "@nktkas/hyperliquid";
148
- * import { privateKeyToAccount } from "viem/accounts";
149
- *
150
- * const wallet = privateKeyToAccount("0x...");
151
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
152
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
153
- *
154
- * const data = await exchClient.approveAgent({ agentAddress: "0x...", agentName: "agentName" });
155
- * ```
156
- */
157
- async approveAgent(args, signal) {
158
- // Destructure the parameters
159
- const { ...actionArgs } = args;
160
- // Construct an action
161
- const nonce = await this.nonceManager();
162
- const action = {
163
- ...actionArgs,
164
- agentName: args.agentName ?? "",
165
- type: "approveAgent",
166
- hyperliquidChain: this._getHyperliquidChain(),
167
- signatureChainId: await this._getSignatureChainId(),
168
- nonce,
169
- };
170
- // Sign the action
171
- const signature = await (0, signing_js_1.signUserSignedAction)({
172
- wallet: this.wallet,
173
- action,
174
- types: signing_js_1.userSignedActionEip712Types[action.type],
175
- chainId: parseInt(action.signatureChainId, 16),
176
- });
177
- if (action.agentName === "")
178
- action.agentName = null;
179
- // Send a request
180
- return await this._request({ action, signature, nonce }, signal);
181
- }
182
- /**
183
- * Approve a maximum fee rate for a builder.
184
- * @param args - The parameters for the request.
185
- * @param signal - An optional abort signal.
186
- * @returns Successful response without specific data.
187
- * @throws {ApiRequestError} When the API returns an error response.
188
- *
189
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-a-builder-fee
190
- * @example
191
- * ```ts
192
- * import * as hl from "@nktkas/hyperliquid";
193
- * import { privateKeyToAccount } from "viem/accounts";
194
- *
195
- * const wallet = privateKeyToAccount("0x...");
196
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
197
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
198
- *
199
- * const data = await exchClient.approveBuilderFee({ maxFeeRate: "0.01%", builder: "0x..." });
200
- * ```
201
- */
202
- async approveBuilderFee(args, signal) {
203
- // Destructure the parameters
204
- const { ...actionArgs } = args;
205
- // Construct an action
206
- const nonce = await this.nonceManager();
207
- const action = {
208
- ...actionArgs,
209
- type: "approveBuilderFee",
210
- hyperliquidChain: this._getHyperliquidChain(),
211
- signatureChainId: await this._getSignatureChainId(),
212
- nonce,
213
- };
214
- // Sign the action
215
- const signature = await (0, signing_js_1.signUserSignedAction)({
216
- wallet: this.wallet,
217
- action,
218
- types: signing_js_1.userSignedActionEip712Types[action.type],
219
- chainId: parseInt(action.signatureChainId, 16),
220
- });
221
- // Send a request
222
- return await this._request({ action, signature, nonce }, signal);
223
- }
224
- /**
225
- * Modify multiple orders.
226
- * @param args - The parameters for the request.
227
- * @param signal - An optional abort signal.
228
- * @returns Successful variant of {@link OrderResponse} without error statuses.
229
- * @throws {ApiRequestError} When the API returns an error response.
230
- *
231
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders
232
- * @example
233
- * ```ts
234
- * import * as hl from "@nktkas/hyperliquid";
235
- * import { privateKeyToAccount } from "viem/accounts";
236
- *
237
- * const wallet = privateKeyToAccount("0x...");
238
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
239
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
240
- *
241
- * const data = await exchClient.batchModify({
242
- * modifies: [{
243
- * oid: 123,
244
- * order: {
245
- * a: 0, // Asset index
246
- * b: true, // Buy order
247
- * p: "31000", // New price
248
- * s: "0.2", // New size
249
- * r: false, // Not reduce-only
250
- * t: {
251
- * limit: {
252
- * tif: "Gtc", // Good-til-cancelled
253
- * },
254
- * },
255
- * c: "0x...", // Client Order ID (optional)
256
- * },
257
- * }],
258
- * });
259
- * ```
260
- */
261
- async batchModify(args, signal) {
262
- // Destructure the parameters
263
- const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
264
- // Construct an action
265
- const nonce = await this.nonceManager();
266
- const action = {
267
- type: "batchModify",
268
- ...actionArgs,
269
- };
270
- // Sign the action
271
- const signature = await (0, signing_js_1.signL1Action)({
272
- wallet: this.wallet,
273
- action: signing_js_1.actionSorter[action.type](action),
274
- nonce,
275
- isTestnet: this.isTestnet,
276
- vaultAddress,
277
- expiresAfter,
278
- });
279
- // Send a request
280
- return await this._request({ action, signature, nonce, vaultAddress, expiresAfter }, signal);
281
- }
282
- /**
283
- * Cancel order(s).
284
- * @param args - The parameters for the request.
285
- * @param signal - An optional abort signal.
286
- * @returns Successful variant of {@link CancelResponse} without error statuses.
287
- * @throws {ApiRequestError} When the API returns an error response.
288
- *
289
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
290
- * @example
291
- * ```ts
292
- * import * as hl from "@nktkas/hyperliquid";
293
- * import { privateKeyToAccount } from "viem/accounts";
294
- *
295
- * const wallet = privateKeyToAccount("0x...");
296
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
297
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
298
- *
299
- * const data = await exchClient.cancel({
300
- * cancels: [{
301
- * a: 0, // Asset index
302
- * o: 123, // Order ID
303
- * }],
304
- * });
305
- * ```
306
- */
307
- async cancel(args, signal) {
308
- // Destructure the parameters
309
- const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
310
- // Construct an action
311
- const nonce = await this.nonceManager();
312
- const action = {
313
- type: "cancel",
314
- ...actionArgs,
315
- };
316
- // Sign the action
317
- const signature = await (0, signing_js_1.signL1Action)({
318
- wallet: this.wallet,
319
- action: signing_js_1.actionSorter[action.type](action),
320
- nonce,
321
- isTestnet: this.isTestnet,
322
- vaultAddress,
323
- expiresAfter,
324
- });
325
- // Send a request
326
- return await this._request({ action, signature, nonce, vaultAddress, expiresAfter }, signal);
327
- }
328
- /**
329
- * Cancel order(s) by cloid.
330
- * @param args - The parameters for the request.
331
- * @param signal - An optional abort signal.
332
- * @returns Successful variant of {@link CancelResponse} without error statuses.
333
- * @throws {ApiRequestError} When the API returns an error response.
334
- *
335
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid
336
- * @example
337
- * ```ts
338
- * import * as hl from "@nktkas/hyperliquid";
339
- * import { privateKeyToAccount } from "viem/accounts";
340
- *
341
- * const wallet = privateKeyToAccount("0x...");
342
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
343
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
344
- *
345
- * const data = await exchClient.cancelByCloid({
346
- * cancels: [
347
- * { asset: 0, cloid: "0x..." },
348
- * ],
349
- * });
350
- * ```
351
- */
352
- async cancelByCloid(args, signal) {
353
- // Destructure the parameters
354
- const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
355
- // Construct an action
356
- const nonce = await this.nonceManager();
357
- const action = {
358
- type: "cancelByCloid",
359
- ...actionArgs,
360
- };
361
- // Sign the action
362
- const signature = await (0, signing_js_1.signL1Action)({
363
- wallet: this.wallet,
364
- action: signing_js_1.actionSorter[action.type](action),
365
- nonce,
366
- isTestnet: this.isTestnet,
367
- vaultAddress,
368
- expiresAfter,
369
- });
370
- // Send a request
371
- return await this._request({ action, signature, nonce, vaultAddress, expiresAfter }, signal);
372
- }
373
- /**
374
- * Transfer native token from the user's spot account into staking for delegating to validators.
375
- * @param args - The parameters for the request.
376
- * @param signal - An optional abort signal.
377
- * @returns Successful response without specific data.
378
- * @throws {ApiRequestError} When the API returns an error response.
379
- *
380
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-into-staking
381
- * @example
382
- * ```ts
383
- * import * as hl from "@nktkas/hyperliquid";
384
- * import { privateKeyToAccount } from "viem/accounts";
385
- *
386
- * const wallet = privateKeyToAccount("0x...");
387
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
388
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
389
- *
390
- * const data = await exchClient.cDeposit({ wei: 1 * 1e8 });
391
- * ```
392
- */
393
- async cDeposit(args, signal) {
394
- // Destructure the parameters
395
- const { ...actionArgs } = args;
396
- // Construct an action
397
- const nonce = await this.nonceManager();
398
- const action = {
399
- ...actionArgs,
400
- type: "cDeposit",
401
- hyperliquidChain: this._getHyperliquidChain(),
402
- signatureChainId: await this._getSignatureChainId(),
403
- nonce,
404
- };
405
- // Sign the action
406
- const signature = await (0, signing_js_1.signUserSignedAction)({
407
- wallet: this.wallet,
408
- action,
409
- types: signing_js_1.userSignedActionEip712Types[action.type],
410
- chainId: parseInt(action.signatureChainId, 16),
411
- });
412
- // Send a request
413
- return await this._request({ action, signature, nonce }, signal);
414
- }
415
- /**
416
- * Claim rewards from referral program.
417
- * @param args - The parameters for the request.
418
- * @param signal - An optional abort signal.
419
- * @returns Successful response without specific data.
420
- * @throws {ApiRequestError} When the API returns an error response.
421
- *
422
- * @see null - no documentation
423
- * @example
424
- * ```ts
425
- * import * as hl from "@nktkas/hyperliquid";
426
- * import { privateKeyToAccount } from "viem/accounts";
427
- *
428
- * const wallet = privateKeyToAccount("0x...");
429
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
430
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
431
- *
432
- * const data = await exchClient.claimRewards();
433
- * ```
434
- */
435
- async claimRewards(signal) {
436
- // Construct an action
437
- const nonce = await this.nonceManager();
438
- const action = {
439
- type: "claimRewards",
440
- };
441
- // Sign the action
442
- const signature = await (0, signing_js_1.signL1Action)({
443
- wallet: this.wallet,
444
- action: signing_js_1.actionSorter[action.type](action),
445
- nonce,
446
- isTestnet: this.isTestnet,
447
- });
448
- // Send a request
449
- return await this._request({ action, signature, nonce }, signal);
450
- }
451
- /**
452
- * Convert a single-signature account to a multi-signature account or vice versa.
453
- * @param args - The parameters for the request.
454
- * @param signal - An optional abort signal.
455
- * @returns Successful response without specific data.
456
- * @throws {ApiRequestError} When the API returns an error response.
457
- *
458
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/hypercore/multi-sig
459
- * @example
460
- * ```ts
461
- * import * as hl from "@nktkas/hyperliquid";
462
- * import { privateKeyToAccount } from "viem/accounts";
463
- *
464
- * const wallet = privateKeyToAccount("0x...");
465
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
466
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
467
- *
468
- * const data = await exchClient.convertToMultiSigUser({ // convert to multi-sig user
469
- * authorizedUsers: ["0x...", "0x...", "0x..."],
470
- * threshold: 2,
471
- * });
472
- * ```
473
- */
474
- async convertToMultiSigUser(args, signal) {
475
- // Destructure the parameters
476
- const { ...actionArgs } = args;
477
- // Construct an action
478
- const nonce = await this.nonceManager();
479
- const action = {
480
- type: "convertToMultiSigUser",
481
- hyperliquidChain: this._getHyperliquidChain(),
482
- signatureChainId: await this._getSignatureChainId(),
483
- signers: JSON.stringify(actionArgs),
484
- nonce,
485
- };
486
- // Sign the action
487
- const signature = await (0, signing_js_1.signUserSignedAction)({
488
- wallet: this.wallet,
489
- action,
490
- types: signing_js_1.userSignedActionEip712Types[action.type],
491
- chainId: parseInt(action.signatureChainId, 16),
492
- });
493
- // Send a request
494
- return await this._request({ action, signature, nonce }, signal);
495
- }
496
- /**
497
- * Create a sub-account.
498
- * @param args - The parameters for the request.
499
- * @param signal - An optional abort signal.
500
- * @returns Response for creating a sub-account.
501
- * @throws {ApiRequestError} When the API returns an error response.
502
- *
503
- * @see null - no documentation
504
- * @example
505
- * ```ts
506
- * import * as hl from "@nktkas/hyperliquid";
507
- * import { privateKeyToAccount } from "viem/accounts";
508
- *
509
- * const wallet = privateKeyToAccount("0x...");
510
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
511
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
512
- *
513
- * const data = await exchClient.createSubAccount({ name: "subAccountName" });
514
- * ```
515
- */
516
- async createSubAccount(args, signal) {
517
- // Destructure the parameters
518
- const { ...actionArgs } = args;
519
- // Construct an action
520
- const nonce = await this.nonceManager();
521
- const action = {
522
- type: "createSubAccount",
523
- ...actionArgs,
524
- };
525
- // Sign the action
526
- const signature = await (0, signing_js_1.signL1Action)({
527
- wallet: this.wallet,
528
- action: signing_js_1.actionSorter[action.type](action),
529
- nonce,
530
- isTestnet: this.isTestnet,
531
- });
532
- // Send a request
533
- return await this._request({ action, signature, nonce }, signal);
534
- }
535
- /**
536
- * Create a vault.
537
- * @param args - The parameters for the request.
538
- * @param signal - An optional abort signal.
539
- * @returns Response for creating a vault.
540
- * @throws {ApiRequestError} When the API returns an error response.
541
- *
542
- * @see null - no documentation
543
- * @example
544
- * ```ts
545
- * import * as hl from "@nktkas/hyperliquid";
546
- * import { privateKeyToAccount } from "viem/accounts";
547
- *
548
- * const wallet = privateKeyToAccount("0x...");
549
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
550
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
551
- *
552
- * const data = await exchClient.createVault({
553
- * name: "VaultName",
554
- * description: "Vault description",
555
- * initialUsd: 100 * 1e6,
556
- * });
557
- * ```
558
- */
559
- async createVault(args, signal) {
560
- // Destructure the parameters
561
- const { ...actionArgs } = args;
562
- // Construct an action
563
- const nonce = await this.nonceManager();
564
- const action = {
565
- type: "createVault",
566
- nonce,
567
- ...actionArgs,
568
- };
569
- // Sign the action
570
- const signature = await (0, signing_js_1.signL1Action)({
571
- wallet: this.wallet,
572
- action: signing_js_1.actionSorter[action.type](action),
573
- nonce,
574
- isTestnet: this.isTestnet,
575
- });
576
- // Send a request
577
- return await this._request({ action, signature, nonce }, signal);
578
- }
579
- async cSignerAction(args, signal) {
580
- // Destructure the parameters
581
- const { expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
582
- // Construct an action
583
- const nonce = await this.nonceManager();
584
- const action = {
585
- type: "CSignerAction",
586
- ...actionArgs,
587
- };
588
- // Sign the action
589
- const signature = await (0, signing_js_1.signL1Action)({
590
- wallet: this.wallet,
591
- action: signing_js_1.actionSorter[action.type](action),
592
- nonce,
593
- isTestnet: this.isTestnet,
594
- expiresAfter,
595
- });
596
- // Send a request
597
- return await this._request({ action, signature, nonce, expiresAfter }, signal);
598
- }
599
- async cValidatorAction(args, signal) {
600
- // Destructure the parameters
601
- const { expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
602
- // Construct an action
603
- const nonce = await this.nonceManager();
604
- const action = {
605
- type: "CValidatorAction",
606
- ...actionArgs,
607
- };
608
- // Sign the action
609
- const signature = await (0, signing_js_1.signL1Action)({
610
- wallet: this.wallet,
611
- action: signing_js_1.actionSorter[action.type](action),
612
- nonce,
613
- isTestnet: this.isTestnet,
614
- expiresAfter,
615
- });
616
- // Send a request
617
- return await this._request({ action, signature, nonce, expiresAfter }, signal);
618
- }
619
- /**
620
- * Transfer native token from staking into the user's spot account.
621
- * @param args - The parameters for the request.
622
- * @param signal - An optional abort signal.
623
- * @returns Successful response without specific data.
624
- * @throws {ApiRequestError} When the API returns an error response.
625
- *
626
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#withdraw-from-staking
627
- * @example
628
- * ```ts
629
- * import * as hl from "@nktkas/hyperliquid";
630
- * import { privateKeyToAccount } from "viem/accounts";
631
- *
632
- * const wallet = privateKeyToAccount("0x...");
633
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
634
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
635
- *
636
- * const data = await exchClient.cWithdraw({ wei: 1 * 1e8 });
637
- * ```
638
- */
639
- async cWithdraw(args, signal) {
640
- // Destructure the parameters
641
- const { ...actionArgs } = args;
642
- // Construct an action
643
- const nonce = await this.nonceManager();
644
- const action = {
645
- ...actionArgs,
646
- type: "cWithdraw",
647
- hyperliquidChain: this._getHyperliquidChain(),
648
- signatureChainId: await this._getSignatureChainId(),
649
- nonce,
650
- };
651
- // Sign the action
652
- const signature = await (0, signing_js_1.signUserSignedAction)({
653
- wallet: this.wallet,
654
- action,
655
- types: signing_js_1.userSignedActionEip712Types[action.type],
656
- chainId: parseInt(action.signatureChainId, 16),
657
- });
658
- // Send a request
659
- return await this._request({ action, signature, nonce }, signal);
660
- }
661
- /**
662
- * Configure block type for EVM transactions.
663
- * @param args - The parameters for the request.
664
- * @param signal - An optional abort signal.
665
- * @returns Response for creating a sub-account.
666
- * @throws {ApiRequestError} When the API returns an error response.
667
- *
668
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/evm/dual-block-architecture
669
- * @example
670
- * ```ts
671
- * import * as hl from "@nktkas/hyperliquid";
672
- * import { privateKeyToAccount } from "viem/accounts";
673
- *
674
- * const wallet = privateKeyToAccount("0x...");
675
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
676
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
677
- *
678
- * const data = await exchClient.evmUserModify({ usingBigBlocks: true });
679
- * ```
680
- */
681
- async evmUserModify(args, signal) {
682
- // Destructure the parameters
683
- const { ...actionArgs } = args;
684
- // Construct an action
685
- const nonce = await this.nonceManager();
686
- const action = {
687
- type: "evmUserModify",
688
- ...actionArgs,
689
- };
690
- // Sign the action
691
- const signature = await (0, signing_js_1.signL1Action)({
692
- wallet: this.wallet,
693
- action: signing_js_1.actionSorter[action.type](action),
694
- nonce,
695
- isTestnet: this.isTestnet,
696
- });
697
- // Send a request
698
- return await this._request({ action, signature, nonce }, signal);
699
- }
700
- /**
701
- * Modify an order.
702
- * @param args - The parameters for the request.
703
- * @param signal - An optional abort signal.
704
- * @returns Successful response without specific data.
705
- * @throws {ApiRequestError} When the API returns an error response.
706
- *
707
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-an-order
708
- * @example
709
- * ```ts
710
- * import * as hl from "@nktkas/hyperliquid";
711
- * import { privateKeyToAccount } from "viem/accounts";
712
- *
713
- * const wallet = privateKeyToAccount("0x...");
714
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
715
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
716
- *
717
- * const data = await exchClient.modify({
718
- * oid: 123,
719
- * order: {
720
- * a: 0, // Asset index
721
- * b: true, // Buy order
722
- * p: "31000", // New price
723
- * s: "0.2", // New size
724
- * r: false, // Not reduce-only
725
- * t: {
726
- * limit: {
727
- * tif: "Gtc", // Good-til-cancelled
728
- * },
729
- * },
730
- * c: "0x...", // Client Order ID (optional)
731
- * },
732
- * });
733
- * ```
734
- */
735
- async modify(args, signal) {
736
- // Destructure the parameters
737
- const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
738
- // Construct an action
739
- const nonce = await this.nonceManager();
740
- const action = {
741
- type: "modify",
742
- ...actionArgs,
743
- };
744
- // Sign the action
745
- const signature = await (0, signing_js_1.signL1Action)({
746
- wallet: this.wallet,
747
- action: signing_js_1.actionSorter[action.type](action),
748
- nonce,
749
- isTestnet: this.isTestnet,
750
- vaultAddress,
751
- expiresAfter,
752
- });
753
- // Send a request
754
- return await this._request({ action, signature, nonce, vaultAddress, expiresAfter }, signal);
755
- }
756
- /**
757
- * A multi-signature request.
758
- * @param args - The parameters for the request.
759
- * @param signal - An optional abort signal.
760
- * @returns Successful response without specific data.
761
- * @throws {ApiRequestError} When the API returns an error response.
762
- *
763
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/hypercore/multi-sig
764
- * @example
765
- * ```ts
766
- * import * as hl from "@nktkas/hyperliquid";
767
- * import { privateKeyToAccount } from "viem/accounts";
768
- *
769
- * const wallet = privateKeyToAccount("0x...");
770
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
771
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
772
- *
773
- * const multiSigUser = "0x..."; // Multi-sig user address
774
- *
775
- * const nonce = Date.now();
776
- * const action = { type: "scheduleCancel", time: Date.now() + 10000 };
777
- *
778
- * const signature = await hl.signL1Action({
779
- * wallet,
780
- * action: [multiSigUser.toLowerCase(), signer1.address.toLowerCase(), action],
781
- * nonce,
782
- * isTestnet: true,
783
- * });
784
- *
785
- * const data = await exchClient.multiSig({
786
- * signatures: [signature],
787
- * payload: {
788
- * multiSigUser,
789
- * outerSigner: wallet.address,
790
- * action,
791
- * },
792
- * nonce,
793
- * });
794
- * ```
795
- */
796
- async multiSig(args, signal) {
797
- // Destructure the parameters
798
- const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), nonce, ...actionArgs } = args;
799
- // Construct an action
800
- const action = {
801
- type: "multiSig",
802
- signatureChainId: await this._getSignatureChainId(),
803
- ...actionArgs,
804
- };
805
- // Sign the action
806
- const actionForMultiSig = signing_js_1.actionSorter[action.type](action);
807
- delete actionForMultiSig.type;
808
- const signature = await (0, signing_js_1.signMultiSigAction)({
809
- wallet: this.wallet,
810
- action: actionForMultiSig,
811
- nonce,
812
- vaultAddress,
813
- expiresAfter,
814
- hyperliquidChain: this._getHyperliquidChain(),
815
- signatureChainId: action.signatureChainId,
816
- });
817
- // Send a request
818
- return await this._request({ action, signature, nonce, vaultAddress, expiresAfter }, signal);
819
- }
820
- /**
821
- * Place an order(s).
822
- * @param args - The parameters for the request.
823
- * @param signal - An optional abort signal.
824
- * @returns Successful variant of {@link OrderResponse} without error statuses.
825
- * @throws {ApiRequestError} When the API returns an error response.
826
- *
827
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order
828
- * @example
829
- * ```ts
830
- * import * as hl from "@nktkas/hyperliquid";
831
- * import { privateKeyToAccount } from "viem/accounts";
832
- *
833
- * const wallet = privateKeyToAccount("0x...");
834
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
835
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
836
- *
837
- * const data = await exchClient.order({
838
- * orders: [{
839
- * a: 0, // Asset index
840
- * b: true, // Buy order
841
- * p: "30000", // Price
842
- * s: "0.1", // Size
843
- * r: false, // Not reduce-only
844
- * t: {
845
- * limit: {
846
- * tif: "Gtc", // Good-til-cancelled
847
- * },
848
- * },
849
- * c: "0x...", // Client Order ID (optional)
850
- * }],
851
- * grouping: "na", // No grouping
852
- * });
853
- * ```
854
- */
855
- async order(args, signal) {
856
- // Destructure the parameters
857
- const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
858
- // Construct an action
859
- const nonce = await this.nonceManager();
860
- const action = {
861
- type: "order",
862
- ...actionArgs,
863
- };
864
- // Sign the action
865
- const signature = await (0, signing_js_1.signL1Action)({
866
- wallet: this.wallet,
867
- action: signing_js_1.actionSorter[action.type](action),
868
- nonce,
869
- isTestnet: this.isTestnet,
870
- vaultAddress,
871
- expiresAfter,
872
- });
873
- // Send a request
874
- return await this._request({ action, signature, nonce, vaultAddress, expiresAfter }, signal);
875
- }
876
- async perpDeploy(args, signal) {
877
- // Destructure the parameters
878
- const { ...actionArgs } = args;
879
- // Construct an action
880
- const nonce = await this.nonceManager();
881
- const action = {
882
- type: "perpDeploy",
883
- ...actionArgs,
884
- };
885
- // Sign the action
886
- const signature = await (0, signing_js_1.signL1Action)({
887
- wallet: this.wallet,
888
- action: signing_js_1.actionSorter[action.type](action),
889
- nonce,
890
- isTestnet: this.isTestnet,
891
- });
892
- // Send a request
893
- return await this._request({ action, signature, nonce }, signal);
894
- }
895
- /**
896
- * Transfer funds between Spot account and Perp dex account.
897
- * @param args - The parameters for the request.
898
- * @param signal - An optional abort signal.
899
- * @returns Successful response without specific data.
900
- * @throws {ApiRequestError} When the API returns an error response.
901
- *
902
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
903
- * @example
904
- * ```ts
905
- * import * as hl from "@nktkas/hyperliquid";
906
- * import { privateKeyToAccount } from "viem/accounts";
907
- *
908
- * const wallet = privateKeyToAccount("0x...");
909
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
910
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
911
- *
912
- * const data = await exchClient.perpDexClassTransfer({
913
- * dex: "test",
914
- * token: "USDC",
915
- * amount: "1",
916
- * toPerp: true,
917
- * });
918
- * ```
919
- */
920
- async perpDexClassTransfer(args, signal) {
921
- // Destructure the parameters
922
- const { ...actionArgs } = args;
923
- // Construct an action
924
- const nonce = await this.nonceManager();
925
- const action = {
926
- ...actionArgs,
927
- type: "PerpDexClassTransfer",
928
- hyperliquidChain: this._getHyperliquidChain(),
929
- signatureChainId: await this._getSignatureChainId(),
930
- nonce,
931
- };
932
- // Sign the action
933
- const signature = await (0, signing_js_1.signUserSignedAction)({
934
- wallet: this.wallet,
935
- action,
936
- types: signing_js_1.userSignedActionEip712Types[action.type],
937
- chainId: parseInt(action.signatureChainId, 16),
938
- });
939
- // Send a request
940
- return await this._request({ action, signature, nonce }, signal);
941
- }
942
- /**
943
- * Create a referral code.
944
- * @param args - The parameters for the request.
945
- * @param signal - An optional abort signal.
946
- * @returns Successful response without specific data.
947
- * @throws {ApiRequestError} When the API returns an error response.
948
- *
949
- * @see null - no documentation
950
- * @example
951
- * ```ts
952
- * import * as hl from "@nktkas/hyperliquid";
953
- * import { privateKeyToAccount } from "viem/accounts";
954
- *
955
- * const wallet = privateKeyToAccount("0x...");
956
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
957
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
958
- *
959
- * const data = await exchClient.registerReferrer({ code: "TEST" });
960
- * ```
961
- */
962
- async registerReferrer(args, signal) {
963
- // Destructure the parameters
964
- const { ...actionArgs } = args;
965
- // Construct an action
966
- const nonce = await this.nonceManager();
967
- const action = {
968
- type: "registerReferrer",
969
- ...actionArgs,
970
- };
971
- // Sign the action
972
- const signature = await (0, signing_js_1.signL1Action)({
973
- wallet: this.wallet,
974
- action: signing_js_1.actionSorter[action.type](action),
975
- nonce,
976
- isTestnet: this.isTestnet,
977
- });
978
- // Send a request
979
- return await this._request({ action, signature, nonce }, signal);
980
- }
981
- /**
982
- * Reserve additional rate-limited actions for a fee.
983
- * @param args - The parameters for the request.
984
- * @param signal - An optional abort signal.
985
- * @returns Successful response without specific data.
986
- * @throws {ApiRequestError} When the API returns an error response.
987
- *
988
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#reserve-additional-actions
989
- * @example
990
- * ```ts
991
- * import * as hl from "@nktkas/hyperliquid";
992
- * import { privateKeyToAccount } from "viem/accounts";
993
- *
994
- * const wallet = privateKeyToAccount("0x...");
995
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
996
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
997
- *
998
- * const data = await exchClient.reserveRequestWeight({ weight: 10 });
999
- * ```
1000
- */
1001
- async reserveRequestWeight(args, signal) {
1002
- // Destructure the parameters
1003
- const { expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1004
- // Construct an action
1005
- const nonce = await this.nonceManager();
1006
- const action = {
1007
- type: "reserveRequestWeight",
1008
- ...actionArgs,
1009
- };
1010
- // Sign the action
1011
- const signature = await (0, signing_js_1.signL1Action)({
1012
- wallet: this.wallet,
1013
- action: signing_js_1.actionSorter[action.type](action),
1014
- nonce,
1015
- isTestnet: this.isTestnet,
1016
- expiresAfter,
1017
- });
1018
- // Send a request
1019
- return await this._request({ action, signature, nonce, expiresAfter }, signal);
1020
- }
1021
- async scheduleCancel(args_or_signal, maybeSignal) {
1022
- const args = args_or_signal instanceof AbortSignal ? {} : args_or_signal ?? {};
1023
- const signal = args_or_signal instanceof AbortSignal ? args_or_signal : maybeSignal;
1024
- // Destructure the parameters
1025
- const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1026
- // Construct an action
1027
- const nonce = await this.nonceManager();
1028
- const action = {
1029
- type: "scheduleCancel",
1030
- ...actionArgs,
1031
- };
1032
- // Sign the action
1033
- const signature = await (0, signing_js_1.signL1Action)({
1034
- wallet: this.wallet,
1035
- action: signing_js_1.actionSorter[action.type](action),
1036
- nonce,
1037
- isTestnet: this.isTestnet,
1038
- vaultAddress,
1039
- expiresAfter,
1040
- });
1041
- // Send a request
1042
- return await this._request({ action, signature, nonce, vaultAddress, expiresAfter }, signal);
1043
- }
1044
- /**
1045
- * Set the display name in the leaderboard.
1046
- * @param args - The parameters for the request.
1047
- * @param signal - An optional abort signal.
1048
- * @returns Successful response without specific data.
1049
- * @throws {ApiRequestError} When the API returns an error response.
1050
- *
1051
- * @see null - no documentation
1052
- * @example
1053
- * ```ts
1054
- * import * as hl from "@nktkas/hyperliquid";
1055
- * import { privateKeyToAccount } from "viem/accounts";
1056
- *
1057
- * const wallet = privateKeyToAccount("0x...");
1058
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1059
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1060
- *
1061
- * const data = await exchClient.setDisplayName({ displayName: "My Name" });
1062
- * ```
1063
- */
1064
- async setDisplayName(args, signal) {
1065
- // Destructure the parameters
1066
- const { ...actionArgs } = args;
1067
- // Construct an action
1068
- const nonce = await this.nonceManager();
1069
- const action = {
1070
- type: "setDisplayName",
1071
- ...actionArgs,
1072
- };
1073
- // Sign the action
1074
- const signature = await (0, signing_js_1.signL1Action)({
1075
- wallet: this.wallet,
1076
- action: signing_js_1.actionSorter[action.type](action),
1077
- nonce,
1078
- isTestnet: this.isTestnet,
1079
- });
1080
- // Send a request
1081
- return await this._request({ action, signature, nonce }, signal);
1082
- }
1083
- /**
1084
- * Set a referral code.
1085
- * @param args - The parameters for the request.
1086
- * @param signal - An optional abort signal.
1087
- * @returns Successful response without specific data.
1088
- * @throws {ApiRequestError} When the API returns an error response.
1089
- *
1090
- * @see null - no documentation
1091
- * @example
1092
- * ```ts
1093
- * import * as hl from "@nktkas/hyperliquid";
1094
- * import { privateKeyToAccount } from "viem/accounts";
1095
- *
1096
- * const wallet = privateKeyToAccount("0x...");
1097
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1098
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1099
- *
1100
- * const data = await exchClient.setReferrer({ code: "TEST" });
1101
- * ```
1102
- */
1103
- async setReferrer(args, signal) {
1104
- // Destructure the parameters
1105
- const { ...actionArgs } = args;
1106
- // Construct an action
1107
- const nonce = await this.nonceManager();
1108
- const action = {
1109
- type: "setReferrer",
1110
- ...actionArgs,
1111
- };
1112
- // Sign the action
1113
- const signature = await (0, signing_js_1.signL1Action)({
1114
- wallet: this.wallet,
1115
- action: signing_js_1.actionSorter[action.type](action),
1116
- nonce,
1117
- isTestnet: this.isTestnet,
1118
- });
1119
- // Send a request
1120
- return await this._request({ action, signature, nonce }, signal);
1121
- }
1122
- async spotDeploy(args, signal) {
1123
- // Destructure the parameters
1124
- const { ...actionArgs } = args;
1125
- // Construct an action
1126
- const nonce = await this.nonceManager();
1127
- const action = {
1128
- type: "spotDeploy",
1129
- ...actionArgs,
1130
- };
1131
- // Sign the action
1132
- const signature = await (0, signing_js_1.signL1Action)({
1133
- wallet: this.wallet,
1134
- action: signing_js_1.actionSorter[action.type](action),
1135
- nonce,
1136
- isTestnet: this.isTestnet,
1137
- });
1138
- // Send a request
1139
- return await this._request({ action, signature, nonce }, signal);
1140
- }
1141
- /**
1142
- * Send spot assets to another address.
1143
- * @param args - The parameters for the request.
1144
- * @param signal - An optional abort signal.
1145
- * @returns Successful response without specific data.
1146
- * @throws {ApiRequestError} When the API returns an error response.
1147
- *
1148
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#core-spot-transfer
1149
- * @example
1150
- * ```ts
1151
- * import * as hl from "@nktkas/hyperliquid";
1152
- * import { privateKeyToAccount } from "viem/accounts";
1153
- *
1154
- * const wallet = privateKeyToAccount("0x...");
1155
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1156
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1157
- *
1158
- * const data = await exchClient.spotSend({
1159
- * destination: "0x...",
1160
- * token: "USDC:0xeb62eee3685fc4c43992febcd9e75443",
1161
- * amount: "1",
1162
- * });
1163
- * ```
1164
- */
1165
- async spotSend(args, signal) {
1166
- // Destructure the parameters
1167
- const { ...actionArgs } = args;
1168
- // Construct an action
1169
- const nonce = await this.nonceManager();
1170
- const action = {
1171
- ...actionArgs,
1172
- type: "spotSend",
1173
- hyperliquidChain: this._getHyperliquidChain(),
1174
- signatureChainId: await this._getSignatureChainId(),
1175
- time: nonce,
1176
- };
1177
- // Sign the action
1178
- const signature = await (0, signing_js_1.signUserSignedAction)({
1179
- wallet: this.wallet,
1180
- action,
1181
- types: signing_js_1.userSignedActionEip712Types[action.type],
1182
- chainId: parseInt(action.signatureChainId, 16),
1183
- });
1184
- // Send a request
1185
- return await this._request({ action, signature, nonce }, signal);
1186
- }
1187
- /**
1188
- * Opt Out of Spot Dusting.
1189
- * @param args - The parameters for the request.
1190
- * @param signal - An optional abort signal.
1191
- * @returns Successful response without specific data.
1192
- * @throws {ApiRequestError} When the API returns an error response.
1193
- *
1194
- * @see null - no documentation
1195
- * @example
1196
- * ```ts
1197
- * import * as hl from "@nktkas/hyperliquid";
1198
- * import { privateKeyToAccount } from "viem/accounts";
1199
- *
1200
- * const wallet = privateKeyToAccount("0x...");
1201
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1202
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1203
- *
1204
- * const data = await exchClient.spotUser({ toggleSpotDusting: { optOut: false } });
1205
- * ```
1206
- */
1207
- async spotUser(args, signal) {
1208
- // Destructure the parameters
1209
- const { ...actionArgs } = args;
1210
- // Construct an action
1211
- const nonce = await this.nonceManager();
1212
- const action = {
1213
- type: "spotUser",
1214
- ...actionArgs,
1215
- };
1216
- // Sign the action
1217
- const signature = await (0, signing_js_1.signL1Action)({
1218
- wallet: this.wallet,
1219
- action: signing_js_1.actionSorter[action.type](action),
1220
- nonce,
1221
- isTestnet: this.isTestnet,
1222
- });
1223
- // Send a request
1224
- return await this._request({ action, signature, nonce }, signal);
1225
- }
1226
- /**
1227
- * Transfer between sub-accounts (spot).
1228
- * @param args - The parameters for the request.
1229
- * @param signal - An optional abort signal.
1230
- * @returns Successful response without specific data.
1231
- * @throws {ApiRequestError} When the API returns an error response.
1232
- *
1233
- * @see null - no documentation
1234
- * @example
1235
- * ```ts
1236
- * import * as hl from "@nktkas/hyperliquid";
1237
- * import { privateKeyToAccount } from "viem/accounts";
1238
- *
1239
- * const wallet = privateKeyToAccount("0x...");
1240
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1241
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1242
- *
1243
- * const data = await exchClient.subAccountSpotTransfer({
1244
- * subAccountUser: "0x...",
1245
- * isDeposit: true,
1246
- * token: "USDC:0xeb62eee3685fc4c43992febcd9e75443",
1247
- * amount: "1",
1248
- * });
1249
- * ```
1250
- */
1251
- async subAccountSpotTransfer(args, signal) {
1252
- // Destructure the parameters
1253
- const { ...actionArgs } = args;
1254
- // Construct an action
1255
- const nonce = await this.nonceManager();
1256
- const action = {
1257
- type: "subAccountSpotTransfer",
1258
- ...actionArgs,
1259
- };
1260
- // Sign the action
1261
- const signature = await (0, signing_js_1.signL1Action)({
1262
- wallet: this.wallet,
1263
- action: signing_js_1.actionSorter[action.type](action),
1264
- nonce,
1265
- isTestnet: this.isTestnet,
1266
- });
1267
- // Send a request
1268
- return await this._request({ action, signature, nonce }, signal);
1269
- }
1270
- /**
1271
- * Transfer between sub-accounts (perpetual).
1272
- * @param args - The parameters for the request.
1273
- * @param signal - An optional abort signal.
1274
- * @returns Successful response without specific data.
1275
- * @throws {ApiRequestError} When the API returns an error response.
1276
- *
1277
- * @see null - no documentation
1278
- * @example
1279
- * ```ts
1280
- * import * as hl from "@nktkas/hyperliquid";
1281
- * import { privateKeyToAccount } from "viem/accounts";
1282
- *
1283
- * const wallet = privateKeyToAccount("0x...");
1284
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1285
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1286
- *
1287
- * const data = await exchClient.subAccountTransfer({
1288
- * subAccountUser: "0x...",
1289
- * isDeposit: true,
1290
- * usd: 1 * 1e6,
1291
- * });
1292
- * ```
1293
- */
1294
- async subAccountTransfer(args, signal) {
1295
- // Destructure the parameters
1296
- const { ...actionArgs } = args;
1297
- // Construct an action
1298
- const nonce = await this.nonceManager();
1299
- const action = {
1300
- type: "subAccountTransfer",
1301
- ...actionArgs,
1302
- };
1303
- // Sign the action
1304
- const signature = await (0, signing_js_1.signL1Action)({
1305
- wallet: this.wallet,
1306
- action: signing_js_1.actionSorter[action.type](action),
1307
- nonce,
1308
- isTestnet: this.isTestnet,
1309
- });
1310
- // Send a request
1311
- return await this._request({ action, signature, nonce }, signal);
1312
- }
1313
- /**
1314
- * Delegate or undelegate native tokens to or from a validator.
1315
- * @param args - The parameters for the request.
1316
- * @param signal - An optional abort signal.
1317
- * @returns Successful response without specific data.
1318
- * @throws {ApiRequestError} When the API returns an error response.
1319
- *
1320
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#delegate-or-undelegate-stake-from-validator
1321
- * @example
1322
- * ```ts
1323
- * import * as hl from "@nktkas/hyperliquid";
1324
- * import { privateKeyToAccount } from "viem/accounts";
1325
- *
1326
- * const wallet = privateKeyToAccount("0x...");
1327
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1328
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1329
- *
1330
- * const data = await exchClient.tokenDelegate({
1331
- * validator: "0x...",
1332
- * isUndelegate: true,
1333
- * wei: 1 * 1e8,
1334
- * });
1335
- * ```
1336
- */
1337
- async tokenDelegate(args, signal) {
1338
- // Destructure the parameters
1339
- const { ...actionArgs } = args;
1340
- // Construct an action
1341
- const nonce = await this.nonceManager();
1342
- const action = {
1343
- ...actionArgs,
1344
- type: "tokenDelegate",
1345
- hyperliquidChain: this._getHyperliquidChain(),
1346
- signatureChainId: await this._getSignatureChainId(),
1347
- nonce,
1348
- };
1349
- // Sign the action
1350
- const signature = await (0, signing_js_1.signUserSignedAction)({
1351
- wallet: this.wallet,
1352
- action,
1353
- types: signing_js_1.userSignedActionEip712Types[action.type],
1354
- chainId: parseInt(action.signatureChainId, 16),
1355
- });
1356
- // Send a request
1357
- return await this._request({ action, signature, nonce }, signal);
1358
- }
1359
- /**
1360
- * Cancel a TWAP order.
1361
- * @param args - The parameters for the request.
1362
- * @param signal - An optional abort signal.
1363
- * @returns Successful variant of {@link TwapCancelResponse} without error status.
1364
- * @throws {ApiRequestError} When the API returns an error response.
1365
- *
1366
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-a-twap-order
1367
- * @example
1368
- * ```ts
1369
- * import * as hl from "@nktkas/hyperliquid";
1370
- * import { privateKeyToAccount } from "viem/accounts";
1371
- *
1372
- * const wallet = privateKeyToAccount("0x...");
1373
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1374
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1375
- *
1376
- * const data = await exchClient.twapCancel({
1377
- * a: 0, // Asset index
1378
- * t: 1, // TWAP ID
1379
- * });
1380
- * ```
1381
- */
1382
- async twapCancel(args, signal) {
1383
- // Destructure the parameters
1384
- const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1385
- // Construct an action
1386
- const nonce = await this.nonceManager();
1387
- const action = {
1388
- type: "twapCancel",
1389
- ...actionArgs,
1390
- };
1391
- // Sign the action
1392
- const signature = await (0, signing_js_1.signL1Action)({
1393
- wallet: this.wallet,
1394
- action: signing_js_1.actionSorter[action.type](action),
1395
- nonce,
1396
- isTestnet: this.isTestnet,
1397
- vaultAddress,
1398
- expiresAfter,
1399
- });
1400
- // Send a request
1401
- return await this._request({ action, signature, nonce, vaultAddress, expiresAfter }, signal);
1402
- }
1403
- /**
1404
- * Place a TWAP order.
1405
- * @param args - The parameters for the request.
1406
- * @param signal - An optional abort signal.
1407
- * @returns Successful variant of {@link TwapOrderResponse} without error status.
1408
- * @throws {ApiRequestError} When the API returns an error response.
1409
- *
1410
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-a-twap-order
1411
- * @example
1412
- * ```ts
1413
- * import * as hl from "@nktkas/hyperliquid";
1414
- * import { privateKeyToAccount } from "viem/accounts";
1415
- *
1416
- * const wallet = privateKeyToAccount("0x...");
1417
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1418
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1419
- *
1420
- * const data = await exchClient.twapOrder({
1421
- * a: 0, // Asset index
1422
- * b: true, // Buy order
1423
- * s: "1", // Size
1424
- * r: false, // Not reduce-only
1425
- * m: 10, // Duration in minutes
1426
- * t: true, // Randomize order timing
1427
- * });
1428
- * ```
1429
- */
1430
- async twapOrder(args, signal) {
1431
- // Destructure the parameters
1432
- const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1433
- // Construct an action
1434
- const nonce = await this.nonceManager();
1435
- const action = {
1436
- type: "twapOrder",
1437
- twap: {
1438
- ...actionArgs,
1439
- },
1440
- };
1441
- // Sign the action
1442
- const signature = await (0, signing_js_1.signL1Action)({
1443
- wallet: this.wallet,
1444
- action: signing_js_1.actionSorter[action.type](action),
1445
- nonce,
1446
- isTestnet: this.isTestnet,
1447
- vaultAddress,
1448
- expiresAfter,
1449
- });
1450
- // Send a request
1451
- return await this._request({ action, signature, nonce, vaultAddress, expiresAfter }, signal);
1452
- }
1453
- /**
1454
- * Add or remove margin from isolated position.
1455
- * @param args - The parameters for the request.
1456
- * @param signal - An optional abort signal.
1457
- * @returns Successful response without specific data.
1458
- * @throws {ApiRequestError} When the API returns an error response.
1459
- *
1460
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-isolated-margin
1461
- * @example
1462
- * ```ts
1463
- * import * as hl from "@nktkas/hyperliquid";
1464
- * import { privateKeyToAccount } from "viem/accounts";
1465
- *
1466
- * const wallet = privateKeyToAccount("0x...");
1467
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1468
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1469
- *
1470
- * const data = await exchClient.updateIsolatedMargin({
1471
- * asset: 0,
1472
- * isBuy: true,
1473
- * ntli: 1 * 1e6,
1474
- * });
1475
- * ```
1476
- */
1477
- async updateIsolatedMargin(args, signal) {
1478
- // Destructure the parameters
1479
- const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1480
- // Construct an action
1481
- const nonce = await this.nonceManager();
1482
- const action = {
1483
- type: "updateIsolatedMargin",
1484
- ...actionArgs,
1485
- };
1486
- // Sign the action
1487
- const signature = await (0, signing_js_1.signL1Action)({
1488
- wallet: this.wallet,
1489
- action: signing_js_1.actionSorter[action.type](action),
1490
- nonce,
1491
- isTestnet: this.isTestnet,
1492
- vaultAddress,
1493
- expiresAfter,
1494
- });
1495
- // Send a request
1496
- return await this._request({ action, signature, nonce, vaultAddress, expiresAfter }, signal);
1497
- }
1498
- /**
1499
- * Update cross or isolated leverage on a coin.
1500
- * @param args - The parameters for the request.
1501
- * @param signal - An optional abort signal.
1502
- * @returns Successful response without specific data.
1503
- * @throws {ApiRequestError} When the API returns an error response.
1504
- *
1505
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-leverage
1506
- * @example
1507
- * ```ts
1508
- * import * as hl from "@nktkas/hyperliquid";
1509
- * import { privateKeyToAccount } from "viem/accounts";
1510
- *
1511
- * const wallet = privateKeyToAccount("0x...");
1512
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1513
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1514
- *
1515
- * const data = await exchClient.updateLeverage({
1516
- * asset: 0,
1517
- * isCross: true,
1518
- * leverage: 5,
1519
- * });
1520
- * ```
1521
- */
1522
- async updateLeverage(args, signal) {
1523
- // Destructure the parameters
1524
- const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1525
- // Construct an action
1526
- const nonce = await this.nonceManager();
1527
- const action = {
1528
- type: "updateLeverage",
1529
- ...actionArgs,
1530
- };
1531
- // Sign the action
1532
- const signature = await (0, signing_js_1.signL1Action)({
1533
- wallet: this.wallet,
1534
- action: signing_js_1.actionSorter[action.type](action),
1535
- nonce,
1536
- isTestnet: this.isTestnet,
1537
- vaultAddress,
1538
- expiresAfter,
1539
- });
1540
- // Send a request
1541
- return await this._request({ action, signature, nonce, vaultAddress, expiresAfter }, signal);
1542
- }
1543
- /**
1544
- * Transfer funds between Spot account and Perp account.
1545
- * @param args - The parameters for the request.
1546
- * @param signal - An optional abort signal.
1547
- * @returns Successful response without specific data.
1548
- * @throws {ApiRequestError} When the API returns an error response.
1549
- *
1550
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
1551
- * @example
1552
- * ```ts
1553
- * import * as hl from "@nktkas/hyperliquid";
1554
- * import { privateKeyToAccount } from "viem/accounts";
1555
- *
1556
- * const wallet = privateKeyToAccount("0x...");
1557
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1558
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1559
- *
1560
- * const data = await exchClient.usdClassTransfer({ amount: "1", toPerp: true });
1561
- * ```
1562
- */
1563
- async usdClassTransfer(args, signal) {
1564
- // Destructure the parameters
1565
- const { ...actionArgs } = args;
1566
- // Construct an action
1567
- const nonce = await this.nonceManager();
1568
- const action = {
1569
- ...actionArgs,
1570
- type: "usdClassTransfer",
1571
- hyperliquidChain: this._getHyperliquidChain(),
1572
- signatureChainId: await this._getSignatureChainId(),
1573
- nonce,
1574
- };
1575
- // Sign the action
1576
- const signature = await (0, signing_js_1.signUserSignedAction)({
1577
- wallet: this.wallet,
1578
- action,
1579
- types: signing_js_1.userSignedActionEip712Types[action.type],
1580
- chainId: parseInt(action.signatureChainId, 16),
1581
- });
1582
- // Send a request
1583
- return await this._request({ action, signature, nonce }, signal);
1584
- }
1585
- /**
1586
- * Send usd to another address.
1587
- * @param args - The parameters for the request.
1588
- * @param signal - An optional abort signal.
1589
- * @returns Successful response without specific data.
1590
- * @throws {ApiRequestError} When the API returns an error response.
1591
- *
1592
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#core-usdc-transfer
1593
- * @example
1594
- * ```ts
1595
- * import * as hl from "@nktkas/hyperliquid";
1596
- * import { privateKeyToAccount } from "viem/accounts";
1597
- *
1598
- * const wallet = privateKeyToAccount("0x...");
1599
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1600
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1601
- *
1602
- * const data = await exchClient.usdSend({ destination: "0x...", amount: "1" });
1603
- * ```
1604
- */
1605
- async usdSend(args, signal) {
1606
- // Destructure the parameters
1607
- const { ...actionArgs } = args;
1608
- // Construct an action
1609
- const nonce = await this.nonceManager();
1610
- const action = {
1611
- ...actionArgs,
1612
- type: "usdSend",
1613
- hyperliquidChain: this._getHyperliquidChain(),
1614
- signatureChainId: await this._getSignatureChainId(),
1615
- time: nonce,
1616
- };
1617
- // Sign the action
1618
- const signature = await (0, signing_js_1.signUserSignedAction)({
1619
- wallet: this.wallet,
1620
- action,
1621
- types: signing_js_1.userSignedActionEip712Types[action.type],
1622
- chainId: parseInt(action.signatureChainId, 16),
1623
- });
1624
- // Send a request
1625
- return await this._request({ action, signature, nonce }, signal);
1626
- }
1627
- /**
1628
- * Distribute funds from a vault between followers.
1629
- * @param args - The parameters for the request.
1630
- * @param signal - An optional abort signal.
1631
- * @returns Successful response without specific data.
1632
- * @throws {ApiRequestError} When the API returns an error response.
1633
- *
1634
- * @see null - no documentation
1635
- * @example
1636
- * ```ts
1637
- * import * as hl from "@nktkas/hyperliquid";
1638
- * import { privateKeyToAccount } from "viem/accounts";
1639
- *
1640
- * const wallet = privateKeyToAccount("0x...");
1641
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1642
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1643
- *
1644
- * const data = await exchClient.vaultDistribute({ vaultAddress: "0x...", usd: 10 * 1e6 });
1645
- * ```
1646
- */
1647
- async vaultDistribute(args, signal) {
1648
- // Destructure the parameters
1649
- const { ...actionArgs } = args;
1650
- // Construct an action
1651
- const nonce = await this.nonceManager();
1652
- const action = {
1653
- type: "vaultDistribute",
1654
- ...actionArgs,
1655
- };
1656
- // Sign the action
1657
- const signature = await (0, signing_js_1.signL1Action)({
1658
- wallet: this.wallet,
1659
- action: signing_js_1.actionSorter[action.type](action),
1660
- nonce,
1661
- isTestnet: this.isTestnet,
1662
- });
1663
- // Send a request
1664
- return await this._request({ action, signature, nonce }, signal);
49
+ getNonce() {
50
+ let nonce = Date.now();
51
+ if (nonce <= this.lastNonce) {
52
+ nonce = ++this.lastNonce;
1665
53
  }
1666
- /**
1667
- * Modify a vault's configuration.
1668
- * @param args - The parameters for the request.
1669
- * @param signal - An optional abort signal.
1670
- * @returns Successful response without specific data.
1671
- * @throws {ApiRequestError} When the API returns an error response.
1672
- *
1673
- * @see null - no documentation
1674
- * @example
1675
- * ```ts
1676
- * import * as hl from "@nktkas/hyperliquid";
1677
- * import { privateKeyToAccount } from "viem/accounts";
1678
- *
1679
- * const wallet = privateKeyToAccount("0x...");
1680
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1681
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1682
- *
1683
- * const data = await exchClient.vaultModify({
1684
- * vaultAddress: "0x...",
1685
- * allowDeposits: true,
1686
- * alwaysCloseOnWithdraw: false,
1687
- * });
1688
- * ```
1689
- */
1690
- async vaultModify(args, signal) {
1691
- // Destructure the parameters
1692
- const { ...actionArgs } = args;
1693
- // Construct an action
1694
- const nonce = await this.nonceManager();
1695
- const action = {
1696
- type: "vaultModify",
1697
- ...actionArgs,
1698
- };
1699
- // Sign the action
1700
- const signature = await (0, signing_js_1.signL1Action)({
1701
- wallet: this.wallet,
1702
- action: signing_js_1.actionSorter[action.type](action),
1703
- nonce,
1704
- isTestnet: this.isTestnet,
1705
- });
1706
- // Send a request
1707
- return await this._request({ action, signature, nonce }, signal);
54
+ else {
55
+ this.lastNonce = nonce;
1708
56
  }
1709
- /**
1710
- * Deposit or withdraw from a vault.
1711
- * @param args - The parameters for the request.
1712
- * @param signal - An optional abort signal.
1713
- * @returns Successful response without specific data.
1714
- * @throws {ApiRequestError} When the API returns an error response.
1715
- *
1716
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-or-withdraw-from-a-vault
1717
- * @example
1718
- * ```ts
1719
- * import * as hl from "@nktkas/hyperliquid";
1720
- * import { privateKeyToAccount } from "viem/accounts";
1721
- *
1722
- * const wallet = privateKeyToAccount("0x...");
1723
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1724
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1725
- *
1726
- * const data = await exchClient.vaultTransfer({
1727
- * vaultAddress: "0x...",
1728
- * isDeposit: true,
1729
- * usd: 10 * 1e6,
1730
- * });
1731
- * ```
1732
- */
1733
- async vaultTransfer(args, signal) {
1734
- // Destructure the parameters
1735
- const { expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1736
- // Construct an action
1737
- const nonce = await this.nonceManager();
1738
- const action = {
1739
- type: "vaultTransfer",
1740
- ...actionArgs,
1741
- };
1742
- // Sign the action
1743
- const signature = await (0, signing_js_1.signL1Action)({
1744
- wallet: this.wallet,
1745
- action: signing_js_1.actionSorter[action.type](action),
1746
- nonce,
1747
- isTestnet: this.isTestnet,
1748
- expiresAfter,
1749
- });
1750
- // Send a request
1751
- return await this._request({ action, signature, nonce, expiresAfter }, signal);
1752
- }
1753
- /**
1754
- * Initiate a withdrawal request.
1755
- * @param args - The parameters for the request.
1756
- * @param signal - An optional abort signal.
1757
- * @returns Successful response without specific data.
1758
- * @throws {ApiRequestError} When the API returns an error response.
1759
- *
1760
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#initiate-a-withdrawal-request
1761
- * @example
1762
- * ```ts
1763
- * import * as hl from "@nktkas/hyperliquid";
1764
- * import { privateKeyToAccount } from "viem/accounts";
1765
- *
1766
- * const wallet = privateKeyToAccount("0x...");
1767
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
1768
- * const exchClient = new hl.ExchangeClient({ wallet, transport });
1769
- *
1770
- * const data = await exchClient.withdraw3({ destination: "0x...", amount: "1" });
1771
- * ```
1772
- */
1773
- async withdraw3(args, signal) {
1774
- // Destructure the parameters
1775
- const { ...actionArgs } = args;
1776
- // Construct an action
1777
- const nonce = await this.nonceManager();
1778
- const action = {
1779
- ...actionArgs,
1780
- type: "withdraw3",
1781
- hyperliquidChain: this._getHyperliquidChain(),
1782
- signatureChainId: await this._getSignatureChainId(),
1783
- time: nonce,
1784
- };
1785
- // Sign the action
1786
- const signature = await (0, signing_js_1.signUserSignedAction)({
1787
- wallet: this.wallet,
1788
- action,
1789
- types: signing_js_1.userSignedActionEip712Types[action.type],
1790
- chainId: parseInt(action.signatureChainId, 16),
1791
- });
1792
- // Send a request
1793
- return await this._request({ action, signature, nonce }, signal);
1794
- }
1795
- /** Send an API request and validate the response. */
1796
- async _request(payload, signal) {
1797
- const response = await this.transport.request("exchange", payload, signal);
1798
- this._validateResponse(response);
1799
- return response;
1800
- }
1801
- /** Guesses the chain ID based on the wallet type or the isTestnet flag. */
1802
- async _guessSignatureChainId() {
1803
- // Trying to get chain ID of the wallet
1804
- if ((0, signing_js_1.isAbstractViemWalletClient)(this.wallet)) {
1805
- if ("getChainId" in this.wallet && typeof this.wallet.getChainId === "function") {
1806
- const chainId = await this.wallet.getChainId();
1807
- return `0x${chainId.toString(16)}`;
1808
- }
1809
- }
1810
- else if ((0, signing_js_1.isAbstractEthersSigner)(this.wallet) || (0, signing_js_1.isAbstractEthersV5Signer)(this.wallet)) {
1811
- if ("provider" in this.wallet &&
1812
- typeof this.wallet.provider === "object" && this.wallet.provider !== null &&
1813
- "getNetwork" in this.wallet.provider &&
1814
- typeof this.wallet.provider.getNetwork === "function") {
1815
- const network = await this.wallet.provider.getNetwork();
1816
- return `0x${network.chainId.toString(16)}`;
1817
- }
1818
- }
1819
- else if ((0, signing_js_1.isAbstractWindowEthereum)(this.wallet)) {
1820
- const [chainId] = await this.wallet.request({ method: "eth_chainId", params: [] });
1821
- return chainId;
57
+ return nonce;
58
+ }
59
+ }
60
+ /**
61
+ * Exchange client for interacting with the Hyperliquid API.
62
+ * @typeParam T The transport used to connect to the Hyperliquid API.
63
+ * @typeParam W The wallet used for signing transactions.
64
+ */
65
+ class ExchangeClient {
66
+ transport;
67
+ wallet;
68
+ isTestnet;
69
+ defaultVaultAddress;
70
+ defaultExpiresAfter;
71
+ signatureChainId;
72
+ nonceManager;
73
+ /**
74
+ * Initialises a new instance.
75
+ * @param args - The parameters for the client.
76
+ *
77
+ * @example Private key directly
78
+ * ```ts
79
+ * import * as hl from "@nktkas/hyperliquid";
80
+ *
81
+ * const privateKey = "0x...";
82
+ *
83
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
84
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
85
+ * ```
86
+ *
87
+ * @example [Viem](https://viem.sh/docs/clients/wallet#local-accounts-private-key-mnemonic-etc)
88
+ * ```ts
89
+ * import * as hl from "@nktkas/hyperliquid";
90
+ * import { privateKeyToAccount } from "viem/accounts";
91
+ *
92
+ * const account = privateKeyToAccount("0x...");
93
+ *
94
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
95
+ * const exchClient = new hl.ExchangeClient({ wallet: account, transport });
96
+ * ```
97
+ *
98
+ * @example [ethers.js](https://docs.ethers.org/v6/api/wallet/#Wallet) or [ethers.js v5](https://docs.ethers.org/v5/api/signer/#Wallet)
99
+ * ```ts
100
+ * import * as hl from "@nktkas/hyperliquid";
101
+ * import { ethers } from "ethers";
102
+ *
103
+ * const wallet = new ethers.Wallet("0x...");
104
+ *
105
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
106
+ * const exchClient = new hl.ExchangeClient({ wallet, transport });
107
+ * ```
108
+ *
109
+ * @example External wallet (e.g. MetaMask) via [viem](https://viem.sh/docs/clients/wallet)
110
+ * ```ts
111
+ * import * as hl from "@nktkas/hyperliquid";
112
+ * import { createWalletClient, custom } from "viem";
113
+ *
114
+ * const ethereum = (window as any).ethereum;
115
+ * const [account] = await ethereum.request({ method: "eth_requestAccounts" });
116
+ * const wallet = createWalletClient({ account, transport: custom(ethereum) });
117
+ *
118
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
119
+ * const exchClient = new hl.ExchangeClient({ wallet, transport });
120
+ * ```
121
+ *
122
+ * @example External wallet (e.g. MetaMask) via [`window.ethereum`](https://eips.ethereum.org/EIPS/eip-1193)
123
+ * ```ts
124
+ * import * as hl from "@nktkas/hyperliquid";
125
+ *
126
+ * const ethereum = (window as any).ethereum;
127
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
128
+ * const exchClient = new hl.ExchangeClient({ wallet: ethereum, transport });
129
+ * ```
130
+ */
131
+ constructor(args) {
132
+ this.transport = args.transport;
133
+ this.wallet = args.wallet;
134
+ this.isTestnet = args.isTestnet ?? false;
135
+ this.defaultVaultAddress = args.defaultVaultAddress;
136
+ this.defaultExpiresAfter = args.defaultExpiresAfter;
137
+ this.signatureChainId = args.signatureChainId ?? this._guessSignatureChainId;
138
+ this.nonceManager = args.nonceManager ?? new NonceManager().getNonce;
139
+ }
140
+ /**
141
+ * Approve an agent to sign on behalf of the master account.
142
+ * @param args - The parameters for the request.
143
+ * @param signal - An optional abort signal
144
+ * @returns Successful response without specific data.
145
+ *
146
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
147
+ * @throws {TransportError} When the transport layer throws an error.
148
+ *
149
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-an-api-wallet
150
+ * @example
151
+ * ```ts
152
+ * import * as hl from "@nktkas/hyperliquid";
153
+ *
154
+ * const privateKey = "0x..."; // or `viem`, `ethers`
155
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
156
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
157
+ *
158
+ * await exchClient.approveAgent({ agentAddress: "0x...", agentName: "..." });
159
+ * ```
160
+ */
161
+ async approveAgent(args, signal) {
162
+ // Destructure the parameters
163
+ const { ...actionArgs } = args;
164
+ // Construct an action
165
+ const nonce = await this.nonceManager();
166
+ const action = mod_js_1.actionSorter.approveAgent({
167
+ type: "approveAgent",
168
+ hyperliquidChain: this._getHyperliquidChain(),
169
+ signatureChainId: await this._getSignatureChainId(),
170
+ nonce,
171
+ ...actionArgs,
172
+ });
173
+ // Sign the action
174
+ const signature = await (0, mod_js_1.signUserSignedAction)({
175
+ wallet: this.wallet,
176
+ action,
177
+ types: mod_js_1.userSignedActionEip712Types[action.type],
178
+ });
179
+ if (action.agentName === "")
180
+ action.agentName = null;
181
+ // Send a request
182
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
183
+ this._validateResponse(response);
184
+ return response;
185
+ }
186
+ /**
187
+ * Approve a maximum fee rate for a builder.
188
+ * @param args - The parameters for the request.
189
+ * @param signal - An optional abort signal.
190
+ * @returns Successful response without specific data.
191
+ *
192
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
193
+ * @throws {TransportError} When the transport layer throws an error.
194
+ *
195
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-a-builder-fee
196
+ * @example
197
+ * ```ts
198
+ * import * as hl from "@nktkas/hyperliquid";
199
+ *
200
+ * const privateKey = "0x..."; // or `viem`, `ethers`
201
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
202
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
203
+ *
204
+ * await exchClient.approveBuilderFee({ maxFeeRate: "0.01%", builder: "0x..." });
205
+ * ```
206
+ */
207
+ async approveBuilderFee(args, signal) {
208
+ // Destructure the parameters
209
+ const { ...actionArgs } = args;
210
+ // Construct an action
211
+ const nonce = await this.nonceManager();
212
+ const action = mod_js_1.actionSorter.approveBuilderFee({
213
+ type: "approveBuilderFee",
214
+ hyperliquidChain: this._getHyperliquidChain(),
215
+ signatureChainId: await this._getSignatureChainId(),
216
+ nonce,
217
+ ...actionArgs,
218
+ });
219
+ // Sign the action
220
+ const signature = await (0, mod_js_1.signUserSignedAction)({
221
+ wallet: this.wallet,
222
+ action,
223
+ types: mod_js_1.userSignedActionEip712Types[action.type],
224
+ });
225
+ // Send a request
226
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
227
+ this._validateResponse(response);
228
+ return response;
229
+ }
230
+ /**
231
+ * Modify multiple orders.
232
+ * @param args - The parameters for the request.
233
+ * @param signal - An optional abort signal.
234
+ * @returns Successful variant of {@link OrderResponse} without error statuses.
235
+ *
236
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
237
+ * @throws {TransportError} When the transport layer throws an error.
238
+ *
239
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders
240
+ * @example
241
+ * ```ts
242
+ * import * as hl from "@nktkas/hyperliquid";
243
+ *
244
+ * const privateKey = "0x..."; // or `viem`, `ethers`
245
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
246
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
247
+ *
248
+ * const data = await exchClient.batchModify({
249
+ * modifies: [
250
+ * {
251
+ * oid: 123,
252
+ * order: {
253
+ * a: 0,
254
+ * b: true,
255
+ * p: "31000",
256
+ * s: "0.2",
257
+ * r: false,
258
+ * t: { limit: { tif: "Gtc" } },
259
+ * },
260
+ * },
261
+ * ],
262
+ * });
263
+ * ```
264
+ */
265
+ async batchModify(args, signal) {
266
+ // Destructure the parameters
267
+ const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
268
+ // Construct an action
269
+ const nonce = await this.nonceManager();
270
+ const action = mod_js_1.actionSorter.batchModify({ type: "batchModify", ...actionArgs });
271
+ // Sign the action
272
+ const signature = await (0, mod_js_1.signL1Action)({
273
+ wallet: this.wallet,
274
+ action,
275
+ nonce,
276
+ isTestnet: this.isTestnet,
277
+ vaultAddress,
278
+ expiresAfter,
279
+ });
280
+ // Send a request
281
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
282
+ this._validateResponse(response);
283
+ return response;
284
+ }
285
+ /**
286
+ * Cancel order(s).
287
+ * @param args - The parameters for the request.
288
+ * @param signal - An optional abort signal.
289
+ * @returns Successful variant of {@link CancelResponse} without error statuses.
290
+ *
291
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
292
+ * @throws {TransportError} When the transport layer throws an error.
293
+ *
294
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
295
+ * @example
296
+ * ```ts
297
+ * import * as hl from "@nktkas/hyperliquid";
298
+ *
299
+ * const privateKey = "0x..."; // or `viem`, `ethers`
300
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
301
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
302
+ *
303
+ * const data = await exchClient.cancel({
304
+ * cancels: [
305
+ * { a: 0, o: 123 },
306
+ * ],
307
+ * });
308
+ * ```
309
+ */
310
+ async cancel(args, signal) {
311
+ // Destructure the parameters
312
+ const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
313
+ // Construct an action
314
+ const nonce = await this.nonceManager();
315
+ const action = mod_js_1.actionSorter.cancel({ type: "cancel", ...actionArgs });
316
+ // Sign the action
317
+ const signature = await (0, mod_js_1.signL1Action)({
318
+ wallet: this.wallet,
319
+ action,
320
+ nonce,
321
+ isTestnet: this.isTestnet,
322
+ vaultAddress,
323
+ expiresAfter,
324
+ });
325
+ // Send a request
326
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
327
+ this._validateResponse(response);
328
+ return response;
329
+ }
330
+ /**
331
+ * Cancel order(s) by cloid.
332
+ * @param args - The parameters for the request.
333
+ * @param signal - An optional abort signal.
334
+ * @returns Successful variant of {@link CancelResponse} without error statuses.
335
+ *
336
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
337
+ * @throws {TransportError} When the transport layer throws an error.
338
+ *
339
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid
340
+ * @example
341
+ * ```ts
342
+ * import * as hl from "@nktkas/hyperliquid";
343
+ *
344
+ * const privateKey = "0x..."; // or `viem`, `ethers`
345
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
346
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
347
+ *
348
+ * const data = await exchClient.cancelByCloid({
349
+ * cancels: [
350
+ * { asset: 0, cloid: "0x..." },
351
+ * ],
352
+ * });
353
+ * ```
354
+ */
355
+ async cancelByCloid(args, signal) {
356
+ // Destructure the parameters
357
+ const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
358
+ // Construct an action
359
+ const nonce = await this.nonceManager();
360
+ const action = mod_js_1.actionSorter.cancelByCloid({ type: "cancelByCloid", ...actionArgs });
361
+ // Sign the action
362
+ const signature = await (0, mod_js_1.signL1Action)({
363
+ wallet: this.wallet,
364
+ action,
365
+ nonce,
366
+ isTestnet: this.isTestnet,
367
+ vaultAddress,
368
+ expiresAfter,
369
+ });
370
+ // Send a request
371
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
372
+ this._validateResponse(response);
373
+ return response;
374
+ }
375
+ /**
376
+ * Transfer native token from the user's spot account into staking for delegating to validators.
377
+ * @param args - The parameters for the request.
378
+ * @param signal - An optional abort signal.
379
+ * @returns Successful response without specific data.
380
+ *
381
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
382
+ * @throws {TransportError} When the transport layer throws an error.
383
+ *
384
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-into-staking
385
+ * @example
386
+ * ```ts
387
+ * import * as hl from "@nktkas/hyperliquid";
388
+ *
389
+ * const privateKey = "0x..."; // or `viem`, `ethers`
390
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
391
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
392
+ *
393
+ * await exchClient.cDeposit({ wei: 1 * 1e8 });
394
+ * ```
395
+ */
396
+ async cDeposit(args, signal) {
397
+ // Destructure the parameters
398
+ const { ...actionArgs } = args;
399
+ // Construct an action
400
+ const nonce = await this.nonceManager();
401
+ const action = mod_js_1.actionSorter.cDeposit({
402
+ type: "cDeposit",
403
+ hyperliquidChain: this._getHyperliquidChain(),
404
+ signatureChainId: await this._getSignatureChainId(),
405
+ nonce,
406
+ ...actionArgs,
407
+ });
408
+ // Sign the action
409
+ const signature = await (0, mod_js_1.signUserSignedAction)({
410
+ wallet: this.wallet,
411
+ action,
412
+ types: mod_js_1.userSignedActionEip712Types[action.type],
413
+ });
414
+ // Send a request
415
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
416
+ this._validateResponse(response);
417
+ return response;
418
+ }
419
+ /**
420
+ * Claim rewards from referral program.
421
+ * @param args - The parameters for the request.
422
+ * @param signal - An optional abort signal.
423
+ * @returns Successful response without specific data.
424
+ *
425
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
426
+ * @throws {TransportError} When the transport layer throws an error.
427
+ *
428
+ * @see null
429
+ * @example
430
+ * ```ts
431
+ * import * as hl from "@nktkas/hyperliquid";
432
+ *
433
+ * const privateKey = "0x..."; // or `viem`, `ethers`
434
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
435
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
436
+ *
437
+ * await exchClient.claimRewards();
438
+ * ```
439
+ */
440
+ async claimRewards(signal) {
441
+ // Construct an action
442
+ const nonce = await this.nonceManager();
443
+ const action = mod_js_1.actionSorter.claimRewards({ type: "claimRewards" });
444
+ // Sign the action
445
+ const signature = await (0, mod_js_1.signL1Action)({
446
+ wallet: this.wallet,
447
+ action,
448
+ nonce,
449
+ isTestnet: this.isTestnet,
450
+ });
451
+ // Send a request
452
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
453
+ this._validateResponse(response);
454
+ return response;
455
+ }
456
+ /**
457
+ * Convert a single-signature account to a multi-signature account or vice versa.
458
+ * @param args - The parameters for the request.
459
+ * @param signal - An optional abort signal.
460
+ * @returns Successful response without specific data.
461
+ *
462
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
463
+ * @throws {TransportError} When the transport layer throws an error.
464
+ *
465
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/hypercore/multi-sig
466
+ * @example
467
+ * ```ts
468
+ * import * as hl from "@nktkas/hyperliquid";
469
+ *
470
+ * const privateKey = "0x..."; // or `viem`, `ethers`
471
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
472
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
473
+ *
474
+ * // Convert to multi-sig user
475
+ * await exchClient.convertToMultiSigUser({
476
+ * authorizedUsers: ["0x...", "0x...", "0x..."],
477
+ * threshold: 2,
478
+ * });
479
+ *
480
+ * // Convert to single-sig user
481
+ * await exchClient.convertToMultiSigUser(null);
482
+ * ```
483
+ */
484
+ async convertToMultiSigUser(args, signal) {
485
+ // Destructure the parameters
486
+ const actionArgs = args;
487
+ // Construct an action
488
+ const nonce = await this.nonceManager();
489
+ const action = mod_js_1.actionSorter.convertToMultiSigUser({
490
+ type: "convertToMultiSigUser",
491
+ hyperliquidChain: this._getHyperliquidChain(),
492
+ signatureChainId: await this._getSignatureChainId(),
493
+ signers: JSON.stringify(actionArgs),
494
+ nonce,
495
+ });
496
+ // Sign the action
497
+ const signature = await (0, mod_js_1.signUserSignedAction)({
498
+ wallet: this.wallet,
499
+ action,
500
+ types: mod_js_1.userSignedActionEip712Types[action.type],
501
+ });
502
+ // Send a request
503
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
504
+ this._validateResponse(response);
505
+ return response;
506
+ }
507
+ /**
508
+ * Create a sub-account.
509
+ * @param args - The parameters for the request.
510
+ * @param signal - An optional abort signal.
511
+ * @returns Response for creating a sub-account.
512
+ *
513
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
514
+ * @throws {TransportError} When the transport layer throws an error.
515
+ *
516
+ * @see null
517
+ * @example
518
+ * ```ts
519
+ * import * as hl from "@nktkas/hyperliquid";
520
+ *
521
+ * const privateKey = "0x..."; // or `viem`, `ethers`
522
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
523
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
524
+ *
525
+ * const data = await exchClient.createSubAccount({ name: "..." });
526
+ * ```
527
+ */
528
+ async createSubAccount(args, signal) {
529
+ // Destructure the parameters
530
+ const { ...actionArgs } = args;
531
+ // Construct an action
532
+ const nonce = await this.nonceManager();
533
+ const action = mod_js_1.actionSorter.createSubAccount({ type: "createSubAccount", ...actionArgs });
534
+ // Sign the action
535
+ const signature = await (0, mod_js_1.signL1Action)({
536
+ wallet: this.wallet,
537
+ action,
538
+ nonce,
539
+ isTestnet: this.isTestnet,
540
+ });
541
+ // Send a request
542
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
543
+ this._validateResponse(response);
544
+ return response;
545
+ }
546
+ /**
547
+ * Create a vault.
548
+ * @param args - The parameters for the request.
549
+ * @param signal - An optional abort signal.
550
+ * @returns Response for creating a vault.
551
+ *
552
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
553
+ * @throws {TransportError} When the transport layer throws an error.
554
+ *
555
+ * @see null
556
+ * @example
557
+ * ```ts
558
+ * import * as hl from "@nktkas/hyperliquid";
559
+ *
560
+ * const privateKey = "0x..."; // or `viem`, `ethers`
561
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
562
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
563
+ *
564
+ * const data = await exchClient.createVault({ name: "...", description: "...", initialUsd: 100 * 1e6 });
565
+ * ```
566
+ */
567
+ async createVault(args, signal) {
568
+ // Destructure the parameters
569
+ const { ...actionArgs } = args;
570
+ // Construct an action
571
+ const nonce = await this.nonceManager();
572
+ const action = mod_js_1.actionSorter.createVault({ type: "createVault", nonce, ...actionArgs });
573
+ // Sign the action
574
+ const signature = await (0, mod_js_1.signL1Action)({
575
+ wallet: this.wallet,
576
+ action,
577
+ nonce,
578
+ isTestnet: this.isTestnet,
579
+ });
580
+ // Send a request
581
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
582
+ this._validateResponse(response);
583
+ return response;
584
+ }
585
+ /**
586
+ * Jail or unjail self as a validator signer.
587
+ * @param args - The parameters for the request.
588
+ * @param signal - An optional abort signal.
589
+ * @returns Successful response without specific data.
590
+ *
591
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
592
+ * @throws {TransportError} When the transport layer throws an error.
593
+ *
594
+ * @see null
595
+ * @example
596
+ * ```ts
597
+ * import * as hl from "@nktkas/hyperliquid";
598
+ *
599
+ * const privateKey = "0x..."; // or `viem`, `ethers`
600
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
601
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
602
+ *
603
+ * // Jail self
604
+ * await exchClient.cSignerAction({ jailSelf: null });
605
+ *
606
+ * // Unjail self
607
+ * await exchClient.cSignerAction({ unjailSelf: null });
608
+ * ```
609
+ */
610
+ async cSignerAction(args, signal) {
611
+ // Destructure the parameters
612
+ const { expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
613
+ // Construct an action
614
+ const nonce = await this.nonceManager();
615
+ const action = mod_js_1.actionSorter.CSignerAction({ type: "CSignerAction", ...actionArgs });
616
+ // Sign the action
617
+ const signature = await (0, mod_js_1.signL1Action)({
618
+ wallet: this.wallet,
619
+ action,
620
+ nonce,
621
+ isTestnet: this.isTestnet,
622
+ expiresAfter,
623
+ });
624
+ // Send a request
625
+ const response = await this.transport.request("exchange", { action, signature, nonce, expiresAfter }, signal);
626
+ this._validateResponse(response);
627
+ return response;
628
+ }
629
+ /**
630
+ * Action related to validator management.
631
+ * @param args - The parameters for the request.
632
+ * @param signal - An optional abort signal.
633
+ * @returns Successful response without specific data.
634
+ *
635
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
636
+ * @throws {TransportError} When the transport layer throws an error.
637
+ *
638
+ * @see null
639
+ * @example
640
+ * ```ts
641
+ * import * as hl from "@nktkas/hyperliquid";
642
+ *
643
+ * const privateKey = "0x..."; // or `viem`, `ethers`
644
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
645
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
646
+ *
647
+ * // Change validator profile
648
+ * await exchClient.cValidatorAction({
649
+ * changeProfile: {
650
+ * name: "...",
651
+ * description: "...",
652
+ * unjailed: true,
653
+ * }
654
+ * });
655
+ *
656
+ * // Register a new validator
657
+ * await exchClient.cValidatorAction({
658
+ * register: {
659
+ * profile: {
660
+ * node_ip: { Ip: "1.2.3.4" },
661
+ * name: "...",
662
+ * description: "...",
663
+ * delegations_disabled: true,
664
+ * commission_bps: 1,
665
+ * signer: "0x...",
666
+ * },
667
+ * unjailed: false,
668
+ * initial_wei: 1,
669
+ * },
670
+ * });
671
+ *
672
+ * // Unregister a validator
673
+ * await exchClient.cValidatorAction({ unregister: null });
674
+ * ```
675
+ */
676
+ async cValidatorAction(args, signal) {
677
+ // Destructure the parameters
678
+ const { expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
679
+ // Construct an action
680
+ const nonce = await this.nonceManager();
681
+ const action = mod_js_1.actionSorter.CValidatorAction({ type: "CValidatorAction", ...actionArgs });
682
+ // Sign the action
683
+ const signature = await (0, mod_js_1.signL1Action)({
684
+ wallet: this.wallet,
685
+ action,
686
+ nonce,
687
+ isTestnet: this.isTestnet,
688
+ expiresAfter,
689
+ });
690
+ // Send a request
691
+ const response = await this.transport.request("exchange", { action, signature, nonce, expiresAfter }, signal);
692
+ this._validateResponse(response);
693
+ return response;
694
+ }
695
+ /**
696
+ * Transfer native token from staking into the user's spot account.
697
+ * @param args - The parameters for the request.
698
+ * @param signal - An optional abort signal.
699
+ * @returns Successful response without specific data.
700
+ *
701
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
702
+ * @throws {TransportError} When the transport layer throws an error.
703
+ *
704
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#withdraw-from-staking
705
+ * @example
706
+ * ```ts
707
+ * import * as hl from "@nktkas/hyperliquid";
708
+ *
709
+ * const privateKey = "0x..."; // or `viem`, `ethers`
710
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
711
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
712
+ *
713
+ * await exchClient.cWithdraw({ wei: 1 * 1e8 });
714
+ * ```
715
+ */
716
+ async cWithdraw(args, signal) {
717
+ // Destructure the parameters
718
+ const { ...actionArgs } = args;
719
+ // Construct an action
720
+ const nonce = await this.nonceManager();
721
+ const action = mod_js_1.actionSorter.cWithdraw({
722
+ type: "cWithdraw",
723
+ hyperliquidChain: this._getHyperliquidChain(),
724
+ signatureChainId: await this._getSignatureChainId(),
725
+ nonce,
726
+ ...actionArgs,
727
+ });
728
+ // Sign the action
729
+ const signature = await (0, mod_js_1.signUserSignedAction)({
730
+ wallet: this.wallet,
731
+ action,
732
+ types: mod_js_1.userSignedActionEip712Types[action.type],
733
+ });
734
+ // Send a request
735
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
736
+ this._validateResponse(response);
737
+ return response;
738
+ }
739
+ /**
740
+ * Configure block type for EVM transactions.
741
+ * @param args - The parameters for the request.
742
+ * @param signal - An optional abort signal.
743
+ * @returns Response for creating a sub-account.
744
+ *
745
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
746
+ * @throws {TransportError} When the transport layer throws an error.
747
+ *
748
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/evm/dual-block-architecture
749
+ * @example
750
+ * ```ts
751
+ * import * as hl from "@nktkas/hyperliquid";
752
+ *
753
+ * const privateKey = "0x..."; // or `viem`, `ethers`
754
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
755
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
756
+ *
757
+ * const data = await exchClient.evmUserModify({ usingBigBlocks: true });
758
+ * ```
759
+ */
760
+ async evmUserModify(args, signal) {
761
+ // Destructure the parameters
762
+ const { ...actionArgs } = args;
763
+ // Construct an action
764
+ const nonce = await this.nonceManager();
765
+ const action = mod_js_1.actionSorter.evmUserModify({ type: "evmUserModify", ...actionArgs });
766
+ // Sign the action
767
+ const signature = await (0, mod_js_1.signL1Action)({
768
+ wallet: this.wallet,
769
+ action,
770
+ nonce,
771
+ isTestnet: this.isTestnet,
772
+ });
773
+ // Send a request
774
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
775
+ this._validateResponse(response);
776
+ return response;
777
+ }
778
+ /**
779
+ * Modify an order.
780
+ * @param args - The parameters for the request.
781
+ * @param signal - An optional abort signal.
782
+ * @returns Successful response without specific data.
783
+ *
784
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
785
+ * @throws {TransportError} When the transport layer throws an error.
786
+ *
787
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-an-order
788
+ * @example
789
+ * ```ts
790
+ * import * as hl from "@nktkas/hyperliquid";
791
+ *
792
+ * const privateKey = "0x..."; // or `viem`, `ethers`
793
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
794
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
795
+ *
796
+ * await exchClient.modify({
797
+ * oid: 123,
798
+ * order: {
799
+ * a: 0,
800
+ * b: true,
801
+ * p: "31000",
802
+ * s: "0.2",
803
+ * r: false,
804
+ * t: { limit: { tif: "Gtc" } },
805
+ * c: "0x...",
806
+ * },
807
+ * });
808
+ * ```
809
+ */
810
+ async modify(args, signal) {
811
+ // Destructure the parameters
812
+ const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
813
+ // Construct an action
814
+ const nonce = await this.nonceManager();
815
+ const action = mod_js_1.actionSorter.modify({ type: "modify", ...actionArgs });
816
+ // Sign the action
817
+ const signature = await (0, mod_js_1.signL1Action)({
818
+ wallet: this.wallet,
819
+ action,
820
+ nonce,
821
+ isTestnet: this.isTestnet,
822
+ vaultAddress,
823
+ expiresAfter,
824
+ });
825
+ // Send a request
826
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
827
+ this._validateResponse(response);
828
+ return response;
829
+ }
830
+ /**
831
+ * A multi-signature request.
832
+ * @param args - The parameters for the request.
833
+ * @param signal - An optional abort signal.
834
+ * @returns Any successful response.
835
+ *
836
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
837
+ * @throws {TransportError} When the transport layer throws an error.
838
+ *
839
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/hypercore/multi-sig
840
+ * @example
841
+ * ```ts
842
+ * import * as hl from "@nktkas/hyperliquid";
843
+ * import { actionSorter, signL1Action } from "@nktkas/hyperliquid/signing";
844
+ * import { privateKeyToAccount } from "viem/accounts";
845
+ *
846
+ * const wallet = privateKeyToAccount("0x..."); // or any other wallet libraries
847
+ * const multiSigUser = "0x...";
848
+ *
849
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
850
+ * const exchClient = new hl.ExchangeClient({ wallet, transport });
851
+ *
852
+ * const nonce = Date.now();
853
+ * const action = {
854
+ * type: "scheduleCancel",
855
+ * time: Date.now() + 10000,
856
+ * } as const;
857
+ *
858
+ * // Create the required number of signatures
859
+ * const signature = await signL1Action({
860
+ * wallet,
861
+ * action: [multiSigUser.toLowerCase(), wallet.address.toLowerCase(), actionSorter[action.type](action)],
862
+ * nonce,
863
+ * });
864
+ *
865
+ * const data = await exchClient.multiSig({
866
+ * signatures: [signature],
867
+ * payload: {
868
+ * multiSigUser,
869
+ * outerSigner: wallet.address,
870
+ * action,
871
+ * },
872
+ * nonce,
873
+ * });
874
+ * ```
875
+ */
876
+ async multiSig(args, signal) {
877
+ // Destructure the parameters
878
+ const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), nonce, ...actionArgs } = args;
879
+ // Construct an action
880
+ const action = mod_js_1.actionSorter.multiSig({
881
+ type: "multiSig",
882
+ signatureChainId: await this._getSignatureChainId(),
883
+ ...actionArgs,
884
+ });
885
+ // Sign the action
886
+ // deno-lint-ignore no-explicit-any
887
+ const actionWithoutType = structuredClone(action);
888
+ delete actionWithoutType.type;
889
+ const signature = await (0, mod_js_1.signMultiSigAction)({
890
+ wallet: this.wallet,
891
+ action: actionWithoutType,
892
+ nonce,
893
+ isTestnet: this.isTestnet,
894
+ vaultAddress,
895
+ expiresAfter,
896
+ });
897
+ // Send a request
898
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
899
+ this._validateResponse(response);
900
+ return response;
901
+ }
902
+ /**
903
+ * Place an order(s).
904
+ * @param args - The parameters for the request.
905
+ * @param signal - An optional abort signal.
906
+ * @returns Successful variant of {@link OrderResponse} without error statuses.
907
+ *
908
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
909
+ * @throws {TransportError} When the transport layer throws an error.
910
+ *
911
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order
912
+ * @example
913
+ * ```ts
914
+ * import * as hl from "@nktkas/hyperliquid";
915
+ *
916
+ * const privateKey = "0x..."; // or `viem`, `ethers`
917
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
918
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
919
+ *
920
+ * const data = await exchClient.order({
921
+ * orders: [
922
+ * {
923
+ * a: 0,
924
+ * b: true,
925
+ * p: "30000",
926
+ * s: "0.1",
927
+ * r: false,
928
+ * t: { limit: { tif: "Gtc" } },
929
+ * c: "0x...",
930
+ * },
931
+ * ],
932
+ * grouping: "na",
933
+ * });
934
+ * ```
935
+ */
936
+ async order(args, signal) {
937
+ // Destructure the parameters
938
+ const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
939
+ // Construct an action
940
+ const nonce = await this.nonceManager();
941
+ const action = mod_js_1.actionSorter.order({ type: "order", ...actionArgs });
942
+ // Sign the action
943
+ const signature = await (0, mod_js_1.signL1Action)({
944
+ wallet: this.wallet,
945
+ action,
946
+ nonce,
947
+ isTestnet: this.isTestnet,
948
+ vaultAddress,
949
+ expiresAfter,
950
+ });
951
+ // Send a request
952
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
953
+ this._validateResponse(response);
954
+ return response;
955
+ }
956
+ /**
957
+ * Deploying HIP-3 assets.
958
+ * @param args - The parameters for the request.
959
+ * @param signal - An optional abort signal.
960
+ * @returns Successful response without specific data.
961
+ *
962
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
963
+ * @throws {TransportError} When the transport layer throws an error.
964
+ *
965
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/deploying-hip-3-assets
966
+ * @example
967
+ * ```ts
968
+ * import * as hl from "@nktkas/hyperliquid";
969
+ *
970
+ * const privateKey = "0x..."; // or `viem`, `ethers`
971
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
972
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
973
+ *
974
+ * await exchClient.perpDeploy({
975
+ * registerAsset: {
976
+ * maxGas: 1000000,
977
+ * assetRequest: {
978
+ * coin: "USDC",
979
+ * szDecimals: 8,
980
+ * oraclePx: "1",
981
+ * marginTableId: 1,
982
+ * onlyIsolated: false,
983
+ * },
984
+ * dex: "test",
985
+ * },
986
+ * });
987
+ * ```
988
+ */
989
+ async perpDeploy(args, signal) {
990
+ // Destructure the parameters
991
+ const { ...actionArgs } = args;
992
+ // Construct an action
993
+ const nonce = await this.nonceManager();
994
+ const action = mod_js_1.actionSorter.perpDeploy({ type: "perpDeploy", ...actionArgs });
995
+ // Sign the action
996
+ const signature = await (0, mod_js_1.signL1Action)({
997
+ wallet: this.wallet,
998
+ action,
999
+ nonce,
1000
+ isTestnet: this.isTestnet,
1001
+ });
1002
+ // Send a request
1003
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1004
+ this._validateResponse(response);
1005
+ return response;
1006
+ }
1007
+ /**
1008
+ * Transfer funds between Spot account and Perp dex account.
1009
+ * @param args - The parameters for the request.
1010
+ * @param signal - An optional abort signal.
1011
+ * @returns Successful response without specific data.
1012
+ *
1013
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1014
+ * @throws {TransportError} When the transport layer throws an error.
1015
+ *
1016
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
1017
+ * @example
1018
+ * ```ts
1019
+ * import * as hl from "@nktkas/hyperliquid";
1020
+ *
1021
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1022
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1023
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1024
+ *
1025
+ * await exchClient.perpDexClassTransfer({ dex: "test", token: "USDC", amount: "1", toPerp: true });
1026
+ * ```
1027
+ */
1028
+ async perpDexClassTransfer(args, signal) {
1029
+ // Destructure the parameters
1030
+ const { ...actionArgs } = args;
1031
+ // Construct an action
1032
+ const nonce = await this.nonceManager();
1033
+ const action = mod_js_1.actionSorter.PerpDexClassTransfer({
1034
+ type: "PerpDexClassTransfer",
1035
+ hyperliquidChain: this._getHyperliquidChain(),
1036
+ signatureChainId: await this._getSignatureChainId(),
1037
+ nonce,
1038
+ ...actionArgs,
1039
+ });
1040
+ // Sign the action
1041
+ const signature = await (0, mod_js_1.signUserSignedAction)({
1042
+ wallet: this.wallet,
1043
+ action,
1044
+ types: mod_js_1.userSignedActionEip712Types[action.type],
1045
+ });
1046
+ // Send a request
1047
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1048
+ this._validateResponse(response);
1049
+ return response;
1050
+ }
1051
+ /**
1052
+ * Transfer collateral tokens between different perp dexes for the same user.
1053
+ * @param args - The parameters for the request.
1054
+ * @param signal - An optional abort signal.
1055
+ * @returns Successful response without specific data.
1056
+ *
1057
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1058
+ * @throws {TransportError} When the transport layer throws an error.
1059
+ *
1060
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-perp-account-to-perp-account-for-builder-deployed-dex
1061
+ * @example
1062
+ * ```ts
1063
+ * import * as hl from "@nktkas/hyperliquid";
1064
+ *
1065
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1066
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1067
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1068
+ *
1069
+ * await exchClient.perpDexTransfer({ sourceDex: "", destinationDex: "test", amount: "1" });
1070
+ * ```
1071
+ */
1072
+ async perpDexTransfer(args, signal) {
1073
+ // Destructure the parameters
1074
+ const { ...actionArgs } = args;
1075
+ // Construct an action
1076
+ const nonce = await this.nonceManager();
1077
+ const action = mod_js_1.actionSorter.PerpDexTransfer({
1078
+ type: "PerpDexTransfer",
1079
+ hyperliquidChain: this._getHyperliquidChain(),
1080
+ signatureChainId: await this._getSignatureChainId(),
1081
+ nonce,
1082
+ ...actionArgs,
1083
+ });
1084
+ // Sign the action
1085
+ const signature = await (0, mod_js_1.signUserSignedAction)({
1086
+ wallet: this.wallet,
1087
+ action,
1088
+ types: mod_js_1.userSignedActionEip712Types[action.type],
1089
+ });
1090
+ // Send a request
1091
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1092
+ this._validateResponse(response);
1093
+ return response;
1094
+ }
1095
+ /**
1096
+ * Create a referral code.
1097
+ * @param args - The parameters for the request.
1098
+ * @param signal - An optional abort signal.
1099
+ * @returns Successful response without specific data.
1100
+ *
1101
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1102
+ * @throws {TransportError} When the transport layer throws an error.
1103
+ *
1104
+ * @see null
1105
+ * @example
1106
+ * ```ts
1107
+ * import * as hl from "@nktkas/hyperliquid";
1108
+ *
1109
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1110
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1111
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1112
+ *
1113
+ * await exchClient.registerReferrer({ code: "..." });
1114
+ * ```
1115
+ */
1116
+ async registerReferrer(args, signal) {
1117
+ // Destructure the parameters
1118
+ const { ...actionArgs } = args;
1119
+ // Construct an action
1120
+ const nonce = await this.nonceManager();
1121
+ const action = mod_js_1.actionSorter.registerReferrer({ type: "registerReferrer", ...actionArgs });
1122
+ // Sign the action
1123
+ const signature = await (0, mod_js_1.signL1Action)({
1124
+ wallet: this.wallet,
1125
+ action,
1126
+ nonce,
1127
+ isTestnet: this.isTestnet,
1128
+ });
1129
+ // Send a request
1130
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1131
+ this._validateResponse(response);
1132
+ return response;
1133
+ }
1134
+ /**
1135
+ * Reserve additional rate-limited actions for a fee.
1136
+ * @param args - The parameters for the request.
1137
+ * @param signal - An optional abort signal.
1138
+ * @returns Successful response without specific data.
1139
+ *
1140
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1141
+ * @throws {TransportError} When the transport layer throws an error.
1142
+ *
1143
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#reserve-additional-actions
1144
+ * @example
1145
+ * ```ts
1146
+ * import * as hl from "@nktkas/hyperliquid";
1147
+ *
1148
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1149
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1150
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1151
+ *
1152
+ * await exchClient.reserveRequestWeight({ weight: 10 });
1153
+ * ```
1154
+ */
1155
+ async reserveRequestWeight(args, signal) {
1156
+ // Destructure the parameters
1157
+ const { expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1158
+ // Construct an action
1159
+ const nonce = await this.nonceManager();
1160
+ const action = mod_js_1.actionSorter.reserveRequestWeight({ type: "reserveRequestWeight", ...actionArgs });
1161
+ // Sign the action
1162
+ const signature = await (0, mod_js_1.signL1Action)({
1163
+ wallet: this.wallet,
1164
+ action,
1165
+ nonce,
1166
+ isTestnet: this.isTestnet,
1167
+ expiresAfter,
1168
+ });
1169
+ // Send a request
1170
+ const response = await this.transport.request("exchange", { action, signature, nonce, expiresAfter }, signal);
1171
+ this._validateResponse(response);
1172
+ return response;
1173
+ }
1174
+ async scheduleCancel(args_or_signal, maybeSignal) {
1175
+ const args = args_or_signal instanceof AbortSignal ? {} : args_or_signal ?? {};
1176
+ const signal = args_or_signal instanceof AbortSignal ? args_or_signal : maybeSignal;
1177
+ // Destructure the parameters
1178
+ const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1179
+ // Construct an action
1180
+ const nonce = await this.nonceManager();
1181
+ const action = mod_js_1.actionSorter.scheduleCancel({ type: "scheduleCancel", ...actionArgs });
1182
+ // Sign the action
1183
+ const signature = await (0, mod_js_1.signL1Action)({
1184
+ wallet: this.wallet,
1185
+ action,
1186
+ nonce,
1187
+ isTestnet: this.isTestnet,
1188
+ vaultAddress,
1189
+ expiresAfter,
1190
+ });
1191
+ // Send a request
1192
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
1193
+ this._validateResponse(response);
1194
+ return response;
1195
+ }
1196
+ /**
1197
+ * Set the display name in the leaderboard.
1198
+ * @param args - The parameters for the request.
1199
+ * @param signal - An optional abort signal.
1200
+ * @returns Successful response without specific data.
1201
+ *
1202
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1203
+ * @throws {TransportError} When the transport layer throws an error.
1204
+ *
1205
+ * @see null
1206
+ * @example
1207
+ * ```ts
1208
+ * import * as hl from "@nktkas/hyperliquid";
1209
+ *
1210
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1211
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1212
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1213
+ *
1214
+ * await exchClient.setDisplayName({ displayName: "..." });
1215
+ * ```
1216
+ */
1217
+ async setDisplayName(args, signal) {
1218
+ // Destructure the parameters
1219
+ const { ...actionArgs } = args;
1220
+ // Construct an action
1221
+ const nonce = await this.nonceManager();
1222
+ const action = mod_js_1.actionSorter.setDisplayName({ type: "setDisplayName", ...actionArgs });
1223
+ // Sign the action
1224
+ const signature = await (0, mod_js_1.signL1Action)({
1225
+ wallet: this.wallet,
1226
+ action,
1227
+ nonce,
1228
+ isTestnet: this.isTestnet,
1229
+ });
1230
+ // Send a request
1231
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1232
+ this._validateResponse(response);
1233
+ return response;
1234
+ }
1235
+ /**
1236
+ * Set a referral code.
1237
+ * @param args - The parameters for the request.
1238
+ * @param signal - An optional abort signal.
1239
+ * @returns Successful response without specific data.
1240
+ *
1241
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1242
+ * @throws {TransportError} When the transport layer throws an error.
1243
+ *
1244
+ * @see null
1245
+ * @example
1246
+ * ```ts
1247
+ * import * as hl from "@nktkas/hyperliquid";
1248
+ *
1249
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1250
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1251
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1252
+ *
1253
+ * await exchClient.setReferrer({ code: "..." });
1254
+ * ```
1255
+ */
1256
+ async setReferrer(args, signal) {
1257
+ // Destructure the parameters
1258
+ const { ...actionArgs } = args;
1259
+ // Construct an action
1260
+ const nonce = await this.nonceManager();
1261
+ const action = mod_js_1.actionSorter.setReferrer({ type: "setReferrer", ...actionArgs });
1262
+ // Sign the action
1263
+ const signature = await (0, mod_js_1.signL1Action)({
1264
+ wallet: this.wallet,
1265
+ action,
1266
+ nonce,
1267
+ isTestnet: this.isTestnet,
1268
+ });
1269
+ // Send a request
1270
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1271
+ this._validateResponse(response);
1272
+ return response;
1273
+ }
1274
+ /**
1275
+ * Deploying HIP-1 and HIP-2 assets.
1276
+ * @param args - The parameters for the request.
1277
+ * @param signal - An optional abort signal.
1278
+ * @returns Successful response without specific data.
1279
+ *
1280
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1281
+ * @throws {TransportError} When the transport layer throws an error.
1282
+ *
1283
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/deploying-hip-1-and-hip-2-assets
1284
+ * @example
1285
+ * ```ts
1286
+ * import * as hl from "@nktkas/hyperliquid";
1287
+ *
1288
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1289
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1290
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1291
+ *
1292
+ * await exchClient.spotDeploy({
1293
+ * registerToken2: {
1294
+ * spec: {
1295
+ * name: "USDC",
1296
+ * szDecimals: 8,
1297
+ * weiDecimals: 8,
1298
+ * },
1299
+ * maxGas: 1000000,
1300
+ * fullName: "USD Coin",
1301
+ * },
1302
+ * });
1303
+ * ```
1304
+ */
1305
+ async spotDeploy(args, signal) {
1306
+ // Destructure the parameters
1307
+ const { ...actionArgs } = args;
1308
+ // Construct an action
1309
+ const nonce = await this.nonceManager();
1310
+ const action = mod_js_1.actionSorter.spotDeploy({ type: "spotDeploy", ...actionArgs });
1311
+ // Sign the action
1312
+ const signature = await (0, mod_js_1.signL1Action)({
1313
+ wallet: this.wallet,
1314
+ action,
1315
+ nonce,
1316
+ isTestnet: this.isTestnet,
1317
+ });
1318
+ // Send a request
1319
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1320
+ this._validateResponse(response);
1321
+ return response;
1322
+ }
1323
+ /**
1324
+ * Send spot assets to another address.
1325
+ * @param args - The parameters for the request.
1326
+ * @param signal - An optional abort signal.
1327
+ * @returns Successful response without specific data.
1328
+ *
1329
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1330
+ * @throws {TransportError} When the transport layer throws an error.
1331
+ *
1332
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#core-spot-transfer
1333
+ * @example
1334
+ * ```ts
1335
+ * import * as hl from "@nktkas/hyperliquid";
1336
+ *
1337
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1338
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1339
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1340
+ *
1341
+ * await exchClient.spotSend({
1342
+ * destination: "0x...",
1343
+ * token: "USDC:0xeb62eee3685fc4c43992febcd9e75443",
1344
+ * amount: "1",
1345
+ * });
1346
+ * ```
1347
+ */
1348
+ async spotSend(args, signal) {
1349
+ // Destructure the parameters
1350
+ const { ...actionArgs } = args;
1351
+ // Construct an action
1352
+ const nonce = await this.nonceManager();
1353
+ const action = mod_js_1.actionSorter.spotSend({
1354
+ type: "spotSend",
1355
+ hyperliquidChain: this._getHyperliquidChain(),
1356
+ signatureChainId: await this._getSignatureChainId(),
1357
+ time: nonce,
1358
+ ...actionArgs,
1359
+ });
1360
+ // Sign the action
1361
+ const signature = await (0, mod_js_1.signUserSignedAction)({
1362
+ wallet: this.wallet,
1363
+ action,
1364
+ types: mod_js_1.userSignedActionEip712Types[action.type],
1365
+ });
1366
+ // Send a request
1367
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1368
+ this._validateResponse(response);
1369
+ return response;
1370
+ }
1371
+ /**
1372
+ * Opt Out of Spot Dusting.
1373
+ * @param args - The parameters for the request.
1374
+ * @param signal - An optional abort signal.
1375
+ * @returns Successful response without specific data.
1376
+ *
1377
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1378
+ * @throws {TransportError} When the transport layer throws an error.
1379
+ *
1380
+ * @see null
1381
+ * @example
1382
+ * ```ts
1383
+ * import * as hl from "@nktkas/hyperliquid";
1384
+ *
1385
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1386
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1387
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1388
+ *
1389
+ * await exchClient.spotUser({ toggleSpotDusting: { optOut: false } });
1390
+ * ```
1391
+ */
1392
+ async spotUser(args, signal) {
1393
+ // Destructure the parameters
1394
+ const { ...actionArgs } = args;
1395
+ // Construct an action
1396
+ const nonce = await this.nonceManager();
1397
+ const action = mod_js_1.actionSorter.spotUser({ type: "spotUser", ...actionArgs });
1398
+ // Sign the action
1399
+ const signature = await (0, mod_js_1.signL1Action)({
1400
+ wallet: this.wallet,
1401
+ action,
1402
+ nonce,
1403
+ isTestnet: this.isTestnet,
1404
+ });
1405
+ // Send a request
1406
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1407
+ this._validateResponse(response);
1408
+ return response;
1409
+ }
1410
+ /**
1411
+ * Transfer between sub-accounts (spot).
1412
+ * @param args - The parameters for the request.
1413
+ * @param signal - An optional abort signal.
1414
+ * @returns Successful response without specific data.
1415
+ *
1416
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1417
+ * @throws {TransportError} When the transport layer throws an error.
1418
+ *
1419
+ * @see null
1420
+ * @example
1421
+ * ```ts
1422
+ * import * as hl from "@nktkas/hyperliquid";
1423
+ *
1424
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1425
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1426
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1427
+ *
1428
+ * await exchClient.subAccountSpotTransfer({
1429
+ * subAccountUser: "0x...",
1430
+ * isDeposit: true,
1431
+ * token: "USDC:0xeb62eee3685fc4c43992febcd9e75443",
1432
+ * amount: "1",
1433
+ * });
1434
+ * ```
1435
+ */
1436
+ async subAccountSpotTransfer(args, signal) {
1437
+ // Destructure the parameters
1438
+ const { ...actionArgs } = args;
1439
+ // Construct an action
1440
+ const nonce = await this.nonceManager();
1441
+ const action = mod_js_1.actionSorter.subAccountSpotTransfer({ type: "subAccountSpotTransfer", ...actionArgs });
1442
+ // Sign the action
1443
+ const signature = await (0, mod_js_1.signL1Action)({
1444
+ wallet: this.wallet,
1445
+ action,
1446
+ nonce,
1447
+ isTestnet: this.isTestnet,
1448
+ });
1449
+ // Send a request
1450
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1451
+ this._validateResponse(response);
1452
+ return response;
1453
+ }
1454
+ /**
1455
+ * Transfer between sub-accounts (perpetual).
1456
+ * @param args - The parameters for the request.
1457
+ * @param signal - An optional abort signal.
1458
+ * @returns Successful response without specific data.
1459
+ *
1460
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1461
+ * @throws {TransportError} When the transport layer throws an error.
1462
+ *
1463
+ * @see null
1464
+ * @example
1465
+ * ```ts
1466
+ * import * as hl from "@nktkas/hyperliquid";
1467
+ *
1468
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1469
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1470
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1471
+ *
1472
+ * await exchClient.subAccountTransfer({ subAccountUser: "0x...", isDeposit: true, usd: 1 * 1e6 });
1473
+ * ```
1474
+ */
1475
+ async subAccountTransfer(args, signal) {
1476
+ // Destructure the parameters
1477
+ const { ...actionArgs } = args;
1478
+ // Construct an action
1479
+ const nonce = await this.nonceManager();
1480
+ const action = mod_js_1.actionSorter.subAccountTransfer({ type: "subAccountTransfer", ...actionArgs });
1481
+ // Sign the action
1482
+ const signature = await (0, mod_js_1.signL1Action)({
1483
+ wallet: this.wallet,
1484
+ action,
1485
+ nonce,
1486
+ isTestnet: this.isTestnet,
1487
+ });
1488
+ // Send a request
1489
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1490
+ this._validateResponse(response);
1491
+ return response;
1492
+ }
1493
+ /**
1494
+ * Delegate or undelegate native tokens to or from a validator.
1495
+ * @param args - The parameters for the request.
1496
+ * @param signal - An optional abort signal.
1497
+ * @returns Successful response without specific data.
1498
+ *
1499
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1500
+ * @throws {TransportError} When the transport layer throws an error.
1501
+ *
1502
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#delegate-or-undelegate-stake-from-validator
1503
+ * @example
1504
+ * ```ts
1505
+ * import * as hl from "@nktkas/hyperliquid";
1506
+ *
1507
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1508
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1509
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1510
+ *
1511
+ * await exchClient.tokenDelegate({ validator: "0x...", isUndelegate: true, wei: 1 * 1e8 });
1512
+ * ```
1513
+ */
1514
+ async tokenDelegate(args, signal) {
1515
+ // Destructure the parameters
1516
+ const { ...actionArgs } = args;
1517
+ // Construct an action
1518
+ const nonce = await this.nonceManager();
1519
+ const action = mod_js_1.actionSorter.tokenDelegate({
1520
+ type: "tokenDelegate",
1521
+ hyperliquidChain: this._getHyperliquidChain(),
1522
+ signatureChainId: await this._getSignatureChainId(),
1523
+ nonce,
1524
+ ...actionArgs,
1525
+ });
1526
+ // Sign the action
1527
+ const signature = await (0, mod_js_1.signUserSignedAction)({
1528
+ wallet: this.wallet,
1529
+ action,
1530
+ types: mod_js_1.userSignedActionEip712Types[action.type],
1531
+ });
1532
+ // Send a request
1533
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1534
+ this._validateResponse(response);
1535
+ return response;
1536
+ }
1537
+ /**
1538
+ * Cancel a TWAP order.
1539
+ * @param args - The parameters for the request.
1540
+ * @param signal - An optional abort signal.
1541
+ * @returns Successful variant of {@link TwapCancelResponse} without error status.
1542
+ *
1543
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1544
+ * @throws {TransportError} When the transport layer throws an error.
1545
+ *
1546
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-a-twap-order
1547
+ * @example
1548
+ * ```ts
1549
+ * import * as hl from "@nktkas/hyperliquid";
1550
+ *
1551
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1552
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1553
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1554
+ *
1555
+ * const data = await exchClient.twapCancel({ a: 0, t: 1 });
1556
+ * ```
1557
+ */
1558
+ async twapCancel(args, signal) {
1559
+ // Destructure the parameters
1560
+ const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1561
+ // Construct an action
1562
+ const nonce = await this.nonceManager();
1563
+ const action = mod_js_1.actionSorter.twapCancel({ type: "twapCancel", ...actionArgs });
1564
+ // Sign the action
1565
+ const signature = await (0, mod_js_1.signL1Action)({
1566
+ wallet: this.wallet,
1567
+ action,
1568
+ nonce,
1569
+ isTestnet: this.isTestnet,
1570
+ vaultAddress,
1571
+ expiresAfter,
1572
+ });
1573
+ // Send a request
1574
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
1575
+ this._validateResponse(response);
1576
+ return response;
1577
+ }
1578
+ /**
1579
+ * Place a TWAP order.
1580
+ * @param args - The parameters for the request.
1581
+ * @param signal - An optional abort signal.
1582
+ * @returns Successful variant of {@link TwapOrderResponse} without error status.
1583
+ *
1584
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1585
+ * @throws {TransportError} When the transport layer throws an error.
1586
+ *
1587
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-a-twap-order
1588
+ * @example
1589
+ * ```ts
1590
+ * import * as hl from "@nktkas/hyperliquid";
1591
+ *
1592
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1593
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1594
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1595
+ *
1596
+ * const data = await exchClient.twapOrder({
1597
+ * a: 0,
1598
+ * b: true,
1599
+ * s: "1",
1600
+ * r: false,
1601
+ * m: 10,
1602
+ * t: true,
1603
+ * });
1604
+ * ```
1605
+ */
1606
+ async twapOrder(args, signal) {
1607
+ // Destructure the parameters
1608
+ const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1609
+ // Construct an action
1610
+ const nonce = await this.nonceManager();
1611
+ const action = mod_js_1.actionSorter.twapOrder({ type: "twapOrder", twap: { ...actionArgs } });
1612
+ // Sign the action
1613
+ const signature = await (0, mod_js_1.signL1Action)({
1614
+ wallet: this.wallet,
1615
+ action,
1616
+ nonce,
1617
+ isTestnet: this.isTestnet,
1618
+ vaultAddress,
1619
+ expiresAfter,
1620
+ });
1621
+ // Send a request
1622
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
1623
+ this._validateResponse(response);
1624
+ return response;
1625
+ }
1626
+ /**
1627
+ * Add or remove margin from isolated position.
1628
+ * @param args - The parameters for the request.
1629
+ * @param signal - An optional abort signal.
1630
+ * @returns Successful response without specific data.
1631
+ *
1632
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1633
+ * @throws {TransportError} When the transport layer throws an error.
1634
+ *
1635
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-isolated-margin
1636
+ * @example
1637
+ * ```ts
1638
+ * import * as hl from "@nktkas/hyperliquid";
1639
+ *
1640
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1641
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1642
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1643
+ *
1644
+ * await exchClient.updateIsolatedMargin({ asset: 0, isBuy: true, ntli: 1 * 1e6 });
1645
+ * ```
1646
+ */
1647
+ async updateIsolatedMargin(args, signal) {
1648
+ // Destructure the parameters
1649
+ const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1650
+ // Construct an action
1651
+ const nonce = await this.nonceManager();
1652
+ const action = mod_js_1.actionSorter.updateIsolatedMargin({ type: "updateIsolatedMargin", ...actionArgs });
1653
+ // Sign the action
1654
+ const signature = await (0, mod_js_1.signL1Action)({
1655
+ wallet: this.wallet,
1656
+ action,
1657
+ nonce,
1658
+ isTestnet: this.isTestnet,
1659
+ vaultAddress,
1660
+ expiresAfter,
1661
+ });
1662
+ // Send a request
1663
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
1664
+ this._validateResponse(response);
1665
+ return response;
1666
+ }
1667
+ /**
1668
+ * Update cross or isolated leverage on a coin.
1669
+ * @param args - The parameters for the request.
1670
+ * @param signal - An optional abort signal.
1671
+ * @returns Successful response without specific data.
1672
+ *
1673
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1674
+ * @throws {TransportError} When the transport layer throws an error.
1675
+ *
1676
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-leverage
1677
+ * @example
1678
+ * ```ts
1679
+ * import * as hl from "@nktkas/hyperliquid";
1680
+ *
1681
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1682
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1683
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1684
+ *
1685
+ * await exchClient.updateLeverage({ asset: 0, isCross: true, leverage: 5 });
1686
+ * ```
1687
+ */
1688
+ async updateLeverage(args, signal) {
1689
+ // Destructure the parameters
1690
+ const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1691
+ // Construct an action
1692
+ const nonce = await this.nonceManager();
1693
+ const action = mod_js_1.actionSorter.updateLeverage({ type: "updateLeverage", ...actionArgs });
1694
+ // Sign the action
1695
+ const signature = await (0, mod_js_1.signL1Action)({
1696
+ wallet: this.wallet,
1697
+ action,
1698
+ nonce,
1699
+ isTestnet: this.isTestnet,
1700
+ vaultAddress,
1701
+ expiresAfter,
1702
+ });
1703
+ // Send a request
1704
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
1705
+ this._validateResponse(response);
1706
+ return response;
1707
+ }
1708
+ /**
1709
+ * Transfer funds between Spot account and Perp account.
1710
+ * @param args - The parameters for the request.
1711
+ * @param signal - An optional abort signal.
1712
+ * @returns Successful response without specific data.
1713
+ *
1714
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1715
+ * @throws {TransportError} When the transport layer throws an error.
1716
+ *
1717
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
1718
+ * @example
1719
+ * ```ts
1720
+ * import * as hl from "@nktkas/hyperliquid";
1721
+ *
1722
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1723
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1724
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1725
+ *
1726
+ * await exchClient.usdClassTransfer({ amount: "1", toPerp: true });
1727
+ * ```
1728
+ */
1729
+ async usdClassTransfer(args, signal) {
1730
+ // Destructure the parameters
1731
+ const { ...actionArgs } = args;
1732
+ // Construct an action
1733
+ const nonce = await this.nonceManager();
1734
+ const action = mod_js_1.actionSorter.usdClassTransfer({
1735
+ type: "usdClassTransfer",
1736
+ hyperliquidChain: this._getHyperliquidChain(),
1737
+ signatureChainId: await this._getSignatureChainId(),
1738
+ nonce,
1739
+ ...actionArgs,
1740
+ });
1741
+ // Sign the action
1742
+ const signature = await (0, mod_js_1.signUserSignedAction)({
1743
+ wallet: this.wallet,
1744
+ action,
1745
+ types: mod_js_1.userSignedActionEip712Types[action.type],
1746
+ });
1747
+ // Send a request
1748
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1749
+ this._validateResponse(response);
1750
+ return response;
1751
+ }
1752
+ /**
1753
+ * Send usd to another address.
1754
+ * @param args - The parameters for the request.
1755
+ * @param signal - An optional abort signal.
1756
+ * @returns Successful response without specific data.
1757
+ *
1758
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1759
+ * @throws {TransportError} When the transport layer throws an error.
1760
+ *
1761
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#core-usdc-transfer
1762
+ * @example
1763
+ * ```ts
1764
+ * import * as hl from "@nktkas/hyperliquid";
1765
+ *
1766
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1767
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1768
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1769
+ *
1770
+ * await exchClient.usdSend({ destination: "0x...", amount: "1" });
1771
+ * ```
1772
+ */
1773
+ async usdSend(args, signal) {
1774
+ // Destructure the parameters
1775
+ const { ...actionArgs } = args;
1776
+ // Construct an action
1777
+ const nonce = await this.nonceManager();
1778
+ const action = mod_js_1.actionSorter.usdSend({
1779
+ type: "usdSend",
1780
+ hyperliquidChain: this._getHyperliquidChain(),
1781
+ signatureChainId: await this._getSignatureChainId(),
1782
+ time: nonce,
1783
+ ...actionArgs,
1784
+ });
1785
+ // Sign the action
1786
+ const signature = await (0, mod_js_1.signUserSignedAction)({
1787
+ wallet: this.wallet,
1788
+ action,
1789
+ types: mod_js_1.userSignedActionEip712Types[action.type],
1790
+ });
1791
+ // Send a request
1792
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1793
+ this._validateResponse(response);
1794
+ return response;
1795
+ }
1796
+ /**
1797
+ * Distribute funds from a vault between followers.
1798
+ * @param args - The parameters for the request.
1799
+ * @param signal - An optional abort signal.
1800
+ * @returns Successful response without specific data.
1801
+ *
1802
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1803
+ * @throws {TransportError} When the transport layer throws an error.
1804
+ *
1805
+ * @see null
1806
+ * @example
1807
+ * ```ts
1808
+ * import * as hl from "@nktkas/hyperliquid";
1809
+ *
1810
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1811
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1812
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1813
+ *
1814
+ * await exchClient.vaultDistribute({ vaultAddress: "0x...", usd: 10 * 1e6 });
1815
+ * ```
1816
+ */
1817
+ async vaultDistribute(args, signal) {
1818
+ // Destructure the parameters
1819
+ const { ...actionArgs } = args;
1820
+ // Construct an action
1821
+ const nonce = await this.nonceManager();
1822
+ const action = mod_js_1.actionSorter.vaultDistribute({ type: "vaultDistribute", ...actionArgs });
1823
+ // Sign the action
1824
+ const signature = await (0, mod_js_1.signL1Action)({
1825
+ wallet: this.wallet,
1826
+ action,
1827
+ nonce,
1828
+ isTestnet: this.isTestnet,
1829
+ });
1830
+ // Send a request
1831
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1832
+ this._validateResponse(response);
1833
+ return response;
1834
+ }
1835
+ /**
1836
+ * Modify a vault's configuration.
1837
+ * @param args - The parameters for the request.
1838
+ * @param signal - An optional abort signal.
1839
+ * @returns Successful response without specific data.
1840
+ *
1841
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1842
+ * @throws {TransportError} When the transport layer throws an error.
1843
+ *
1844
+ * @see null
1845
+ * @example
1846
+ * ```ts
1847
+ * import * as hl from "@nktkas/hyperliquid";
1848
+ *
1849
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1850
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1851
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1852
+ *
1853
+ * await exchClient.vaultModify({
1854
+ * vaultAddress: "0x...",
1855
+ * allowDeposits: true,
1856
+ * alwaysCloseOnWithdraw: false,
1857
+ * });
1858
+ * ```
1859
+ */
1860
+ async vaultModify(args, signal) {
1861
+ // Destructure the parameters
1862
+ const { ...actionArgs } = args;
1863
+ // Construct an action
1864
+ const nonce = await this.nonceManager();
1865
+ const action = mod_js_1.actionSorter.vaultModify({ type: "vaultModify", ...actionArgs });
1866
+ // Sign the action
1867
+ const signature = await (0, mod_js_1.signL1Action)({
1868
+ wallet: this.wallet,
1869
+ action,
1870
+ nonce,
1871
+ isTestnet: this.isTestnet,
1872
+ });
1873
+ // Send a request
1874
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1875
+ this._validateResponse(response);
1876
+ return response;
1877
+ }
1878
+ /**
1879
+ * Deposit or withdraw from a vault.
1880
+ * @param args - The parameters for the request.
1881
+ * @param signal - An optional abort signal.
1882
+ * @returns Successful response without specific data.
1883
+ *
1884
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1885
+ * @throws {TransportError} When the transport layer throws an error.
1886
+ *
1887
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-or-withdraw-from-a-vault
1888
+ * @example
1889
+ * ```ts
1890
+ * import * as hl from "@nktkas/hyperliquid";
1891
+ *
1892
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1893
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1894
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1895
+ *
1896
+ * await exchClient.vaultTransfer({ vaultAddress: "0x...", isDeposit: true, usd: 10 * 1e6 });
1897
+ * ```
1898
+ */
1899
+ async vaultTransfer(args, signal) {
1900
+ // Destructure the parameters
1901
+ const { expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1902
+ // Construct an action
1903
+ const nonce = await this.nonceManager();
1904
+ const action = mod_js_1.actionSorter.vaultTransfer({ type: "vaultTransfer", ...actionArgs });
1905
+ // Sign the action
1906
+ const signature = await (0, mod_js_1.signL1Action)({
1907
+ wallet: this.wallet,
1908
+ action,
1909
+ nonce,
1910
+ isTestnet: this.isTestnet,
1911
+ expiresAfter,
1912
+ });
1913
+ // Send a request
1914
+ const response = await this.transport.request("exchange", { action, signature, nonce, expiresAfter }, signal);
1915
+ this._validateResponse(response);
1916
+ return response;
1917
+ }
1918
+ /**
1919
+ * Initiate a withdrawal request.
1920
+ * @param args - The parameters for the request.
1921
+ * @param signal - An optional abort signal.
1922
+ * @returns Successful response without specific data.
1923
+ *
1924
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1925
+ * @throws {TransportError} When the transport layer throws an error.
1926
+ *
1927
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#initiate-a-withdrawal-request
1928
+ * @example
1929
+ * ```ts
1930
+ * import * as hl from "@nktkas/hyperliquid";
1931
+ *
1932
+ * const privateKey = "0x..."; // or `viem`, `ethers`
1933
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1934
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1935
+ *
1936
+ * await exchClient.withdraw3({ destination: "0x...", amount: "1" });
1937
+ * ```
1938
+ */
1939
+ async withdraw3(args, signal) {
1940
+ // Destructure the parameters
1941
+ const { ...actionArgs } = args;
1942
+ // Construct an action
1943
+ const nonce = await this.nonceManager();
1944
+ const action = mod_js_1.actionSorter.withdraw3({
1945
+ type: "withdraw3",
1946
+ hyperliquidChain: this._getHyperliquidChain(),
1947
+ signatureChainId: await this._getSignatureChainId(),
1948
+ time: nonce,
1949
+ ...actionArgs,
1950
+ });
1951
+ // Sign the action
1952
+ const signature = await (0, mod_js_1.signUserSignedAction)({
1953
+ wallet: this.wallet,
1954
+ action,
1955
+ types: mod_js_1.userSignedActionEip712Types[action.type],
1956
+ });
1957
+ // Send a request
1958
+ const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1959
+ this._validateResponse(response);
1960
+ return response;
1961
+ }
1962
+ /** Guesses the chain ID based on the wallet type or the isTestnet flag. */
1963
+ async _guessSignatureChainId() {
1964
+ // Trying to get chain ID of the wallet
1965
+ if ((0, mod_js_1.isAbstractViemWalletClient)(this.wallet)) {
1966
+ if ("getChainId" in this.wallet && typeof this.wallet.getChainId === "function") {
1967
+ const chainId = await this.wallet.getChainId();
1968
+ return `0x${chainId.toString(16)}`;
1822
1969
  }
1823
- // Attempt to guess chain ID based on isTestnet
1824
- return this.isTestnet ? "0x66eee" : "0xa4b1";
1825
1970
  }
1826
- /** Get the default expiration time for an action. */
1827
- async _getDefaultExpiresAfter() {
1828
- return typeof this.defaultExpiresAfter === "number"
1829
- ? this.defaultExpiresAfter
1830
- : await this.defaultExpiresAfter?.();
1971
+ else if ((0, mod_js_1.isAbstractEthersSigner)(this.wallet) || (0, mod_js_1.isAbstractEthersV5Signer)(this.wallet)) {
1972
+ if ("provider" in this.wallet &&
1973
+ typeof this.wallet.provider === "object" && this.wallet.provider !== null &&
1974
+ "getNetwork" in this.wallet.provider &&
1975
+ typeof this.wallet.provider.getNetwork === "function") {
1976
+ const network = await this.wallet.provider.getNetwork();
1977
+ return `0x${network.chainId.toString(16)}`;
1978
+ }
1831
1979
  }
1832
- /** Get the signature chain ID for the wallet. */
1833
- async _getSignatureChainId() {
1834
- return typeof this.signatureChainId === "string" ? this.signatureChainId : await this.signatureChainId();
1980
+ else if ((0, mod_js_1.isAbstractWindowEthereum)(this.wallet)) {
1981
+ const [chainId] = await this.wallet.request({ method: "eth_chainId", params: [] });
1982
+ return chainId;
1835
1983
  }
1836
- /** Get the Hyperliquid chain based on the isTestnet flag. */
1837
- _getHyperliquidChain() {
1838
- return this.isTestnet ? "Testnet" : "Mainnet";
1984
+ // Attempt to guess chain ID based on isTestnet
1985
+ return this.isTestnet ? "0x66eee" : "0xa4b1";
1986
+ }
1987
+ /** Get the default expiration time for an action. */
1988
+ async _getDefaultExpiresAfter() {
1989
+ return typeof this.defaultExpiresAfter === "number"
1990
+ ? this.defaultExpiresAfter
1991
+ : await this.defaultExpiresAfter?.();
1992
+ }
1993
+ /** Get the signature chain ID for the wallet. */
1994
+ async _getSignatureChainId() {
1995
+ return typeof this.signatureChainId === "string" ? this.signatureChainId : await this.signatureChainId();
1996
+ }
1997
+ /** Get the Hyperliquid chain based on the isTestnet flag. */
1998
+ _getHyperliquidChain() {
1999
+ return this.isTestnet ? "Testnet" : "Mainnet";
2000
+ }
2001
+ /** Validate a response from the API. */
2002
+ _validateResponse(response) {
2003
+ if (response.status === "err") {
2004
+ throw new ApiRequestError(response);
1839
2005
  }
1840
- /** Validate a response from the API. */
1841
- _validateResponse(response) {
1842
- if (response.status === "err") {
2006
+ else if (response.response.type === "order" || response.response.type === "cancel") {
2007
+ if (response.response.data.statuses.some((status) => typeof status === "object" && "error" in status)) {
1843
2008
  throw new ApiRequestError(response);
1844
2009
  }
1845
- else if (response.response.type === "order" || response.response.type === "cancel") {
1846
- if (response.response.data.statuses.some((status) => typeof status === "object" && "error" in status)) {
1847
- throw new ApiRequestError(response);
1848
- }
1849
- }
1850
- else if (response.response.type === "twapOrder" || response.response.type === "twapCancel") {
1851
- if (typeof response.response.data.status === "object" && "error" in response.response.data.status) {
1852
- throw new ApiRequestError(response);
1853
- }
1854
- }
1855
2010
  }
1856
- async [Symbol.asyncDispose]() {
1857
- await this.transport[Symbol.asyncDispose]?.();
2011
+ else if (response.response.type === "twapOrder" || response.response.type === "twapCancel") {
2012
+ if (typeof response.response.data.status === "object" && "error" in response.response.data.status) {
2013
+ throw new ApiRequestError(response);
2014
+ }
1858
2015
  }
1859
2016
  }
1860
- exports.ExchangeClient = ExchangeClient;
1861
- });
2017
+ async [Symbol.asyncDispose]() {
2018
+ await this.transport[Symbol.asyncDispose]?.();
2019
+ }
2020
+ }
2021
+ exports.ExchangeClient = ExchangeClient;