@nktkas/hyperliquid 0.23.1 → 0.24.1

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