@nktkas/hyperliquid 0.13.0 → 0.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +42 -17
  3. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  4. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  5. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.js +2 -0
  6. package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  7. package/esm/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  8. package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.js +1 -1
  9. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  10. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.js +2 -0
  12. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  13. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.js +1 -1
  14. package/{script/deps/jsr.io/@std/encoding/1.0.6 → esm/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  15. package/esm/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  16. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/hex.js +1 -1
  17. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  18. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  19. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +2 -0
  20. package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  21. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  22. package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.js +2 -2
  23. package/esm/mod.d.ts +15 -14
  24. package/esm/mod.d.ts.map +1 -1
  25. package/esm/src/clients/event.d.ts +64 -21
  26. package/esm/src/clients/event.d.ts.map +1 -1
  27. package/esm/src/clients/event.js +76 -18
  28. package/esm/src/clients/public.d.ts +244 -52
  29. package/esm/src/clients/public.d.ts.map +1 -1
  30. package/esm/src/clients/public.js +243 -47
  31. package/esm/src/clients/wallet.d.ts +122 -53
  32. package/esm/src/clients/wallet.d.ts.map +1 -1
  33. package/esm/src/clients/wallet.js +201 -65
  34. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  35. package/esm/src/transports/http/http_transport.js +4 -1
  36. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts +27 -21
  37. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -1
  38. package/esm/src/transports/websocket/hyperliquid_event_target.js +37 -0
  39. package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts.map +1 -1
  40. package/esm/src/transports/websocket/websocket_request_dispatcher.js +4 -4
  41. package/esm/src/transports/websocket/websocket_transport.d.ts +7 -10
  42. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  43. package/esm/src/transports/websocket/websocket_transport.js +41 -35
  44. package/esm/src/types/common.d.ts +3 -0
  45. package/esm/src/types/common.d.ts.map +1 -0
  46. package/esm/src/types/common.js +1 -0
  47. package/esm/src/types/exchange/common.d.ts +36 -0
  48. package/esm/src/types/exchange/common.d.ts.map +1 -0
  49. package/esm/src/types/exchange/common.js +1 -0
  50. package/esm/src/types/exchange/requests.d.ts +502 -0
  51. package/esm/src/types/exchange/requests.d.ts.map +1 -0
  52. package/esm/src/types/exchange/requests.js +1 -0
  53. package/esm/src/types/exchange/responses.d.ts +141 -0
  54. package/esm/src/types/exchange/responses.d.ts.map +1 -0
  55. package/esm/src/types/exchange/responses.js +1 -0
  56. package/esm/src/types/explorer/common.d.ts +37 -0
  57. package/esm/src/types/explorer/common.d.ts.map +1 -0
  58. package/esm/src/types/explorer/common.js +1 -0
  59. package/esm/src/types/explorer/requests.d.ts +35 -0
  60. package/esm/src/types/explorer/requests.d.ts.map +1 -0
  61. package/esm/src/types/explorer/requests.js +1 -0
  62. package/esm/src/types/explorer/responses.d.ts +23 -0
  63. package/esm/src/types/explorer/responses.d.ts.map +1 -0
  64. package/esm/src/types/explorer/responses.js +1 -0
  65. package/esm/src/types/info/accounts.d.ts +489 -0
  66. package/esm/src/types/info/accounts.d.ts.map +1 -0
  67. package/esm/src/types/info/accounts.js +1 -0
  68. package/esm/src/types/info/assets.d.ts +275 -0
  69. package/esm/src/types/info/assets.d.ts.map +1 -0
  70. package/esm/src/types/info/assets.js +1 -0
  71. package/esm/src/types/info/delegations.d.ts +117 -0
  72. package/esm/src/types/info/delegations.d.ts.map +1 -0
  73. package/esm/src/types/info/delegations.js +1 -0
  74. package/esm/src/types/info/orders.d.ts +209 -0
  75. package/esm/src/types/info/orders.d.ts.map +1 -0
  76. package/esm/src/types/info/orders.js +1 -0
  77. package/esm/src/types/info/requests.d.ts +445 -0
  78. package/esm/src/types/info/requests.d.ts.map +1 -0
  79. package/esm/src/types/info/requests.js +1 -0
  80. package/esm/src/types/info/vaults.d.ts +90 -0
  81. package/esm/src/types/info/vaults.d.ts.map +1 -0
  82. package/esm/src/types/info/vaults.js +1 -0
  83. package/esm/src/types/subscriptions/common.d.ts +208 -0
  84. package/esm/src/types/subscriptions/common.d.ts.map +1 -0
  85. package/esm/src/types/subscriptions/common.js +1 -0
  86. package/esm/src/types/subscriptions/requests.d.ts +134 -0
  87. package/esm/src/types/subscriptions/requests.d.ts.map +1 -0
  88. package/esm/src/types/subscriptions/requests.js +1 -0
  89. package/esm/src/utils/key_sort.d.ts +3 -3
  90. package/esm/src/utils/key_sort.d.ts.map +1 -1
  91. package/esm/src/utils/signing.d.ts +1 -1
  92. package/esm/src/utils/signing.js +2 -2
  93. package/package.json +2 -2
  94. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +13 -3
  95. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +23 -13
  96. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +55 -45
  97. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +97 -87
  98. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +14 -4
  99. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +288 -278
  100. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +238 -228
  101. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  102. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  103. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.js +13 -0
  104. package/script/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  105. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  106. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.js +45 -0
  107. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  108. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  109. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.js +13 -0
  110. package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  111. package/script/deps/jsr.io/@std/encoding/1.0.7/_validate_binary_like.js +39 -0
  112. package/{esm/deps/jsr.io/@std/encoding/1.0.6 → script/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  113. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  114. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.js +123 -0
  115. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  116. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  117. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +13 -0
  118. package/script/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  119. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  120. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +250 -0
  121. package/script/mod.d.ts +15 -14
  122. package/script/mod.d.ts.map +1 -1
  123. package/script/mod.js +23 -13
  124. package/script/src/clients/event.d.ts +64 -21
  125. package/script/src/clients/event.d.ts.map +1 -1
  126. package/script/src/clients/event.js +551 -483
  127. package/script/src/clients/public.d.ts +244 -52
  128. package/script/src/clients/public.d.ts.map +1 -1
  129. package/script/src/clients/public.js +914 -708
  130. package/script/src/clients/wallet.d.ts +122 -53
  131. package/script/src/clients/wallet.d.ts.map +1 -1
  132. package/script/src/clients/wallet.js +1121 -975
  133. package/script/src/transports/base.js +25 -15
  134. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  135. package/script/src/transports/http/http_transport.js +174 -161
  136. package/script/src/transports/websocket/hyperliquid_event_target.d.ts +27 -21
  137. package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -1
  138. package/script/src/transports/websocket/hyperliquid_event_target.js +80 -33
  139. package/script/src/transports/websocket/reconnecting_websocket.js +364 -354
  140. package/script/src/transports/websocket/websocket_request_dispatcher.d.ts.map +1 -1
  141. package/script/src/transports/websocket/websocket_request_dispatcher.js +196 -186
  142. package/script/src/transports/websocket/websocket_transport.d.ts +7 -10
  143. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  144. package/script/src/transports/websocket/websocket_transport.js +236 -220
  145. package/script/src/types/common.d.ts +3 -0
  146. package/script/src/types/common.d.ts.map +1 -0
  147. package/script/src/types/common.js +12 -0
  148. package/script/src/types/exchange/common.d.ts +36 -0
  149. package/script/src/types/exchange/common.d.ts.map +1 -0
  150. package/script/src/types/exchange/common.js +12 -0
  151. package/script/src/types/exchange/requests.d.ts +502 -0
  152. package/script/src/types/exchange/requests.d.ts.map +1 -0
  153. package/script/src/types/exchange/requests.js +12 -0
  154. package/script/src/types/exchange/responses.d.ts +141 -0
  155. package/script/src/types/exchange/responses.d.ts.map +1 -0
  156. package/script/src/types/exchange/responses.js +12 -0
  157. package/script/src/types/explorer/common.d.ts +37 -0
  158. package/script/src/types/explorer/common.d.ts.map +1 -0
  159. package/script/src/types/explorer/common.js +12 -0
  160. package/script/src/types/explorer/requests.d.ts +35 -0
  161. package/script/src/types/explorer/requests.d.ts.map +1 -0
  162. package/script/src/types/explorer/requests.js +12 -0
  163. package/script/src/types/explorer/responses.d.ts +23 -0
  164. package/script/src/types/explorer/responses.d.ts.map +1 -0
  165. package/script/src/types/explorer/responses.js +12 -0
  166. package/script/src/types/info/accounts.d.ts +489 -0
  167. package/script/src/types/info/accounts.d.ts.map +1 -0
  168. package/script/src/types/info/accounts.js +12 -0
  169. package/script/src/types/info/assets.d.ts +275 -0
  170. package/script/src/types/info/assets.d.ts.map +1 -0
  171. package/script/src/types/info/assets.js +12 -0
  172. package/script/src/types/info/delegations.d.ts +117 -0
  173. package/script/src/types/info/delegations.d.ts.map +1 -0
  174. package/script/src/types/info/delegations.js +12 -0
  175. package/script/src/types/info/orders.d.ts +209 -0
  176. package/script/src/types/info/orders.d.ts.map +1 -0
  177. package/script/src/types/info/orders.js +12 -0
  178. package/script/src/types/info/requests.d.ts +445 -0
  179. package/script/src/types/info/requests.d.ts.map +1 -0
  180. package/script/src/types/info/requests.js +12 -0
  181. package/script/src/types/info/vaults.d.ts +90 -0
  182. package/script/src/types/info/vaults.d.ts.map +1 -0
  183. package/script/src/types/info/vaults.js +12 -0
  184. package/script/src/types/subscriptions/common.d.ts +208 -0
  185. package/script/src/types/subscriptions/common.d.ts.map +1 -0
  186. package/script/src/types/subscriptions/common.js +12 -0
  187. package/script/src/types/subscriptions/requests.d.ts +134 -0
  188. package/script/src/types/subscriptions/requests.d.ts.map +1 -0
  189. package/script/src/types/subscriptions/requests.js +12 -0
  190. package/script/src/utils/key_sort.d.ts +3 -3
  191. package/script/src/utils/key_sort.d.ts.map +1 -1
  192. package/script/src/utils/key_sort.js +133 -123
  193. package/script/src/utils/signing.d.ts +1 -1
  194. package/script/src/utils/signing.js +172 -162
  195. package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  196. package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  197. package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  198. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  199. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.js +0 -35
  200. package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +0 -29
  201. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  202. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.js +0 -113
  203. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  204. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.js +0 -240
  205. /package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
  206. /package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
@@ -1,989 +1,1135 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WalletClient = exports.ApiRequestError = void 0;
4
- const key_sort_js_1 = require("../utils/key_sort.js");
5
- const signing_js_1 = require("../utils/signing.js");
6
- // ———————————————Errors———————————————
7
- /** Error thrown when the API returns an error response. */
8
- class ApiRequestError extends Error {
9
- constructor(response) {
10
- let message = "";
11
- if (response.status === "err") {
12
- message = response.response;
13
- }
14
- else {
15
- if ("statuses" in response.response.data) {
16
- message = response.response.data.statuses
17
- .reduce((acc, status, index) => {
18
- if (typeof status === "object" && "error" in status) {
19
- acc.push(`[${index}] ${status.error}`);
20
- }
21
- return acc;
22
- }, [])
23
- .join(", ");
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", "../utils/key_sort.js", "../utils/signing.js"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.WalletClient = exports.ApiRequestError = void 0;
13
+ const key_sort_js_1 = require("../utils/key_sort.js");
14
+ const signing_js_1 = require("../utils/signing.js");
15
+ // ——————————————— Errors ———————————————
16
+ /** Error thrown when the API returns an error response. */
17
+ class ApiRequestError extends Error {
18
+ constructor(response) {
19
+ let message = "Cannot process API request";
20
+ if (response.status === "err") {
21
+ message += `: ${response.response}`;
24
22
  }
25
23
  else {
26
- if (typeof response.response.data.status === "object" && "error" in response.response.data.status) {
27
- message = response.response.data.status.error;
24
+ if ("statuses" in response.response.data) {
25
+ const errors = response.response.data.statuses
26
+ .reduce((acc, status, index) => {
27
+ if (typeof status === "object" && "error" in status) {
28
+ acc.push(`Order ${index} failed: ${status.error}`);
29
+ }
30
+ return acc;
31
+ }, []);
32
+ if (errors.length > 0) {
33
+ message += `: ${errors.join(", ")}`;
34
+ }
35
+ }
36
+ else {
37
+ if (typeof response.response.data.status === "object" && "error" in response.response.data.status) {
38
+ message += `: ${response.response.data.status.error}`;
39
+ }
28
40
  }
29
41
  }
42
+ super(message);
43
+ Object.defineProperty(this, "response", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: response
48
+ });
49
+ this.name = "ApiRequestError";
30
50
  }
31
- super(message);
32
- Object.defineProperty(this, "response", {
33
- enumerable: true,
34
- configurable: true,
35
- writable: true,
36
- value: response
37
- });
38
- this.name = "ApiRequestError";
39
- }
40
- }
41
- exports.ApiRequestError = ApiRequestError;
42
- // ———————————————Client———————————————
43
- /**
44
- * Wallet client for interacting with the Hyperliquid API.
45
- * @typeParam T - The transport used to connect to the Hyperliquid API.
46
- * @typeParam W - The WalletClient/Account ([viem](https://viem.sh/docs/clients/wallet)) or Signer ([ethers.js](https://docs.ethers.io/v6/api/providers/#Signer)) used for signing transactions.
47
- */
48
- class WalletClient {
49
- /**
50
- * Initialises a new instance.
51
- * @param args - The parameters for the client.
52
- *
53
- * @example Private key via [viem](https://viem.sh/docs/clients/wallet#local-accounts-private-key-mnemonic-etc)
54
- * ```ts
55
- * import * as hl from "@nktkas/hyperliquid";
56
- * import { privateKeyToAccount } from "viem/accounts";
57
- *
58
- * const wallet = privateKeyToAccount("0x...");
59
- *
60
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
61
- * const client = new hl.WalletClient({ wallet, transport });
62
- * ```
63
- *
64
- * @example Private key via [ethers.js](https://docs.ethers.org/v6/api/wallet/#Wallet)
65
- * ```ts
66
- * import * as hl from "@nktkas/hyperliquid";
67
- * import { ethers } from "ethers";
68
- *
69
- * const wallet = new ethers.Wallet("0x...");
70
- *
71
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
72
- * const client = new hl.WalletClient({ wallet, transport });
73
- * ```
74
- *
75
- * @example External wallet (e.g. MetaMask) via [viem](https://viem.sh/docs/clients/wallet#optional-hoist-the-account)
76
- * ```ts
77
- * import * as hl from "@nktkas/hyperliquid";
78
- * import { createWalletClient, custom } from "viem";
79
- * import { arbitrum } from "viem/chains";
80
- *
81
- * const [account] = await window.ethereum.request({ method: "eth_requestAccounts" });
82
- * const wallet = createWalletClient({ account, chain: arbitrum, transport: custom(window.ethereum) });
83
- *
84
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
85
- * const client = new hl.WalletClient({ wallet, transport });
86
- * ```
87
- */
88
- constructor(args) {
89
- /** The transport used to connect to the Hyperliquid API. */
90
- Object.defineProperty(this, "transport", {
91
- enumerable: true,
92
- configurable: true,
93
- writable: true,
94
- value: void 0
95
- });
96
- /** The WalletClient/Account ([viem](https://viem.sh/docs/clients/wallet)) or Signer ([ethers.js](https://docs.ethers.org/v6/api/providers/#Signer)) used for signing transactions. */
97
- Object.defineProperty(this, "wallet", {
98
- enumerable: true,
99
- configurable: true,
100
- writable: true,
101
- value: void 0
102
- });
103
- /** Specifies whether the client uses testnet. */
104
- Object.defineProperty(this, "isTestnet", {
105
- enumerable: true,
106
- configurable: true,
107
- writable: true,
108
- value: void 0
109
- });
110
- /** Sets a default vaultAddress to be used if no vaultAddress is explicitly passed to a method. */
111
- Object.defineProperty(this, "defaultVaultAddress", {
112
- enumerable: true,
113
- configurable: true,
114
- writable: true,
115
- value: void 0
116
- });
117
- this.transport = args.transport;
118
- this.wallet = args.wallet;
119
- this.isTestnet = args.isTestnet ?? false;
120
- this.defaultVaultAddress = args.defaultVaultAddress;
121
- }
122
- // ———————————————Actions———————————————
123
- /**
124
- * Approve an agent to sign on behalf of the master or sub-accounts.
125
- * @param args - The parameters for the request.
126
- * @param signal - An optional abort signal
127
- * @returns {SuccessResponse} Successful response without specific data.
128
- * @throws {ApiRequestError} When the API returns an error response.
129
- *
130
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-an-api-wallet | Hyperliquid GitBook}
131
- * @example
132
- * ```ts
133
- * import * as hl from "@nktkas/hyperliquid";
134
- * import { privateKeyToAccount } from "viem/accounts";
135
- *
136
- * const wallet = privateKeyToAccount("0x...");
137
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
138
- * const client = new hl.WalletClient({ wallet, transport });
139
- *
140
- * const result = await client.approveAgent({
141
- * agentAddress: "0x...",
142
- * agentName: "agentName",
143
- * });
144
- */
145
- async approveAgent(args, signal) {
146
- const action = {
147
- ...args,
148
- type: "approveAgent",
149
- hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
150
- signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
151
- nonce: args.nonce ?? Date.now(),
152
- };
153
- const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
154
- "HyperliquidTransaction:ApproveAgent": [
155
- { name: "hyperliquidChain", type: "string" },
156
- { name: "agentAddress", type: "address" },
157
- { name: "agentName", type: "string" },
158
- { name: "nonce", type: "uint64" },
159
- ],
160
- }, parseInt(action.signatureChainId, 16));
161
- const request = {
162
- action,
163
- signature,
164
- nonce: action.nonce,
165
- };
166
- const response = await this.transport.request("action", request, signal);
167
- this._validateResponse(response);
168
- return response;
169
- }
170
- /**
171
- * Approve a max fee rate for a builder address.
172
- * @param args - The parameters for the request.
173
- * @param signal - An optional abort signal.
174
- * @returns {SuccessResponse} Successful response without specific data.
175
- * @throws {ApiRequestError} When the API returns an error response.
176
- *
177
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-a-builder-fee | Hyperliquid GitBook}
178
- * @example
179
- * ```ts
180
- * import * as hl from "@nktkas/hyperliquid";
181
- * import { privateKeyToAccount } from "viem/accounts";
182
- *
183
- * const wallet = privateKeyToAccount("0x...");
184
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
185
- * const client = new hl.WalletClient({ wallet, transport });
186
- *
187
- * const result = await client.approveBuilderFee({
188
- * maxFeeRate: "0.01%",
189
- * builder: "0x...",
190
- * });
191
- */
192
- async approveBuilderFee(args, signal) {
193
- const action = {
194
- ...args,
195
- type: "approveBuilderFee",
196
- hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
197
- signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
198
- nonce: args.nonce ?? Date.now(),
199
- };
200
- const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
201
- "HyperliquidTransaction:ApproveBuilderFee": [
202
- { name: "hyperliquidChain", type: "string" },
203
- { name: "maxFeeRate", type: "string" },
204
- { name: "builder", type: "address" },
205
- { name: "nonce", type: "uint64" },
206
- ],
207
- }, parseInt(action.signatureChainId, 16));
208
- const request = {
209
- action,
210
- signature,
211
- nonce: action.nonce,
212
- };
213
- const response = await this.transport.request("action", request, signal);
214
- this._validateResponse(response);
215
- return response;
216
- }
217
- /**
218
- * Modify multiple orders.
219
- * @param args - The parameters for the request.
220
- * @param signal - An optional abort signal.
221
- * @returns {OrderResponseSuccess} Successful variant of {@link OrderResponse} without error statuses.
222
- * @throws {ApiRequestError} When the API returns an error response.
223
- *
224
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders | Hyperliquid GitBook}
225
- * @example
226
- * ```ts
227
- * import * as hl from "@nktkas/hyperliquid";
228
- * import { privateKeyToAccount } from "viem/accounts";
229
- *
230
- * const wallet = privateKeyToAccount("0x...");
231
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
232
- * const client = new hl.WalletClient({ wallet, transport });
233
- *
234
- * const result = await client.batchModify({
235
- * modifies: [{
236
- * oid: 123, // Order ID
237
- * order: {
238
- * a: 0, // Asset index
239
- * b: true, // Buy order
240
- * p: "31000", // New price
241
- * s: "0.2", // New size
242
- * r: false, // Not reduce-only
243
- * t: {
244
- * limit: {
245
- * tif: "Gtc", // Good-til-cancelled
246
- * },
247
- * },
248
- * c: "0x...", // Optional: Client Order ID
249
- * },
250
- * }],
251
- * });
252
- * ```
253
- */
254
- async batchModify(args, signal) {
255
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
256
- const sortedAction = key_sort_js_1.sorters.batchModify({ type: "batchModify", ...actionArgs });
257
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
258
- const request = {
259
- action: sortedAction,
260
- signature,
261
- nonce,
262
- vaultAddress,
263
- };
264
- const response = await this.transport.request("action", request, signal);
265
- this._validateResponse(response);
266
- return response;
267
- }
268
- /**
269
- * Cancel order(s).
270
- * @param args - The parameters for the request.
271
- * @param signal - An optional abort signal.
272
- * @returns {CancelResponseSuccess} Successful variant of {@link CancelResponse} without error statuses.
273
- * @throws {ApiRequestError} When the API returns an error response.
274
- *
275
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s | Hyperliquid GitBook}
276
- * @example
277
- * ```ts
278
- * import * as hl from "@nktkas/hyperliquid";
279
- * import { privateKeyToAccount } from "viem/accounts";
280
- *
281
- * const wallet = privateKeyToAccount("0x...");
282
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
283
- * const client = new hl.WalletClient({ wallet, transport });
284
- *
285
- * const result = await client.cancel({
286
- * cancels: [{
287
- * a: 0, // Asset index
288
- * o: 123, // Order ID
289
- * }],
290
- * });
291
- * ```
292
- */
293
- async cancel(args, signal) {
294
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
295
- const sortedAction = key_sort_js_1.sorters.cancel({ type: "cancel", ...actionArgs });
296
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
297
- const request = {
298
- action: sortedAction,
299
- signature,
300
- nonce,
301
- vaultAddress,
302
- };
303
- const response = await this.transport.request("action", request, signal);
304
- this._validateResponse(response);
305
- return response;
306
- }
307
- /**
308
- * Cancel order(s) by Client Order ID.
309
- * @param args - The parameters for the request.
310
- * @param signal - An optional abort signal.
311
- * @returns {CancelResponseSuccess} Successful variant of {@link CancelResponse} without error statuses.
312
- * @throws {ApiRequestError} When the API returns an error response.
313
- *
314
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid | Hyperliquid GitBook}
315
- * @example
316
- * ```ts
317
- * import * as hl from "@nktkas/hyperliquid";
318
- * import { privateKeyToAccount } from "viem/accounts";
319
- *
320
- * const wallet = privateKeyToAccount("0x...");
321
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
322
- * const client = new hl.WalletClient({ wallet, transport });
323
- *
324
- * const result = await client.cancelByCloid({
325
- * cancels: [{
326
- * asset: 0,
327
- * cloid: "0x...", // Client Order ID
328
- * }],
329
- * });
330
- * ```
331
- */
332
- async cancelByCloid(args, signal) {
333
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
334
- const sortedAction = key_sort_js_1.sorters.cancelByCloid({ type: "cancelByCloid", ...actionArgs });
335
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
336
- const request = {
337
- action: sortedAction,
338
- signature,
339
- nonce,
340
- vaultAddress,
341
- };
342
- const response = await this.transport.request("action", request, signal);
343
- this._validateResponse(response);
344
- return response;
345
- }
346
- /**
347
- * Create a sub-account.
348
- * @param args - The parameters for the request.
349
- * @param signal - An optional abort signal.
350
- * @returns {CreateSubAccountResponse} Response for creating a sub-account.
351
- * @throws {ApiRequestError} When the API returns an error response.
352
- *
353
- * @example
354
- * ```ts
355
- * import * as hl from "@nktkas/hyperliquid";
356
- * import { privateKeyToAccount } from "viem/accounts";
357
- *
358
- * const wallet = privateKeyToAccount("0x...");
359
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
360
- * const client = new hl.WalletClient({ wallet, transport });
361
- *
362
- * const result = await client.createSubAccount({
363
- * name: "subAccountName",
364
- * });
365
- * ```
366
- */
367
- async createSubAccount(args, signal) {
368
- const { nonce = Date.now(), ...actionArgs } = args;
369
- const sortedAction = key_sort_js_1.sorters.createSubAccount({ type: "createSubAccount", ...actionArgs });
370
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce);
371
- const request = {
372
- action: sortedAction,
373
- signature,
374
- nonce,
375
- };
376
- const response = await this.transport.request("action", request, signal);
377
- this._validateResponse(response);
378
- return response;
379
- }
380
- /**
381
- * Modify an order.
382
- * @param args - The parameters for the request.
383
- * @param signal - An optional abort signal.
384
- * @returns {SuccessResponse} Successful response without specific data.
385
- * @throws {ApiRequestError} When the API returns an error response.
386
- *
387
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-an-order | Hyperliquid GitBook}
388
- * @example
389
- * ```ts
390
- * import * as hl from "@nktkas/hyperliquid";
391
- * import { privateKeyToAccount } from "viem/accounts";
392
- *
393
- * const wallet = privateKeyToAccount("0x...");
394
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
395
- * const client = new hl.WalletClient({ wallet, transport });
396
- *
397
- * const result = await client.modify({
398
- * oid: 123, // Order ID
399
- * order: {
400
- * a: 0, // Asset index
401
- * b: true, // Buy order
402
- * p: "31000", // New price
403
- * s: "0.2", // New size
404
- * r: false, // Not reduce-only
405
- * t: {
406
- * limit: {
407
- * tif: "Gtc", // Good-til-cancelled
408
- * },
409
- * },
410
- * c: "0x...", // Optional: Client Order ID
411
- * },
412
- * });
413
- * ```
414
- */
415
- async modify(args, signal) {
416
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
417
- const sortedAction = key_sort_js_1.sorters.modify({ type: "modify", ...actionArgs });
418
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
419
- const request = {
420
- action: sortedAction,
421
- signature,
422
- nonce,
423
- vaultAddress,
424
- };
425
- const response = await this.transport.request("action", request, signal);
426
- this._validateResponse(response);
427
- return response;
428
- }
429
- /**
430
- * Place an order(s).
431
- * @param args - The parameters for the request.
432
- * @param signal - An optional abort signal.
433
- * @returns {OrderResponseSuccess} Successful variant of {@link OrderResponse} without error statuses.
434
- * @throws {ApiRequestError} When the API returns an error response.
435
- *
436
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order | Hyperliquid GitBook}
437
- * @example
438
- * ```ts
439
- * import * as hl from "@nktkas/hyperliquid";
440
- * import { privateKeyToAccount } from "viem/accounts";
441
- *
442
- * const wallet = privateKeyToAccount("0x...");
443
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
444
- * const client = new hl.WalletClient({ wallet, transport });
445
- *
446
- * const result = await client.order({
447
- * orders: [{
448
- * a: 0, // Asset index
449
- * b: true, // Buy order
450
- * p: "30000", // Price
451
- * s: "0.1", // Size
452
- * r: false, // Not reduce-only
453
- * t: {
454
- * limit: {
455
- * tif: "Gtc", // Good-til-cancelled
456
- * },
457
- * },
458
- * c: "0x...", // Optional: Client Order ID
459
- * }],
460
- * grouping: "na", // No grouping
461
- * });
462
- * ```
463
- */
464
- async order(args, signal) {
465
- const clonedArgs = structuredClone(args); // Clone to prevent mutation of original object
466
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = clonedArgs;
467
- if (actionArgs.builder)
468
- actionArgs.builder.b = actionArgs.builder.b.toLowerCase();
469
- const sortedAction = key_sort_js_1.sorters.order({ type: "order", ...actionArgs });
470
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
471
- const request = {
472
- action: sortedAction,
473
- signature,
474
- nonce,
475
- vaultAddress,
476
- };
477
- const response = await this.transport.request("action", request, signal);
478
- this._validateResponse(response);
479
- return response;
480
- }
481
- /**
482
- * Schedule a time to cancel all open orders.
483
- * @param args - The parameters for the request.
484
- * @param signal - An optional abort signal.
485
- * @returns {SuccessResponse} Successful response without specific data.
486
- * @throws {ApiRequestError} When the API returns an error response.
487
- *
488
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#schedule-cancel-dead-mans-switch | Hyperliquid GitBook}
489
- * @example
490
- * ```ts
491
- * import * as hl from "@nktkas/hyperliquid";
492
- * import { privateKeyToAccount } from "viem/accounts";
493
- *
494
- * const wallet = privateKeyToAccount("0x...");
495
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
496
- * const client = new hl.WalletClient({ wallet, transport });
497
- *
498
- * const result = await client.scheduleCancel({
499
- * time: Date.now() + 3600000, // Schedule cancellation 1 hour from now
500
- * });
501
- * ```
502
- */
503
- async scheduleCancel(args = {}, signal) {
504
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
505
- const sortedAction = key_sort_js_1.sorters.scheduleCancel({ type: "scheduleCancel", ...actionArgs });
506
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
507
- const request = {
508
- action: sortedAction,
509
- signature,
510
- nonce,
511
- vaultAddress,
512
- };
513
- const response = await this.transport.request("action", request, signal);
514
- this._validateResponse(response);
515
- return response;
516
- }
517
- /**
518
- * Set a referral code.
519
- * @param args - The parameters for the request.
520
- * @param signal - An optional abort signal.
521
- * @returns {SuccessResponse} Successful response without specific data.
522
- * @throws {ApiRequestError} When the API returns an error response.
523
- *
524
- * @example
525
- * ```ts
526
- * import * as hl from "@nktkas/hyperliquid";
527
- * import { privateKeyToAccount } from "viem/accounts";
528
- *
529
- * const wallet = privateKeyToAccount("0x...");
530
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
531
- * const client = new hl.WalletClient({ wallet, transport });
532
- *
533
- * const result = await client.setReferrer({
534
- * code: "TEST",
535
- * });
536
- * ```
537
- */
538
- async setReferrer(args, signal) {
539
- const { nonce = Date.now(), ...actionArgs } = args;
540
- const sortedAction = key_sort_js_1.sorters.setReferrer({ type: "setReferrer", ...actionArgs });
541
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce);
542
- const request = {
543
- action: sortedAction,
544
- signature,
545
- nonce,
546
- };
547
- const response = await this.transport.request("action", request, signal);
548
- this._validateResponse(response);
549
- return response;
550
- }
551
- /**
552
- * Transfer a spot asset on L1 to another address.
553
- * @param args - The parameters for the request.
554
- * @param signal - An optional abort signal.
555
- * @returns {SuccessResponse} Successful response without specific data.
556
- * @throws {ApiRequestError} When the API returns an error response.
557
- *
558
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#l1-spot-transfer | Hyperliquid GitBook}
559
- * @example
560
- * ```ts
561
- * import * as hl from "@nktkas/hyperliquid";
562
- * import { privateKeyToAccount } from "viem/accounts";
563
- *
564
- * const wallet = privateKeyToAccount("0x...");
565
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
566
- * const client = new hl.WalletClient({ wallet, transport });
567
- *
568
- * const result = await client.spotSend({
569
- * destination: "0x...",
570
- * token: "USDC:0xeb62eee3685fc4c43992febcd9e75443",
571
- * amount: "1",
572
- * });
573
- * ```
574
- */
575
- async spotSend(args, signal) {
576
- const action = {
577
- ...args,
578
- type: "spotSend",
579
- hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
580
- signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
581
- time: args.time ?? Date.now(),
582
- };
583
- const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
584
- "HyperliquidTransaction:SpotSend": [
585
- { name: "hyperliquidChain", type: "string" },
586
- { name: "destination", type: "string" },
587
- { name: "token", type: "string" },
588
- { name: "amount", type: "string" },
589
- { name: "time", type: "uint64" },
590
- ],
591
- }, parseInt(action.signatureChainId, 16));
592
- const request = {
593
- action,
594
- signature,
595
- nonce: action.time,
596
- };
597
- const response = await this.transport.request("action", request, signal);
598
- this._validateResponse(response);
599
- return response;
600
- }
601
- /**
602
- * Transfer between sub-accounts.
603
- * @param args - The parameters for the request.
604
- * @param signal - An optional abort signal.
605
- * @returns {SuccessResponse} Successful response without specific data.
606
- * @throws {ApiRequestError} When the API returns an error response.
607
- *
608
- * @example
609
- * ```ts
610
- * import * as hl from "@nktkas/hyperliquid";
611
- * import { privateKeyToAccount } from "viem/accounts";
612
- *
613
- * const wallet = privateKeyToAccount("0x...");
614
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
615
- * const client = new hl.WalletClient({ wallet, transport });
616
- *
617
- * const result = await client.subAccountTransfer({
618
- * subAccountUser: "0x...",
619
- * isDeposit: true,
620
- * usd: 1000000, // 1 USD in raw units (float amount * 1e6)
621
- * });
622
- * ```
623
- */
624
- async subAccountTransfer(args, signal) {
625
- const { nonce = Date.now(), ...actionArgs } = args;
626
- const sortedAction = key_sort_js_1.sorters.subAccountTransfer({ type: "subAccountTransfer", ...actionArgs });
627
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce);
628
- const request = {
629
- action: sortedAction,
630
- signature,
631
- nonce,
632
- };
633
- const response = await this.transport.request("action", request, signal);
634
- this._validateResponse(response);
635
- return response;
636
- }
637
- /**
638
- * Cancel a TWAP order.
639
- * @param args - The parameters for the request.
640
- * @param signal - An optional abort signal.
641
- * @returns {TwapCancelResponseSuccess} Successful variant of {@link TwapCancelResponse} without error status.
642
- * @throws {ApiRequestError} When the API returns an error response.
643
- *
644
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-a-twap-order | Hyperliquid GitBook}
645
- * @example
646
- * ```ts
647
- * import * as hl from "@nktkas/hyperliquid";
648
- * import { privateKeyToAccount } from "viem/accounts";
649
- *
650
- * const wallet = privateKeyToAccount("0x...");
651
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
652
- * const client = new hl.WalletClient({ wallet, transport });
653
- *
654
- * const result = await client.twapCancel({
655
- * a: 0, // Asset index
656
- * t: 1, // TWAP ID
657
- * });
658
- * ```
659
- */
660
- async twapCancel(args, signal) {
661
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
662
- const sortedAction = key_sort_js_1.sorters.twapCancel({ type: "twapCancel", ...actionArgs });
663
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
664
- const request = {
665
- action: sortedAction,
666
- signature,
667
- nonce,
668
- vaultAddress,
669
- };
670
- const response = await this.transport.request("action", request, signal);
671
- this._validateResponse(response);
672
- return response;
673
- }
674
- /**
675
- * Place a TWAP order.
676
- * @param args - The parameters for the request.
677
- * @param signal - An optional abort signal.
678
- * @returns {TwapOrderResponseSuccess} Successful variant of {@link TwapOrderResponse} without error status.
679
- * @throws {ApiRequestError} When the API returns an error response.
680
- *
681
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-a-twap-order | Hyperliquid GitBook}
682
- * @example
683
- * ```ts
684
- * import * as hl from "@nktkas/hyperliquid";
685
- * import { privateKeyToAccount } from "viem/accounts";
686
- *
687
- * const wallet = privateKeyToAccount("0x...");
688
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
689
- * const client = new hl.WalletClient({ wallet, transport });
690
- *
691
- * const result = await client.twapOrder({
692
- * a: 0, // Asset index
693
- * b: true, // Buy order
694
- * s: "1", // Size
695
- * r: false, // Not reduce-only
696
- * m: 10, // Duration in minutes
697
- * t: true, // Randomize order timing
698
- * });
699
- * ```
700
- */
701
- async twapOrder(args, signal) {
702
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
703
- const sortedAction = key_sort_js_1.sorters.twapOrder({ type: "twapOrder", twap: actionArgs });
704
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
705
- const request = {
706
- action: sortedAction,
707
- signature,
708
- nonce,
709
- vaultAddress,
710
- };
711
- const response = await this.transport.request("action", request, signal);
712
- this._validateResponse(response);
713
- return response;
714
- }
715
- /**
716
- * Update isolated margin for a position.
717
- * @param args - The parameters for the request.
718
- * @param signal - An optional abort signal.
719
- * @returns {SuccessResponse} Successful response without specific data.
720
- * @throws {ApiRequestError} When the API returns an error response.
721
- *
722
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-isolated-margin | Hyperliquid GitBook}
723
- * @example
724
- * ```ts
725
- * import * as hl from "@nktkas/hyperliquid";
726
- * import { privateKeyToAccount } from "viem/accounts";
727
- *
728
- * const wallet = privateKeyToAccount("0x...");
729
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
730
- * const client = new hl.WalletClient({ wallet, transport });
731
- *
732
- * const result = await client.updateIsolatedMargin({
733
- * asset: 0,
734
- * isBuy: true, // Add to long position
735
- * ntli: 1000, // Add 1000 USD margin (integer only)
736
- * });
737
- * ```
738
- */
739
- async updateIsolatedMargin(args, signal) {
740
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
741
- const sortedAction = key_sort_js_1.sorters.updateIsolatedMargin({ type: "updateIsolatedMargin", ...actionArgs });
742
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
743
- const request = {
744
- action: sortedAction,
745
- signature,
746
- nonce,
747
- vaultAddress,
748
- };
749
- const response = await this.transport.request("action", request, signal);
750
- this._validateResponse(response);
751
- return response;
752
- }
753
- /**
754
- * Update leverage for cross or isolated margin.
755
- * @param args - The parameters for the request.
756
- * @param signal - An optional abort signal.
757
- * @returns {SuccessResponse} Successful response without specific data.
758
- * @throws {ApiRequestError} When the API returns an error response.
759
- *
760
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-leverage | Hyperliquid GitBook}
761
- * @example
762
- * ```ts
763
- * import * as hl from "@nktkas/hyperliquid";
764
- * import { privateKeyToAccount } from "viem/accounts";
765
- *
766
- * const wallet = privateKeyToAccount("0x...");
767
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
768
- * const client = new hl.WalletClient({ wallet, transport });
769
- *
770
- * const result = await client.updateLeverage({
771
- * asset: 0,
772
- * isCross: true,
773
- * leverage: 5,
774
- * });
775
- * ```
776
- */
777
- async updateLeverage(args, signal) {
778
- const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
779
- const sortedAction = key_sort_js_1.sorters.updateLeverage({ type: "updateLeverage", ...actionArgs });
780
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
781
- const request = {
782
- action: sortedAction,
783
- signature,
784
- nonce,
785
- vaultAddress,
786
- };
787
- const response = await this.transport.request("action", request, signal);
788
- this._validateResponse(response);
789
- return response;
790
- }
791
- /**
792
- * Transfer funds between Spot and Perp accounts.
793
- * @param args - The parameters for the request.
794
- * @param signal - An optional abort signal.
795
- * @returns {SuccessResponse} Successful response without specific data.
796
- * @throws {ApiRequestError} When the API returns an error response.
797
- *
798
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa | Hyperliquid GitBook}
799
- * @example
800
- * ```ts
801
- * import * as hl from "@nktkas/hyperliquid";
802
- * import { privateKeyToAccount } from "viem/accounts";
803
- *
804
- * const wallet = privateKeyToAccount("0x...");
805
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
806
- * const client = new hl.WalletClient({ wallet, transport });
807
- *
808
- * const result = await client.usdClassTransfer({
809
- * amount: "1000",
810
- * toPerp: true, // Transfer from Spot to Perp
811
- * });
812
- * ```
813
- */
814
- async usdClassTransfer(args, signal) {
815
- const action = {
816
- ...args,
817
- type: "usdClassTransfer",
818
- hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
819
- signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
820
- nonce: args.nonce ?? Date.now(),
821
- };
822
- const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
823
- "HyperliquidTransaction:UsdClassTransfer": [
824
- { name: "hyperliquidChain", type: "string" },
825
- { name: "amount", type: "string" },
826
- { name: "toPerp", type: "bool" },
827
- { name: "nonce", type: "uint64" },
828
- ],
829
- }, parseInt(action.signatureChainId, 16));
830
- const request = {
831
- action,
832
- signature,
833
- nonce: action.nonce,
834
- };
835
- const response = await this.transport.request("action", request, signal);
836
- this._validateResponse(response);
837
- return response;
838
51
  }
