@nktkas/hyperliquid 0.16.0 → 0.17.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.
- package/README.md +58 -106
- package/esm/src/clients/event.d.ts.map +1 -1
- package/esm/src/clients/event.js +0 -1
- package/esm/src/clients/public.d.ts +4 -4
- package/esm/src/clients/public.d.ts.map +1 -1
- package/esm/src/clients/public.js +4 -7
- package/esm/src/clients/wallet.d.ts +129 -132
- package/esm/src/clients/wallet.d.ts.map +1 -1
- package/esm/src/clients/wallet.js +290 -268
- package/esm/src/signing.d.ts +149 -32
- package/esm/src/signing.d.ts.map +1 -1
- package/esm/src/signing.js +123 -15
- package/esm/src/transports/http/http_transport.d.ts +14 -24
- package/esm/src/transports/http/http_transport.d.ts.map +1 -1
- package/esm/src/transports/http/http_transport.js +20 -15
- package/esm/src/transports/websocket/websocket_transport.d.ts +6 -2
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/esm/src/types/exchange/requests.d.ts +52 -38
- package/esm/src/types/exchange/requests.d.ts.map +1 -1
- package/esm/src/types/info/accounts.d.ts +4 -0
- package/esm/src/types/info/accounts.d.ts.map +1 -1
- package/esm/src/types/info/assets.d.ts +4 -6
- package/esm/src/types/info/assets.d.ts.map +1 -1
- package/esm/src/types/info/orders.d.ts +28 -20
- package/esm/src/types/info/orders.d.ts.map +1 -1
- package/esm/src/types/info/requests.d.ts +8 -8
- package/esm/src/types/info/requests.d.ts.map +1 -1
- package/esm/src/types/info/vaults.d.ts +2 -0
- package/esm/src/types/info/vaults.d.ts.map +1 -1
- package/esm/src/types/mod.d.ts +21 -0
- package/esm/src/types/mod.d.ts.map +1 -1
- package/esm/src/types/mod.js +21 -0
- package/esm/src/types/subscriptions/requests.d.ts +5 -14
- package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
- package/package.json +4 -1
- package/script/src/clients/event.d.ts.map +1 -1
- package/script/src/clients/event.js +0 -1
- package/script/src/clients/public.d.ts +4 -4
- package/script/src/clients/public.d.ts.map +1 -1
- package/script/src/clients/public.js +4 -7
- package/script/src/clients/wallet.d.ts +129 -132
- package/script/src/clients/wallet.d.ts.map +1 -1
- package/script/src/clients/wallet.js +290 -268
- package/script/src/signing.d.ts +149 -32
- package/script/src/signing.d.ts.map +1 -1
- package/script/src/signing.js +124 -16
- package/script/src/transports/http/http_transport.d.ts +14 -24
- package/script/src/transports/http/http_transport.d.ts.map +1 -1
- package/script/src/transports/http/http_transport.js +20 -15
- package/script/src/transports/websocket/websocket_transport.d.ts +6 -2
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/script/src/types/exchange/requests.d.ts +52 -38
- package/script/src/types/exchange/requests.d.ts.map +1 -1
- package/script/src/types/info/accounts.d.ts +4 -0
- package/script/src/types/info/accounts.d.ts.map +1 -1
- package/script/src/types/info/assets.d.ts +4 -6
- package/script/src/types/info/assets.d.ts.map +1 -1
- package/script/src/types/info/orders.d.ts +28 -20
- package/script/src/types/info/orders.d.ts.map +1 -1
- package/script/src/types/info/requests.d.ts +8 -8
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/info/vaults.d.ts +2 -0
- package/script/src/types/info/vaults.d.ts.map +1 -1
- package/script/src/types/mod.d.ts +21 -0
- package/script/src/types/mod.d.ts.map +1 -1
- package/script/src/types/mod.js +21 -0
- package/script/src/types/subscriptions/requests.d.ts +5 -14
- package/script/src/types/subscriptions/requests.d.ts.map +1 -1
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { HyperliquidError } from "../base.js";
|
|
2
2
|
import { isAbstractEthersSigner, isAbstractEthersV5Signer, isAbstractViemWalletClient, isAbstractWindowEthereum, signL1Action, signUserSignedAction, } from "../signing.js";
|
|
3
|
-
// ——————————————— Errors ———————————————
|
|
4
3
|
/** Error thrown when the API returns an error response. */
|
|
5
4
|
export class ApiRequestError extends HyperliquidError {
|
|
6
5
|
constructor(response) {
|
|
@@ -39,24 +38,38 @@ export class ApiRequestError extends HyperliquidError {
|
|
|
39
38
|
this.name = "ApiRequestError";
|
|
40
39
|
}
|
|
41
40
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
41
|
+
/** Nonce manager for generating unique nonces for signing transactions. */
|
|
42
|
+
class NonceManager {
|
|
43
|
+
constructor() {
|
|
44
|
+
/** The last nonce used for signing transactions. */
|
|
45
|
+
Object.defineProperty(this, "lastNonce", {
|
|
46
|
+
enumerable: true,
|
|
47
|
+
configurable: true,
|
|
48
|
+
writable: true,
|
|
49
|
+
value: 0
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Gets the next nonce for signing transactions.
|
|
54
|
+
* @returns The next nonce.
|
|
55
|
+
*/
|
|
56
|
+
getNonce() {
|
|
51
57
|
let nonce = Date.now();
|
|
52
|
-
if (nonce <= this.
|
|
53
|
-
nonce = ++this.
|
|
58
|
+
if (nonce <= this.lastNonce) {
|
|
59
|
+
nonce = ++this.lastNonce;
|
|
54
60
|
}
|
|
55
61
|
else {
|
|
56
|
-
this.
|
|
62
|
+
this.lastNonce = nonce;
|
|
57
63
|
}
|
|
58
64
|
return nonce;
|
|
59
65
|
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Wallet client for interacting with the Hyperliquid API.
|
|
69
|
+
* @typeParam T The transport used to connect to the Hyperliquid API.
|
|
70
|
+
* @typeParam W The WalletClient/Account ([viem](https://viem.sh/docs/clients/wallet)) or Signer ([ethers.js](https://docs.ethers.io/v6/api/providers/#Signer)) used for signing transactions.
|
|
71
|
+
*/
|
|
72
|
+
export class WalletClient {
|
|
60
73
|
/**
|
|
61
74
|
* Initialises a new instance.
|
|
62
75
|
* @param args - The parameters for the client.
|
|
@@ -142,43 +155,20 @@ export class WalletClient {
|
|
|
142
155
|
writable: true,
|
|
143
156
|
value: void 0
|
|
144
157
|
});
|
|
145
|
-
/**
|
|
146
|
-
Object.defineProperty(this, "
|
|
158
|
+
/** Function to get the next nonce for signing transactions. */
|
|
159
|
+
Object.defineProperty(this, "nonceManager", {
|
|
147
160
|
enumerable: true,
|
|
148
161
|
configurable: true,
|
|
149
162
|
writable: true,
|
|
150
|
-
value: 0
|
|
163
|
+
value: void 0
|
|
151
164
|
});
|
|
152
165
|
this.transport = args.transport;
|
|
153
166
|
this.wallet = args.wallet;
|
|
154
167
|
this.isTestnet = args.isTestnet ?? false;
|
|
155
168
|
this.defaultVaultAddress = args.defaultVaultAddress;
|
|
156
|
-
this.signatureChainId = args.signatureChainId ??
|
|
157
|
-
|
|
158
|
-
if (isAbstractViemWalletClient(this.wallet)) {
|
|
159
|
-
if ("getChainId" in this.wallet && typeof this.wallet.getChainId === "function") {
|
|
160
|
-
const chainId = await this.wallet.getChainId();
|
|
161
|
-
return `0x${chainId.toString(16)}`;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
else if (isAbstractEthersSigner(this.wallet) || isAbstractEthersV5Signer(this.wallet)) {
|
|
165
|
-
if ("provider" in this.wallet &&
|
|
166
|
-
typeof this.wallet.provider === "object" && this.wallet.provider !== null &&
|
|
167
|
-
"getNetwork" in this.wallet.provider &&
|
|
168
|
-
typeof this.wallet.provider.getNetwork === "function") {
|
|
169
|
-
const network = await this.wallet.provider.getNetwork();
|
|
170
|
-
return `0x${network.chainId.toString(16)}`;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
else if (isAbstractWindowEthereum(this.wallet)) {
|
|
174
|
-
const [chainId] = await this.wallet.request({ method: "eth_chainId", params: [] });
|
|
175
|
-
return chainId;
|
|
176
|
-
}
|
|
177
|
-
// Trying to guess chain id based on isTestnet
|
|
178
|
-
return this.isTestnet ? "0x66eee" : "0xa4b1";
|
|
179
|
-
});
|
|
169
|
+
this.signatureChainId = args.signatureChainId ?? this._guessSignatureChainId;
|
|
170
|
+
this.nonceManager = args.nonceManager ?? new NonceManager().getNonce;
|
|
180
171
|
}
|
|
181
|
-
// ——————————————— Exchange API ———————————————
|
|
182
172
|
/**
|
|
183
173
|
* Approve an agent to sign on behalf of the master or sub-accounts.
|
|
184
174
|
* @param args - The parameters for the request.
|
|
@@ -196,10 +186,7 @@ export class WalletClient {
|
|
|
196
186
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
197
187
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
198
188
|
*
|
|
199
|
-
* const result = await client.approveAgent({
|
|
200
|
-
* agentAddress: "0x...",
|
|
201
|
-
* agentName: "agentName",
|
|
202
|
-
* });
|
|
189
|
+
* const result = await client.approveAgent({ agentAddress: "0x...", agentName: "agentName" });
|
|
203
190
|
* ```
|
|
204
191
|
*/
|
|
205
192
|
async approveAgent(args, signal) {
|
|
@@ -211,7 +198,7 @@ export class WalletClient {
|
|
|
211
198
|
signatureChainId: typeof this.signatureChainId === "string"
|
|
212
199
|
? this.signatureChainId
|
|
213
200
|
: await this.signatureChainId(),
|
|
214
|
-
nonce:
|
|
201
|
+
nonce: await this.nonceManager(),
|
|
215
202
|
};
|
|
216
203
|
// Sign the action
|
|
217
204
|
const signature = await signUserSignedAction({
|
|
@@ -251,10 +238,7 @@ export class WalletClient {
|
|
|
251
238
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
252
239
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
253
240
|
*
|
|
254
|
-
* const result = await client.approveBuilderFee({
|
|
255
|
-
* maxFeeRate: "0.01%",
|
|
256
|
-
* builder: "0x...",
|
|
257
|
-
* });
|
|
241
|
+
* const result = await client.approveBuilderFee({ maxFeeRate: "0.01%", builder: "0x..." });
|
|
258
242
|
* ```
|
|
259
243
|
*/
|
|
260
244
|
async approveBuilderFee(args, signal) {
|
|
@@ -266,7 +250,7 @@ export class WalletClient {
|
|
|
266
250
|
signatureChainId: typeof this.signatureChainId === "string"
|
|
267
251
|
? this.signatureChainId
|
|
268
252
|
: await this.signatureChainId(),
|
|
269
|
-
nonce:
|
|
253
|
+
nonce: await this.nonceManager(),
|
|
270
254
|
};
|
|
271
255
|
// Sign the action
|
|
272
256
|
const signature = await signUserSignedAction({
|
|
@@ -308,7 +292,7 @@ export class WalletClient {
|
|
|
308
292
|
*
|
|
309
293
|
* const result = await client.batchModify({
|
|
310
294
|
* modifies: [{
|
|
311
|
-
* oid: 123,
|
|
295
|
+
* oid: 123,
|
|
312
296
|
* order: {
|
|
313
297
|
* a: 0, // Asset index
|
|
314
298
|
* b: true, // Buy order
|
|
@@ -328,8 +312,9 @@ export class WalletClient {
|
|
|
328
312
|
*/
|
|
329
313
|
async batchModify(args, signal) {
|
|
330
314
|
// Destructure the parameters
|
|
331
|
-
const { vaultAddress = this.defaultVaultAddress,
|
|
315
|
+
const { vaultAddress = this.defaultVaultAddress, ...actionArgs } = args;
|
|
332
316
|
// Construct an action
|
|
317
|
+
const nonce = await this.nonceManager();
|
|
333
318
|
const action = {
|
|
334
319
|
type: "batchModify",
|
|
335
320
|
modifies: actionArgs.modifies.map((modify) => {
|
|
@@ -404,8 +389,9 @@ export class WalletClient {
|
|
|
404
389
|
*/
|
|
405
390
|
async cancel(args, signal) {
|
|
406
391
|
// Destructure the parameters
|
|
407
|
-
const { vaultAddress = this.defaultVaultAddress,
|
|
392
|
+
const { vaultAddress = this.defaultVaultAddress, ...actionArgs } = args;
|
|
408
393
|
// Construct an action
|
|
394
|
+
const nonce = await this.nonceManager();
|
|
409
395
|
const action = {
|
|
410
396
|
type: "cancel",
|
|
411
397
|
cancels: actionArgs.cancels.map((cancel) => ({
|
|
@@ -428,6 +414,57 @@ export class WalletClient {
|
|
|
428
414
|
this._validateResponse(response);
|
|
429
415
|
return response;
|
|
430
416
|
}
|
|
417
|
+
/**
|
|
418
|
+
* Cancel order(s) by cloid.
|
|
419
|
+
* @param args - The parameters for the request.
|
|
420
|
+
* @param signal - An optional abort signal.
|
|
421
|
+
* @returns Successful variant of {@link CancelResponse} without error statuses.
|
|
422
|
+
* @throws {ApiRequestError} When the API returns an error response.
|
|
423
|
+
*
|
|
424
|
+
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid
|
|
425
|
+
* @example
|
|
426
|
+
* ```ts
|
|
427
|
+
* import * as hl from "@nktkas/hyperliquid";
|
|
428
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
429
|
+
*
|
|
430
|
+
* const wallet = privateKeyToAccount("0x...");
|
|
431
|
+
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
432
|
+
* const client = new hl.WalletClient({ wallet, transport });
|
|
433
|
+
*
|
|
434
|
+
* const result = await client.cancelByCloid({
|
|
435
|
+
* cancels: [
|
|
436
|
+
* { asset: 0, cloid: "0x..." },
|
|
437
|
+
* ],
|
|
438
|
+
* });
|
|
439
|
+
* ```
|
|
440
|
+
*/
|
|
441
|
+
async cancelByCloid(args, signal) {
|
|
442
|
+
// Destructure the parameters
|
|
443
|
+
const { vaultAddress = this.defaultVaultAddress, ...actionArgs } = args;
|
|
444
|
+
// Construct an action
|
|
445
|
+
const nonce = await this.nonceManager();
|
|
446
|
+
const action = {
|
|
447
|
+
type: "cancelByCloid",
|
|
448
|
+
cancels: actionArgs.cancels.map((cancel) => ({
|
|
449
|
+
asset: cancel.asset,
|
|
450
|
+
cloid: cancel.cloid,
|
|
451
|
+
})),
|
|
452
|
+
};
|
|
453
|
+
// Sign the action
|
|
454
|
+
const signature = await signL1Action({
|
|
455
|
+
wallet: this.wallet,
|
|
456
|
+
action,
|
|
457
|
+
nonce,
|
|
458
|
+
isTestnet: this.isTestnet,
|
|
459
|
+
vaultAddress,
|
|
460
|
+
});
|
|
461
|
+
// Send a request
|
|
462
|
+
const request = { action, signature, nonce, vaultAddress };
|
|
463
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
464
|
+
// Validate a response
|
|
465
|
+
this._validateResponse(response);
|
|
466
|
+
return response;
|
|
467
|
+
}
|
|
431
468
|
/**
|
|
432
469
|
* Deposit into staking balance.
|
|
433
470
|
* @param args - The parameters for the request.
|
|
@@ -457,7 +494,7 @@ export class WalletClient {
|
|
|
457
494
|
signatureChainId: typeof this.signatureChainId === "string"
|
|
458
495
|
? this.signatureChainId
|
|
459
496
|
: await this.signatureChainId(),
|
|
460
|
-
nonce:
|
|
497
|
+
nonce: await this.nonceManager(),
|
|
461
498
|
};
|
|
462
499
|
// Sign the action
|
|
463
500
|
const signature = await signUserSignedAction({
|
|
@@ -499,10 +536,9 @@ export class WalletClient {
|
|
|
499
536
|
* const result = await client.claimRewards();
|
|
500
537
|
* ```
|
|
501
538
|
*/
|
|
502
|
-
async claimRewards(
|
|
503
|
-
// Destructure the parameters
|
|
504
|
-
const { nonce = this._nonce, } = args;
|
|
539
|
+
async claimRewards(signal) {
|
|
505
540
|
// Construct an action
|
|
541
|
+
const nonce = await this.nonceManager();
|
|
506
542
|
const sortedAction = { type: "claimRewards" };
|
|
507
543
|
// Sign the action
|
|
508
544
|
const signature = await signL1Action({
|
|
@@ -519,13 +555,13 @@ export class WalletClient {
|
|
|
519
555
|
return response;
|
|
520
556
|
}
|
|
521
557
|
/**
|
|
522
|
-
*
|
|
558
|
+
* Create a sub-account.
|
|
523
559
|
* @param args - The parameters for the request.
|
|
524
560
|
* @param signal - An optional abort signal.
|
|
525
|
-
* @returns
|
|
561
|
+
* @returns Response for creating a sub-account.
|
|
526
562
|
* @throws {ApiRequestError} When the API returns an error response.
|
|
527
563
|
*
|
|
528
|
-
* @see
|
|
564
|
+
* @see null - no documentation
|
|
529
565
|
* @example
|
|
530
566
|
* ```ts
|
|
531
567
|
* import * as hl from "@nktkas/hyperliquid";
|
|
@@ -535,24 +571,15 @@ export class WalletClient {
|
|
|
535
571
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
536
572
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
537
573
|
*
|
|
538
|
-
* const result = await client.
|
|
539
|
-
* cancels: [{
|
|
540
|
-
* asset: 0,
|
|
541
|
-
* cloid: "0x...", // Client Order ID
|
|
542
|
-
* }],
|
|
543
|
-
* });
|
|
574
|
+
* const result = await client.createSubAccount({ name: "subAccountName" });
|
|
544
575
|
* ```
|
|
545
576
|
*/
|
|
546
|
-
async
|
|
547
|
-
// Destructure the parameters
|
|
548
|
-
const { vaultAddress = this.defaultVaultAddress, nonce = this._nonce, ...actionArgs } = args;
|
|
577
|
+
async createSubAccount(args, signal) {
|
|
549
578
|
// Construct an action
|
|
579
|
+
const nonce = await this.nonceManager();
|
|
550
580
|
const action = {
|
|
551
|
-
type: "
|
|
552
|
-
|
|
553
|
-
asset: cancel.asset,
|
|
554
|
-
cloid: cancel.cloid,
|
|
555
|
-
})),
|
|
581
|
+
type: "createSubAccount",
|
|
582
|
+
name: args.name,
|
|
556
583
|
};
|
|
557
584
|
// Sign the action
|
|
558
585
|
const signature = await signL1Action({
|
|
@@ -560,74 +587,22 @@ export class WalletClient {
|
|
|
560
587
|
action,
|
|
561
588
|
nonce,
|
|
562
589
|
isTestnet: this.isTestnet,
|
|
563
|
-
vaultAddress,
|
|
564
590
|
});
|
|
565
591
|
// Send a request
|
|
566
|
-
const request = { action, signature, nonce
|
|
567
|
-
const response = await this.transport.request("exchange", request, signal);
|
|
568
|
-
// Validate a response
|
|
569
|
-
this._validateResponse(response);
|
|
570
|
-
return response;
|
|
571
|
-
}
|
|
572
|
-
/**
|
|
573
|
-
* Withdraw from staking balance.
|
|
574
|
-
* @param args - The parameters for the request.
|
|
575
|
-
* @param signal - An optional abort signal.
|
|
576
|
-
* @returns Successful response without specific data.
|
|
577
|
-
* @throws {ApiRequestError} When the API returns an error response.
|
|
578
|
-
*
|
|
579
|
-
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#withdraw-from-staking
|
|
580
|
-
* @example
|
|
581
|
-
* ```ts
|
|
582
|
-
* import * as hl from "@nktkas/hyperliquid";
|
|
583
|
-
* import { privateKeyToAccount } from "viem/accounts";
|
|
584
|
-
*
|
|
585
|
-
* const wallet = privateKeyToAccount("0x...");
|
|
586
|
-
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
587
|
-
* const client = new hl.WalletClient({ wallet, transport });
|
|
588
|
-
*
|
|
589
|
-
* const result = await client.cWithdraw({ wei: 1 * 1e8 });
|
|
590
|
-
* ```
|
|
591
|
-
*/
|
|
592
|
-
async cWithdraw(args, signal) {
|
|
593
|
-
// Construct an action
|
|
594
|
-
const action = {
|
|
595
|
-
...args,
|
|
596
|
-
type: "cWithdraw",
|
|
597
|
-
hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
|
|
598
|
-
signatureChainId: typeof this.signatureChainId === "string"
|
|
599
|
-
? this.signatureChainId
|
|
600
|
-
: await this.signatureChainId(),
|
|
601
|
-
nonce: args.nonce ?? this._nonce,
|
|
602
|
-
};
|
|
603
|
-
// Sign the action
|
|
604
|
-
const signature = await signUserSignedAction({
|
|
605
|
-
wallet: this.wallet,
|
|
606
|
-
action,
|
|
607
|
-
types: {
|
|
608
|
-
"HyperliquidTransaction:CWithdraw": [
|
|
609
|
-
{ name: "hyperliquidChain", type: "string" },
|
|
610
|
-
{ name: "wei", type: "uint64" },
|
|
611
|
-
{ name: "nonce", type: "uint64" },
|
|
612
|
-
],
|
|
613
|
-
},
|
|
614
|
-
chainId: parseInt(action.signatureChainId, 16),
|
|
615
|
-
});
|
|
616
|
-
// Send a request
|
|
617
|
-
const request = { action, signature, nonce: action.nonce };
|
|
592
|
+
const request = { action, signature, nonce };
|
|
618
593
|
const response = await this.transport.request("exchange", request, signal);
|
|
619
594
|
// Validate a response
|
|
620
595
|
this._validateResponse(response);
|
|
621
596
|
return response;
|
|
622
597
|
}
|
|
623
598
|
/**
|
|
624
|
-
*
|
|
599
|
+
* Create a vault.
|
|
625
600
|
* @param args - The parameters for the request.
|
|
626
601
|
* @param signal - An optional abort signal.
|
|
627
|
-
* @returns Response for creating a
|
|
602
|
+
* @returns Response for creating a vault.
|
|
628
603
|
* @throws {ApiRequestError} When the API returns an error response.
|
|
629
604
|
*
|
|
630
|
-
* @see
|
|
605
|
+
* @see null - no documentation
|
|
631
606
|
* @example
|
|
632
607
|
* ```ts
|
|
633
608
|
* import * as hl from "@nktkas/hyperliquid";
|
|
@@ -637,16 +612,22 @@ export class WalletClient {
|
|
|
637
612
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
638
613
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
639
614
|
*
|
|
640
|
-
* const result = await client.
|
|
615
|
+
* const result = await client.createVault({
|
|
616
|
+
* name: "VaultName",
|
|
617
|
+
* description: "This is an example of a vault description",
|
|
618
|
+
* initialUsd: 100 * 1e6,
|
|
619
|
+
* });
|
|
641
620
|
* ```
|
|
642
621
|
*/
|
|
643
|
-
async
|
|
644
|
-
// Destructure the parameters
|
|
645
|
-
const { nonce = this._nonce, ...actionArgs } = args;
|
|
622
|
+
async createVault(args, signal) {
|
|
646
623
|
// Construct an action
|
|
624
|
+
const nonce = await this.nonceManager();
|
|
647
625
|
const action = {
|
|
648
|
-
type: "
|
|
649
|
-
|
|
626
|
+
type: "createVault",
|
|
627
|
+
name: args.name,
|
|
628
|
+
description: args.description,
|
|
629
|
+
initialUsd: args.initialUsd,
|
|
630
|
+
nonce,
|
|
650
631
|
};
|
|
651
632
|
// Sign the action
|
|
652
633
|
const signature = await signL1Action({
|
|
@@ -663,13 +644,13 @@ export class WalletClient {
|
|
|
663
644
|
return response;
|
|
664
645
|
}
|
|
665
646
|
/**
|
|
666
|
-
*
|
|
647
|
+
* Withdraw from staking balance.
|
|
667
648
|
* @param args - The parameters for the request.
|
|
668
649
|
* @param signal - An optional abort signal.
|
|
669
|
-
* @returns
|
|
650
|
+
* @returns Successful response without specific data.
|
|
670
651
|
* @throws {ApiRequestError} When the API returns an error response.
|
|
671
652
|
*
|
|
672
|
-
* @see
|
|
653
|
+
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#withdraw-from-staking
|
|
673
654
|
* @example
|
|
674
655
|
* ```ts
|
|
675
656
|
* import * as hl from "@nktkas/hyperliquid";
|
|
@@ -679,39 +660,48 @@ export class WalletClient {
|
|
|
679
660
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
680
661
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
681
662
|
*
|
|
682
|
-
* const result = await client.
|
|
663
|
+
* const result = await client.cWithdraw({ wei: 1 * 1e8 });
|
|
683
664
|
* ```
|
|
684
665
|
*/
|
|
685
|
-
async
|
|
686
|
-
// Destructure the parameters
|
|
687
|
-
const { nonce = this._nonce, ...actionArgs } = args;
|
|
666
|
+
async cWithdraw(args, signal) {
|
|
688
667
|
// Construct an action
|
|
689
668
|
const action = {
|
|
690
|
-
|
|
691
|
-
|
|
669
|
+
...args,
|
|
670
|
+
type: "cWithdraw",
|
|
671
|
+
hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
|
|
672
|
+
signatureChainId: typeof this.signatureChainId === "string"
|
|
673
|
+
? this.signatureChainId
|
|
674
|
+
: await this.signatureChainId(),
|
|
675
|
+
nonce: await this.nonceManager(),
|
|
692
676
|
};
|
|
693
677
|
// Sign the action
|
|
694
|
-
const signature = await
|
|
678
|
+
const signature = await signUserSignedAction({
|
|
695
679
|
wallet: this.wallet,
|
|
696
680
|
action,
|
|
697
|
-
|
|
698
|
-
|
|
681
|
+
types: {
|
|
682
|
+
"HyperliquidTransaction:CWithdraw": [
|
|
683
|
+
{ name: "hyperliquidChain", type: "string" },
|
|
684
|
+
{ name: "wei", type: "uint64" },
|
|
685
|
+
{ name: "nonce", type: "uint64" },
|
|
686
|
+
],
|
|
687
|
+
},
|
|
688
|
+
chainId: parseInt(action.signatureChainId, 16),
|
|
699
689
|
});
|
|
700
690
|
// Send a request
|
|
701
|
-
const request = { action, signature, nonce };
|
|
691
|
+
const request = { action, signature, nonce: action.nonce };
|
|
702
692
|
const response = await this.transport.request("exchange", request, signal);
|
|
703
693
|
// Validate a response
|
|
704
694
|
this._validateResponse(response);
|
|
705
695
|
return response;
|
|
706
696
|
}
|
|
707
697
|
/**
|
|
708
|
-
*
|
|
698
|
+
* Configure block type for EVM transactions.
|
|
709
699
|
* @param args - The parameters for the request.
|
|
710
700
|
* @param signal - An optional abort signal.
|
|
711
|
-
* @returns Response for creating a
|
|
701
|
+
* @returns Response for creating a sub-account.
|
|
712
702
|
* @throws {ApiRequestError} When the API returns an error response.
|
|
713
703
|
*
|
|
714
|
-
* @see
|
|
704
|
+
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/evm/dual-block-architecture
|
|
715
705
|
* @example
|
|
716
706
|
* ```ts
|
|
717
707
|
* import * as hl from "@nktkas/hyperliquid";
|
|
@@ -721,23 +711,15 @@ export class WalletClient {
|
|
|
721
711
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
722
712
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
723
713
|
*
|
|
724
|
-
* const result = await client.
|
|
725
|
-
* name: "VaultName",
|
|
726
|
-
* description: "This is an example of a vault description",
|
|
727
|
-
* initialUsd: 100 * 1e6,
|
|
728
|
-
* });
|
|
714
|
+
* const result = await client.evmUserModify({ usingBigBlocks: true });
|
|
729
715
|
* ```
|
|
730
716
|
*/
|
|
731
|
-
async
|
|
732
|
-
// Destructure the parameters
|
|
733
|
-
const { nonce = this._nonce, ...actionArgs } = args;
|
|
717
|
+
async evmUserModify(args, signal) {
|
|
734
718
|
// Construct an action
|
|
719
|
+
const nonce = await this.nonceManager();
|
|
735
720
|
const action = {
|
|
736
|
-
type: "
|
|
737
|
-
|
|
738
|
-
description: actionArgs.description,
|
|
739
|
-
initialUsd: actionArgs.initialUsd,
|
|
740
|
-
nonce,
|
|
721
|
+
type: "evmUserModify",
|
|
722
|
+
usingBigBlocks: args.usingBigBlocks,
|
|
741
723
|
};
|
|
742
724
|
// Sign the action
|
|
743
725
|
const signature = await signL1Action({
|
|
@@ -771,7 +753,7 @@ export class WalletClient {
|
|
|
771
753
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
772
754
|
*
|
|
773
755
|
* const result = await client.modify({
|
|
774
|
-
* oid: 123,
|
|
756
|
+
* oid: 123,
|
|
775
757
|
* order: {
|
|
776
758
|
* a: 0, // Asset index
|
|
777
759
|
* b: true, // Buy order
|
|
@@ -790,8 +772,9 @@ export class WalletClient {
|
|
|
790
772
|
*/
|
|
791
773
|
async modify(args, signal) {
|
|
792
774
|
// Destructure the parameters
|
|
793
|
-
const { vaultAddress = this.defaultVaultAddress,
|
|
775
|
+
const { vaultAddress = this.defaultVaultAddress, ...actionArgs } = args;
|
|
794
776
|
// Construct an action
|
|
777
|
+
const nonce = await this.nonceManager();
|
|
795
778
|
const action = {
|
|
796
779
|
type: "modify",
|
|
797
780
|
oid: actionArgs.oid,
|
|
@@ -871,8 +854,9 @@ export class WalletClient {
|
|
|
871
854
|
*/
|
|
872
855
|
async order(args, signal) {
|
|
873
856
|
// Destructure the parameters
|
|
874
|
-
const { vaultAddress = this.defaultVaultAddress,
|
|
857
|
+
const { vaultAddress = this.defaultVaultAddress, ...actionArgs } = args;
|
|
875
858
|
// Construct an action
|
|
859
|
+
const nonce = await this.nonceManager();
|
|
876
860
|
const action = {
|
|
877
861
|
type: "order",
|
|
878
862
|
orders: actionArgs.orders.map((order) => {
|
|
@@ -926,6 +910,47 @@ export class WalletClient {
|
|
|
926
910
|
this._validateResponse(response);
|
|
927
911
|
return response;
|
|
928
912
|
}
|
|
913
|
+
/**
|
|
914
|
+
* Reserve additional rate-limited actions for a fee.
|
|
915
|
+
* @param args - The parameters for the request.
|
|
916
|
+
* @param signal - An optional abort signal.
|
|
917
|
+
* @returns Successful response indicating the weight reservation.
|
|
918
|
+
* @throws {ApiRequestError} When the API returns an error response.
|
|
919
|
+
*
|
|
920
|
+
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#reserve-additional-actions
|
|
921
|
+
* @example
|
|
922
|
+
* ```ts
|
|
923
|
+
* import * as hl from "@nktkas/hyperliquid";
|
|
924
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
925
|
+
*
|
|
926
|
+
* const wallet = privateKeyToAccount("0x...");
|
|
927
|
+
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
928
|
+
* const client = new hl.WalletClient({ wallet, transport });
|
|
929
|
+
*
|
|
930
|
+
* const result = await client.reserveRequestWeight({ weight: 10 });
|
|
931
|
+
* ```
|
|
932
|
+
*/
|
|
933
|
+
async reserveRequestWeight(args, signal) {
|
|
934
|
+
// Construct an action
|
|
935
|
+
const nonce = await this.nonceManager();
|
|
936
|
+
const action = {
|
|
937
|
+
type: "reserveRequestWeight",
|
|
938
|
+
weight: args.weight,
|
|
939
|
+
};
|
|
940
|
+
// Sign the action
|
|
941
|
+
const signature = await signL1Action({
|
|
942
|
+
wallet: this.wallet,
|
|
943
|
+
action,
|
|
944
|
+
nonce,
|
|
945
|
+
isTestnet: this.isTestnet,
|
|
946
|
+
});
|
|
947
|
+
// Send a request
|
|
948
|
+
const request = { action, signature, nonce };
|
|
949
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
950
|
+
// Validate a response
|
|
951
|
+
this._validateResponse(response);
|
|
952
|
+
return response;
|
|
953
|
+
}
|
|
929
954
|
/**
|
|
930
955
|
* Schedule a time to cancel all open orders.
|
|
931
956
|
* @param args - The parameters for the request.
|
|
@@ -948,8 +973,9 @@ export class WalletClient {
|
|
|
948
973
|
*/
|
|
949
974
|
async scheduleCancel(args = {}, signal) {
|
|
950
975
|
// Destructure the parameters
|
|
951
|
-
const { vaultAddress = this.defaultVaultAddress,
|
|
976
|
+
const { vaultAddress = this.defaultVaultAddress, ...actionArgs } = args;
|
|
952
977
|
// Construct an action
|
|
978
|
+
const nonce = await this.nonceManager();
|
|
953
979
|
const action = {
|
|
954
980
|
type: "scheduleCancel",
|
|
955
981
|
time: actionArgs.time,
|
|
@@ -992,12 +1018,11 @@ export class WalletClient {
|
|
|
992
1018
|
* ```
|
|
993
1019
|
*/
|
|
994
1020
|
async setDisplayName(args, signal) {
|
|
995
|
-
// Destructure the parameters
|
|
996
|
-
const { nonce = this._nonce, ...actionArgs } = args;
|
|
997
1021
|
// Construct an action
|
|
1022
|
+
const nonce = await this.nonceManager();
|
|
998
1023
|
const action = {
|
|
999
1024
|
type: "setDisplayName",
|
|
1000
|
-
displayName:
|
|
1025
|
+
displayName: args.displayName,
|
|
1001
1026
|
};
|
|
1002
1027
|
// Sign the action
|
|
1003
1028
|
const signature = await signL1Action({
|
|
@@ -1034,12 +1059,11 @@ export class WalletClient {
|
|
|
1034
1059
|
* ```
|
|
1035
1060
|
*/
|
|
1036
1061
|
async setReferrer(args, signal) {
|
|
1037
|
-
// Destructure the parameters
|
|
1038
|
-
const { nonce = this._nonce, ...actionArgs } = args;
|
|
1039
1062
|
// Construct an action
|
|
1063
|
+
const nonce = await this.nonceManager();
|
|
1040
1064
|
const action = {
|
|
1041
1065
|
type: "setReferrer",
|
|
1042
|
-
code:
|
|
1066
|
+
code: args.code,
|
|
1043
1067
|
};
|
|
1044
1068
|
// Sign the action
|
|
1045
1069
|
const signature = await signL1Action({
|
|
@@ -1073,7 +1097,6 @@ export class WalletClient {
|
|
|
1073
1097
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
1074
1098
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
1075
1099
|
*
|
|
1076
|
-
* // Unknown what the successful response will be
|
|
1077
1100
|
* const result = await client.spotDeploy({
|
|
1078
1101
|
* registerToken2: {
|
|
1079
1102
|
* spec: {
|
|
@@ -1088,48 +1111,47 @@ export class WalletClient {
|
|
|
1088
1111
|
* ```
|
|
1089
1112
|
*/
|
|
1090
1113
|
async spotDeploy(args, signal) {
|
|
1091
|
-
// Destructure the parameters
|
|
1092
|
-
const { nonce = this._nonce, ...actionArgs } = args;
|
|
1093
1114
|
// Construct an action
|
|
1115
|
+
const nonce = await this.nonceManager();
|
|
1094
1116
|
let action;
|
|
1095
|
-
if ("registerToken2" in
|
|
1117
|
+
if ("registerToken2" in args) {
|
|
1096
1118
|
action = {
|
|
1097
1119
|
type: "spotDeploy",
|
|
1098
1120
|
registerToken2: {
|
|
1099
1121
|
spec: {
|
|
1100
|
-
name:
|
|
1101
|
-
szDecimals:
|
|
1102
|
-
weiDecimals:
|
|
1122
|
+
name: args.registerToken2.spec.name,
|
|
1123
|
+
szDecimals: args.registerToken2.spec.szDecimals,
|
|
1124
|
+
weiDecimals: args.registerToken2.spec.weiDecimals,
|
|
1103
1125
|
},
|
|
1104
|
-
maxGas:
|
|
1105
|
-
fullName:
|
|
1126
|
+
maxGas: args.registerToken2.maxGas,
|
|
1127
|
+
fullName: args.registerToken2.fullName,
|
|
1106
1128
|
},
|
|
1107
1129
|
};
|
|
1108
1130
|
}
|
|
1109
|
-
else if ("userGenesis" in
|
|
1131
|
+
else if ("userGenesis" in args) {
|
|
1110
1132
|
action = {
|
|
1111
1133
|
type: "spotDeploy",
|
|
1112
1134
|
userGenesis: {
|
|
1113
|
-
token:
|
|
1114
|
-
userAndWei:
|
|
1115
|
-
existingTokenAndWei:
|
|
1135
|
+
token: args.userGenesis.token,
|
|
1136
|
+
userAndWei: args.userGenesis.userAndWei,
|
|
1137
|
+
existingTokenAndWei: args.userGenesis.existingTokenAndWei,
|
|
1116
1138
|
},
|
|
1117
1139
|
};
|
|
1118
1140
|
}
|
|
1119
|
-
else if ("genesis" in
|
|
1141
|
+
else if ("genesis" in args) {
|
|
1120
1142
|
action = {
|
|
1121
1143
|
type: "spotDeploy",
|
|
1122
1144
|
genesis: {
|
|
1123
|
-
token:
|
|
1124
|
-
maxSupply:
|
|
1145
|
+
token: args.genesis.token,
|
|
1146
|
+
maxSupply: args.genesis.maxSupply,
|
|
1125
1147
|
},
|
|
1126
1148
|
};
|
|
1127
1149
|
}
|
|
1128
|
-
else if ("registerSpot" in
|
|
1150
|
+
else if ("registerSpot" in args) {
|
|
1129
1151
|
action = {
|
|
1130
1152
|
type: "spotDeploy",
|
|
1131
1153
|
registerSpot: {
|
|
1132
|
-
tokens:
|
|
1154
|
+
tokens: args.registerSpot.tokens,
|
|
1133
1155
|
},
|
|
1134
1156
|
};
|
|
1135
1157
|
}
|
|
@@ -1137,11 +1159,11 @@ export class WalletClient {
|
|
|
1137
1159
|
action = {
|
|
1138
1160
|
type: "spotDeploy",
|
|
1139
1161
|
registerHyperliquidity: {
|
|
1140
|
-
spot:
|
|
1141
|
-
startPx:
|
|
1142
|
-
orderSz:
|
|
1143
|
-
nOrders:
|
|
1144
|
-
nSeededLevels:
|
|
1162
|
+
spot: args.registerHyperliquidity.spot,
|
|
1163
|
+
startPx: args.registerHyperliquidity.startPx,
|
|
1164
|
+
orderSz: args.registerHyperliquidity.orderSz,
|
|
1165
|
+
nOrders: args.registerHyperliquidity.nOrders,
|
|
1166
|
+
nSeededLevels: args.registerHyperliquidity.nSeededLevels,
|
|
1145
1167
|
},
|
|
1146
1168
|
};
|
|
1147
1169
|
}
|
|
@@ -1166,7 +1188,7 @@ export class WalletClient {
|
|
|
1166
1188
|
* @returns Successful response without specific data.
|
|
1167
1189
|
* @throws {ApiRequestError} When the API returns an error response.
|
|
1168
1190
|
*
|
|
1169
|
-
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#
|
|
1191
|
+
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#core-spot-transfer
|
|
1170
1192
|
* @example
|
|
1171
1193
|
* ```ts
|
|
1172
1194
|
* import * as hl from "@nktkas/hyperliquid";
|
|
@@ -1192,7 +1214,7 @@ export class WalletClient {
|
|
|
1192
1214
|
signatureChainId: typeof this.signatureChainId === "string"
|
|
1193
1215
|
? this.signatureChainId
|
|
1194
1216
|
: await this.signatureChainId(),
|
|
1195
|
-
time:
|
|
1217
|
+
time: await this.nonceManager(),
|
|
1196
1218
|
};
|
|
1197
1219
|
// Sign the action
|
|
1198
1220
|
const signature = await signUserSignedAction({
|
|
@@ -1233,19 +1255,16 @@ export class WalletClient {
|
|
|
1233
1255
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
1234
1256
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
1235
1257
|
*
|
|
1236
|
-
* const result = await client.spotUser({
|
|
1237
|
-
* toggleSpotDusting: { optOut: false },
|
|
1238
|
-
* });
|
|
1258
|
+
* const result = await client.spotUser({ toggleSpotDusting: { optOut: false } });
|
|
1239
1259
|
* ```
|
|
1240
1260
|
*/
|
|
1241
1261
|
async spotUser(args, signal) {
|
|
1242
|
-
// Destructure the parameters
|
|
1243
|
-
const { nonce = this._nonce, ...actionArgs } = args;
|
|
1244
1262
|
// Construct an action
|
|
1263
|
+
const nonce = await this.nonceManager();
|
|
1245
1264
|
const action = {
|
|
1246
1265
|
type: "spotUser",
|
|
1247
1266
|
toggleSpotDusting: {
|
|
1248
|
-
optOut:
|
|
1267
|
+
optOut: args.toggleSpotDusting.optOut,
|
|
1249
1268
|
},
|
|
1250
1269
|
};
|
|
1251
1270
|
// Sign the action
|
|
@@ -1288,15 +1307,14 @@ export class WalletClient {
|
|
|
1288
1307
|
* ```
|
|
1289
1308
|
*/
|
|
1290
1309
|
async subAccountSpotTransfer(args, signal) {
|
|
1291
|
-
// Destructure the parameters
|
|
1292
|
-
const { nonce = this._nonce, ...actionArgs } = args;
|
|
1293
1310
|
// Construct an action
|
|
1311
|
+
const nonce = await this.nonceManager();
|
|
1294
1312
|
const action = {
|
|
1295
1313
|
type: "subAccountSpotTransfer",
|
|
1296
|
-
subAccountUser:
|
|
1297
|
-
isDeposit:
|
|
1298
|
-
token:
|
|
1299
|
-
amount:
|
|
1314
|
+
subAccountUser: args.subAccountUser,
|
|
1315
|
+
isDeposit: args.isDeposit,
|
|
1316
|
+
token: args.token,
|
|
1317
|
+
amount: args.amount,
|
|
1300
1318
|
};
|
|
1301
1319
|
// Sign the action
|
|
1302
1320
|
const signature = await signL1Action({
|
|
@@ -1337,14 +1355,13 @@ export class WalletClient {
|
|
|
1337
1355
|
* ```
|
|
1338
1356
|
*/
|
|
1339
1357
|
async subAccountTransfer(args, signal) {
|
|
1340
|
-
// Destructure the parameters
|
|
1341
|
-
const { nonce = this._nonce, ...actionArgs } = args;
|
|
1342
1358
|
// Construct an action
|
|
1359
|
+
const nonce = await this.nonceManager();
|
|
1343
1360
|
const action = {
|
|
1344
1361
|
type: "subAccountTransfer",
|
|
1345
|
-
subAccountUser:
|
|
1346
|
-
isDeposit:
|
|
1347
|
-
usd:
|
|
1362
|
+
subAccountUser: args.subAccountUser,
|
|
1363
|
+
isDeposit: args.isDeposit,
|
|
1364
|
+
usd: args.usd,
|
|
1348
1365
|
};
|
|
1349
1366
|
// Sign the action
|
|
1350
1367
|
const signature = await signL1Action({
|
|
@@ -1393,7 +1410,7 @@ export class WalletClient {
|
|
|
1393
1410
|
signatureChainId: typeof this.signatureChainId === "string"
|
|
1394
1411
|
? this.signatureChainId
|
|
1395
1412
|
: await this.signatureChainId(),
|
|
1396
|
-
nonce:
|
|
1413
|
+
nonce: await this.nonceManager(),
|
|
1397
1414
|
};
|
|
1398
1415
|
// Sign the action
|
|
1399
1416
|
const signature = await signUserSignedAction({
|
|
@@ -1442,8 +1459,9 @@ export class WalletClient {
|
|
|
1442
1459
|
*/
|
|
1443
1460
|
async twapCancel(args, signal) {
|
|
1444
1461
|
// Destructure the parameters
|
|
1445
|
-
const { vaultAddress = this.defaultVaultAddress,
|
|
1462
|
+
const { vaultAddress = this.defaultVaultAddress, ...actionArgs } = args;
|
|
1446
1463
|
// Construct an action
|
|
1464
|
+
const nonce = await this.nonceManager();
|
|
1447
1465
|
const action = {
|
|
1448
1466
|
type: "twapCancel",
|
|
1449
1467
|
a: actionArgs.a,
|
|
@@ -1493,8 +1511,9 @@ export class WalletClient {
|
|
|
1493
1511
|
*/
|
|
1494
1512
|
async twapOrder(args, signal) {
|
|
1495
1513
|
// Destructure the parameters
|
|
1496
|
-
const { vaultAddress = this.defaultVaultAddress,
|
|
1514
|
+
const { vaultAddress = this.defaultVaultAddress, ...actionArgs } = args;
|
|
1497
1515
|
// Construct an action
|
|
1516
|
+
const nonce = await this.nonceManager();
|
|
1498
1517
|
const action = {
|
|
1499
1518
|
type: "twapOrder",
|
|
1500
1519
|
twap: {
|
|
@@ -1538,17 +1557,14 @@ export class WalletClient {
|
|
|
1538
1557
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
1539
1558
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
1540
1559
|
*
|
|
1541
|
-
* const result = await client.updateIsolatedMargin({
|
|
1542
|
-
* asset: 0,
|
|
1543
|
-
* isBuy: true, // Add to long position
|
|
1544
|
-
* ntli: 1, // Add 1 USD margin (integer only)
|
|
1545
|
-
* });
|
|
1560
|
+
* const result = await client.updateIsolatedMargin({ asset: 0, isBuy: true, ntli: 1 * 1e6 });
|
|
1546
1561
|
* ```
|
|
1547
1562
|
*/
|
|
1548
1563
|
async updateIsolatedMargin(args, signal) {
|
|
1549
1564
|
// Destructure the parameters
|
|
1550
|
-
const { vaultAddress = this.defaultVaultAddress,
|
|
1565
|
+
const { vaultAddress = this.defaultVaultAddress, ...actionArgs } = args;
|
|
1551
1566
|
// Construct an action
|
|
1567
|
+
const nonce = await this.nonceManager();
|
|
1552
1568
|
const action = {
|
|
1553
1569
|
type: "updateIsolatedMargin",
|
|
1554
1570
|
asset: actionArgs.asset,
|
|
@@ -1587,17 +1603,14 @@ export class WalletClient {
|
|
|
1587
1603
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
1588
1604
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
1589
1605
|
*
|
|
1590
|
-
* const result = await client.updateLeverage({
|
|
1591
|
-
* asset: 0,
|
|
1592
|
-
* isCross: true,
|
|
1593
|
-
* leverage: 5,
|
|
1594
|
-
* });
|
|
1606
|
+
* const result = await client.updateLeverage({ asset: 0, isCross: true, leverage: 5 });
|
|
1595
1607
|
* ```
|
|
1596
1608
|
*/
|
|
1597
1609
|
async updateLeverage(args, signal) {
|
|
1598
1610
|
// Destructure the parameters
|
|
1599
|
-
const { vaultAddress = this.defaultVaultAddress,
|
|
1611
|
+
const { vaultAddress = this.defaultVaultAddress, ...actionArgs } = args;
|
|
1600
1612
|
// Construct an action
|
|
1613
|
+
const nonce = await this.nonceManager();
|
|
1601
1614
|
const action = {
|
|
1602
1615
|
type: "updateLeverage",
|
|
1603
1616
|
asset: actionArgs.asset,
|
|
@@ -1636,10 +1649,7 @@ export class WalletClient {
|
|
|
1636
1649
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
1637
1650
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
1638
1651
|
*
|
|
1639
|
-
* const result = await client.usdClassTransfer({
|
|
1640
|
-
* amount: "1",
|
|
1641
|
-
* toPerp: true, // Transfer from Spot to Perp
|
|
1642
|
-
* });
|
|
1652
|
+
* const result = await client.usdClassTransfer({ amount: "1", toPerp: true });
|
|
1643
1653
|
* ```
|
|
1644
1654
|
*/
|
|
1645
1655
|
async usdClassTransfer(args, signal) {
|
|
@@ -1651,7 +1661,7 @@ export class WalletClient {
|
|
|
1651
1661
|
signatureChainId: typeof this.signatureChainId === "string"
|
|
1652
1662
|
? this.signatureChainId
|
|
1653
1663
|
: await this.signatureChainId(),
|
|
1654
|
-
nonce:
|
|
1664
|
+
nonce: await this.nonceManager(),
|
|
1655
1665
|
};
|
|
1656
1666
|
// Sign the action
|
|
1657
1667
|
const signature = await signUserSignedAction({
|
|
@@ -1681,7 +1691,7 @@ export class WalletClient {
|
|
|
1681
1691
|
* @returns Successful response without specific data.
|
|
1682
1692
|
* @throws {ApiRequestError} When the API returns an error response.
|
|
1683
1693
|
*
|
|
1684
|
-
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#
|
|
1694
|
+
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#core-usdc-transfer
|
|
1685
1695
|
* @example
|
|
1686
1696
|
* ```ts
|
|
1687
1697
|
* import * as hl from "@nktkas/hyperliquid";
|
|
@@ -1691,10 +1701,7 @@ export class WalletClient {
|
|
|
1691
1701
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
1692
1702
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
1693
1703
|
*
|
|
1694
|
-
* const result = await client.usdSend({
|
|
1695
|
-
* destination: "0x...",
|
|
1696
|
-
* amount: "1",
|
|
1697
|
-
* });
|
|
1704
|
+
* const result = await client.usdSend({ destination: "0x...", amount: "1" });
|
|
1698
1705
|
* ```
|
|
1699
1706
|
*/
|
|
1700
1707
|
async usdSend(args, signal) {
|
|
@@ -1706,7 +1713,7 @@ export class WalletClient {
|
|
|
1706
1713
|
signatureChainId: typeof this.signatureChainId === "string"
|
|
1707
1714
|
? this.signatureChainId
|
|
1708
1715
|
: await this.signatureChainId(),
|
|
1709
|
-
time:
|
|
1716
|
+
time: await this.nonceManager(),
|
|
1710
1717
|
};
|
|
1711
1718
|
// Sign the action
|
|
1712
1719
|
const signature = await signUserSignedAction({
|
|
@@ -1746,20 +1753,16 @@ export class WalletClient {
|
|
|
1746
1753
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
1747
1754
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
1748
1755
|
*
|
|
1749
|
-
* const result = await client.vaultDistribute({
|
|
1750
|
-
* vaultAddress: "0x...",
|
|
1751
|
-
* usd: 10 * 1e6,
|
|
1752
|
-
* });
|
|
1756
|
+
* const result = await client.vaultDistribute({ vaultAddress: "0x...", usd: 10 * 1e6 });
|
|
1753
1757
|
* ```
|
|
1754
1758
|
*/
|
|
1755
1759
|
async vaultDistribute(args, signal) {
|
|
1756
|
-
// Destructure the parameters
|
|
1757
|
-
const { nonce = this._nonce, ...actionArgs } = args;
|
|
1758
1760
|
// Construct an action
|
|
1761
|
+
const nonce = await this.nonceManager();
|
|
1759
1762
|
const action = {
|
|
1760
1763
|
type: "vaultDistribute",
|
|
1761
|
-
vaultAddress:
|
|
1762
|
-
usd:
|
|
1764
|
+
vaultAddress: args.vaultAddress,
|
|
1765
|
+
usd: args.usd,
|
|
1763
1766
|
};
|
|
1764
1767
|
// Sign the action
|
|
1765
1768
|
const signature = await signL1Action({
|
|
@@ -1800,14 +1803,13 @@ export class WalletClient {
|
|
|
1800
1803
|
* ```
|
|
1801
1804
|
*/
|
|
1802
1805
|
async vaultModify(args, signal) {
|
|
1803
|
-
// Destructure the parameters
|
|
1804
|
-
const { nonce = this._nonce, ...actionArgs } = args;
|
|
1805
1806
|
// Construct an action
|
|
1807
|
+
const nonce = await this.nonceManager();
|
|
1806
1808
|
const action = {
|
|
1807
1809
|
type: "vaultModify",
|
|
1808
|
-
vaultAddress:
|
|
1809
|
-
allowDeposits:
|
|
1810
|
-
alwaysCloseOnWithdraw:
|
|
1810
|
+
vaultAddress: args.vaultAddress,
|
|
1811
|
+
allowDeposits: args.allowDeposits,
|
|
1812
|
+
alwaysCloseOnWithdraw: args.alwaysCloseOnWithdraw,
|
|
1811
1813
|
};
|
|
1812
1814
|
// Sign the action
|
|
1813
1815
|
const signature = await signL1Action({
|
|
@@ -1848,14 +1850,13 @@ export class WalletClient {
|
|
|
1848
1850
|
* ```
|
|
1849
1851
|
*/
|
|
1850
1852
|
async vaultTransfer(args, signal) {
|
|
1851
|
-
// Destructure the parameters
|
|
1852
|
-
const { nonce = this._nonce, ...actionArgs } = args;
|
|
1853
1853
|
// Construct an action
|
|
1854
|
+
const nonce = await this.nonceManager();
|
|
1854
1855
|
const action = {
|
|
1855
1856
|
type: "vaultTransfer",
|
|
1856
|
-
vaultAddress:
|
|
1857
|
-
isDeposit:
|
|
1858
|
-
usd:
|
|
1857
|
+
vaultAddress: args.vaultAddress,
|
|
1858
|
+
isDeposit: args.isDeposit,
|
|
1859
|
+
usd: args.usd,
|
|
1859
1860
|
};
|
|
1860
1861
|
// Sign the action
|
|
1861
1862
|
const signature = await signL1Action({
|
|
@@ -1888,10 +1889,7 @@ export class WalletClient {
|
|
|
1888
1889
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
1889
1890
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
1890
1891
|
*
|
|
1891
|
-
* const result = await client.withdraw3({
|
|
1892
|
-
* destination: "0x...",
|
|
1893
|
-
* amount: "1",
|
|
1894
|
-
* });
|
|
1892
|
+
* const result = await client.withdraw3({ destination: "0x...", amount: "1" });
|
|
1895
1893
|
* ```
|
|
1896
1894
|
*/
|
|
1897
1895
|
async withdraw3(args, signal) {
|
|
@@ -1903,7 +1901,7 @@ export class WalletClient {
|
|
|
1903
1901
|
signatureChainId: typeof this.signatureChainId === "string"
|
|
1904
1902
|
? this.signatureChainId
|
|
1905
1903
|
: await this.signatureChainId(),
|
|
1906
|
-
time:
|
|
1904
|
+
time: await this.nonceManager(),
|
|
1907
1905
|
};
|
|
1908
1906
|
// Sign the action
|
|
1909
1907
|
const signature = await signUserSignedAction({
|
|
@@ -1926,7 +1924,6 @@ export class WalletClient {
|
|
|
1926
1924
|
this._validateResponse(response);
|
|
1927
1925
|
return response;
|
|
1928
1926
|
}
|
|
1929
|
-
// ——————————————— Private methods ———————————————
|
|
1930
1927
|
/** Formats a decimal number as a string, removing trailing zeros. */
|
|
1931
1928
|
_formatDecimal(numStr) {
|
|
1932
1929
|
if (!numStr.includes("."))
|
|
@@ -1935,6 +1932,31 @@ export class WalletClient {
|
|
|
1935
1932
|
const newFrac = fracPart.replace(/0+$/, "");
|
|
1936
1933
|
return newFrac ? `${intPart}.${newFrac}` : intPart;
|
|
1937
1934
|
}
|
|
1935
|
+
/** Guesses the chain ID based on the wallet type or the isTestnet flag. */
|
|
1936
|
+
async _guessSignatureChainId() {
|
|
1937
|
+
// Trying to get chain ID of the wallet
|
|
1938
|
+
if (isAbstractViemWalletClient(this.wallet)) {
|
|
1939
|
+
if ("getChainId" in this.wallet && typeof this.wallet.getChainId === "function") {
|
|
1940
|
+
const chainId = await this.wallet.getChainId();
|
|
1941
|
+
return `0x${chainId.toString(16)}`;
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
else if (isAbstractEthersSigner(this.wallet) || isAbstractEthersV5Signer(this.wallet)) {
|
|
1945
|
+
if ("provider" in this.wallet &&
|
|
1946
|
+
typeof this.wallet.provider === "object" && this.wallet.provider !== null &&
|
|
1947
|
+
"getNetwork" in this.wallet.provider &&
|
|
1948
|
+
typeof this.wallet.provider.getNetwork === "function") {
|
|
1949
|
+
const network = await this.wallet.provider.getNetwork();
|
|
1950
|
+
return `0x${network.chainId.toString(16)}`;
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
else if (isAbstractWindowEthereum(this.wallet)) {
|
|
1954
|
+
const [chainId] = await this.wallet.request({ method: "eth_chainId", params: [] });
|
|
1955
|
+
return chainId;
|
|
1956
|
+
}
|
|
1957
|
+
// Attempt to guess chain ID based on isTestnet
|
|
1958
|
+
return this.isTestnet ? "0x66eee" : "0xa4b1";
|
|
1959
|
+
}
|
|
1938
1960
|
/** Validate a response from the API. */
|
|
1939
1961
|
_validateResponse(response) {
|
|
1940
1962
|
if (response.status === "err") {
|