@nktkas/hyperliquid 0.22.2 → 0.23.1

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