52
+ exports.ApiRequestError = ApiRequestError;
53
+ // ——————————————— Client ———————————————
839
54
  /**
840
- * Transfer USDC on L1 to another address.
841
- * @param args - The parameters for the request.
842
- * @param signal - An optional abort signal.
843
- * @returns {SuccessResponse} Successful response without specific data.
844
- * @throws {ApiRequestError} When the API returns an error response.
845
- *
846
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#l1-usdc-transfer | Hyperliquid GitBook}
847
- * @example
848
- * ```ts
849
- * import * as hl from "@nktkas/hyperliquid";
850
- * import { privateKeyToAccount } from "viem/accounts";
851
- *
852
- * const wallet = privateKeyToAccount("0x...");
853
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
854
- * const client = new hl.WalletClient({ wallet, transport });
855
- *
856
- * const result = await client.usdSend({
857
- * destination: "0x...",
858
- * amount: "1000",
859
- * });
860
- * ```
55
+ * Wallet client for interacting with the Hyperliquid API.
56
+ * @typeParam T - The transport used to connect to the Hyperliquid API.
57
+ * @typeParam W - The WalletClient/Account ([viem](https://viem.sh/docs/clients/wallet)) or Signer ([ethers.js](https://docs.ethers.io/v6/api/providers/#Signer)) used for signing transactions.
861
58
  */
