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