@nktkas/hyperliquid 0.23.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/CONTRIBUTING.md +3 -2
  2. package/README.md +184 -186
  3. package/esm/mod.d.ts +2 -2
  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 +430 -393
  8. package/esm/src/clients/exchange.d.ts.map +1 -1
  9. package/esm/src/clients/exchange.js +467 -927
  10. package/esm/src/clients/info.d.ts +330 -185
  11. package/esm/src/clients/info.d.ts.map +1 -1
  12. package/esm/src/clients/info.js +348 -334
  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 +95 -93
  17. package/esm/src/clients/subscription.d.ts.map +1 -1
  18. package/esm/src/clients/subscription.js +101 -146
  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 +12 -21
  32. package/esm/src/signing/_sorter.d.ts.map +1 -1
  33. package/esm/src/signing/_sorter.js +30 -63
  34. package/esm/src/signing/mod.d.ts +177 -127
  35. package/esm/src/signing/mod.d.ts.map +1 -1
  36. package/esm/src/signing/mod.js +182 -130
  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 +492 -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 +403 -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 +35 -8
  66. package/esm/src/types/info/markets.d.ts.map +1 -1
  67. package/esm/src/types/info/orders.d.ts +62 -17
  68. package/esm/src/types/info/orders.d.ts.map +1 -1
  69. package/esm/src/types/info/requests.d.ts +194 -76
  70. package/esm/src/types/info/requests.d.ts.map +1 -1
  71. package/{script/src/types/info/delegations.d.ts → esm/src/types/info/validators.d.ts} +45 -15
  72. package/esm/src/types/info/validators.d.ts.map +1 -0
  73. package/esm/src/types/info/vaults.d.ts +32 -10
  74. package/esm/src/types/info/vaults.d.ts.map +1 -1
  75. package/esm/src/types/mod.d.ts +2 -2
  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 -2
  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 +430 -393
  87. package/script/src/clients/exchange.d.ts.map +1 -1
  88. package/script/src/clients/exchange.js +466 -926
  89. package/script/src/clients/info.d.ts +330 -185
  90. package/script/src/clients/info.d.ts.map +1 -1
  91. package/script/src/clients/info.js +348 -334
  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 +95 -93
  96. package/script/src/clients/subscription.d.ts.map +1 -1
  97. package/script/src/clients/subscription.js +101 -146
  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 +12 -21
  111. package/script/src/signing/_sorter.d.ts.map +1 -1
  112. package/script/src/signing/_sorter.js +30 -63
  113. package/script/src/signing/mod.d.ts +177 -127
  114. package/script/src/signing/mod.d.ts.map +1 -1
  115. package/script/src/signing/mod.js +185 -148
  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 +492 -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 +403 -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 +35 -8
  145. package/script/src/types/info/markets.d.ts.map +1 -1
  146. package/script/src/types/info/orders.d.ts +62 -17
  147. package/script/src/types/info/orders.d.ts.map +1 -1
  148. package/script/src/types/info/requests.d.ts +194 -76
  149. package/script/src/types/info/requests.d.ts.map +1 -1
  150. package/{esm/src/types/info/delegations.d.ts → script/src/types/info/validators.d.ts} +45 -15
  151. package/script/src/types/info/validators.d.ts.map +1 -0
  152. package/script/src/types/info/vaults.d.ts +32 -10
  153. package/script/src/types/info/vaults.d.ts.map +1 -1
  154. package/script/src/types/mod.d.ts +2 -2
  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/esm/src/types/info/delegations.d.ts.map +0 -1
  164. package/script/src/signing/_signTypedData/window.d.ts +0 -29
  165. package/script/src/signing/_signTypedData/window.d.ts.map +0 -1
  166. package/script/src/signing/_signTypedData/window.js +0 -34
  167. package/script/src/types/info/delegations.d.ts.map +0 -1
  168. /package/esm/src/types/info/{delegations.js → validators.js} +0 -0
  169. /package/script/src/types/info/{delegations.js → validators.js} +0 -0
@@ -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) {
@@ -118,15 +118,6 @@ class ExchangeClient {
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.
@@ -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,19 @@ 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);
955
774
  }
956
775
  /**
957
776
  * Deploying HIP-3 assets.
958
- * @param args - The parameters for the request.
959
- * @param signal - An optional abort signal.
777
+ * @param params - Action-specific parameters.
778
+ * @param opts - Request execution options.
960
779
  * @returns Successful response without specific data.
961
780
  *
962
781
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -967,7 +786,7 @@ class ExchangeClient {
967
786
  * ```ts
968
787
  * import * as hl from "@nktkas/hyperliquid";
969
788
  *
970
- * const privateKey = "0x..."; // or `viem`, `ethers`
789
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
971
790
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
972
791
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
973
792
  *
@@ -982,120 +801,93 @@ class ExchangeClient {
982
801
  * onlyIsolated: false,
983
802
  * },
984
803
  * dex: "test",
804
+ * schema: null,
985
805
  * },
986
806
  * });
987
807
  * ```
988
808
  */
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,
809
+ async perpDeploy(params, opts) {
810
+ const action = mod_js_1.actionSorter.perpDeploy({
811
+ type: "perpDeploy",
812
+ ...params,
1001
813
  });
1002
- // Send a request
1003
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1004
- this._validateResponse(response);
1005
- return response;
814
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
815
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
1006
816
  }