862
- async usdSend(args, signal) {
863
- const action = {
864
- ...args,
865
- type: "usdSend",
866
- hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
867
- signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
868
- time: args.time ?? Date.now(),
869
- };
870
- const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
871
- "HyperliquidTransaction:UsdSend": [
872
- { name: "hyperliquidChain", type: "string" },
873
- { name: "destination", type: "string" },
874
- { name: "amount", type: "string" },
875
- { name: "time", type: "uint64" },
876
- ],
877
- }, parseInt(action.signatureChainId, 16));
878
- const request = {
879
- action,
880
- signature,
881
- nonce: action.time,
882
- };
883
- const response = await this.transport.request("action", request, signal);
884
- this._validateResponse(response);
885
- return response;
886
- }
887
- /**
888
- * Transfer funds to/from a vault.
889
- * @param args - The parameters for the request.
890
- * @param signal - An optional abort signal.
891
- * @returns {SuccessResponse} Successful response without specific data.
892
- * @throws {ApiRequestError} When the API returns an error response.
893
- *
894
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-or-withdraw-from-a-vault | Hyperliquid GitBook}
895
- * @example
896
- * ```ts
897
- * import * as hl from "@nktkas/hyperliquid";
898
- * import { privateKeyToAccount } from "viem/accounts";
899
- *
900
- * const wallet = privateKeyToAccount("0x...");
901
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
902
- * const client = new hl.WalletClient({ wallet, transport });
903
- *
904
- * const result = await client.vaultTransfer({
905
- * vaultAddress: "0x...",
906
- * isDeposit: true,
907
- * usd: 1000000, // 1 USD in raw units (float amount * 1e6)
908
- * });
909
- * ```
910
- */
911
- async vaultTransfer(args, signal) {
912
- const { nonce = Date.now(), ...actionArgs } = args;
913
- const sortedAction = key_sort_js_1.sorters.vaultTransfer({ type: "vaultTransfer", ...actionArgs });
914
- const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce);
915
- const request = {
916
- action: sortedAction,
917
- signature,
918
- nonce,
919
- };
920
- const response = await this.transport.request("action", request, signal);
921
- this._validateResponse(response);
922
- return response;
923
- }
924
- /**
925
- * Initiate a withdrawal request.
926
- * @param args - The parameters for the request.
927
- * @param signal - An optional abort signal.
928
- * @returns {SuccessResponse} Successful response without specific data.
929
- * @throws {ApiRequestError} When the API returns an error response.
930
- *
931
- * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#initiate-a-withdrawal-request | Hyperliquid GitBook}
932
- * @example
933
- * ```ts
934
- * import * as hl from "@nktkas/hyperliquid";
935
- * import { privateKeyToAccount } from "viem/accounts";
936
- *
937
- * const wallet = privateKeyToAccount("0x...");
938
- * const transport = new hl.HttpTransport(); // or WebSocketTransport
939
- * const client = new hl.WalletClient({ wallet, transport });
940
- *
941
- * const result = await client.withdraw3({
942
- * destination: "0x...",
943
- * amount: "1000",
944
- * });
945
- * ```
946
- */
947
- async withdraw3(args, signal) {
948
- const action = {
949
- ...args,
950
- type: "withdraw3",
951
- hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
952
- signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
953
- time: args.time ?? Date.now(),
954
- };
955
- const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
956
- "HyperliquidTransaction:Withdraw": [
957
- { name: "hyperliquidChain", type: "string" },
958
- { name: "destination", type: "string" },
959
- { name: "amount", type: "string" },
960
- { name: "time", type: "uint64" },
961
- ],
962
- }, parseInt(action.signatureChainId, 16));
963
- const request = {
964
- action,
965
- signature,
966
- nonce: action.time,
967
- };
968
- const response = await this.transport.request("action", request, signal);
969
- this._validateResponse(response);
970
- return response;
971
- }
972
- /** Validate a response from the API. */
973
- _validateResponse(response) {
974
- if (response.status === "err") {
975
- throw new ApiRequestError(response);
59
+ class WalletClient {
60
+ /**
61
+ * Initialises a new instance.
62
+ * @param args - The parameters for the client.
63
+ *
64
+ * @example Private key via [viem](https://viem.sh/docs/clients/wallet#local-accounts-private-key-mnemonic-etc)
65
+ * ```ts
66
+ * import * as hl from "@nktkas/hyperliquid";
67
+ * import { privateKeyToAccount } from "viem/accounts";
68
+ *
69
+ * const wallet = privateKeyToAccount("0x...");
70
+ *
71
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
72
+ * const client = new hl.WalletClient({ wallet, transport });
73
+ * ```
74
+ *
75
+ * @example Private key via [ethers.js](https://docs.ethers.org/v6/api/wallet/#Wallet)
76
+ * ```ts
77
+ * import * as hl from "@nktkas/hyperliquid";
78
+ * import { ethers } from "ethers";
79
+ *
80
+ * const wallet = new ethers.Wallet("0x...");
81
+ *
82
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
83
+ * const client = new hl.WalletClient({ wallet, transport });
84
+ * ```
85
+ *
86
+ * @example External wallet (e.g. MetaMask) via [viem](https://viem.sh/docs/clients/wallet#optional-hoist-the-account)
87
+ * ```ts
88
+ * import * as hl from "@nktkas/hyperliquid";
89
+ * import { createWalletClient, custom } from "viem";
90
+ *
91
+ * const [account] = await window.ethereum.request({ method: "eth_requestAccounts" });
92
+ * const wallet = createWalletClient({ account, transport: custom(window.ethereum) });
93
+ *
94
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
95
+ * const client = new hl.WalletClient({ wallet, transport });
96
+ * ```
97
+ */
98
+ constructor(args) {
99
+ /** The transport used to connect to the Hyperliquid API. */
100
+ Object.defineProperty(this, "transport", {
101
+ enumerable: true,
102
+ configurable: true,
103
+ writable: true,
104
+ value: void 0
105
+ });
106
+ /** The WalletClient/Account ([viem](https://viem.sh/docs/clients/wallet)) or Signer ([ethers.js](https://docs.ethers.org/v6/api/providers/#Signer)) used for signing transactions. */
107
+ Object.defineProperty(this, "wallet", {
108
+ enumerable: true,
109
+ configurable: true,
110
+ writable: true,
111
+ value: void 0
112
+ });
113
+ /** Specifies whether the client uses testnet. */
114
+ Object.defineProperty(this, "isTestnet", {
115
+ enumerable: true,
116
+ configurable: true,
117
+ writable: true,
118
+ value: void 0
119
+ });
120
+ /** Sets a default vaultAddress to be used if no vaultAddress is explicitly passed to a method. */
121
+ Object.defineProperty(this, "defaultVaultAddress", {
122
+ enumerable: true,
123
+ configurable: true,
124
+ writable: true,
125
+ value: void 0
126
+ });
127
+ this.transport = args.transport;
128
+ this.wallet = args.wallet;
129
+ this.isTestnet = args.isTestnet ?? false;
130
+ this.defaultVaultAddress = args.defaultVaultAddress;
976
131
  }
977
- else if (response.response.type === "order" || response.response.type === "cancel") {
978
- if (response.response.data.statuses.some((status) => typeof status === "object" && "error" in status)) {
979
- throw new ApiRequestError(response);
980
- }
132
+ // ———————————————Actions———————————————
133
+ /**
134
+ * Approve an agent to sign on behalf of the master or sub-accounts.
135
+ * @param args - The parameters for the request.
136
+ * @param signal - An optional abort signal
137
+ * @returns Successful response without specific data.
138
+ * @throws {ApiRequestError} When the API returns an error response.
139
+ *
140
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-an-api-wallet
141
+ * @example
142
+ * ```ts
143
+ * import * as hl from "@nktkas/hyperliquid";
144
+ * import { privateKeyToAccount } from "viem/accounts";
145
+ *
146
+ * const wallet = privateKeyToAccount("0x...");
147
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
148
+ * const client = new hl.WalletClient({ wallet, transport });
149
+ *
150
+ * const result = await client.approveAgent({
151
+ * agentAddress: "0x...",
152
+ * agentName: "agentName",
153
+ * });
154
+ */
155
+ async approveAgent(args, signal) {
156
+ const action = {
157
+ ...args,
158
+ type: "approveAgent",
159
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
160
+ signatureChainId: args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1"),
161
+ nonce: args.nonce ?? Date.now(),
162
+ };
163
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
164
+ "HyperliquidTransaction:ApproveAgent": [
165
+ { name: "hyperliquidChain", type: "string" },
166
+ { name: "agentAddress", type: "address" },
167
+ { name: "agentName", type: "string" },
168
+ { name: "nonce", type: "uint64" },
169
+ ],
170
+ }, parseInt(action.signatureChainId, 16));
171
+ const request = {
172
+ action,
173
+ signature,
174
+ nonce: action.nonce,
175
+ };
176
+ const response = await this.transport.request("action", request, signal);
177
+ this._validateResponse(response);
178
+ return response;
179
+ }
180
+ /**
181
+ * Approve a max fee rate for a builder address.
182
+ * @param args - The parameters for the request.
183
+ * @param signal - An optional abort signal.
184
+ * @returns Successful response without specific data.
185
+ * @throws {ApiRequestError} When the API returns an error response.
186
+ *
187
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-a-builder-fee
188
+ * @example
189
+ * ```ts
190
+ * import * as hl from "@nktkas/hyperliquid";
191
+ * import { privateKeyToAccount } from "viem/accounts";
192
+ *
193
+ * const wallet = privateKeyToAccount("0x...");
194
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
195
+ * const client = new hl.WalletClient({ wallet, transport });
196
+ *
197
+ * const result = await client.approveBuilderFee({
198
+ * maxFeeRate: "0.01%",
199
+ * builder: "0x...",
200
+ * });
201
+ */
202
+ async approveBuilderFee(args, signal) {
203
+ const action = {
204
+ ...args,
205
+ type: "approveBuilderFee",
206
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
207
+ signatureChainId: args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1"),
208
+ nonce: args.nonce ?? Date.now(),
209
+ };
210
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
211
+ "HyperliquidTransaction:ApproveBuilderFee": [
212
+ { name: "hyperliquidChain", type: "string" },
213
+ { name: "maxFeeRate", type: "string" },
214
+ { name: "builder", type: "address" },
215
+ { name: "nonce", type: "uint64" },
216
+ ],
217
+ }, parseInt(action.signatureChainId, 16));
218
+ const request = {
219
+ action,
220
+ signature,
221
+ nonce: action.nonce,
222
+ };
223
+ const response = await this.transport.request("action", request, signal);
224
+ this._validateResponse(response);
225
+ return response;
226
+ }
227
+ /**
228
+ * Modify multiple orders.
229
+ * @param args - The parameters for the request.
230
+ * @param signal - An optional abort signal.
231
+ * @returns Successful variant of {@link OrderResponse} without error statuses.
232
+ * @throws {ApiRequestError} When the API returns an error response.
233
+ *
234
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders
235
+ * @example
236
+ * ```ts
237
+ * import * as hl from "@nktkas/hyperliquid";
238
+ * import { privateKeyToAccount } from "viem/accounts";
239
+ *
240
+ * const wallet = privateKeyToAccount("0x...");
241
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
242
+ * const client = new hl.WalletClient({ wallet, transport });
243
+ *
244
+ * const result = await client.batchModify({
245
+ * modifies: [{
246
+ * oid: 123, // Order ID
247
+ * order: {
248
+ * a: 0, // Asset index
249
+ * b: true, // Buy order
250
+ * p: "31000", // New price
251
+ * s: "0.2", // New size
252
+ * r: false, // Not reduce-only
253
+ * t: {
254
+ * limit: {
255
+ * tif: "Gtc", // Good-til-cancelled
256
+ * },
257
+ * },
258
+ * c: "0x...", // Optional: Client Order ID
259
+ * },
260
+ * }],
261
+ * });
262
+ * ```
263
+ */
264
+ async batchModify(args, signal) {
265
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
266
+ const sortedAction = key_sort_js_1.sorters.batchModify({ type: "batchModify", ...actionArgs });
267
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
268
+ const request = {
269
+ action: sortedAction,
270
+ signature,
271
+ nonce,
272
+ vaultAddress,
273
+ };
274
+ const response = await this.transport.request("action", request, signal);
275
+ this._validateResponse(response);
276
+ return response;
277
+ }
278
+ /**
279
+ * Cancel order(s).
280
+ * @param args - The parameters for the request.
281
+ * @param signal - An optional abort signal.
282
+ * @returns Successful variant of {@link CancelResponse} without error statuses.
283
+ * @throws {ApiRequestError} When the API returns an error response.
284
+ *
285
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
286
+ * @example
287
+ * ```ts
288
+ * import * as hl from "@nktkas/hyperliquid";
289
+ * import { privateKeyToAccount } from "viem/accounts";
290
+ *
291
+ * const wallet = privateKeyToAccount("0x...");
292
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
293
+ * const client = new hl.WalletClient({ wallet, transport });
294
+ *
295
+ * const result = await client.cancel({
296
+ * cancels: [{
297
+ * a: 0, // Asset index
298
+ * o: 123, // Order ID
299
+ * }],
300
+ * });
301
+ * ```
302
+ */
303
+ async cancel(args, signal) {
304
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
305
+ const sortedAction = key_sort_js_1.sorters.cancel({ type: "cancel", ...actionArgs });
306
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
307
+ const request = {
308
+ action: sortedAction,
309
+ signature,
310
+ nonce,
311
+ vaultAddress,
312
+ };
313
+ const response = await this.transport.request("action", request, signal);
314
+ this._validateResponse(response);
315
+ return response;
981
316
  }
