@nktkas/hyperliquid 0.13.2 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +4 -7
- package/README.md +297 -103
- package/esm/mod.d.ts +3 -12
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +2 -2
- package/esm/src/{transports/base.d.ts → base.d.ts} +11 -14
- package/esm/src/base.d.ts.map +1 -0
- package/esm/src/base.js +14 -0
- package/esm/src/clients/event.d.ts +4 -4
- package/esm/src/clients/event.d.ts.map +1 -1
- package/esm/src/clients/event.js +2 -2
- package/esm/src/clients/public.d.ts +109 -6
- package/esm/src/clients/public.d.ts.map +1 -1
- package/esm/src/clients/public.js +106 -3
- package/esm/src/clients/wallet.d.ts +232 -29
- package/esm/src/clients/wallet.d.ts.map +1 -1
- package/esm/src/clients/wallet.js +859 -243
- package/esm/src/signing.d.ts +135 -0
- package/esm/src/signing.d.ts.map +1 -0
- package/esm/src/signing.js +188 -0
- package/esm/src/transports/http/http_transport.d.ts +2 -4
- package/esm/src/transports/http/http_transport.d.ts.map +1 -1
- package/esm/src/transports/http/http_transport.js +8 -13
- package/{script/src/transports/websocket/hyperliquid_event_target.d.ts → esm/src/transports/websocket/_hyperliquid_event_target.d.ts} +7 -15
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -0
- package/esm/src/transports/websocket/{hyperliquid_event_target.js → _hyperliquid_event_target.js} +4 -22
- package/esm/src/transports/websocket/{reconnecting_websocket.d.ts → _reconnecting_websocket.d.ts} +26 -26
- package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -0
- package/esm/src/transports/websocket/{reconnecting_websocket.js → _reconnecting_websocket.js} +91 -76
- package/esm/src/transports/websocket/{websocket_request_dispatcher.d.ts → _websocket_request_dispatcher.d.ts} +3 -7
- package/esm/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +1 -0
- package/esm/src/transports/websocket/{websocket_request_dispatcher.js → _websocket_request_dispatcher.js} +3 -7
- package/esm/src/transports/websocket/websocket_transport.d.ts +13 -16
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/esm/src/transports/websocket/websocket_transport.js +49 -49
- package/esm/src/types/exchange/requests.d.ts +172 -20
- package/esm/src/types/exchange/requests.d.ts.map +1 -1
- package/esm/src/types/exchange/responses.d.ts +32 -32
- package/esm/src/types/exchange/responses.d.ts.map +1 -1
- package/esm/src/types/explorer/requests.d.ts +12 -12
- package/esm/src/types/explorer/requests.d.ts.map +1 -1
- package/esm/src/types/explorer/responses.d.ts +36 -1
- package/esm/src/types/explorer/responses.d.ts.map +1 -1
- package/esm/src/types/info/accounts.d.ts +130 -105
- package/esm/src/types/info/accounts.d.ts.map +1 -1
- package/esm/src/types/info/assets.d.ts +154 -152
- package/esm/src/types/info/assets.d.ts.map +1 -1
- package/esm/src/types/info/delegations.d.ts +20 -20
- package/esm/src/types/info/delegations.d.ts.map +1 -1
- package/esm/src/types/info/orders.d.ts +55 -55
- package/esm/src/types/info/orders.d.ts.map +1 -1
- package/esm/src/types/info/requests.d.ts +73 -10
- package/esm/src/types/info/requests.d.ts.map +1 -1
- package/esm/src/types/info/vaults.d.ts +1 -1
- package/esm/src/types/info/vaults.d.ts.map +1 -1
- package/esm/src/types/mod.d.ts +14 -0
- package/esm/src/types/mod.d.ts.map +1 -0
- package/esm/src/types/subscriptions/requests.d.ts +1 -1
- package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
- package/{script/src/types/subscriptions/common.d.ts → esm/src/types/subscriptions/responses.d.ts} +16 -4
- package/esm/src/types/subscriptions/responses.d.ts.map +1 -0
- package/package.json +20 -4
- package/script/mod.d.ts +3 -12
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +3 -5
- package/script/src/{transports/base.d.ts → base.d.ts} +11 -14
- package/script/src/base.d.ts.map +1 -0
- package/script/src/{transports/base.js → base.js} +13 -12
- package/script/src/clients/event.d.ts +4 -4
- package/script/src/clients/event.d.ts.map +1 -1
- package/script/src/clients/event.js +2 -2
- package/script/src/clients/public.d.ts +109 -6
- package/script/src/clients/public.d.ts.map +1 -1
- package/script/src/clients/public.js +106 -3
- package/script/src/clients/wallet.d.ts +232 -29
- package/script/src/clients/wallet.d.ts.map +1 -1
- package/script/src/clients/wallet.js +860 -244
- package/script/src/signing.d.ts +135 -0
- package/script/src/signing.d.ts.map +1 -0
- package/script/src/signing.js +203 -0
- package/script/src/transports/http/http_transport.d.ts +2 -4
- package/script/src/transports/http/http_transport.d.ts.map +1 -1
- package/script/src/transports/http/http_transport.js +9 -14
- package/{esm/src/transports/websocket/hyperliquid_event_target.d.ts → script/src/transports/websocket/_hyperliquid_event_target.d.ts} +7 -15
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -0
- package/script/src/transports/websocket/{hyperliquid_event_target.js → _hyperliquid_event_target.js} +4 -22
- package/script/src/transports/websocket/{reconnecting_websocket.d.ts → _reconnecting_websocket.d.ts} +26 -26
- package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -0
- package/script/src/transports/websocket/{reconnecting_websocket.js → _reconnecting_websocket.js} +94 -78
- package/script/src/transports/websocket/{websocket_request_dispatcher.d.ts → _websocket_request_dispatcher.d.ts} +3 -7
- package/script/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +1 -0
- package/script/src/transports/websocket/{websocket_request_dispatcher.js → _websocket_request_dispatcher.js} +4 -8
- package/script/src/transports/websocket/websocket_transport.d.ts +13 -16
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/script/src/transports/websocket/websocket_transport.js +55 -55
- package/script/src/types/exchange/requests.d.ts +172 -20
- package/script/src/types/exchange/requests.d.ts.map +1 -1
- package/script/src/types/exchange/responses.d.ts +32 -32
- package/script/src/types/exchange/responses.d.ts.map +1 -1
- package/script/src/types/explorer/requests.d.ts +12 -12
- package/script/src/types/explorer/requests.d.ts.map +1 -1
- package/script/src/types/explorer/responses.d.ts +36 -1
- package/script/src/types/explorer/responses.d.ts.map +1 -1
- package/script/src/types/info/accounts.d.ts +130 -105
- package/script/src/types/info/accounts.d.ts.map +1 -1
- package/script/src/types/info/assets.d.ts +154 -152
- package/script/src/types/info/assets.d.ts.map +1 -1
- package/script/src/types/info/delegations.d.ts +20 -20
- package/script/src/types/info/delegations.d.ts.map +1 -1
- package/script/src/types/info/orders.d.ts +55 -55
- package/script/src/types/info/orders.d.ts.map +1 -1
- package/script/src/types/info/requests.d.ts +73 -10
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/info/vaults.d.ts +1 -1
- package/script/src/types/info/vaults.d.ts.map +1 -1
- package/script/src/types/mod.d.ts +14 -0
- package/script/src/types/mod.d.ts.map +1 -0
- package/script/src/types/subscriptions/requests.d.ts +1 -1
- package/script/src/types/subscriptions/requests.d.ts.map +1 -1
- package/{esm/src/types/subscriptions/common.d.ts → script/src/types/subscriptions/responses.d.ts} +16 -4
- package/script/src/types/subscriptions/responses.d.ts.map +1 -0
- package/esm/src/transports/base.d.ts.map +0 -1
- package/esm/src/transports/base.js +0 -14
- package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +0 -1
- package/esm/src/transports/websocket/reconnecting_websocket.d.ts.map +0 -1
- package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts.map +0 -1
- package/esm/src/types/common.d.ts +0 -3
- package/esm/src/types/common.d.ts.map +0 -1
- package/esm/src/types/exchange/common.d.ts +0 -36
- package/esm/src/types/exchange/common.d.ts.map +0 -1
- package/esm/src/types/explorer/common.d.ts +0 -37
- package/esm/src/types/explorer/common.d.ts.map +0 -1
- package/esm/src/types/explorer/common.js +0 -1
- package/esm/src/types/subscriptions/common.d.ts.map +0 -1
- package/esm/src/types/subscriptions/common.js +0 -1
- package/esm/src/utils/key_sort.d.ts +0 -21
- package/esm/src/utils/key_sort.d.ts.map +0 -1
- package/esm/src/utils/key_sort.js +0 -124
- package/esm/src/utils/signing.d.ts +0 -109
- package/esm/src/utils/signing.d.ts.map +0 -1
- package/esm/src/utils/signing.js +0 -164
- package/script/src/transports/base.d.ts.map +0 -1
- package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +0 -1
- package/script/src/transports/websocket/reconnecting_websocket.d.ts.map +0 -1
- package/script/src/transports/websocket/websocket_request_dispatcher.d.ts.map +0 -1
- package/script/src/types/common.d.ts +0 -3
- package/script/src/types/common.d.ts.map +0 -1
- package/script/src/types/exchange/common.d.ts +0 -36
- package/script/src/types/exchange/common.d.ts.map +0 -1
- package/script/src/types/explorer/common.d.ts +0 -37
- package/script/src/types/explorer/common.d.ts.map +0 -1
- package/script/src/types/explorer/common.js +0 -12
- package/script/src/types/subscriptions/common.d.ts.map +0 -1
- package/script/src/types/subscriptions/common.js +0 -12
- package/script/src/utils/key_sort.d.ts +0 -21
- package/script/src/utils/key_sort.d.ts.map +0 -1
- package/script/src/utils/key_sort.js +0 -137
- package/script/src/utils/signing.d.ts +0 -109
- package/script/src/utils/signing.d.ts.map +0 -1
- package/script/src/utils/signing.js +0 -182
- /package/esm/src/types/{common.js → mod.js} +0 -0
- /package/esm/src/types/{exchange/common.js → subscriptions/responses.js} +0 -0
- /package/script/src/types/{common.js → mod.js} +0 -0
- /package/script/src/types/{exchange/common.js → subscriptions/responses.js} +0 -0
|
@@ -4,26 +4,27 @@
|
|
|
4
4
|
if (v !== undefined) module.exports = v;
|
|
5
5
|
}
|
|
6
6
|
else if (typeof define === "function" && define.amd) {
|
|
7
|
-
define(["require", "exports", "../
|
|
7
|
+
define(["require", "exports", "../base.js", "../signing.js"], factory);
|
|
8
8
|
}
|
|
9
9
|
})(function (require, exports) {
|
|
10
10
|
"use strict";
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.WalletClient = exports.ApiRequestError = void 0;
|
|
13
|
-
const
|
|
14
|
-
const signing_js_1 = require("../
|
|
13
|
+
const base_js_1 = require("../base.js");
|
|
14
|
+
const signing_js_1 = require("../signing.js");
|
|
15
15
|
// ——————————————— Errors ———————————————
|
|
16
16
|
/** Error thrown when the API returns an error response. */
|
|
17
|
-
class ApiRequestError extends
|
|
17
|
+
class ApiRequestError extends base_js_1.HyperliquidError {
|
|
18
18
|
constructor(response) {
|
|
19
19
|
let message = "Cannot process API request";
|
|
20
20
|
if (response.status === "err") {
|
|
21
|
+
// For ErrorResponse
|
|
21
22
|
message += `: ${response.response}`;
|
|
22
23
|
}
|
|
23
24
|
else {
|
|
24
25
|
if ("statuses" in response.response.data) {
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
// For OrderResponse, CancelResponse
|
|
27
|
+
const errors = response.response.data.statuses.reduce((acc, status, index) => {
|
|
27
28
|
if (typeof status === "object" && "error" in status) {
|
|
28
29
|
acc.push(`Order ${index} failed: ${status.error}`);
|
|
29
30
|
}
|
|
@@ -34,6 +35,7 @@
|
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
37
|
else {
|
|
38
|
+
// For TwapOrderResponse, TwapCancelResponse
|
|
37
39
|
if (typeof response.response.data.status === "object" && "error" in response.response.data.status) {
|
|
38
40
|
message += `: ${response.response.data.status.error}`;
|
|
39
41
|
}
|
|
@@ -53,8 +55,8 @@
|
|
|
53
55
|
// ——————————————— Client ———————————————
|
|
54
56
|
/**
|
|
55
57
|
* Wallet client for interacting with the Hyperliquid API.
|
|
56
|
-
* @typeParam T
|
|
57
|
-
* @typeParam W
|
|
58
|
+
* @typeParam T The transport used to connect to the Hyperliquid API.
|
|
59
|
+
* @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.
|
|
58
60
|
*/
|
|
59
61
|
class WalletClient {
|
|
60
62
|
/**
|
|
@@ -72,7 +74,7 @@
|
|
|
72
74
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
73
75
|
* ```
|
|
74
76
|
*
|
|
75
|
-
* @example Private key via [ethers.js](https://docs.ethers.org/v6/api/wallet/#Wallet)
|
|
77
|
+
* @example Private key via [ethers.js](https://docs.ethers.org/v6/api/wallet/#Wallet) or [ethers.js v5](https://docs.ethers.org/v5/api/signer/#Wallet)
|
|
76
78
|
* ```ts
|
|
77
79
|
* import * as hl from "@nktkas/hyperliquid";
|
|
78
80
|
* import { ethers } from "ethers";
|
|
@@ -94,6 +96,14 @@
|
|
|
94
96
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
95
97
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
96
98
|
* ```
|
|
99
|
+
*
|
|
100
|
+
* @example External wallet (e.g. MetaMask) via `window.ethereum` directly
|
|
101
|
+
* ```ts
|
|
102
|
+
* import * as hl from "@nktkas/hyperliquid";
|
|
103
|
+
*
|
|
104
|
+
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
105
|
+
* const client = new hl.WalletClient({ wallet: window.ethereum, transport });
|
|
106
|
+
* ```
|
|
97
107
|
*/
|
|
98
108
|
constructor(args) {
|
|
99
109
|
/** The transport used to connect to the Hyperliquid API. */
|
|
@@ -103,7 +113,10 @@
|
|
|
103
113
|
writable: true,
|
|
104
114
|
value: void 0
|
|
105
115
|
});
|
|
106
|
-
/**
|
|
116
|
+
/**
|
|
117
|
+
* The [viem](https://viem.sh/docs/clients/wallet) or [ethers.js](https://docs.ethers.org/v6/api/providers/#Signer)
|
|
118
|
+
* used for signing transactions.
|
|
119
|
+
*/
|
|
107
120
|
Object.defineProperty(this, "wallet", {
|
|
108
121
|
enumerable: true,
|
|
109
122
|
configurable: true,
|
|
@@ -124,10 +137,21 @@
|
|
|
124
137
|
writable: true,
|
|
125
138
|
value: void 0
|
|
126
139
|
});
|
|
140
|
+
/**
|
|
141
|
+
* The network that will be used to sign transactions.
|
|
142
|
+
* Must match the network of the {@link wallet}.
|
|
143
|
+
*/
|
|
144
|
+
Object.defineProperty(this, "signatureChainId", {
|
|
145
|
+
enumerable: true,
|
|
146
|
+
configurable: true,
|
|
147
|
+
writable: true,
|
|
148
|
+
value: void 0
|
|
149
|
+
});
|
|
127
150
|
this.transport = args.transport;
|
|
128
151
|
this.wallet = args.wallet;
|
|
129
152
|
this.isTestnet = args.isTestnet ?? false;
|
|
130
153
|
this.defaultVaultAddress = args.defaultVaultAddress;
|
|
154
|
+
this.signatureChainId = args.signatureChainId ?? (this.isTestnet ? "0x66eee" : "0xa4b1");
|
|
131
155
|
}
|
|
132
156
|
// ———————————————Actions———————————————
|
|
133
157
|
/**
|
|
@@ -151,29 +175,35 @@
|
|
|
151
175
|
* agentAddress: "0x...",
|
|
152
176
|
* agentName: "agentName",
|
|
153
177
|
* });
|
|
178
|
+
* ```
|
|
154
179
|
*/
|
|
155
180
|
async approveAgent(args, signal) {
|
|
181
|
+
// Construct an action
|
|
156
182
|
const action = {
|
|
157
183
|
...args,
|
|
158
184
|
type: "approveAgent",
|
|
159
185
|
hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
|
|
160
|
-
signatureChainId:
|
|
186
|
+
signatureChainId: this.signatureChainId,
|
|
161
187
|
nonce: args.nonce ?? Date.now(),
|
|
162
188
|
};
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
{ name: "agentAddress", type: "address" },
|
|
167
|
-
{ name: "agentName", type: "string" },
|
|
168
|
-
{ name: "nonce", type: "uint64" },
|
|
169
|
-
],
|
|
170
|
-
}, parseInt(action.signatureChainId, 16));
|
|
171
|
-
const request = {
|
|
189
|
+
// Sign the action
|
|
190
|
+
const signature = await (0, signing_js_1.signUserSignedAction)({
|
|
191
|
+
wallet: this.wallet,
|
|
172
192
|
action,
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
193
|
+
types: {
|
|
194
|
+
"HyperliquidTransaction:ApproveAgent": [
|
|
195
|
+
{ name: "hyperliquidChain", type: "string" },
|
|
196
|
+
{ name: "agentAddress", type: "address" },
|
|
197
|
+
{ name: "agentName", type: "string" },
|
|
198
|
+
{ name: "nonce", type: "uint64" },
|
|
199
|
+
],
|
|
200
|
+
},
|
|
201
|
+
chainId: parseInt(action.signatureChainId, 16),
|
|
202
|
+
});
|
|
203
|
+
// Send a request
|
|
204
|
+
const request = { action, signature, nonce: action.nonce };
|
|
205
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
206
|
+
// Validate a response
|
|
177
207
|
this._validateResponse(response);
|
|
178
208
|
return response;
|
|
179
209
|
}
|
|
@@ -198,29 +228,35 @@
|
|
|
198
228
|
* maxFeeRate: "0.01%",
|
|
199
229
|
* builder: "0x...",
|
|
200
230
|
* });
|
|
231
|
+
* ```
|
|
201
232
|
*/
|
|
202
233
|
async approveBuilderFee(args, signal) {
|
|
234
|
+
// Construct an action
|
|
203
235
|
const action = {
|
|
204
236
|
...args,
|
|
205
237
|
type: "approveBuilderFee",
|
|
206
238
|
hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
|
|
207
|
-
signatureChainId:
|
|
239
|
+
signatureChainId: this.signatureChainId,
|
|
208
240
|
nonce: args.nonce ?? Date.now(),
|
|
209
241
|
};
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
{ name: "maxFeeRate", type: "string" },
|
|
214
|
-
{ name: "builder", type: "address" },
|
|
215
|
-
{ name: "nonce", type: "uint64" },
|
|
216
|
-
],
|
|
217
|
-
}, parseInt(action.signatureChainId, 16));
|
|
218
|
-
const request = {
|
|
242
|
+
// Sign the action
|
|
243
|
+
const signature = await (0, signing_js_1.signUserSignedAction)({
|
|
244
|
+
wallet: this.wallet,
|
|
219
245
|
action,
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
246
|
+
types: {
|
|
247
|
+
"HyperliquidTransaction:ApproveBuilderFee": [
|
|
248
|
+
{ name: "hyperliquidChain", type: "string" },
|
|
249
|
+
{ name: "maxFeeRate", type: "string" },
|
|
250
|
+
{ name: "builder", type: "address" },
|
|
251
|
+
{ name: "nonce", type: "uint64" },
|
|
252
|
+
],
|
|
253
|
+
},
|
|
254
|
+
chainId: parseInt(action.signatureChainId, 16),
|
|
255
|
+
});
|
|
256
|
+
// Send a request
|
|
257
|
+
const request = { action, signature, nonce: action.nonce };
|
|
258
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
259
|
+
// Validate a response
|
|
224
260
|
this._validateResponse(response);
|
|
225
261
|
return response;
|
|
226
262
|
}
|
|
@@ -262,16 +298,53 @@
|
|
|
262
298
|
* ```
|
|
263
299
|
*/
|
|
264
300
|
async batchModify(args, signal) {
|
|
301
|
+
// Destructure the parameters
|
|
265
302
|
const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
|
|
266
|
-
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
303
|
+
// Construct an action
|
|
304
|
+
const action = {
|
|
305
|
+
type: "batchModify",
|
|
306
|
+
modifies: actionArgs.modifies.map((modify) => {
|
|
307
|
+
const sortedModify = {
|
|
308
|
+
oid: modify.oid,
|
|
309
|
+
order: {
|
|
310
|
+
a: modify.order.a,
|
|
311
|
+
b: modify.order.b,
|
|
312
|
+
p: modify.order.p,
|
|
313
|
+
s: modify.order.s,
|
|
314
|
+
r: modify.order.r,
|
|
315
|
+
t: "limit" in modify.order.t
|
|
316
|
+
? {
|
|
317
|
+
limit: {
|
|
318
|
+
tif: modify.order.t.limit.tif,
|
|
319
|
+
},
|
|
320
|
+
}
|
|
321
|
+
: {
|
|
322
|
+
trigger: {
|
|
323
|
+
isMarket: modify.order.t.trigger.isMarket,
|
|
324
|
+
triggerPx: modify.order.t.trigger.triggerPx,
|
|
325
|
+
tpsl: modify.order.t.trigger.tpsl,
|
|
326
|
+
},
|
|
327
|
+
},
|
|
328
|
+
c: modify.order.c,
|
|
329
|
+
},
|
|
330
|
+
};
|
|
331
|
+
if (sortedModify.order.c === undefined)
|
|
332
|
+
delete sortedModify.order.c;
|
|
333
|
+
return sortedModify;
|
|
334
|
+
}),
|
|
335
|
+
};
|
|
336
|
+
// Sign the action
|
|
337
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
338
|
+
wallet: this.wallet,
|
|
339
|
+
action,
|
|
271
340
|
nonce,
|
|
341
|
+
isTestnet: this.isTestnet,
|
|
272
342
|
vaultAddress,
|
|
273
|
-
};
|
|
274
|
-
|
|
343
|
+
});
|
|
344
|
+
// Send a request
|
|
345
|
+
const request = { action, signature, nonce, vaultAddress };
|
|
346
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
347
|
+
// Validate a response
|
|
275
348
|
this._validateResponse(response);
|
|
276
349
|
return response;
|
|
277
350
|
}
|
|
@@ -301,16 +374,28 @@
|
|
|
301
374
|
* ```
|
|
302
375
|
*/
|
|
303
376
|
async cancel(args, signal) {
|
|
377
|
+
// Destructure the parameters
|
|
304
378
|
const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
|
|
305
|
-
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
379
|
+
// Construct an action
|
|
380
|
+
const action = {
|
|
381
|
+
type: "cancel",
|
|
382
|
+
cancels: actionArgs.cancels.map((cancel) => ({
|
|
383
|
+
a: cancel.a,
|
|
384
|
+
o: cancel.o,
|
|
385
|
+
})),
|
|
386
|
+
};
|
|
387
|
+
// Sign the action
|
|
388
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
389
|
+
wallet: this.wallet,
|
|
390
|
+
action,
|
|
310
391
|
nonce,
|
|
392
|
+
isTestnet: this.isTestnet,
|
|
311
393
|
vaultAddress,
|
|
312
|
-
};
|
|
313
|
-
|
|
394
|
+
});
|
|
395
|
+
// Send a request
|
|
396
|
+
const request = { action, signature, nonce, vaultAddress };
|
|
397
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
398
|
+
// Validate a response
|
|
314
399
|
this._validateResponse(response);
|
|
315
400
|
return response;
|
|
316
401
|
}
|
|
@@ -331,30 +416,74 @@
|
|
|
331
416
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
332
417
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
333
418
|
*
|
|
334
|
-
* const result = await client.cDeposit({ wei:
|
|
419
|
+
* const result = await client.cDeposit({ wei: 1 * 1e8 });
|
|
335
420
|
* ```
|
|
336
421
|
*/
|
|
337
422
|
async cDeposit(args, signal) {
|
|
423
|
+
// Construct an action
|
|
338
424
|
const action = {
|
|
339
425
|
...args,
|
|
340
426
|
type: "cDeposit",
|
|
341
427
|
hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
|
|
342
|
-
signatureChainId:
|
|
428
|
+
signatureChainId: this.signatureChainId,
|
|
343
429
|
nonce: args.nonce ?? Date.now(),
|
|
344
430
|
};
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
{ name: "wei", type: "uint64" },
|
|
349
|
-
{ name: "nonce", type: "uint64" },
|
|
350
|
-
],
|
|
351
|
-
}, parseInt(action.signatureChainId, 16));
|
|
352
|
-
const request = {
|
|
431
|
+
// Sign the action
|
|
432
|
+
const signature = await (0, signing_js_1.signUserSignedAction)({
|
|
433
|
+
wallet: this.wallet,
|
|
353
434
|
action,
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
435
|
+
types: {
|
|
436
|
+
"HyperliquidTransaction:CDeposit": [
|
|
437
|
+
{ name: "hyperliquidChain", type: "string" },
|
|
438
|
+
{ name: "wei", type: "uint64" },
|
|
439
|
+
{ name: "nonce", type: "uint64" },
|
|
440
|
+
],
|
|
441
|
+
},
|
|
442
|
+
chainId: parseInt(action.signatureChainId, 16),
|
|
443
|
+
});
|
|
444
|
+
// Send a request
|
|
445
|
+
const request = { action, signature, nonce: action.nonce };
|
|
446
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
447
|
+
// Validate a response
|
|
448
|
+
this._validateResponse(response);
|
|
449
|
+
return response;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Claim rewards from referral program.
|
|
453
|
+
* @param args - The parameters for the request.
|
|
454
|
+
* @param signal - An optional abort signal.
|
|
455
|
+
* @returns Successful response without specific data.
|
|
456
|
+
* @throws {ApiRequestError} When the API returns an error response.
|
|
457
|
+
*
|
|
458
|
+
* @see null - no documentation
|
|
459
|
+
* @example
|
|
460
|
+
* ```ts
|
|
461
|
+
* import * as hl from "@nktkas/hyperliquid";
|
|
462
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
463
|
+
*
|
|
464
|
+
* const wallet = privateKeyToAccount("0x...");
|
|
465
|
+
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
466
|
+
* const client = new hl.WalletClient({ wallet, transport });
|
|
467
|
+
*
|
|
468
|
+
* const result = await client.claimRewards();
|
|
469
|
+
* ```
|
|
470
|
+
*/
|
|
471
|
+
async claimRewards(args = {}, signal) {
|
|
472
|
+
// Destructure the parameters
|
|
473
|
+
const { nonce = Date.now() } = args;
|
|
474
|
+
// Construct an action
|
|
475
|
+
const sortedAction = { type: "claimRewards" };
|
|
476
|
+
// Sign the action
|
|
477
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
478
|
+
wallet: this.wallet,
|
|
479
|
+
action: sortedAction,
|
|
480
|
+
nonce,
|
|
481
|
+
isTestnet: this.isTestnet,
|
|
482
|
+
});
|
|
483
|
+
// Send a request
|
|
484
|
+
const request = { action: sortedAction, signature, nonce };
|
|
485
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
486
|
+
// Validate a response
|
|
358
487
|
this._validateResponse(response);
|
|
359
488
|
return response;
|
|
360
489
|
}
|
|
@@ -384,16 +513,28 @@
|
|
|
384
513
|
* ```
|
|
385
514
|
*/
|
|
386
515
|
async cancelByCloid(args, signal) {
|
|
516
|
+
// Destructure the parameters
|
|
387
517
|
const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
|
|
388
|
-
|
|
389
|
-
const
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
518
|
+
// Construct an action
|
|
519
|
+
const action = {
|
|
520
|
+
type: "cancelByCloid",
|
|
521
|
+
cancels: actionArgs.cancels.map((cancel) => ({
|
|
522
|
+
asset: cancel.asset,
|
|
523
|
+
cloid: cancel.cloid,
|
|
524
|
+
})),
|
|
525
|
+
};
|
|
526
|
+
// Sign the action
|
|
527
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
528
|
+
wallet: this.wallet,
|
|
529
|
+
action,
|
|
393
530
|
nonce,
|
|
531
|
+
isTestnet: this.isTestnet,
|
|
394
532
|
vaultAddress,
|
|
395
|
-
};
|
|
396
|
-
|
|
533
|
+
});
|
|
534
|
+
// Send a request
|
|
535
|
+
const request = { action, signature, nonce, vaultAddress };
|
|
536
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
537
|
+
// Validate a response
|
|
397
538
|
this._validateResponse(response);
|
|
398
539
|
return response;
|
|
399
540
|
}
|
|
@@ -414,30 +555,77 @@
|
|
|
414
555
|
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
415
556
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
416
557
|
*
|
|
417
|
-
* const result = await client.cWithdraw({ wei:
|
|
558
|
+
* const result = await client.cWithdraw({ wei: 1 * 1e8 });
|
|
418
559
|
* ```
|
|
419
560
|
*/
|
|
420
561
|
async cWithdraw(args, signal) {
|
|
562
|
+
// Construct an action
|
|
421
563
|
const action = {
|
|
422
564
|
...args,
|
|
423
565
|
type: "cWithdraw",
|
|
424
566
|
hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
|
|
425
|
-
signatureChainId:
|
|
567
|
+
signatureChainId: this.signatureChainId,
|
|
426
568
|
nonce: args.nonce ?? Date.now(),
|
|
427
569
|
};
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
{ name: "wei", type: "uint64" },
|
|
432
|
-
{ name: "nonce", type: "uint64" },
|
|
433
|
-
],
|
|
434
|
-
}, parseInt(action.signatureChainId, 16));
|
|
435
|
-
const request = {
|
|
570
|
+
// Sign the action
|
|
571
|
+
const signature = await (0, signing_js_1.signUserSignedAction)({
|
|
572
|
+
wallet: this.wallet,
|
|
436
573
|
action,
|
|
437
|
-
|
|
438
|
-
|
|
574
|
+
types: {
|
|
575
|
+
"HyperliquidTransaction:CWithdraw": [
|
|
576
|
+
{ name: "hyperliquidChain", type: "string" },
|
|
577
|
+
{ name: "wei", type: "uint64" },
|
|
578
|
+
{ name: "nonce", type: "uint64" },
|
|
579
|
+
],
|
|
580
|
+
},
|
|
581
|
+
chainId: parseInt(action.signatureChainId, 16),
|
|
582
|
+
});
|
|
583
|
+
// Send a request
|
|
584
|
+
const request = { action, signature, nonce: action.nonce };
|
|
585
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
586
|
+
// Validate a response
|
|
587
|
+
this._validateResponse(response);
|
|
588
|
+
return response;
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Configure block type for EVM transactions.
|
|
592
|
+
* @param args - The parameters for the request.
|
|
593
|
+
* @param signal - An optional abort signal.
|
|
594
|
+
* @returns Response for creating a sub-account.
|
|
595
|
+
* @throws {ApiRequestError} When the API returns an error response.
|
|
596
|
+
*
|
|
597
|
+
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/evm/dual-block-architecture
|
|
598
|
+
* @example
|
|
599
|
+
* ```ts
|
|
600
|
+
* import * as hl from "@nktkas/hyperliquid";
|
|
601
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
602
|
+
*
|
|
603
|
+
* const wallet = privateKeyToAccount("0x...");
|
|
604
|
+
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
605
|
+
* const client = new hl.WalletClient({ wallet, transport });
|
|
606
|
+
*
|
|
607
|
+
* const result = await client.evmUserModify({ usingBigBlocks: true });
|
|
608
|
+
* ```
|
|
609
|
+
*/
|
|
610
|
+
async evmUserModify(args, signal) {
|
|
611
|
+
// Destructure the parameters
|
|
612
|
+
const { nonce = Date.now(), ...actionArgs } = args;
|
|
613
|
+
// Construct an action
|
|
614
|
+
const action = {
|
|
615
|
+
type: "evmUserModify",
|
|
616
|
+
usingBigBlocks: actionArgs.usingBigBlocks,
|
|
439
617
|
};
|
|
440
|
-
|
|
618
|
+
// Sign the action
|
|
619
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
620
|
+
wallet: this.wallet,
|
|
621
|
+
action,
|
|
622
|
+
nonce,
|
|
623
|
+
isTestnet: this.isTestnet,
|
|
624
|
+
});
|
|
625
|
+
// Send a request
|
|
626
|
+
const request = { action, signature, nonce };
|
|
627
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
628
|
+
// Validate a response
|
|
441
629
|
this._validateResponse(response);
|
|
442
630
|
return response;
|
|
443
631
|
}
|
|
@@ -462,15 +650,24 @@
|
|
|
462
650
|
* ```
|
|
463
651
|
*/
|
|
464
652
|
async createSubAccount(args, signal) {
|
|
653
|
+
// Destructure the parameters
|
|
465
654
|
const { nonce = Date.now(), ...actionArgs } = args;
|
|
466
|
-
|
|
467
|
-
const
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
signature,
|
|
471
|
-
nonce,
|
|
655
|
+
// Construct an action
|
|
656
|
+
const action = {
|
|
657
|
+
type: "createSubAccount",
|
|
658
|
+
name: actionArgs.name,
|
|
472
659
|
};
|
|
473
|
-
|
|
660
|
+
// Sign the action
|
|
661
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
662
|
+
wallet: this.wallet,
|
|
663
|
+
action,
|
|
664
|
+
nonce,
|
|
665
|
+
isTestnet: this.isTestnet,
|
|
666
|
+
});
|
|
667
|
+
// Send a request
|
|
668
|
+
const request = { action, signature, nonce };
|
|
669
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
670
|
+
// Validate a response
|
|
474
671
|
this._validateResponse(response);
|
|
475
672
|
return response;
|
|
476
673
|
}
|
|
@@ -510,16 +707,48 @@
|
|
|
510
707
|
* ```
|
|
511
708
|
*/
|
|
512
709
|
async modify(args, signal) {
|
|
710
|
+
// Destructure the parameters
|
|
513
711
|
const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
|
|
514
|
-
|
|
515
|
-
const
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
712
|
+
// Construct an action
|
|
713
|
+
const action = {
|
|
714
|
+
type: "modify",
|
|
715
|
+
oid: actionArgs.oid,
|
|
716
|
+
order: {
|
|
717
|
+
a: actionArgs.order.a,
|
|
718
|
+
b: actionArgs.order.b,
|
|
719
|
+
p: actionArgs.order.p,
|
|
720
|
+
s: actionArgs.order.s,
|
|
721
|
+
r: actionArgs.order.r,
|
|
722
|
+
t: "limit" in actionArgs.order.t
|
|
723
|
+
? {
|
|
724
|
+
limit: {
|
|
725
|
+
tif: actionArgs.order.t.limit.tif,
|
|
726
|
+
},
|
|
727
|
+
}
|
|
728
|
+
: {
|
|
729
|
+
trigger: {
|
|
730
|
+
isMarket: actionArgs.order.t.trigger.isMarket,
|
|
731
|
+
triggerPx: actionArgs.order.t.trigger.triggerPx,
|
|
732
|
+
tpsl: actionArgs.order.t.trigger.tpsl,
|
|
733
|
+
},
|
|
734
|
+
},
|
|
735
|
+
c: actionArgs.order.c,
|
|
736
|
+
},
|
|
737
|
+
};
|
|
738
|
+
if (action.order.c === undefined)
|
|
739
|
+
delete action.order.c;
|
|
740
|
+
// Sign the action
|
|
741
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
742
|
+
wallet: this.wallet,
|
|
743
|
+
action,
|
|
519
744
|
nonce,
|
|
745
|
+
isTestnet: this.isTestnet,
|
|
520
746
|
vaultAddress,
|
|
521
|
-
};
|
|
522
|
-
|
|
747
|
+
});
|
|
748
|
+
// Send a request
|
|
749
|
+
const request = { action, signature, nonce, vaultAddress };
|
|
750
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
751
|
+
// Validate a response
|
|
523
752
|
this._validateResponse(response);
|
|
524
753
|
return response;
|
|
525
754
|
}
|
|
@@ -559,19 +788,59 @@
|
|
|
559
788
|
* ```
|
|
560
789
|
*/
|
|
561
790
|
async order(args, signal) {
|
|
562
|
-
|
|
563
|
-
const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } =
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
791
|
+
// Destructure the parameters
|
|
792
|
+
const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
|
|
793
|
+
// Construct an action
|
|
794
|
+
const action = {
|
|
795
|
+
type: "order",
|
|
796
|
+
orders: actionArgs.orders.map((order) => {
|
|
797
|
+
const sortedOrder = {
|
|
798
|
+
a: order.a,
|
|
799
|
+
b: order.b,
|
|
800
|
+
p: order.p,
|
|
801
|
+
s: order.s,
|
|
802
|
+
r: order.r,
|
|
803
|
+
t: "limit" in order.t
|
|
804
|
+
? {
|
|
805
|
+
limit: {
|
|
806
|
+
tif: order.t.limit.tif,
|
|
807
|
+
},
|
|
808
|
+
}
|
|
809
|
+
: {
|
|
810
|
+
trigger: {
|
|
811
|
+
isMarket: order.t.trigger.isMarket,
|
|
812
|
+
triggerPx: order.t.trigger.triggerPx,
|
|
813
|
+
tpsl: order.t.trigger.tpsl,
|
|
814
|
+
},
|
|
815
|
+
},
|
|
816
|
+
c: order.c,
|
|
817
|
+
};
|
|
818
|
+
if (order.c === undefined)
|
|
819
|
+
delete sortedOrder.c;
|
|
820
|
+
return sortedOrder;
|
|
821
|
+
}),
|
|
822
|
+
grouping: actionArgs.grouping,
|
|
823
|
+
builder: actionArgs.builder
|
|
824
|
+
? {
|
|
825
|
+
b: actionArgs.builder.b.toLowerCase(),
|
|
826
|
+
f: actionArgs.builder.f,
|
|
827
|
+
}
|
|
828
|
+
: actionArgs.builder,
|
|
829
|
+
};
|
|
830
|
+
if (action.builder === undefined)
|
|
831
|
+
delete action.builder;
|
|
832
|
+
// Sign the action
|
|
833
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
834
|
+
wallet: this.wallet,
|
|
835
|
+
action,
|
|
571
836
|
nonce,
|
|
837
|
+
isTestnet: this.isTestnet,
|
|
572
838
|
vaultAddress,
|
|
573
|
-
};
|
|
574
|
-
|
|
839
|
+
});
|
|
840
|
+
// Send a request
|
|
841
|
+
const request = { action, signature, nonce, vaultAddress };
|
|
842
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
843
|
+
// Validate a response
|
|
575
844
|
this._validateResponse(response);
|
|
576
845
|
return response;
|
|
577
846
|
}
|
|
@@ -596,16 +865,69 @@
|
|
|
596
865
|
* ```
|
|
597
866
|
*/
|
|
598
867
|
async scheduleCancel(args = {}, signal) {
|
|
868
|
+
// Destructure the parameters
|
|
599
869
|
const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
|
|
600
|
-
|
|
601
|
-
const
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
870
|
+
// Construct an action
|
|
871
|
+
const action = {
|
|
872
|
+
type: "scheduleCancel",
|
|
873
|
+
time: actionArgs.time,
|
|
874
|
+
};
|
|
875
|
+
if (action.time === undefined)
|
|
876
|
+
delete action.time;
|
|
877
|
+
// Sign the action
|
|
878
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
879
|
+
wallet: this.wallet,
|
|
880
|
+
action,
|
|
605
881
|
nonce,
|
|
882
|
+
isTestnet: this.isTestnet,
|
|
606
883
|
vaultAddress,
|
|
884
|
+
});
|
|
885
|
+
// Send a request
|
|
886
|
+
const request = { action, signature, nonce, vaultAddress };
|
|
887
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
888
|
+
// Validate a response
|
|
889
|
+
this._validateResponse(response);
|
|
890
|
+
return response;
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* Set the display name in the leaderboard.
|
|
894
|
+
* @param args - The parameters for the request.
|
|
895
|
+
* @param signal - An optional abort signal.
|
|
896
|
+
* @returns Successful response without specific data.
|
|
897
|
+
* @throws {ApiRequestError} When the API returns an error response.
|
|
898
|
+
*
|
|
899
|
+
* @see null - no documentation
|
|
900
|
+
* @example
|
|
901
|
+
* ```ts
|
|
902
|
+
* import * as hl from "@nktkas/hyperliquid";
|
|
903
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
904
|
+
*
|
|
905
|
+
* const wallet = privateKeyToAccount("0x...");
|
|
906
|
+
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
907
|
+
* const client = new hl.WalletClient({ wallet, transport });
|
|
908
|
+
*
|
|
909
|
+
* const result = await client.setDisplayName({ displayName: "My Name" });
|
|
910
|
+
* ```
|
|
911
|
+
*/
|
|
912
|
+
async setDisplayName(args, signal) {
|
|
913
|
+
// Destructure the parameters
|
|
914
|
+
const { nonce = Date.now(), ...actionArgs } = args;
|
|
915
|
+
// Construct an action
|
|
916
|
+
const action = {
|
|
917
|
+
type: "setDisplayName",
|
|
918
|
+
displayName: actionArgs.displayName,
|
|
607
919
|
};
|
|
608
|
-
|
|
920
|
+
// Sign the action
|
|
921
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
922
|
+
wallet: this.wallet,
|
|
923
|
+
action,
|
|
924
|
+
nonce,
|
|
925
|
+
isTestnet: this.isTestnet,
|
|
926
|
+
});
|
|
927
|
+
// Send a request
|
|
928
|
+
const request = { action, signature, nonce };
|
|
929
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
930
|
+
// Validate a response
|
|
609
931
|
this._validateResponse(response);
|
|
610
932
|
return response;
|
|
611
933
|
}
|
|
@@ -630,15 +952,24 @@
|
|
|
630
952
|
* ```
|
|
631
953
|
*/
|
|
632
954
|
async setReferrer(args, signal) {
|
|
955
|
+
// Destructure the parameters
|
|
633
956
|
const { nonce = Date.now(), ...actionArgs } = args;
|
|
634
|
-
|
|
635
|
-
const
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
signature,
|
|
639
|
-
nonce,
|
|
957
|
+
// Construct an action
|
|
958
|
+
const action = {
|
|
959
|
+
type: "setReferrer",
|
|
960
|
+
code: actionArgs.code,
|
|
640
961
|
};
|
|
641
|
-
|
|
962
|
+
// Sign the action
|
|
963
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
964
|
+
wallet: this.wallet,
|
|
965
|
+
action,
|
|
966
|
+
nonce,
|
|
967
|
+
isTestnet: this.isTestnet,
|
|
968
|
+
});
|
|
969
|
+
// Send a request
|
|
970
|
+
const request = { action, signature, nonce };
|
|
971
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
972
|
+
// Validate a response
|
|
642
973
|
this._validateResponse(response);
|
|
643
974
|
return response;
|
|
644
975
|
}
|
|
@@ -667,33 +998,134 @@
|
|
|
667
998
|
* ```
|
|
668
999
|
*/
|
|
669
1000
|
async spotSend(args, signal) {
|
|
1001
|
+
// Construct an action
|
|
670
1002
|
const action = {
|
|
671
1003
|
...args,
|
|
672
1004
|
type: "spotSend",
|
|
673
1005
|
hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
|
|
674
|
-
signatureChainId:
|
|
1006
|
+
signatureChainId: this.signatureChainId,
|
|
675
1007
|
time: args.time ?? Date.now(),
|
|
676
1008
|
};
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
1009
|
+
// Sign the action
|
|
1010
|
+
const signature = await (0, signing_js_1.signUserSignedAction)({
|
|
1011
|
+
wallet: this.wallet,
|
|
1012
|
+
action,
|
|
1013
|
+
types: {
|
|
1014
|
+
"HyperliquidTransaction:SpotSend": [
|
|
1015
|
+
{ name: "hyperliquidChain", type: "string" },
|
|
1016
|
+
{ name: "destination", type: "string" },
|
|
1017
|
+
{ name: "token", type: "string" },
|
|
1018
|
+
{ name: "amount", type: "string" },
|
|
1019
|
+
{ name: "time", type: "uint64" },
|
|
1020
|
+
],
|
|
1021
|
+
},
|
|
1022
|
+
chainId: parseInt(action.signatureChainId, 16),
|
|
1023
|
+
});
|
|
1024
|
+
// Send a request
|
|
1025
|
+
const request = { action, signature, nonce: action.time };
|
|
1026
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1027
|
+
// Validate a response
|
|
1028
|
+
this._validateResponse(response);
|
|
1029
|
+
return response;
|
|
1030
|
+
}
|
|
1031
|
+
/**
|
|
1032
|
+
* Opt Out of Spot Dusting.
|
|
1033
|
+
* @param args - The parameters for the request.
|
|
1034
|
+
* @param signal - An optional abort signal.
|
|
1035
|
+
* @returns Successful response without specific data.
|
|
1036
|
+
* @throws {ApiRequestError} When the API returns an error response.
|
|
1037
|
+
*
|
|
1038
|
+
* @see null - no documentation
|
|
1039
|
+
* @example
|
|
1040
|
+
* ```ts
|
|
1041
|
+
* import * as hl from "@nktkas/hyperliquid";
|
|
1042
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
1043
|
+
*
|
|
1044
|
+
* const wallet = privateKeyToAccount("0x...");
|
|
1045
|
+
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
1046
|
+
* const client = new hl.WalletClient({ wallet, transport });
|
|
1047
|
+
*
|
|
1048
|
+
* const result = await client.spotUser({
|
|
1049
|
+
* toggleSpotDusting: { optOut: false },
|
|
1050
|
+
* });
|
|
1051
|
+
* ```
|
|
1052
|
+
*/
|
|
1053
|
+
async spotUser(args, signal) {
|
|
1054
|
+
// Destructure the parameters
|
|
1055
|
+
const { nonce = Date.now(), ...actionArgs } = args;
|
|
1056
|
+
// Construct an action
|
|
1057
|
+
const action = {
|
|
1058
|
+
type: "spotUser",
|
|
1059
|
+
toggleSpotDusting: {
|
|
1060
|
+
optOut: actionArgs.toggleSpotDusting.optOut,
|
|
1061
|
+
},
|
|
1062
|
+
};
|
|
1063
|
+
// Sign the action
|
|
1064
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
1065
|
+
wallet: this.wallet,
|
|
687
1066
|
action,
|
|
688
|
-
|
|
689
|
-
|
|
1067
|
+
nonce,
|
|
1068
|
+
isTestnet: this.isTestnet,
|
|
1069
|
+
});
|
|
1070
|
+
// Send a request
|
|
1071
|
+
const request = { action, signature, nonce };
|
|
1072
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1073
|
+
// Validate a response
|
|
1074
|
+
this._validateResponse(response);
|
|
1075
|
+
return response;
|
|
1076
|
+
}
|
|
1077
|
+
/**
|
|
1078
|
+
* Transfer between sub-accounts (spot).
|
|
1079
|
+
* @param args - The parameters for the request.
|
|
1080
|
+
* @param signal - An optional abort signal.
|
|
1081
|
+
* @returns Successful response without specific data.
|
|
1082
|
+
* @throws {ApiRequestError} When the API returns an error response.
|
|
1083
|
+
*
|
|
1084
|
+
* @see null - no documentation
|
|
1085
|
+
* @example
|
|
1086
|
+
* ```ts
|
|
1087
|
+
* import * as hl from "@nktkas/hyperliquid";
|
|
1088
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
1089
|
+
*
|
|
1090
|
+
* const wallet = privateKeyToAccount("0x...");
|
|
1091
|
+
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
1092
|
+
* const client = new hl.WalletClient({ wallet, transport });
|
|
1093
|
+
*
|
|
1094
|
+
* const result = await client.subAccountSpotTransfer({
|
|
1095
|
+
* subAccountUser: "0x...",
|
|
1096
|
+
* isDeposit: true,
|
|
1097
|
+
* token: "USDC:0xeb62eee3685fc4c43992febcd9e75443",
|
|
1098
|
+
* amount: "1",
|
|
1099
|
+
* });
|
|
1100
|
+
* ```
|
|
1101
|
+
*/
|
|
1102
|
+
async subAccountSpotTransfer(args, signal) {
|
|
1103
|
+
// Destructure the parameters
|
|
1104
|
+
const { nonce = Date.now(), ...actionArgs } = args;
|
|
1105
|
+
// Construct an action
|
|
1106
|
+
const action = {
|
|
1107
|
+
type: "subAccountSpotTransfer",
|
|
1108
|
+
subAccountUser: actionArgs.subAccountUser,
|
|
1109
|
+
isDeposit: actionArgs.isDeposit,
|
|
1110
|
+
token: actionArgs.token,
|
|
1111
|
+
amount: actionArgs.amount,
|
|
690
1112
|
};
|
|
691
|
-
|
|
1113
|
+
// Sign the action
|
|
1114
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
1115
|
+
wallet: this.wallet,
|
|
1116
|
+
action,
|
|
1117
|
+
nonce,
|
|
1118
|
+
isTestnet: this.isTestnet,
|
|
1119
|
+
});
|
|
1120
|
+
// Send a request
|
|
1121
|
+
const request = { action, signature, nonce };
|
|
1122
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1123
|
+
// Validate a response
|
|
692
1124
|
this._validateResponse(response);
|
|
693
1125
|
return response;
|
|
694
1126
|
}
|
|
695
1127
|
/**
|
|
696
|
-
* Transfer between sub-accounts.
|
|
1128
|
+
* Transfer between sub-accounts (perpetual).
|
|
697
1129
|
* @param args - The parameters for the request.
|
|
698
1130
|
* @param signal - An optional abort signal.
|
|
699
1131
|
* @returns Successful response without specific data.
|
|
@@ -712,20 +1144,31 @@
|
|
|
712
1144
|
* const result = await client.subAccountTransfer({
|
|
713
1145
|
* subAccountUser: "0x...",
|
|
714
1146
|
* isDeposit: true,
|
|
715
|
-
* usd:
|
|
1147
|
+
* usd: 1 * 1e6,
|
|
716
1148
|
* });
|
|
717
1149
|
* ```
|
|
718
1150
|
*/
|
|
719
1151
|
async subAccountTransfer(args, signal) {
|
|
1152
|
+
// Destructure the parameters
|
|
720
1153
|
const { nonce = Date.now(), ...actionArgs } = args;
|
|
721
|
-
|
|
722
|
-
const
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
1154
|
+
// Construct an action
|
|
1155
|
+
const action = {
|
|
1156
|
+
type: "subAccountTransfer",
|
|
1157
|
+
subAccountUser: actionArgs.subAccountUser,
|
|
1158
|
+
isDeposit: actionArgs.isDeposit,
|
|
1159
|
+
usd: actionArgs.usd,
|
|
727
1160
|
};
|
|
728
|
-
|
|
1161
|
+
// Sign the action
|
|
1162
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
1163
|
+
wallet: this.wallet,
|
|
1164
|
+
action,
|
|
1165
|
+
nonce,
|
|
1166
|
+
isTestnet: this.isTestnet,
|
|
1167
|
+
});
|
|
1168
|
+
// Send a request
|
|
1169
|
+
const request = { action, signature, nonce };
|
|
1170
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1171
|
+
// Validate a response
|
|
729
1172
|
this._validateResponse(response);
|
|
730
1173
|
return response;
|
|
731
1174
|
}
|
|
@@ -749,33 +1192,38 @@
|
|
|
749
1192
|
* const result = await client.tokenDelegate({
|
|
750
1193
|
* validator: "0x...",
|
|
751
1194
|
* isUndelegate: true,
|
|
752
|
-
* wei:
|
|
1195
|
+
* wei: 1 * 1e8,
|
|
753
1196
|
* });
|
|
754
1197
|
* ```
|
|
755
1198
|
*/
|
|
756
1199
|
async tokenDelegate(args, signal) {
|
|
1200
|
+
// Construct an action
|
|
757
1201
|
const action = {
|
|
758
1202
|
...args,
|
|
759
1203
|
type: "tokenDelegate",
|
|
760
1204
|
hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
|
|
761
|
-
signatureChainId:
|
|
1205
|
+
signatureChainId: this.signatureChainId,
|
|
762
1206
|
nonce: args.nonce ?? Date.now(),
|
|
763
1207
|
};
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
{ name: "validator", type: "address" },
|
|
768
|
-
{ name: "wei", type: "uint64" },
|
|
769
|
-
{ name: "isUndelegate", type: "bool" },
|
|
770
|
-
{ name: "nonce", type: "uint64" },
|
|
771
|
-
],
|
|
772
|
-
}, parseInt(action.signatureChainId, 16));
|
|
773
|
-
const request = {
|
|
1208
|
+
// Sign the action
|
|
1209
|
+
const signature = await (0, signing_js_1.signUserSignedAction)({
|
|
1210
|
+
wallet: this.wallet,
|
|
774
1211
|
action,
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
1212
|
+
types: {
|
|
1213
|
+
"HyperliquidTransaction:TokenDelegate": [
|
|
1214
|
+
{ name: "hyperliquidChain", type: "string" },
|
|
1215
|
+
{ name: "validator", type: "address" },
|
|
1216
|
+
{ name: "wei", type: "uint64" },
|
|
1217
|
+
{ name: "isUndelegate", type: "bool" },
|
|
1218
|
+
{ name: "nonce", type: "uint64" },
|
|
1219
|
+
],
|
|
1220
|
+
},
|
|
1221
|
+
chainId: parseInt(action.signatureChainId, 16),
|
|
1222
|
+
});
|
|
1223
|
+
// Send a request
|
|
1224
|
+
const request = { action, signature, nonce: action.nonce };
|
|
1225
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1226
|
+
// Validate a response
|
|
779
1227
|
this._validateResponse(response);
|
|
780
1228
|
return response;
|
|
781
1229
|
}
|
|
@@ -803,16 +1251,26 @@
|
|
|
803
1251
|
* ```
|
|
804
1252
|
*/
|
|
805
1253
|
async twapCancel(args, signal) {
|
|
1254
|
+
// Destructure the parameters
|
|
806
1255
|
const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
|
|
807
|
-
|
|
808
|
-
const
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
1256
|
+
// Construct an action
|
|
1257
|
+
const action = {
|
|
1258
|
+
type: "twapCancel",
|
|
1259
|
+
a: actionArgs.a,
|
|
1260
|
+
t: actionArgs.t,
|
|
1261
|
+
};
|
|
1262
|
+
// Sign the action
|
|
1263
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
1264
|
+
wallet: this.wallet,
|
|
1265
|
+
action,
|
|
812
1266
|
nonce,
|
|
1267
|
+
isTestnet: this.isTestnet,
|
|
813
1268
|
vaultAddress,
|
|
814
|
-
};
|
|
815
|
-
|
|
1269
|
+
});
|
|
1270
|
+
// Send a request
|
|
1271
|
+
const request = { action, signature, nonce, vaultAddress };
|
|
1272
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1273
|
+
// Validate a response
|
|
816
1274
|
this._validateResponse(response);
|
|
817
1275
|
return response;
|
|
818
1276
|
}
|
|
@@ -844,16 +1302,32 @@
|
|
|
844
1302
|
* ```
|
|
845
1303
|
*/
|
|
846
1304
|
async twapOrder(args, signal) {
|
|
1305
|
+
// Destructure the parameters
|
|
847
1306
|
const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
|
|
848
|
-
|
|
849
|
-
const
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
1307
|
+
// Construct an action
|
|
1308
|
+
const action = {
|
|
1309
|
+
type: "twapOrder",
|
|
1310
|
+
twap: {
|
|
1311
|
+
a: actionArgs.a,
|
|
1312
|
+
b: actionArgs.b,
|
|
1313
|
+
s: actionArgs.s,
|
|
1314
|
+
r: actionArgs.r,
|
|
1315
|
+
m: actionArgs.m,
|
|
1316
|
+
t: actionArgs.t,
|
|
1317
|
+
},
|
|
1318
|
+
};
|
|
1319
|
+
// Sign the action
|
|
1320
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
1321
|
+
wallet: this.wallet,
|
|
1322
|
+
action,
|
|
853
1323
|
nonce,
|
|
1324
|
+
isTestnet: this.isTestnet,
|
|
854
1325
|
vaultAddress,
|
|
855
|
-
};
|
|
856
|
-
|
|
1326
|
+
});
|
|
1327
|
+
// Send a request
|
|
1328
|
+
const request = { action, signature, nonce, vaultAddress };
|
|
1329
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1330
|
+
// Validate a response
|
|
857
1331
|
this._validateResponse(response);
|
|
858
1332
|
return response;
|
|
859
1333
|
}
|
|
@@ -877,21 +1351,32 @@
|
|
|
877
1351
|
* const result = await client.updateIsolatedMargin({
|
|
878
1352
|
* asset: 0,
|
|
879
1353
|
* isBuy: true, // Add to long position
|
|
880
|
-
* ntli:
|
|
1354
|
+
* ntli: 1, // Add 1 USD margin (integer only)
|
|
881
1355
|
* });
|
|
882
1356
|
* ```
|
|
883
1357
|
*/
|
|
884
1358
|
async updateIsolatedMargin(args, signal) {
|
|
1359
|
+
// Destructure the parameters
|
|
885
1360
|
const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
|
|
886
|
-
|
|
887
|
-
const
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
1361
|
+
// Construct an action
|
|
1362
|
+
const action = {
|
|
1363
|
+
type: "updateIsolatedMargin",
|
|
1364
|
+
asset: actionArgs.asset,
|
|
1365
|
+
isBuy: actionArgs.isBuy,
|
|
1366
|
+
ntli: actionArgs.ntli,
|
|
1367
|
+
};
|
|
1368
|
+
// Sign the action
|
|
1369
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
1370
|
+
wallet: this.wallet,
|
|
1371
|
+
action,
|
|
891
1372
|
nonce,
|
|
1373
|
+
isTestnet: this.isTestnet,
|
|
892
1374
|
vaultAddress,
|
|
893
|
-
};
|
|
894
|
-
|
|
1375
|
+
});
|
|
1376
|
+
// Send a request
|
|
1377
|
+
const request = { action, signature, nonce, vaultAddress };
|
|
1378
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1379
|
+
// Validate a response
|
|
895
1380
|
this._validateResponse(response);
|
|
896
1381
|
return response;
|
|
897
1382
|
}
|
|
@@ -920,16 +1405,27 @@
|
|
|
920
1405
|
* ```
|
|
921
1406
|
*/
|
|
922
1407
|
async updateLeverage(args, signal) {
|
|
1408
|
+
// Destructure the parameters
|
|
923
1409
|
const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
|
|
924
|
-
|
|
925
|
-
const
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
1410
|
+
// Construct an action
|
|
1411
|
+
const action = {
|
|
1412
|
+
type: "updateLeverage",
|
|
1413
|
+
asset: actionArgs.asset,
|
|
1414
|
+
isCross: actionArgs.isCross,
|
|
1415
|
+
leverage: actionArgs.leverage,
|
|
1416
|
+
};
|
|
1417
|
+
// Sign the action
|
|
1418
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
1419
|
+
wallet: this.wallet,
|
|
1420
|
+
action,
|
|
929
1421
|
nonce,
|
|
1422
|
+
isTestnet: this.isTestnet,
|
|
930
1423
|
vaultAddress,
|
|
931
|
-
};
|
|
932
|
-
|
|
1424
|
+
});
|
|
1425
|
+
// Send a request
|
|
1426
|
+
const request = { action, signature, nonce, vaultAddress };
|
|
1427
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1428
|
+
// Validate a response
|
|
933
1429
|
this._validateResponse(response);
|
|
934
1430
|
return response;
|
|
935
1431
|
}
|
|
@@ -951,33 +1447,38 @@
|
|
|
951
1447
|
* const client = new hl.WalletClient({ wallet, transport });
|
|
952
1448
|
*
|
|
953
1449
|
* const result = await client.usdClassTransfer({
|
|
954
|
-
* amount: "
|
|
1450
|
+
* amount: "1",
|
|
955
1451
|
* toPerp: true, // Transfer from Spot to Perp
|
|
956
1452
|
* });
|
|
957
1453
|
* ```
|
|
958
1454
|
*/
|
|
959
1455
|
async usdClassTransfer(args, signal) {
|
|
1456
|
+
// Construct an action
|
|
960
1457
|
const action = {
|
|
961
1458
|
...args,
|
|
962
1459
|
type: "usdClassTransfer",
|
|
963
1460
|
hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
|
|
964
|
-
signatureChainId:
|
|
1461
|
+
signatureChainId: this.signatureChainId,
|
|
965
1462
|
nonce: args.nonce ?? Date.now(),
|
|
966
1463
|
};
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
{ name: "amount", type: "string" },
|
|
971
|
-
{ name: "toPerp", type: "bool" },
|
|
972
|
-
{ name: "nonce", type: "uint64" },
|
|
973
|
-
],
|
|
974
|
-
}, parseInt(action.signatureChainId, 16));
|
|
975
|
-
const request = {
|
|
1464
|
+
// Sign the action
|
|
1465
|
+
const signature = await (0, signing_js_1.signUserSignedAction)({
|
|
1466
|
+
wallet: this.wallet,
|
|
976
1467
|
action,
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
1468
|
+
types: {
|
|
1469
|
+
"HyperliquidTransaction:UsdClassTransfer": [
|
|
1470
|
+
{ name: "hyperliquidChain", type: "string" },
|
|
1471
|
+
{ name: "amount", type: "string" },
|
|
1472
|
+
{ name: "toPerp", type: "bool" },
|
|
1473
|
+
{ name: "nonce", type: "uint64" },
|
|
1474
|
+
],
|
|
1475
|
+
},
|
|
1476
|
+
chainId: parseInt(action.signatureChainId, 16),
|
|
1477
|
+
});
|
|
1478
|
+
// Send a request
|
|
1479
|
+
const request = { action, signature, nonce: action.nonce };
|
|
1480
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1481
|
+
// Validate a response
|
|
981
1482
|
this._validateResponse(response);
|
|
982
1483
|
return response;
|
|
983
1484
|
}
|
|
@@ -1000,32 +1501,131 @@
|
|
|
1000
1501
|
*
|
|
1001
1502
|
* const result = await client.usdSend({
|
|
1002
1503
|
* destination: "0x...",
|
|
1003
|
-
* amount: "
|
|
1504
|
+
* amount: "1",
|
|
1004
1505
|
* });
|
|
1005
1506
|
* ```
|
|
1006
1507
|
*/
|
|
1007
1508
|
async usdSend(args, signal) {
|
|
1509
|
+
// Construct an action
|
|
1008
1510
|
const action = {
|
|
1009
1511
|
...args,
|
|
1010
1512
|
type: "usdSend",
|
|
1011
1513
|
hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
|
|
1012
|
-
signatureChainId:
|
|
1514
|
+
signatureChainId: this.signatureChainId,
|
|
1013
1515
|
time: args.time ?? Date.now(),
|
|
1014
1516
|
};
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
{ name: "destination", type: "string" },
|
|
1019
|
-
{ name: "amount", type: "string" },
|
|
1020
|
-
{ name: "time", type: "uint64" },
|
|
1021
|
-
],
|
|
1022
|
-
}, parseInt(action.signatureChainId, 16));
|
|
1023
|
-
const request = {
|
|
1517
|
+
// Sign the action
|
|
1518
|
+
const signature = await (0, signing_js_1.signUserSignedAction)({
|
|
1519
|
+
wallet: this.wallet,
|
|
1024
1520
|
action,
|
|
1025
|
-
|
|
1026
|
-
|
|
1521
|
+
types: {
|
|
1522
|
+
"HyperliquidTransaction:UsdSend": [
|
|
1523
|
+
{ name: "hyperliquidChain", type: "string" },
|
|
1524
|
+
{ name: "destination", type: "string" },
|
|
1525
|
+
{ name: "amount", type: "string" },
|
|
1526
|
+
{ name: "time", type: "uint64" },
|
|
1527
|
+
],
|
|
1528
|
+
},
|
|
1529
|
+
chainId: parseInt(action.signatureChainId, 16),
|
|
1530
|
+
});
|
|
1531
|
+
// Send a request
|
|
1532
|
+
const request = { action, signature, nonce: action.time };
|
|
1533
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1534
|
+
// Validate a response
|
|
1535
|
+
this._validateResponse(response);
|
|
1536
|
+
return response;
|
|
1537
|
+
}
|
|
1538
|
+
/**
|
|
1539
|
+
* Distribute funds from a vault between followers.
|
|
1540
|
+
* @param args - The parameters for the request.
|
|
1541
|
+
* @param signal - An optional abort signal.
|
|
1542
|
+
* @returns Successful response without specific data.
|
|
1543
|
+
* @throws {ApiRequestError} When the API returns an error response.
|
|
1544
|
+
*
|
|
1545
|
+
* @see null - no documentation
|
|
1546
|
+
* @example
|
|
1547
|
+
* ```ts
|
|
1548
|
+
* import * as hl from "@nktkas/hyperliquid";
|
|
1549
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
1550
|
+
*
|
|
1551
|
+
* const wallet = privateKeyToAccount("0x...");
|
|
1552
|
+
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
1553
|
+
* const client = new hl.WalletClient({ wallet, transport });
|
|
1554
|
+
*
|
|
1555
|
+
* const result = await client.vaultDistribute({
|
|
1556
|
+
* vaultAddress: "0x...",
|
|
1557
|
+
* usd: 10 * 1e6,
|
|
1558
|
+
* });
|
|
1559
|
+
* ```
|
|
1560
|
+
*/
|
|
1561
|
+
async vaultDistribute(args, signal) {
|
|
1562
|
+
// Destructure the parameters
|
|
1563
|
+
const { nonce = Date.now(), ...actionArgs } = args;
|
|
1564
|
+
// Construct an action
|
|
1565
|
+
const action = {
|
|
1566
|
+
type: "vaultDistribute",
|
|
1567
|
+
vaultAddress: actionArgs.vaultAddress,
|
|
1568
|
+
usd: actionArgs.usd,
|
|
1027
1569
|
};
|
|
1028
|
-
|
|
1570
|
+
// Sign the action
|
|
1571
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
1572
|
+
wallet: this.wallet,
|
|
1573
|
+
action,
|
|
1574
|
+
nonce,
|
|
1575
|
+
isTestnet: this.isTestnet,
|
|
1576
|
+
});
|
|
1577
|
+
// Send a request
|
|
1578
|
+
const request = { action, signature, nonce };
|
|
1579
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1580
|
+
// Validate a response
|
|
1581
|
+
this._validateResponse(response);
|
|
1582
|
+
return response;
|
|
1583
|
+
}
|
|
1584
|
+
/**
|
|
1585
|
+
* Modify a vault's configuration.
|
|
1586
|
+
* @param args - The parameters for the request.
|
|
1587
|
+
* @param signal - An optional abort signal.
|
|
1588
|
+
* @returns Successful response without specific data.
|
|
1589
|
+
* @throws {ApiRequestError} When the API returns an error response.
|
|
1590
|
+
*
|
|
1591
|
+
* @see null - no documentation
|
|
1592
|
+
* @example
|
|
1593
|
+
* ```ts
|
|
1594
|
+
* import * as hl from "@nktkas/hyperliquid";
|
|
1595
|
+
* import { privateKeyToAccount } from "viem/accounts";
|
|
1596
|
+
*
|
|
1597
|
+
* const wallet = privateKeyToAccount("0x...");
|
|
1598
|
+
* const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
1599
|
+
* const client = new hl.WalletClient({ wallet, transport });
|
|
1600
|
+
*
|
|
1601
|
+
* const result = await client.vaultModify({
|
|
1602
|
+
* vaultAddress: "0x...",
|
|
1603
|
+
* allowDeposits: true,
|
|
1604
|
+
* alwaysCloseOnWithdraw: false,
|
|
1605
|
+
* });
|
|
1606
|
+
* ```
|
|
1607
|
+
*/
|
|
1608
|
+
async vaultModify(args, signal) {
|
|
1609
|
+
// Destructure the parameters
|
|
1610
|
+
const { nonce = Date.now(), ...actionArgs } = args;
|
|
1611
|
+
// Construct an action
|
|
1612
|
+
const action = {
|
|
1613
|
+
type: "vaultModify",
|
|
1614
|
+
vaultAddress: actionArgs.vaultAddress,
|
|
1615
|
+
allowDeposits: actionArgs.allowDeposits,
|
|
1616
|
+
alwaysCloseOnWithdraw: actionArgs.alwaysCloseOnWithdraw,
|
|
1617
|
+
};
|
|
1618
|
+
// Sign the action
|
|
1619
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
1620
|
+
wallet: this.wallet,
|
|
1621
|
+
action,
|
|
1622
|
+
nonce,
|
|
1623
|
+
isTestnet: this.isTestnet,
|
|
1624
|
+
});
|
|
1625
|
+
// Send a request
|
|
1626
|
+
const request = { action, signature, nonce };
|
|
1627
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1628
|
+
// Validate a response
|
|
1029
1629
|
this._validateResponse(response);
|
|
1030
1630
|
return response;
|
|
1031
1631
|
}
|
|
@@ -1049,20 +1649,31 @@
|
|
|
1049
1649
|
* const result = await client.vaultTransfer({
|
|
1050
1650
|
* vaultAddress: "0x...",
|
|
1051
1651
|
* isDeposit: true,
|
|
1052
|
-
* usd:
|
|
1652
|
+
* usd: 10 * 1e6,
|
|
1053
1653
|
* });
|
|
1054
1654
|
* ```
|
|
1055
1655
|
*/
|
|
1056
1656
|
async vaultTransfer(args, signal) {
|
|
1657
|
+
// Destructure the parameters
|
|
1057
1658
|
const { nonce = Date.now(), ...actionArgs } = args;
|
|
1058
|
-
|
|
1059
|
-
const
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1659
|
+
// Construct an action
|
|
1660
|
+
const action = {
|
|
1661
|
+
type: "vaultTransfer",
|
|
1662
|
+
vaultAddress: actionArgs.vaultAddress,
|
|
1663
|
+
isDeposit: actionArgs.isDeposit,
|
|
1664
|
+
usd: actionArgs.usd,
|
|
1064
1665
|
};
|
|
1065
|
-
|
|
1666
|
+
// Sign the action
|
|
1667
|
+
const signature = await (0, signing_js_1.signL1Action)({
|
|
1668
|
+
wallet: this.wallet,
|
|
1669
|
+
action,
|
|
1670
|
+
nonce,
|
|
1671
|
+
isTestnet: this.isTestnet,
|
|
1672
|
+
});
|
|
1673
|
+
// Send a request
|
|
1674
|
+
const request = { action, signature, nonce };
|
|
1675
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1676
|
+
// Validate a response
|
|
1066
1677
|
this._validateResponse(response);
|
|
1067
1678
|
return response;
|
|
1068
1679
|
}
|
|
@@ -1085,32 +1696,37 @@
|
|
|
1085
1696
|
*
|
|
1086
1697
|
* const result = await client.withdraw3({
|
|
1087
1698
|
* destination: "0x...",
|
|
1088
|
-
* amount: "
|
|
1699
|
+
* amount: "1",
|
|
1089
1700
|
* });
|
|
1090
1701
|
* ```
|
|
1091
1702
|
*/
|
|
1092
1703
|
async withdraw3(args, signal) {
|
|
1704
|
+
// Construct an action
|
|
1093
1705
|
const action = {
|
|
1094
1706
|
...args,
|
|
1095
1707
|
type: "withdraw3",
|
|
1096
1708
|
hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
|
|
1097
|
-
signatureChainId:
|
|
1709
|
+
signatureChainId: this.signatureChainId,
|
|
1098
1710
|
time: args.time ?? Date.now(),
|
|
1099
1711
|
};
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
{ name: "destination", type: "string" },
|
|
1104
|
-
{ name: "amount", type: "string" },
|
|
1105
|
-
{ name: "time", type: "uint64" },
|
|
1106
|
-
],
|
|
1107
|
-
}, parseInt(action.signatureChainId, 16));
|
|
1108
|
-
const request = {
|
|
1712
|
+
// Sign the action
|
|
1713
|
+
const signature = await (0, signing_js_1.signUserSignedAction)({
|
|
1714
|
+
wallet: this.wallet,
|
|
1109
1715
|
action,
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1716
|
+
types: {
|
|
1717
|
+
"HyperliquidTransaction:Withdraw": [
|
|
1718
|
+
{ name: "hyperliquidChain", type: "string" },
|
|
1719
|
+
{ name: "destination", type: "string" },
|
|
1720
|
+
{ name: "amount", type: "string" },
|
|
1721
|
+
{ name: "time", type: "uint64" },
|
|
1722
|
+
],
|
|
1723
|
+
},
|
|
1724
|
+
chainId: parseInt(action.signatureChainId, 16),
|
|
1725
|
+
});
|
|
1726
|
+
// Send a request
|
|
1727
|
+
const request = { action, signature, nonce: action.time };
|
|
1728
|
+
const response = await this.transport.request("exchange", request, signal);
|
|
1729
|
+
// Validate a response
|
|
1114
1730
|
this._validateResponse(response);
|
|
1115
1731
|
return response;
|
|
1116
1732
|
}
|