@nktkas/hyperliquid 0.21.1 → 0.22.1

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