1007
817
  /**
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.
818
+ * Create a referral code.
819
+ * @param params - Action-specific parameters.
820
+ * @param opts - Request execution options.
1011
821
  * @returns Successful response without specific data.
1012
822
  *
1013
823
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
1014
824
  * @throws {TransportError} When the transport layer throws an error.
1015
825
  *
1016
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
826
+ * @see null
1017
827
  * @example
1018
828
  * ```ts
1019
829
  * import * as hl from "@nktkas/hyperliquid";
1020
830
  *
1021
- * const privateKey = "0x..."; // or `viem`, `ethers`
831
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1022
832
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1023
833
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1024
834
  *
1025
- * await exchClient.perpDexClassTransfer({ dex: "test", token: "USDC", amount: "1", toPerp: true });
835
+ * await exchClient.registerReferrer({ code: "..." });
1026
836
  * ```
1027
837
  */
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],
838
+ async registerReferrer(params, opts) {
839
+ const action = mod_js_1.actionSorter.registerReferrer({
840
+ type: "registerReferrer",
841
+ ...params,
1045
842
  });
1046
- // Send a request
1047
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1048
- this._validateResponse(response);
1049
- return response;
843
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
844
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
1050
845
  }
1051
846
  /**
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.
847
+ * Reserve additional rate-limited actions for a fee.
848
+ * @param params - Action-specific parameters.
849
+ * @param opts - Request execution options.
1055
850
  * @returns Successful response without specific data.
1056
851
  *
1057
852
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
1058
853
  * @throws {TransportError} When the transport layer throws an error.
1059
854
  *
1060
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-perp-account-to-perp-account-for-builder-deployed-dex
855
+ * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#reserve-additional-actions
1061
856
  * @example
1062
857
  * ```ts
1063
858
  * import * as hl from "@nktkas/hyperliquid";
1064
859
  *
1065
- * const privateKey = "0x..."; // or `viem`, `ethers`
860
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1066
861
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1067
862
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1068
863
  *
1069
- * await exchClient.perpDexTransfer({ sourceDex: "", destinationDex: "test", amount: "1" });
864
+ * await exchClient.reserveRequestWeight({ weight: 10 });
1070
865
  * ```
1071
866
  */
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,
867
+ async reserveRequestWeight(params, opts) {
868
+ const action = mod_js_1.actionSorter.reserveRequestWeight({
869
+ type: "reserveRequestWeight",
870
+ ...params,
1083
871
  });
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],
872
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
873
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
874
+ }
875
+ async scheduleCancel(params_or_opts, maybeOpts) {
876
+ const isFirstArgParams = params_or_opts && "time" in params_or_opts;
877
+ const params = isFirstArgParams ? params_or_opts : {};
878
+ const opts = isFirstArgParams ? maybeOpts : params_or_opts;
879
+ const action = mod_js_1.actionSorter.scheduleCancel({
880
+ type: "scheduleCancel",
881
+ ...params,
1089
882
  });
1090
- // Send a request
1091
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1092
- this._validateResponse(response);
1093
- return response;
883
+ const vaultAddress = opts?.vaultAddress ?? this.defaultVaultAddress;
884
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
885
+ return await this._executeL1Action({ action, vaultAddress, expiresAfter }, opts?.signal);
1094
886
  }