982
- else if (response.response.type === "twapOrder" || response.response.type === "twapCancel") {
983
- if (typeof response.response.data.status === "object" && "error" in response.response.data.status) {
317
+ /**
318
+ * Deposit into staking balance.
319
+ * @param args - The parameters for the request.
320
+ * @param signal - An optional abort signal.
321
+ * @returns Successful response without specific data.
322
+ * @throws {ApiRequestError} When the API returns an error response.
323
+ *
324
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-into-staking
325
+ * @example
326
+ * ```ts
327
+ * import * as hl from "@nktkas/hyperliquid";
328
+ * import { privateKeyToAccount } from "viem/accounts";
329
+ *
330
+ * const wallet = privateKeyToAccount("0x...");
331
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
332
+ * const client = new hl.WalletClient({ wallet, transport });
333
+ *
334
+ * const result = await client.cDeposit({ wei: 100000000 });
335
+ * ```
336
+ */
337
+ async cDeposit(args, signal) {
338
+ const action = {
339
+ ...args,
340
+ type: "cDeposit",
341
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
342
+ signatureChainId: args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1"),
343
+ nonce: args.nonce ?? Date.now(),
344
+ };
345
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
346
+ "HyperliquidTransaction:CDeposit": [
347
+ { name: "hyperliquidChain", type: "string" },
348
+ { name: "wei", type: "uint64" },
349
+ { name: "nonce", type: "uint64" },
350
+ ],
351
+ }, parseInt(action.signatureChainId, 16));
352
+ const request = {
353
+ action,
354
+ signature,
355
+ nonce: action.nonce,
356
+ };
357
+ const response = await this.transport.request("action", request, signal);
358
+ this._validateResponse(response);
359
+ return response;
360
+ }
361
+ /**
362
+ * Cancel order(s) by cloid.
363
+ * @param args - The parameters for the request.
364
+ * @param signal - An optional abort signal.
365
+ * @returns Successful variant of {@link CancelResponse} without error statuses.
366
+ * @throws {ApiRequestError} When the API returns an error response.
367
+ *
368
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid
369
+ * @example
370
+ * ```ts
371
+ * import * as hl from "@nktkas/hyperliquid";
372
+ * import { privateKeyToAccount } from "viem/accounts";
373
+ *
374
+ * const wallet = privateKeyToAccount("0x...");
375
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
376
+ * const client = new hl.WalletClient({ wallet, transport });
377
+ *
378
+ * const result = await client.cancelByCloid({
379
+ * cancels: [{
380
+ * asset: 0,
381
+ * cloid: "0x...", // Client Order ID
382
+ * }],
383
+ * });
384
+ * ```
385
+ */
386
+ async cancelByCloid(args, signal) {
387
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
388
+ const sortedAction = key_sort_js_1.sorters.cancelByCloid({ type: "cancelByCloid", ...actionArgs });
389
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
390
+ const request = {
391
+ action: sortedAction,
392
+ signature,
393
+ nonce,
394
+ vaultAddress,
395
+ };
396
+ const response = await this.transport.request("action", request, signal);
397
+ this._validateResponse(response);
398
+ return response;
399
+ }
400
+ /**
401
+ * Withdraw from staking balance.
402
+ * @param args - The parameters for the request.
403
+ * @param signal - An optional abort signal.
404
+ * @returns Successful response without specific data.
405
+ * @throws {ApiRequestError} When the API returns an error response.
406
+ *
407
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#withdraw-from-staking
408
+ * @example
409
+ * ```ts
410
+ * import * as hl from "@nktkas/hyperliquid";
411
+ * import { privateKeyToAccount } from "viem/accounts";
412
+ *
413
+ * const wallet = privateKeyToAccount("0x...");
414
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
415
+ * const client = new hl.WalletClient({ wallet, transport });
416
+ *
417
+ * const result = await client.cWithdraw({ wei: 100000000 });
418
+ * ```
419
+ */
420
+ async cWithdraw(args, signal) {
421
+ const action = {
422
+ ...args,
423
+ type: "cWithdraw",
424
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
425
+ signatureChainId: args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1"),
426
+ nonce: args.nonce ?? Date.now(),
427
+ };
428
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
429
+ "HyperliquidTransaction:CWithdraw": [
430
+ { name: "hyperliquidChain", type: "string" },
431
+ { name: "wei", type: "uint64" },
432
+ { name: "nonce", type: "uint64" },
433
+ ],
434
+ }, parseInt(action.signatureChainId, 16));
435
+ const request = {
436
+ action,
437
+ signature,
438
+ nonce: action.nonce,
439
+ };
440
+ const response = await this.transport.request("action", request, signal);
441
+ this._validateResponse(response);
442
+ return response;
443
+ }
444
+ /**
445
+ * Create a sub-account.
446
+ * @param args - The parameters for the request.
447
+ * @param signal - An optional abort signal.
448
+ * @returns Response for creating a sub-account.
449
+ * @throws {ApiRequestError} When the API returns an error response.
450
+ *
451
+ * @see null - no documentation
452
+ * @example
453
+ * ```ts
454
+ * import * as hl from "@nktkas/hyperliquid";
455
+ * import { privateKeyToAccount } from "viem/accounts";
456
+ *
457
+ * const wallet = privateKeyToAccount("0x...");
458
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
459
+ * const client = new hl.WalletClient({ wallet, transport });
460
+ *
461
+ * const result = await client.createSubAccount({ name: "subAccountName" });
462
+ * ```
463
+ */
464
+ async createSubAccount(args, signal) {
465
+ const { nonce = Date.now(), ...actionArgs } = args;
466
+ const sortedAction = key_sort_js_1.sorters.createSubAccount({ type: "createSubAccount", ...actionArgs });
467
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce);
468
+ const request = {
469
+ action: sortedAction,
470
+ signature,
471
+ nonce,
472
+ };
473
+ const response = await this.transport.request("action", request, signal);
474
+ this._validateResponse(response);
475
+ return response;
476
+ }
477
+ /**
478
+ * Modify an order.
479
+ * @param args - The parameters for the request.
480
+ * @param signal - An optional abort signal.
481
+ * @returns Successful response without specific data.
482
+ * @throws {ApiRequestError} When the API returns an error response.
483
+ *
484
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-an-order
485
+ * @example
486
+ * ```ts
487
+ * import * as hl from "@nktkas/hyperliquid";
488
+ * import { privateKeyToAccount } from "viem/accounts";
489
+ *
490
+ * const wallet = privateKeyToAccount("0x...");
491
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
492
+ * const client = new hl.WalletClient({ wallet, transport });
493
+ *
494
+ * const result = await client.modify({
495
+ * oid: 123, // Order ID
496
+ * order: {
497
+ * a: 0, // Asset index
498
+ * b: true, // Buy order
499
+ * p: "31000", // New price
500
+ * s: "0.2", // New size
501
+ * r: false, // Not reduce-only
502
+ * t: {
503
+ * limit: {
504
+ * tif: "Gtc", // Good-til-cancelled
505
+ * },
506
+ * },
507
+ * c: "0x...", // Optional: Client Order ID
508
+ * },
509
+ * });
510
+ * ```
511
+ */
512
+ async modify(args, signal) {
513
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
514
+ const sortedAction = key_sort_js_1.sorters.modify({ type: "modify", ...actionArgs });
515
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
516
+ const request = {
517
+ action: sortedAction,
518
+ signature,
519
+ nonce,
520
+ vaultAddress,
521
+ };
522
+ const response = await this.transport.request("action", request, signal);
523
+ this._validateResponse(response);
524
+ return response;
525
+ }
526
+ /**
527
+ * Place an order(s).
528
+ * @param args - The parameters for the request.
529
+ * @param signal - An optional abort signal.
530
+ * @returns Successful variant of {@link OrderResponse} without error statuses.
531
+ * @throws {ApiRequestError} When the API returns an error response.
532
+ *
533
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order
534
+ * @example
535
+ * ```ts
536
+ * import * as hl from "@nktkas/hyperliquid";
537
+ * import { privateKeyToAccount } from "viem/accounts";
538
+ *
539
+ * const wallet = privateKeyToAccount("0x...");
540
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
541
+ * const client = new hl.WalletClient({ wallet, transport });
542
+ *
543
+ * const result = await client.order({
544
+ * orders: [{
545
+ * a: 0, // Asset index
546
+ * b: true, // Buy order
547
+ * p: "30000", // Price
548
+ * s: "0.1", // Size
549
+ * r: false, // Not reduce-only
550
+ * t: {
551
+ * limit: {
552
+ * tif: "Gtc", // Good-til-cancelled
553
+ * },
554
+ * },
555
+ * c: "0x...", // Optional: Client Order ID
556
+ * }],
557
+ * grouping: "na", // No grouping
558
+ * });
559
+ * ```
560
+ */
561
+ async order(args, signal) {
562
+ const clonedArgs = structuredClone(args); // Clone to prevent mutation of original object
563
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = clonedArgs;
564
+ if (actionArgs.builder)
565
+ actionArgs.builder.b = actionArgs.builder.b.toLowerCase();
566
+ const sortedAction = key_sort_js_1.sorters.order({ type: "order", ...actionArgs });
567
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
568
+ const request = {
569
+ action: sortedAction,
570
+ signature,
571
+ nonce,
572
+ vaultAddress,
573
+ };
574
+ const response = await this.transport.request("action", request, signal);
575
+ this._validateResponse(response);
576
+ return response;
577
+ }
578
+ /**
579
+ * Schedule a time to cancel all open orders.
580
+ * @param args - The parameters for the request.
581
+ * @param signal - An optional abort signal.
582
+ * @returns Successful response without specific data.
583
+ * @throws {ApiRequestError} When the API returns an error response.
584
+ *
585
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#schedule-cancel-dead-mans-switch
586
+ * @example
587
+ * ```ts
588
+ * import * as hl from "@nktkas/hyperliquid";
589
+ * import { privateKeyToAccount } from "viem/accounts";
590
+ *
591
+ * const wallet = privateKeyToAccount("0x...");
592
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
593
+ * const client = new hl.WalletClient({ wallet, transport });
594
+ *
595
+ * const result = await client.scheduleCancel({ time: Date.now() + 3600000 });
596
+ * ```
597
+ */
598
+ async scheduleCancel(args = {}, signal) {
599
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
600
+ const sortedAction = key_sort_js_1.sorters.scheduleCancel({ type: "scheduleCancel", ...actionArgs });
601
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
602
+ const request = {
603
+ action: sortedAction,
604
+ signature,
605
+ nonce,
606
+ vaultAddress,
607
+ };
608
+ const response = await this.transport.request("action", request, signal);
609
+ this._validateResponse(response);
610
+ return response;
611
+ }
612
+ /**
613
+ * Set a referral code.
614
+ * @param args - The parameters for the request.
615
+ * @param signal - An optional abort signal.
616
+ * @returns Successful response without specific data.
617
+ * @throws {ApiRequestError} When the API returns an error response.
618
+ *
619
+ * @see null - no documentation
620
+ * @example
621
+ * ```ts
622
+ * import * as hl from "@nktkas/hyperliquid";
623
+ * import { privateKeyToAccount } from "viem/accounts";
624
+ *
625
+ * const wallet = privateKeyToAccount("0x...");
626
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
627
+ * const client = new hl.WalletClient({ wallet, transport });
628
+ *
629
+ * const result = await client.setReferrer({ code: "TEST" });
630
+ * ```
631
+ */
632
+ async setReferrer(args, signal) {
633
+ const { nonce = Date.now(), ...actionArgs } = args;
634
+ const sortedAction = key_sort_js_1.sorters.setReferrer({ type: "setReferrer", ...actionArgs });
635
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce);
636
+ const request = {
637
+ action: sortedAction,
638
+ signature,
639
+ nonce,
640
+ };
641
+ const response = await this.transport.request("action", request, signal);
642
+ this._validateResponse(response);
643
+ return response;
644
+ }
645
+ /**
646
+ * Transfer a spot asset on L1 to another address.
647
+ * @param args - The parameters for the request.
648
+ * @param signal - An optional abort signal.
649
+ * @returns Successful response without specific data.
650
+ * @throws {ApiRequestError} When the API returns an error response.
651
+ *
652
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#l1-spot-transfer
653
+ * @example
654
+ * ```ts
655
+ * import * as hl from "@nktkas/hyperliquid";
656
+ * import { privateKeyToAccount } from "viem/accounts";
657
+ *
658
+ * const wallet = privateKeyToAccount("0x...");
659
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
660
+ * const client = new hl.WalletClient({ wallet, transport });
661
+ *
662
+ * const result = await client.spotSend({
663
+ * destination: "0x...",
664
+ * token: "USDC:0xeb62eee3685fc4c43992febcd9e75443",
665
+ * amount: "1",
666
+ * });
667
+ * ```
668
+ */
669
+ async spotSend(args, signal) {
670
+ const action = {
671
+ ...args,
672
+ type: "spotSend",
673
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
674
+ signatureChainId: args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1"),
675
+ time: args.time ?? Date.now(),
676
+ };
677
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
678
+ "HyperliquidTransaction:SpotSend": [
679
+ { name: "hyperliquidChain", type: "string" },
680
+ { name: "destination", type: "string" },
681
+ { name: "token", type: "string" },
682
+ { name: "amount", type: "string" },
683
+ { name: "time", type: "uint64" },
684
+ ],
685
+ }, parseInt(action.signatureChainId, 16));
686
+ const request = {
687
+ action,
688
+ signature,
689
+ nonce: action.time,
690
+ };
691
+ const response = await this.transport.request("action", request, signal);
692
+ this._validateResponse(response);
693
+ return response;
694
+ }
695
+ /**
696
+ * Transfer between sub-accounts.
697
+ * @param args - The parameters for the request.
698
+ * @param signal - An optional abort signal.
699
+ * @returns Successful response without specific data.
700
+ * @throws {ApiRequestError} When the API returns an error response.
701
+ *
702
+ * @see null - no documentation
703
+ * @example
704
+ * ```ts
705
+ * import * as hl from "@nktkas/hyperliquid";
706
+ * import { privateKeyToAccount } from "viem/accounts";
707
+ *
708
+ * const wallet = privateKeyToAccount("0x...");
709
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
710
+ * const client = new hl.WalletClient({ wallet, transport });
711
+ *
712
+ * const result = await client.subAccountTransfer({
713
+ * subAccountUser: "0x...",
714
+ * isDeposit: true,
715
+ * usd: 1000000, // 1 USD in raw units (float amount * 1e6)
716
+ * });
717
+ * ```
718
+ */
719
+ async subAccountTransfer(args, signal) {
720
+ const { nonce = Date.now(), ...actionArgs } = args;
721
+ const sortedAction = key_sort_js_1.sorters.subAccountTransfer({ type: "subAccountTransfer", ...actionArgs });
722
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce);
723
+ const request = {
724
+ action: sortedAction,
725
+ signature,
726
+ nonce,
727
+ };
728
+ const response = await this.transport.request("action", request, signal);
729
+ this._validateResponse(response);
730
+ return response;
731
+ }
732
+ /**
733
+ * Delegate or undelegate stake from a validator.
734
+ * @param args - The parameters for the request.
735
+ * @param signal - An optional abort signal.
736
+ * @returns Successful response without specific data.
737
+ * @throws {ApiRequestError} When the API returns an error response.
738
+ *
739
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#delegate-or-undelegate-stake-from-validator
740
+ * @example
741
+ * ```ts
742
+ * import * as hl from "@nktkas/hyperliquid";
743
+ * import { privateKeyToAccount } from "viem/accounts";
744
+ *
745
+ * const wallet = privateKeyToAccount("0x...");
746
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
747
+ * const client = new hl.WalletClient({ wallet, transport });
748
+ *
749
+ * const result = await client.tokenDelegate({
750
+ * validator: "0x...",
751
+ * isUndelegate: true,
752
+ * wei: 100000000,
753
+ * });
754
+ * ```
755
+ */
756
+ async tokenDelegate(args, signal) {
757
+ const action = {
758
+ ...args,
759
+ type: "tokenDelegate",
760
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
761
+ signatureChainId: args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1"),
762
+ nonce: args.nonce ?? Date.now(),
763
+ };
764
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
765
+ "HyperliquidTransaction:TokenDelegate": [
766
+ { name: "hyperliquidChain", type: "string" },
767
+ { name: "validator", type: "address" },
768
+ { name: "wei", type: "uint64" },
769
+ { name: "isUndelegate", type: "bool" },
770
+ { name: "nonce", type: "uint64" },
771
+ ],
772
+ }, parseInt(action.signatureChainId, 16));
773
+ const request = {
774
+ action,
775
+ signature,
776
+ nonce: action.nonce,
777
+ };
778
+ const response = await this.transport.request("action", request, signal);
779
+ this._validateResponse(response);
780
+ return response;
781
+ }
782
+ /**
783
+ * Cancel a TWAP order.
784
+ * @param args - The parameters for the request.
785
+ * @param signal - An optional abort signal.
786
+ * @returns Successful variant of {@link TwapCancelResponse} without error status.
787
+ * @throws {ApiRequestError} When the API returns an error response.
788
+ *
789
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-a-twap-order
790
+ * @example
791
+ * ```ts
792
+ * import * as hl from "@nktkas/hyperliquid";
793
+ * import { privateKeyToAccount } from "viem/accounts";
794
+ *
795
+ * const wallet = privateKeyToAccount("0x...");
796
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
797
+ * const client = new hl.WalletClient({ wallet, transport });
798
+ *
799
+ * const result = await client.twapCancel({
800
+ * a: 0, // Asset index
801
+ * t: 1, // TWAP ID
802
+ * });
803
+ * ```
804
+ */
805
+ async twapCancel(args, signal) {
806
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
807
+ const sortedAction = key_sort_js_1.sorters.twapCancel({ type: "twapCancel", ...actionArgs });
808
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
809
+ const request = {
810
+ action: sortedAction,
811
+ signature,
812
+ nonce,
813
+ vaultAddress,
814
+ };
815
+ const response = await this.transport.request("action", request, signal);
816
+ this._validateResponse(response);
817
+ return response;
818
+ }
819
+ /**
820
+ * Place a TWAP order.
821
+ * @param args - The parameters for the request.
822
+ * @param signal - An optional abort signal.
823
+ * @returns Successful variant of {@link TwapOrderResponse} without error status.
824
+ * @throws {ApiRequestError} When the API returns an error response.
825
+ *
826
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-a-twap-order
827
+ * @example
828
+ * ```ts
829
+ * import * as hl from "@nktkas/hyperliquid";
830
+ * import { privateKeyToAccount } from "viem/accounts";
831
+ *
832
+ * const wallet = privateKeyToAccount("0x...");
833
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
834
+ * const client = new hl.WalletClient({ wallet, transport });
835
+ *
836
+ * const result = await client.twapOrder({
837
+ * a: 0, // Asset index
838
+ * b: true, // Buy order
839
+ * s: "1", // Size
840
+ * r: false, // Not reduce-only
841
+ * m: 10, // Duration in minutes
842
+ * t: true, // Randomize order timing
843
+ * });
844
+ * ```
845
+ */
846
+ async twapOrder(args, signal) {
847
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
848
+ const sortedAction = key_sort_js_1.sorters.twapOrder({ type: "twapOrder", twap: actionArgs });
849
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
850
+ const request = {
851
+ action: sortedAction,
852
+ signature,
853
+ nonce,
854
+ vaultAddress,
855
+ };
856
+ const response = await this.transport.request("action", request, signal);
857
+ this._validateResponse(response);
858
+ return response;
859
+ }
860
+ /**
861
+ * Update isolated margin for a position.
862
+ * @param args - The parameters for the request.
863
+ * @param signal - An optional abort signal.
864
+ * @returns Successful response without specific data.
865
+ * @throws {ApiRequestError} When the API returns an error response.
866
+ *
867
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-isolated-margin
868
+ * @example
869
+ * ```ts
870
+ * import * as hl from "@nktkas/hyperliquid";
871
+ * import { privateKeyToAccount } from "viem/accounts";
872
+ *
873
+ * const wallet = privateKeyToAccount("0x...");
874
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
875
+ * const client = new hl.WalletClient({ wallet, transport });
876
+ *
877
+ * const result = await client.updateIsolatedMargin({
878
+ * asset: 0,
879
+ * isBuy: true, // Add to long position
880
+ * ntli: 1000, // Add 1000 USD margin (integer only)
881
+ * });
882
+ * ```
883
+ */
884
+ async updateIsolatedMargin(args, signal) {
885
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
886
+ const sortedAction = key_sort_js_1.sorters.updateIsolatedMargin({ type: "updateIsolatedMargin", ...actionArgs });
887
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
888
+ const request = {
889
+ action: sortedAction,
890
+ signature,
891
+ nonce,
892
+ vaultAddress,
893
+ };
894
+ const response = await this.transport.request("action", request, signal);
895
+ this._validateResponse(response);
896
+ return response;
897
+ }
898
+ /**
899
+ * Update leverage for cross or isolated margin.
900
+ * @param args - The parameters for the request.
901
+ * @param signal - An optional abort signal.
902
+ * @returns Successful response without specific data.
903
+ * @throws {ApiRequestError} When the API returns an error response.
904
+ *
905
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-leverage
906
+ * @example
907
+ * ```ts
908
+ * import * as hl from "@nktkas/hyperliquid";
909
+ * import { privateKeyToAccount } from "viem/accounts";
910
+ *
911
+ * const wallet = privateKeyToAccount("0x...");
912
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
913
+ * const client = new hl.WalletClient({ wallet, transport });
914
+ *
915
+ * const result = await client.updateLeverage({
916
+ * asset: 0,
917
+ * isCross: true,
918
+ * leverage: 5,
919
+ * });
920
+ * ```
921
+ */
922
+ async updateLeverage(args, signal) {
923
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
924
+ const sortedAction = key_sort_js_1.sorters.updateLeverage({ type: "updateLeverage", ...actionArgs });
925
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
926
+ const request = {
927
+ action: sortedAction,
928
+ signature,
929
+ nonce,
930
+ vaultAddress,
931
+ };
932
+ const response = await this.transport.request("action", request, signal);
933
+ this._validateResponse(response);
934
+ return response;
935
+ }
936
+ /**
937
+ * Transfer funds between Spot and Perp accounts.
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 https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
944
+ * @example
945
+ * ```ts
946
+ * import * as hl from "@nktkas/hyperliquid";
947
+ * import { privateKeyToAccount } from "viem/accounts";
948
+ *
949
+ * const wallet = privateKeyToAccount("0x...");
950
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
951
+ * const client = new hl.WalletClient({ wallet, transport });
952
+ *
953
+ * const result = await client.usdClassTransfer({
954
+ * amount: "1000",
955
+ * toPerp: true, // Transfer from Spot to Perp
956
+ * });
957
+ * ```
958
+ */
959
+ async usdClassTransfer(args, signal) {
960
+ const action = {
961
+ ...args,
962
+ type: "usdClassTransfer",
963
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
964
+ signatureChainId: args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1"),
965
+ nonce: args.nonce ?? Date.now(),
966
+ };
967
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
968
+ "HyperliquidTransaction:UsdClassTransfer": [
969
+ { name: "hyperliquidChain", type: "string" },
970
+ { name: "amount", type: "string" },
971
+ { name: "toPerp", type: "bool" },
972
+ { name: "nonce", type: "uint64" },
973
+ ],
974
+ }, parseInt(action.signatureChainId, 16));
975
+ const request = {
976
+ action,
977
+ signature,
978
+ nonce: action.nonce,
979
+ };
980
+ const response = await this.transport.request("action", request, signal);
981
+ this._validateResponse(response);
982
+ return response;
983
+ }
984
+ /**
985
+ * Transfer USDC on L1 to another address.
986
+ * @param args - The parameters for the request.
987
+ * @param signal - An optional abort signal.
988
+ * @returns Successful response without specific data.
989
+ * @throws {ApiRequestError} When the API returns an error response.
990
+ *
991
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#l1-usdc-transfer
992
+ * @example
993
+ * ```ts
994
+ * import * as hl from "@nktkas/hyperliquid";
995
+ * import { privateKeyToAccount } from "viem/accounts";
996
+ *
997
+ * const wallet = privateKeyToAccount("0x...");
998
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
999
+ * const client = new hl.WalletClient({ wallet, transport });
1000
+ *
1001
+ * const result = await client.usdSend({
1002
+ * destination: "0x...",
1003
+ * amount: "1000",
1004
+ * });
1005
+ * ```
1006
+ */
1007
+ async usdSend(args, signal) {
1008
+ const action = {
1009
+ ...args,
1010
+ type: "usdSend",
1011
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1012
+ signatureChainId: args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1"),
1013
+ time: args.time ?? Date.now(),
1014
+ };
1015
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
1016
+ "HyperliquidTransaction:UsdSend": [
1017
+ { name: "hyperliquidChain", type: "string" },
1018
+ { name: "destination", type: "string" },
1019
+ { name: "amount", type: "string" },
1020
+ { name: "time", type: "uint64" },
1021
+ ],
1022
+ }, parseInt(action.signatureChainId, 16));
1023
+ const request = {
1024
+ action,
1025
+ signature,
1026
+ nonce: action.time,
1027
+ };
1028
+ const response = await this.transport.request("action", request, signal);
1029
+ this._validateResponse(response);
1030
+ return response;
1031
+ }
1032
+ /**
1033
+ * Add or remove funds from a vault.
1034
+ * @param args - The parameters for the request.
1035
+ * @param signal - An optional abort signal.
1036
+ * @returns Successful response without specific data.
1037
+ * @throws {ApiRequestError} When the API returns an error response.
1038
+ *
1039
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-or-withdraw-from-a-vault
1040
+ * @example
1041
+ * ```ts
1042
+ * import * as hl from "@nktkas/hyperliquid";
1043
+ * import { privateKeyToAccount } from "viem/accounts";
1044
+ *
1045
+ * const wallet = privateKeyToAccount("0x...");
1046
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
1047
+ * const client = new hl.WalletClient({ wallet, transport });
1048
+ *
1049
+ * const result = await client.vaultTransfer({
1050
+ * vaultAddress: "0x...",
1051
+ * isDeposit: true,
1052
+ * usd: 1000000, // 1 USD in raw units (float amount * 1e6)
1053
+ * });
1054
+ * ```
1055
+ */
1056
+ async vaultTransfer(args, signal) {
1057
+ const { nonce = Date.now(), ...actionArgs } = args;
1058
+ const sortedAction = key_sort_js_1.sorters.vaultTransfer({ type: "vaultTransfer", ...actionArgs });
1059
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce);
1060
+ const request = {
1061
+ action: sortedAction,
1062
+ signature,
1063
+ nonce,
1064
+ };
1065
+ const response = await this.transport.request("action", request, signal);
1066
+ this._validateResponse(response);
1067
+ return response;
1068
+ }
1069
+ /**
1070
+ * Initiate a withdrawal request.
1071
+ * @param args - The parameters for the request.
1072
+ * @param signal - An optional abort signal.
1073
+ * @returns Successful response without specific data.
1074
+ * @throws {ApiRequestError} When the API returns an error response.
1075
+ *
1076
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#initiate-a-withdrawal-request
1077
+ * @example
1078
+ * ```ts
1079
+ * import * as hl from "@nktkas/hyperliquid";
1080
+ * import { privateKeyToAccount } from "viem/accounts";
1081
+ *
1082
+ * const wallet = privateKeyToAccount("0x...");
1083
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
1084
+ * const client = new hl.WalletClient({ wallet, transport });
1085
+ *
1086
+ * const result = await client.withdraw3({
1087
+ * destination: "0x...",
1088
+ * amount: "1000",
1089
+ * });
1090
+ * ```
1091
+ */
1092
+ async withdraw3(args, signal) {
1093
+ const action = {
1094
+ ...args,
1095
+ type: "withdraw3",
1096
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
1097
+ signatureChainId: args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1"),
1098
+ time: args.time ?? Date.now(),
1099
+ };
1100
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
1101
+ "HyperliquidTransaction:Withdraw": [
1102
+ { name: "hyperliquidChain", type: "string" },
1103
+ { name: "destination", type: "string" },
1104
+ { name: "amount", type: "string" },
1105
+ { name: "time", type: "uint64" },
1106
+ ],
1107
+ }, parseInt(action.signatureChainId, 16));
1108
+ const request = {
1109
+ action,
1110
+ signature,
1111
+ nonce: action.time,
1112
+ };
1113
+ const response = await this.transport.request("action", request, signal);
1114
+ this._validateResponse(response);
1115
+ return response;
1116
+ }
1117
+ /** Validate a response from the API. */
1118
+ _validateResponse(response) {
1119
+ if (response.status === "err") {
984
1120
  throw new ApiRequestError(response);
985
1121
  }
1122
+ else if (response.response.type === "order" || response.response.type === "cancel") {
1123
+ if (response.response.data.statuses.some((status) => typeof status === "object" && "error" in status)) {
1124
+ throw new ApiRequestError(response);
1125
+ }
1126
+ }
1127
+ else if (response.response.type === "twapOrder" || response.response.type === "twapCancel") {
1128
+ if (typeof response.response.data.status === "object" && "error" in response.response.data.status) {
1129
+ throw new ApiRequestError(response);
1130
+ }
1131
+ }
986
1132
  }
987
1133
  }
988
- }
989
- exports.WalletClient = WalletClient;
1134
+ exports.WalletClient = WalletClient;
1135
+ });