1095
887
  /**
1096
- * Create a referral code.
1097
- * @param args - The parameters for the request.
1098
- * @param signal - An optional abort signal.
888
+ * Set the display name in the leaderboard.
889
+ * @param params - Action-specific parameters.
890
+ * @param opts - Request execution options.
1099
891
  * @returns Successful response without specific data.
1100
892
  *
1101
893
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1106,136 +898,25 @@ class ExchangeClient {
1106
898
  * ```ts
1107
899
  * import * as hl from "@nktkas/hyperliquid";
1108
900
  *
1109
- * const privateKey = "0x..."; // or `viem`, `ethers`
901
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1110
902
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1111
903
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1112
904
  *
1113
- * await exchClient.registerReferrer({ code: "..." });
905
+ * await exchClient.setDisplayName({ displayName: "..." });
1114
906
  * ```
1115
907
  */
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,
908
+ async setDisplayName(params, opts) {
909
+ const action = mod_js_1.actionSorter.setDisplayName({
910
+ type: "setDisplayName",
911
+ ...params,
1128
912
  });
1129
- // Send a request
1130
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1131
- this._validateResponse(response);
1132
- return response;
1133
- }
1134
- /**
1135
- * Reserve additional rate-limited actions for a fee.
1136
- * @param args - The parameters for the request.
1137
- * @param signal - An optional abort signal.
1138
- * @returns Successful response without specific data.
1139
- *
1140
- * @throws {ApiRequestError} When the API returns an unsuccessful response.
1141
- * @throws {TransportError} When the transport layer throws an error.
1142
- *
1143
- * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#reserve-additional-actions
1144
- * @example
1145
- * ```ts
1146
- * import * as hl from "@nktkas/hyperliquid";
1147
- *
1148
- * const privateKey = "0x..."; // or `viem`, `ethers`
1149
- * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1150
- * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1151
- *
1152
- * await exchClient.reserveRequestWeight({ weight: 10 });
1153
- * ```
1154
- */
1155
- async reserveRequestWeight(args, signal) {
1156
- // Destructure the parameters
1157
- const { expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1158
- // Construct an action
1159
- const nonce = await this.nonceManager();
1160
- const action = mod_js_1.actionSorter.reserveRequestWeight({ type: "reserveRequestWeight", ...actionArgs });
1161
- // Sign the action
1162
- const signature = await (0, mod_js_1.signL1Action)({
1163
- wallet: this.wallet,
1164
- action,
1165
- nonce,
1166
- isTestnet: this.isTestnet,
1167
- expiresAfter,
1168
- });
1169
- // Send a request
1170
- const response = await this.transport.request("exchange", { action, signature, nonce, expiresAfter }, signal);
1171
- this._validateResponse(response);
1172
- return response;
1173
- }
1174
- async scheduleCancel(args_or_signal, maybeSignal) {
1175
- const args = args_or_signal instanceof AbortSignal ? {} : args_or_signal ?? {};
1176
- const signal = args_or_signal instanceof AbortSignal ? args_or_signal : maybeSignal;
1177
- // Destructure the parameters
1178
- const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
1179
- // Construct an action
1180
- const nonce = await this.nonceManager();
1181
- const action = mod_js_1.actionSorter.scheduleCancel({ type: "scheduleCancel", ...actionArgs });
1182
- // Sign the action
1183
- const signature = await (0, mod_js_1.signL1Action)({
1184
- wallet: this.wallet,
1185
- action,
1186
- nonce,
1187
- isTestnet: this.isTestnet,
1188
- vaultAddress,
1189
- expiresAfter,
1190
- });
1191
- // Send a request
1192
- const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
1193
- this._validateResponse(response);
1194
- return response;
1195
- }
1196
- /**
1197
- * Set the display name in the leaderboard.
1198
- * @param args - The parameters for the request.
1199
- * @param signal - An optional abort signal.
1200
- * @returns Successful response without specific data.
1201
- *
1202
- * @throws {ApiRequestError} When the API returns an unsuccessful response.
1203
- * @throws {TransportError} When the transport layer throws an error.
1204
- *
1205
- * @see null
1206
- * @example
1207
- * ```ts
1208
- * import * as hl from "@nktkas/hyperliquid";
1209
- *
1210
- * const privateKey = "0x..."; // or `viem`, `ethers`
1211
- * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1212
- * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1213
- *
1214
- * await exchClient.setDisplayName({ displayName: "..." });
1215
- * ```
1216
- */
1217
- async setDisplayName(args, signal) {
1218
- // Destructure the parameters
1219
- const { ...actionArgs } = args;
1220
- // Construct an action
1221
- const nonce = await this.nonceManager();
1222
- const action = mod_js_1.actionSorter.setDisplayName({ type: "setDisplayName", ...actionArgs });
1223
- // Sign the action
1224
- const signature = await (0, mod_js_1.signL1Action)({
1225
- wallet: this.wallet,
1226
- action,
1227
- nonce,
1228
- isTestnet: this.isTestnet,
1229
- });
1230
- // Send a request
1231
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1232
- this._validateResponse(response);
1233
- return response;
913
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
914
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
1234
915
  }
1235
916
  /**
1236
917
  * Set a referral code.
1237
- * @param args - The parameters for the request.
1238
- * @param signal - An optional abort signal.
918
+ * @param params - Action-specific parameters.
919
+ * @param opts - Request execution options.
1239
920
  * @returns Successful response without specific data.
1240
921
  *
1241
922
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1246,35 +927,25 @@ class ExchangeClient {
1246
927
  * ```ts
1247
928
  * import * as hl from "@nktkas/hyperliquid";
1248
929
  *
1249
- * const privateKey = "0x..."; // or `viem`, `ethers`
930
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1250
931
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1251
932
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1252
933
  *
1253
934
  * await exchClient.setReferrer({ code: "..." });
1254
935
  * ```
1255
936
  */
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,
937
+ async setReferrer(params, opts) {
938
+ const action = mod_js_1.actionSorter.setReferrer({
939
+ type: "setReferrer",
940
+ ...params,
1268
941
  });
1269
- // Send a request
1270
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1271
- this._validateResponse(response);
1272
- return response;
942
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
943
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
1273
944
  }
1274
945
  /**
1275
946
  * Deploying HIP-1 and HIP-2 assets.
1276
- * @param args - The parameters for the request.
1277
- * @param signal - An optional abort signal.
947
+ * @param params - Action-specific parameters.
948
+ * @param opts - Request execution options.
1278
949
  * @returns Successful response without specific data.
1279
950
  *
1280
951
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1285,7 +956,7 @@ class ExchangeClient {
1285
956
  * ```ts
1286
957
  * import * as hl from "@nktkas/hyperliquid";
1287
958
  *
1288
- * const privateKey = "0x..."; // or `viem`, `ethers`
959
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1289
960
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1290
961
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1291
962
  *
@@ -1302,28 +973,18 @@ class ExchangeClient {
1302
973
  * });
1303
974
  * ```
1304
975
  */
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,
976
+ async spotDeploy(params, opts) {
977
+ const action = mod_js_1.actionSorter.spotDeploy({
978
+ type: "spotDeploy",
979
+ ...params,
1317
980
  });
1318
- // Send a request
1319
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1320
- this._validateResponse(response);
1321
- return response;
981
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
982
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
1322
983
  }
1323
984
  /**
1324
985
  * Send spot assets to another address.
1325
- * @param args - The parameters for the request.
1326
- * @param signal - An optional abort signal.
986
+ * @param params - Action-specific parameters.
987
+ * @param opts - Request execution options.
1327
988
  * @returns Successful response without specific data.
1328
989
  *
1329
990
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1334,7 +995,7 @@ class ExchangeClient {
1334
995
  * ```ts
1335
996
  * import * as hl from "@nktkas/hyperliquid";
1336
997
  *
1337
- * const privateKey = "0x..."; // or `viem`, `ethers`
998
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1338
999
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1339
1000
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1340
1001
  *
@@ -1345,33 +1006,20 @@ class ExchangeClient {
1345
1006
  * });
1346
1007
  * ```
1347
1008
  */
1348
- async spotSend(args, signal) {
1349
- // Destructure the parameters
1350
- const { ...actionArgs } = args;
1351
- // Construct an action
1352
- const nonce = await this.nonceManager();
1009
+ async spotSend(params, opts) {
1353
1010
  const action = mod_js_1.actionSorter.spotSend({
1354
1011
  type: "spotSend",
1355
1012
  hyperliquidChain: this._getHyperliquidChain(),
1356
1013
  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],
1014
+ time: await this.nonceManager(),
1015
+ ...params,
1365
1016
  });
1366
- // Send a request
1367
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1368
- this._validateResponse(response);
1369
- return response;
1017
+ return await this._executeUserSignedAction({ action }, opts?.signal);
1370
1018
  }
1371
1019
  /**
1372
1020
  * Opt Out of Spot Dusting.
1373
- * @param args - The parameters for the request.
1374
- * @param signal - An optional abort signal.
1021
+ * @param params - Action-specific parameters.
1022
+ * @param opts - Request execution options.
1375
1023
  * @returns Successful response without specific data.
1376
1024
  *
1377
1025
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1382,35 +1030,54 @@ class ExchangeClient {
1382
1030
  * ```ts
1383
1031
  * import * as hl from "@nktkas/hyperliquid";
1384
1032
  *
1385
- * const privateKey = "0x..."; // or `viem`, `ethers`
1033
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1386
1034
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1387
1035
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1388
1036
  *
1389
1037
  * await exchClient.spotUser({ toggleSpotDusting: { optOut: false } });
1390
1038
  * ```
1391
1039
  */
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,
1040
+ async spotUser(params, opts) {
1041
+ const action = mod_js_1.actionSorter.spotUser({
1042
+ type: "spotUser",
1043
+ ...params,
1404
1044
  });
1405
- // Send a request
1406
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1407
- this._validateResponse(response);
1408
- return response;
1045
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
1046
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
1047
+ }
1048
+ /**
1049
+ * Modify a sub-account's.
1050
+ * @param params - Action-specific parameters.
1051
+ * @param opts - Request execution options.
1052
+ * @returns Successful response without specific data.
1053
+ *
1054
+ * @throws {ApiRequestError} When the API returns an unsuccessful response.
1055
+ * @throws {TransportError} When the transport layer throws an error.
1056
+ *
1057
+ * @see null
1058
+ * @example
1059
+ * ```ts
1060
+ * import * as hl from "@nktkas/hyperliquid";
1061
+ *
1062
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1063
+ * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1064
+ * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1065
+ *
1066
+ * await exchClient.subAccountModify({ subAccountUser: "0x...", name: "..." });
1067
+ * ```
1068
+ */
1069
+ async subAccountModify(params, opts) {
1070
+ const action = mod_js_1.actionSorter.subAccountModify({
1071
+ type: "subAccountModify",
1072
+ ...params,
1073
+ });
1074
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
1075
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
1409
1076
  }
1410
1077
  /**
1411
1078
  * Transfer between sub-accounts (spot).
1412
- * @param args - The parameters for the request.
1413
- * @param signal - An optional abort signal.
1079
+ * @param params - Action-specific parameters.
1080
+ * @param opts - Request execution options.
1414
1081
  * @returns Successful response without specific data.
1415
1082
  *
1416
1083
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1421,7 +1088,7 @@ class ExchangeClient {
1421
1088
  * ```ts
1422
1089
  * import * as hl from "@nktkas/hyperliquid";
1423
1090
  *
1424
- * const privateKey = "0x..."; // or `viem`, `ethers`
1091
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1425
1092
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1426
1093
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1427
1094
  *
@@ -1433,28 +1100,18 @@ class ExchangeClient {
1433
1100
  * });
1434
1101
  * ```
1435
1102
  */
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,
1103
+ async subAccountSpotTransfer(params, opts) {
1104
+ const action = mod_js_1.actionSorter.subAccountSpotTransfer({
1105
+ type: "subAccountSpotTransfer",
1106
+ ...params,
1448
1107
  });
1449
- // Send a request
1450
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1451
- this._validateResponse(response);
1452
- return response;
1108
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
1109
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
1453
1110
  }
1454
1111
  /**
1455
1112
  * Transfer between sub-accounts (perpetual).
1456
- * @param args - The parameters for the request.
1457
- * @param signal - An optional abort signal.
1113
+ * @param params - Action-specific parameters.
1114
+ * @param opts - Request execution options.
1458
1115
  * @returns Successful response without specific data.
1459
1116
  *
1460
1117
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1465,35 +1122,25 @@ class ExchangeClient {
1465
1122
  * ```ts
1466
1123
  * import * as hl from "@nktkas/hyperliquid";
1467
1124
  *
1468
- * const privateKey = "0x..."; // or `viem`, `ethers`
1125
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1469
1126
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1470
1127
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1471
1128
  *
1472
1129
  * await exchClient.subAccountTransfer({ subAccountUser: "0x...", isDeposit: true, usd: 1 * 1e6 });
1473
1130
  * ```
1474
1131
  */
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,
1132
+ async subAccountTransfer(params, opts) {
1133
+ const action = mod_js_1.actionSorter.subAccountTransfer({
1134
+ type: "subAccountTransfer",
1135
+ ...params,
1487
1136
  });
1488
- // Send a request
1489
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1490
- this._validateResponse(response);
1491
- return response;
1137
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
1138
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
1492
1139
  }
1493
1140
  /**
1494
1141
  * 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.
1142
+ * @param params - Action-specific parameters.
1143
+ * @param opts - Request execution options.
1497
1144
  * @returns Successful response without specific data.
1498
1145
  *
1499
1146
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1504,40 +1151,27 @@ class ExchangeClient {
1504
1151
  * ```ts
1505
1152
  * import * as hl from "@nktkas/hyperliquid";
1506
1153
  *
1507
- * const privateKey = "0x..."; // or `viem`, `ethers`
1154
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1508
1155
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1509
1156
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1510
1157
  *
1511
1158
  * await exchClient.tokenDelegate({ validator: "0x...", isUndelegate: true, wei: 1 * 1e8 });
1512
1159
  * ```
1513
1160
  */
1514
- async tokenDelegate(args, signal) {
1515
- // Destructure the parameters
1516
- const { ...actionArgs } = args;
1517
- // Construct an action
1518
- const nonce = await this.nonceManager();
1161
+ async tokenDelegate(params, opts) {
1519
1162
  const action = mod_js_1.actionSorter.tokenDelegate({
1520
1163
  type: "tokenDelegate",
1521
1164
  hyperliquidChain: this._getHyperliquidChain(),
1522
1165
  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],
1166
+ nonce: await this.nonceManager(),
1167
+ ...params,
1531
1168
  });
1532
- // Send a request
1533
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1534
- this._validateResponse(response);
1535
- return response;
1169
+ return await this._executeUserSignedAction({ action }, opts?.signal);
1536
1170
  }
1537
1171
  /**
1538
1172
  * Cancel a TWAP order.
1539
- * @param args - The parameters for the request.
1540
- * @param signal - An optional abort signal.
1173
+ * @param params - Action-specific parameters.
1174
+ * @param opts - Request execution options.
1541
1175
  * @returns Successful variant of {@link TwapCancelResponse} without error status.
1542
1176
  *
1543
1177
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1548,37 +1182,26 @@ class ExchangeClient {
1548
1182
  * ```ts
1549
1183
  * import * as hl from "@nktkas/hyperliquid";
1550
1184
  *
1551
- * const privateKey = "0x..."; // or `viem`, `ethers`
1185
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1552
1186
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1553
1187
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1554
1188
  *
1555
1189
  * const data = await exchClient.twapCancel({ a: 0, t: 1 });
1556
1190
  * ```
1557
1191
  */
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,
1192
+ async twapCancel(params, opts) {
1193
+ const action = mod_js_1.actionSorter.twapCancel({
1194
+ type: "twapCancel",
1195
+ ...params,
1572
1196
  });
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;
1197
+ const vaultAddress = opts?.vaultAddress ?? this.defaultVaultAddress;
1198
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
1199
+ return await this._executeL1Action({ action, vaultAddress, expiresAfter }, opts?.signal);
1577
1200
  }
1578
1201
  /**
1579
1202
  * Place a TWAP order.
1580
- * @param args - The parameters for the request.
1581
- * @param signal - An optional abort signal.
1203
+ * @param params - Action-specific parameters.
1204
+ * @param opts - Request execution options.
1582
1205
  * @returns Successful variant of {@link TwapOrderResponse} without error status.
1583
1206
  *
1584
1207
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1589,44 +1212,35 @@ class ExchangeClient {
1589
1212
  * ```ts
1590
1213
  * import * as hl from "@nktkas/hyperliquid";
1591
1214
  *
1592
- * const privateKey = "0x..."; // or `viem`, `ethers`
1215
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1593
1216
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1594
1217
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1595
1218
  *
1596
1219
  * const data = await exchClient.twapOrder({
1597
- * a: 0,
1598
- * b: true,
1599
- * s: "1",
1600
- * r: false,
1601
- * m: 10,
1602
- * t: true,
1220
+ * twap: {
1221
+ * a: 0,
1222
+ * b: true,
1223
+ * s: "1",
1224
+ * r: false,
1225
+ * m: 10,
1226
+ * t: true,
1227
+ * },
1603
1228
  * });
1604
1229
  * ```
1605
1230
  */
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,
1231
+ async twapOrder(params, opts) {
1232
+ const action = mod_js_1.actionSorter.twapOrder({
1233
+ type: "twapOrder",
1234
+ ...params,
1620
1235
  });
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;
1236
+ const vaultAddress = opts?.vaultAddress ?? this.defaultVaultAddress;
1237
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
1238
+ return await this._executeL1Action({ action, vaultAddress, expiresAfter }, opts?.signal);
1625
1239
  }
1626
1240
  /**
1627
1241
  * Add or remove margin from isolated position.
1628
- * @param args - The parameters for the request.
1629
- * @param signal - An optional abort signal.
1242
+ * @param params - Action-specific parameters.
1243
+ * @param opts - Request execution options.
1630
1244
  * @returns Successful response without specific data.
1631
1245
  *
1632
1246
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1637,37 +1251,26 @@ class ExchangeClient {
1637
1251
  * ```ts
1638
1252
  * import * as hl from "@nktkas/hyperliquid";
1639
1253
  *
1640
- * const privateKey = "0x..."; // or `viem`, `ethers`
1254
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1641
1255
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1642
1256
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1643
1257
  *
1644
1258
  * await exchClient.updateIsolatedMargin({ asset: 0, isBuy: true, ntli: 1 * 1e6 });
1645
1259
  * ```
1646
1260
  */
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,
1261
+ async updateIsolatedMargin(params, opts) {
1262
+ const action = mod_js_1.actionSorter.updateIsolatedMargin({
1263
+ type: "updateIsolatedMargin",
1264
+ ...params,
1661
1265
  });
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;
1266
+ const vaultAddress = opts?.vaultAddress ?? this.defaultVaultAddress;
1267
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
1268
+ return await this._executeL1Action({ action, vaultAddress, expiresAfter }, opts?.signal);
1666
1269
  }
1667
1270
  /**
1668
1271
  * Update cross or isolated leverage on a coin.
1669
- * @param args - The parameters for the request.
1670
- * @param signal - An optional abort signal.
1272
+ * @param params - Action-specific parameters.
1273
+ * @param opts - Request execution options.
1671
1274
  * @returns Successful response without specific data.
1672
1275
  *
1673
1276
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1678,37 +1281,26 @@ class ExchangeClient {
1678
1281
  * ```ts
1679
1282
  * import * as hl from "@nktkas/hyperliquid";
1680
1283
  *
1681
- * const privateKey = "0x..."; // or `viem`, `ethers`
1284
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1682
1285
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1683
1286
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1684
1287
  *
1685
1288
  * await exchClient.updateLeverage({ asset: 0, isCross: true, leverage: 5 });
1686
1289
  * ```
1687
1290
  */
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,
1291
+ async updateLeverage(params, opts) {
1292
+ const action = mod_js_1.actionSorter.updateLeverage({
1293
+ type: "updateLeverage",
1294
+ ...params,
1702
1295
  });
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;
1296
+ const vaultAddress = opts?.vaultAddress ?? this.defaultVaultAddress;
1297
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
1298
+ return await this._executeL1Action({ action, vaultAddress, expiresAfter }, opts?.signal);
1707
1299
  }
1708
1300
  /**
1709
1301
  * Transfer funds between Spot account and Perp account.
1710
- * @param args - The parameters for the request.
1711
- * @param signal - An optional abort signal.
1302
+ * @param params - Action-specific parameters.
1303
+ * @param opts - Request execution options.
1712
1304
  * @returns Successful response without specific data.
1713
1305
  *
1714
1306
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1719,40 +1311,27 @@ class ExchangeClient {
1719
1311
  * ```ts
1720
1312
  * import * as hl from "@nktkas/hyperliquid";
1721
1313
  *
1722
- * const privateKey = "0x..."; // or `viem`, `ethers`
1314
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1723
1315
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1724
1316
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1725
1317
  *
1726
1318
  * await exchClient.usdClassTransfer({ amount: "1", toPerp: true });
1727
1319
  * ```
1728
1320
  */
1729
- async usdClassTransfer(args, signal) {
1730
- // Destructure the parameters
1731
- const { ...actionArgs } = args;
1732
- // Construct an action
1733
- const nonce = await this.nonceManager();
1321
+ async usdClassTransfer(params, opts) {
1734
1322
  const action = mod_js_1.actionSorter.usdClassTransfer({
1735
1323
  type: "usdClassTransfer",
1736
1324
  hyperliquidChain: this._getHyperliquidChain(),
1737
1325
  signatureChainId: await this._getSignatureChainId(),
1738
- nonce,
1739
- ...actionArgs,
1326
+ nonce: await this.nonceManager(),
1327
+ ...params,
1740
1328
  });
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;
1329
+ return await this._executeUserSignedAction({ action }, opts?.signal);
1751
1330
  }
1752
1331
  /**
1753
1332
  * Send usd to another address.
1754
- * @param args - The parameters for the request.
1755
- * @param signal - An optional abort signal.
1333
+ * @param params - Action-specific parameters.
1334
+ * @param opts - Request execution options.
1756
1335
  * @returns Successful response without specific data.
1757
1336
  *
1758
1337
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1763,40 +1342,27 @@ class ExchangeClient {
1763
1342
  * ```ts
1764
1343
  * import * as hl from "@nktkas/hyperliquid";
1765
1344
  *
1766
- * const privateKey = "0x..."; // or `viem`, `ethers`
1345
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1767
1346
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1768
1347
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1769
1348
  *
1770
1349
  * await exchClient.usdSend({ destination: "0x...", amount: "1" });
1771
1350
  * ```
1772
1351
  */
1773
- async usdSend(args, signal) {
1774
- // Destructure the parameters
1775
- const { ...actionArgs } = args;
1776
- // Construct an action
1777
- const nonce = await this.nonceManager();
1352
+ async usdSend(params, opts) {
1778
1353
  const action = mod_js_1.actionSorter.usdSend({
1779
1354
  type: "usdSend",
1780
1355
  hyperliquidChain: this._getHyperliquidChain(),
1781
1356
  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],
1357
+ time: await this.nonceManager(),
1358
+ ...params,
1790
1359
  });
1791
- // Send a request
1792
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1793
- this._validateResponse(response);
1794
- return response;
1360
+ return await this._executeUserSignedAction({ action }, opts?.signal);
1795
1361
  }
1796
1362
  /**
1797
1363
  * Distribute funds from a vault between followers.
1798
- * @param args - The parameters for the request.
1799
- * @param signal - An optional abort signal.
1364
+ * @param params - Action-specific parameters.
1365
+ * @param opts - Request execution options.
1800
1366
  * @returns Successful response without specific data.
1801
1367
  *
1802
1368
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1807,35 +1373,25 @@ class ExchangeClient {
1807
1373
  * ```ts
1808
1374
  * import * as hl from "@nktkas/hyperliquid";
1809
1375
  *
1810
- * const privateKey = "0x..."; // or `viem`, `ethers`
1376
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1811
1377
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1812
1378
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1813
1379
  *
1814
1380
  * await exchClient.vaultDistribute({ vaultAddress: "0x...", usd: 10 * 1e6 });
1815
1381
  * ```
1816
1382
  */
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,
1383
+ async vaultDistribute(params, opts) {
1384
+ const action = mod_js_1.actionSorter.vaultDistribute({
1385
+ type: "vaultDistribute",
1386
+ ...params,
1829
1387
  });
1830
- // Send a request
1831
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1832
- this._validateResponse(response);
1833
- return response;
1388
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
1389
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
1834
1390
  }
1835
1391
  /**
1836
1392
  * Modify a vault's configuration.
1837
- * @param args - The parameters for the request.
1838
- * @param signal - An optional abort signal.
1393
+ * @param params - Action-specific parameters.
1394
+ * @param opts - Request execution options.
1839
1395
  * @returns Successful response without specific data.
1840
1396
  *
1841
1397
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1846,7 +1402,7 @@ class ExchangeClient {
1846
1402
  * ```ts
1847
1403
  * import * as hl from "@nktkas/hyperliquid";
1848
1404
  *
1849
- * const privateKey = "0x..."; // or `viem`, `ethers`
1405
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1850
1406
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1851
1407
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1852
1408
  *
@@ -1857,28 +1413,18 @@ class ExchangeClient {
1857
1413
  * });
1858
1414
  * ```
1859
1415
  */
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,
1416
+ async vaultModify(params, opts) {
1417
+ const action = mod_js_1.actionSorter.vaultModify({
1418
+ type: "vaultModify",
1419
+ ...params,
1872
1420
  });
1873
- // Send a request
1874
- const response = await this.transport.request("exchange", { action, signature, nonce }, signal);
1875
- this._validateResponse(response);
1876
- return response;
1421
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
1422
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
1877
1423
  }
1878
1424
  /**
1879
1425
  * Deposit or withdraw from a vault.
1880
- * @param args - The parameters for the request.
1881
- * @param signal - An optional abort signal.
1426
+ * @param params - Action-specific parameters.
1427
+ * @param opts - Request execution options.
1882
1428
  * @returns Successful response without specific data.
1883
1429
  *
1884
1430
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1889,36 +1435,25 @@ class ExchangeClient {
1889
1435
  * ```ts
1890
1436
  * import * as hl from "@nktkas/hyperliquid";
1891
1437
  *
1892
- * const privateKey = "0x..."; // or `viem`, `ethers`
1438
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1893
1439
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1894
1440
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1895
1441
  *
1896
1442
  * await exchClient.vaultTransfer({ vaultAddress: "0x...", isDeposit: true, usd: 10 * 1e6 });
1897
1443
  * ```
1898
1444
  */
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,
1445
+ async vaultTransfer(params, opts) {
1446
+ const action = mod_js_1.actionSorter.vaultTransfer({
1447
+ type: "vaultTransfer",
1448
+ ...params,
1912
1449
  });
1913
- // Send a request
1914
- const response = await this.transport.request("exchange", { action, signature, nonce, expiresAfter }, signal);
1915
- this._validateResponse(response);
1916
- return response;
1450
+ const expiresAfter = opts?.expiresAfter ?? await this._getDefaultExpiresAfter();
1451
+ return await this._executeL1Action({ action, expiresAfter }, opts?.signal);
1917
1452
  }
1918
1453
  /**
1919
1454
  * Initiate a withdrawal request.
1920
- * @param args - The parameters for the request.
1921
- * @param signal - An optional abort signal.
1455
+ * @param params - Action-specific parameters.
1456
+ * @param opts - Request execution options.
1922
1457
  * @returns Successful response without specific data.
1923
1458
  *
1924
1459
  * @throws {ApiRequestError} When the API returns an unsuccessful response.
@@ -1929,26 +1464,44 @@ class ExchangeClient {
1929
1464
  * ```ts
1930
1465
  * import * as hl from "@nktkas/hyperliquid";
1931
1466
  *
1932
- * const privateKey = "0x..."; // or `viem`, `ethers`
1467
+ * const privateKey = "0x..."; // `viem`, `ethers`, or private key directly`
1933
1468
  * const transport = new hl.HttpTransport(); // or `WebSocketTransport`
1934
1469
  * const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
1935
1470
  *
1936
1471
  * await exchClient.withdraw3({ destination: "0x...", amount: "1" });
1937
1472
  * ```
1938
1473
  */
1939
- async withdraw3(args, signal) {
1940
- // Destructure the parameters
1941
- const { ...actionArgs } = args;
1942
- // Construct an action
1943
- const nonce = await this.nonceManager();
1474
+ async withdraw3(params, opts) {
1944
1475
  const action = mod_js_1.actionSorter.withdraw3({
1945
1476
  type: "withdraw3",
1946
1477
  hyperliquidChain: this._getHyperliquidChain(),
1947
1478
  signatureChainId: await this._getSignatureChainId(),
1948
- time: nonce,
1949
- ...actionArgs,
1479
+ time: await this.nonceManager(),
1480
+ ...params,
1950
1481
  });
1951
- // Sign the action
1482
+ return await this._executeUserSignedAction({ action }, opts?.signal);
1483
+ }
1484
+ async _executeL1Action(request, signal) {
1485
+ const { action, vaultAddress, expiresAfter } = request;
1486
+ // Sign an L1 action
1487
+ const nonce = await this.nonceManager();
1488
+ const signature = await (0, mod_js_1.signL1Action)({
1489
+ wallet: this.wallet,
1490
+ action,
1491
+ nonce,
1492
+ isTestnet: this.isTestnet,
1493
+ vaultAddress,
1494
+ expiresAfter,
1495
+ });
1496
+ // Send a request
1497
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
1498
+ this._validateResponse(response);
1499
+ return response;
1500
+ }
1501
+ async _executeUserSignedAction(request, signal) {
1502
+ const { action } = request;
1503
+ // Sign a user-signed action
1504
+ const nonce = "nonce" in action ? action.nonce : action.time;
1952
1505
  const signature = await (0, mod_js_1.signUserSignedAction)({
1953
1506
  wallet: this.wallet,
1954
1507
  action,
@@ -1959,46 +1512,33 @@ class ExchangeClient {
1959
1512
  this._validateResponse(response);
1960
1513
  return response;
1961
1514
  }
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";
1515
+ async _executeMultiSigAction(request, signal) {
1516
+ const { action, nonce, vaultAddress, expiresAfter } = request;
1517
+ // Sign a multi-signature action
1518
+ const signature = await (0, mod_js_1.signMultiSigAction)({
1519
+ wallet: this.wallet,
1520
+ action,
1521
+ nonce,
1522
+ isTestnet: this.isTestnet,
1523
+ vaultAddress,
1524
+ expiresAfter,
1525
+ });
1526
+ // Send a request
1527
+ const response = await this.transport.request("exchange", { action, signature, nonce, vaultAddress, expiresAfter }, signal);
1528
+ this._validateResponse(response);
1529
+ return response;
1986
1530
  }
1987
- /** Get the default expiration time for an action. */
1988
1531
  async _getDefaultExpiresAfter() {
1989
1532
  return typeof this.defaultExpiresAfter === "number"
1990
1533
  ? this.defaultExpiresAfter
1991
1534
  : await this.defaultExpiresAfter?.();
1992
1535
  }
1993
- /** Get the signature chain ID for the wallet. */
1994
1536
  async _getSignatureChainId() {
1995
1537
  return typeof this.signatureChainId === "string" ? this.signatureChainId : await this.signatureChainId();
1996
1538
  }
1997
- /** Get the Hyperliquid chain based on the isTestnet flag. */
1998
1539
  _getHyperliquidChain() {
1999
1540
  return this.isTestnet ? "Testnet" : "Mainnet";
2000
1541
  }
2001
- /** Validate a response from the API. */
2002
1542
  _validateResponse(response) {
2003
1543
  if (response.status === "err") {
2004
1544
  throw new ApiRequestError(response);