@nktkas/hyperliquid 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/CONTRIBUTING.md +59 -0
  2. package/LICENSE +21 -0
  3. package/README.md +363 -0
  4. package/SECURITY.md +7 -0
  5. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +2 -0
  6. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +1 -0
  7. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +1 -0
  8. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +95 -0
  9. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +1 -0
  10. package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +10 -0
  11. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.d.ts +23 -0
  12. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.d.ts.map +1 -0
  13. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +43 -0
  14. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.d.ts +55 -0
  15. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.d.ts.map +1 -0
  16. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +65 -0
  17. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.d.ts +2 -0
  18. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.d.ts.map +1 -0
  19. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +1 -0
  20. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.d.ts +52 -0
  21. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.d.ts.map +1 -0
  22. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +283 -0
  23. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/utils.d.ts +120 -0
  24. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/utils.d.ts.map +1 -0
  25. package/esm/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +211 -0
  26. package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts +19 -0
  27. package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +1 -0
  28. package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.js +32 -0
  29. package/esm/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.d.ts +2 -0
  30. package/esm/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.d.ts.map +1 -0
  31. package/esm/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +26 -0
  32. package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts +37 -0
  33. package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +1 -0
  34. package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.js +109 -0
  35. package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts +37 -0
  36. package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +1 -0
  37. package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.js +237 -0
  38. package/esm/mod.d.ts +24 -0
  39. package/esm/mod.d.ts.map +1 -0
  40. package/esm/mod.js +9 -0
  41. package/esm/package.json +3 -0
  42. package/esm/src/clients/event.d.ts +374 -0
  43. package/esm/src/clients/event.d.ts.map +1 -0
  44. package/esm/src/clients/event.js +490 -0
  45. package/esm/src/clients/public.d.ts +695 -0
  46. package/esm/src/clients/public.d.ts.map +1 -0
  47. package/esm/src/clients/public.js +704 -0
  48. package/esm/src/clients/wallet.d.ts +682 -0
  49. package/esm/src/clients/wallet.d.ts.map +1 -0
  50. package/esm/src/clients/wallet.js +984 -0
  51. package/esm/src/transports/base.d.ts +55 -0
  52. package/esm/src/transports/base.d.ts.map +1 -0
  53. package/esm/src/transports/base.js +14 -0
  54. package/esm/src/transports/http/http_transport.d.ts +78 -0
  55. package/esm/src/transports/http/http_transport.d.ts.map +1 -0
  56. package/esm/src/transports/http/http_transport.js +170 -0
  57. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts +66 -0
  58. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -0
  59. package/esm/src/transports/websocket/hyperliquid_event_target.js +33 -0
  60. package/esm/src/transports/websocket/reconnecting_websocket.d.ts +160 -0
  61. package/esm/src/transports/websocket/reconnecting_websocket.d.ts.map +1 -0
  62. package/esm/src/transports/websocket/reconnecting_websocket.js +370 -0
  63. package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts +63 -0
  64. package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts.map +1 -0
  65. package/esm/src/transports/websocket/websocket_request_dispatcher.js +201 -0
  66. package/esm/src/transports/websocket/websocket_transport.d.ts +117 -0
  67. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -0
  68. package/esm/src/transports/websocket/websocket_transport.js +233 -0
  69. package/esm/src/utils/key_sort.d.ts +21 -0
  70. package/esm/src/utils/key_sort.d.ts.map +1 -0
  71. package/esm/src/utils/key_sort.js +124 -0
  72. package/esm/src/utils/signing.d.ts +109 -0
  73. package/esm/src/utils/signing.d.ts.map +1 -0
  74. package/esm/src/utils/signing.js +164 -0
  75. package/package.json +34 -0
  76. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +2 -0
  77. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +1 -0
  78. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +17 -0
  79. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +95 -0
  80. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +1 -0
  81. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +14 -0
  82. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.d.ts +23 -0
  83. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.d.ts.map +1 -0
  84. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +49 -0
  85. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.d.ts +55 -0
  86. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.d.ts.map +1 -0
  87. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +88 -0
  88. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.d.ts +2 -0
  89. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.d.ts.map +1 -0
  90. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +4 -0
  91. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.d.ts +52 -0
  92. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.d.ts.map +1 -0
  93. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +288 -0
  94. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.d.ts +120 -0
  95. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.d.ts.map +1 -0
  96. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +235 -0
  97. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts +19 -0
  98. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +1 -0
  99. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.js +35 -0
  100. package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.d.ts +2 -0
  101. package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.d.ts.map +1 -0
  102. package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +29 -0
  103. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts +37 -0
  104. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +1 -0
  105. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.js +113 -0
  106. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts +37 -0
  107. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +1 -0
  108. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.js +240 -0
  109. package/script/mod.d.ts +24 -0
  110. package/script/mod.d.ts.map +1 -0
  111. package/script/mod.js +27 -0
  112. package/script/package.json +3 -0
  113. package/script/src/clients/event.d.ts +374 -0
  114. package/script/src/clients/event.d.ts.map +1 -0
  115. package/script/src/clients/event.js +494 -0
  116. package/script/src/clients/public.d.ts +695 -0
  117. package/script/src/clients/public.d.ts.map +1 -0
  118. package/script/src/clients/public.js +708 -0
  119. package/script/src/clients/wallet.d.ts +682 -0
  120. package/script/src/clients/wallet.d.ts.map +1 -0
  121. package/script/src/clients/wallet.js +989 -0
  122. package/script/src/transports/base.d.ts +55 -0
  123. package/script/src/transports/base.d.ts.map +1 -0
  124. package/script/src/transports/base.js +18 -0
  125. package/script/src/transports/http/http_transport.d.ts +78 -0
  126. package/script/src/transports/http/http_transport.d.ts.map +1 -0
  127. package/script/src/transports/http/http_transport.js +175 -0
  128. package/script/src/transports/websocket/hyperliquid_event_target.d.ts +66 -0
  129. package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -0
  130. package/script/src/transports/websocket/hyperliquid_event_target.js +37 -0
  131. package/script/src/transports/websocket/reconnecting_websocket.d.ts +160 -0
  132. package/script/src/transports/websocket/reconnecting_websocket.d.ts.map +1 -0
  133. package/script/src/transports/websocket/reconnecting_websocket.js +374 -0
  134. package/script/src/transports/websocket/websocket_request_dispatcher.d.ts +63 -0
  135. package/script/src/transports/websocket/websocket_request_dispatcher.d.ts.map +1 -0
  136. package/script/src/transports/websocket/websocket_request_dispatcher.js +206 -0
  137. package/script/src/transports/websocket/websocket_transport.d.ts +117 -0
  138. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -0
  139. package/script/src/transports/websocket/websocket_transport.js +237 -0
  140. package/script/src/utils/key_sort.d.ts +21 -0
  141. package/script/src/utils/key_sort.d.ts.map +1 -0
  142. package/script/src/utils/key_sort.js +127 -0
  143. package/script/src/utils/signing.d.ts +109 -0
  144. package/script/src/utils/signing.d.ts.map +1 -0
  145. package/script/src/utils/signing.js +172 -0
@@ -0,0 +1,989 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WalletClient = exports.ApiRequestError = void 0;
4
+ const key_sort_js_1 = require("../utils/key_sort.js");
5
+ const signing_js_1 = require("../utils/signing.js");
6
+ // ———————————————Errors———————————————
7
+ /** Error thrown when the API returns an error response. */
8
+ class ApiRequestError extends Error {
9
+ constructor(response) {
10
+ let message = "";
11
+ if (response.status === "err") {
12
+ message = response.response;
13
+ }
14
+ else {
15
+ if ("statuses" in response.response.data) {
16
+ message = response.response.data.statuses
17
+ .reduce((acc, status, index) => {
18
+ if (typeof status === "object" && "error" in status) {
19
+ acc.push(`[${index}] ${status.error}`);
20
+ }
21
+ return acc;
22
+ }, [])
23
+ .join(", ");
24
+ }
25
+ else {
26
+ if (typeof response.response.data.status === "object" && "error" in response.response.data.status) {
27
+ message = response.response.data.status.error;
28
+ }
29
+ }
30
+ }
31
+ super(message);
32
+ Object.defineProperty(this, "response", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: response
37
+ });
38
+ this.name = "ApiRequestError";
39
+ }
40
+ }
41
+ exports.ApiRequestError = ApiRequestError;
42
+ // ———————————————Client———————————————
43
+ /**
44
+ * Wallet client for interacting with the Hyperliquid API.
45
+ * @typeParam T - The transport used to connect to the Hyperliquid API.
46
+ * @typeParam W - The WalletClient/Account ([viem](https://viem.sh/docs/clients/wallet)) or Signer ([ethers.js](https://docs.ethers.io/v6/api/providers/#Signer)) used for signing transactions.
47
+ */
48
+ class WalletClient {
49
+ /**
50
+ * Initialises a new instance.
51
+ * @param args - The parameters for the client.
52
+ *
53
+ * @example Private key via [viem](https://viem.sh/docs/clients/wallet#local-accounts-private-key-mnemonic-etc)
54
+ * ```ts
55
+ * import * as hl from "@nktkas/hyperliquid";
56
+ * import { privateKeyToAccount } from "viem/accounts";
57
+ *
58
+ * const wallet = privateKeyToAccount("0x...");
59
+ *
60
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
61
+ * const client = new hl.WalletClient({ wallet, transport });
62
+ * ```
63
+ *
64
+ * @example Private key via [ethers.js](https://docs.ethers.org/v6/api/wallet/#Wallet)
65
+ * ```ts
66
+ * import * as hl from "@nktkas/hyperliquid";
67
+ * import { ethers } from "ethers";
68
+ *
69
+ * const wallet = new ethers.Wallet("0x...");
70
+ *
71
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
72
+ * const client = new hl.WalletClient({ wallet, transport });
73
+ * ```
74
+ *
75
+ * @example External wallet (e.g. MetaMask) via [viem](https://viem.sh/docs/clients/wallet#optional-hoist-the-account)
76
+ * ```ts
77
+ * import * as hl from "@nktkas/hyperliquid";
78
+ * import { createWalletClient, custom } from "viem";
79
+ * import { arbitrum } from "viem/chains";
80
+ *
81
+ * const [account] = await window.ethereum.request({ method: "eth_requestAccounts" });
82
+ * const wallet = createWalletClient({ account, chain: arbitrum, transport: custom(window.ethereum) });
83
+ *
84
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
85
+ * const client = new hl.WalletClient({ wallet, transport });
86
+ * ```
87
+ */
88
+ constructor(args) {
89
+ /** The transport used to connect to the Hyperliquid API. */
90
+ Object.defineProperty(this, "transport", {
91
+ enumerable: true,
92
+ configurable: true,
93
+ writable: true,
94
+ value: void 0
95
+ });
96
+ /** The WalletClient/Account ([viem](https://viem.sh/docs/clients/wallet)) or Signer ([ethers.js](https://docs.ethers.org/v6/api/providers/#Signer)) used for signing transactions. */
97
+ Object.defineProperty(this, "wallet", {
98
+ enumerable: true,
99
+ configurable: true,
100
+ writable: true,
101
+ value: void 0
102
+ });
103
+ /** Specifies whether the client uses testnet. */
104
+ Object.defineProperty(this, "isTestnet", {
105
+ enumerable: true,
106
+ configurable: true,
107
+ writable: true,
108
+ value: void 0
109
+ });
110
+ /** Sets a default vaultAddress to be used if no vaultAddress is explicitly passed to a method. */
111
+ Object.defineProperty(this, "defaultVaultAddress", {
112
+ enumerable: true,
113
+ configurable: true,
114
+ writable: true,
115
+ value: void 0
116
+ });
117
+ this.transport = args.transport;
118
+ this.wallet = args.wallet;
119
+ this.isTestnet = args.isTestnet ?? false;
120
+ this.defaultVaultAddress = args.defaultVaultAddress;
121
+ }
122
+ // ———————————————Actions———————————————
123
+ /**
124
+ * Approve an agent to sign on behalf of the master or sub-accounts.
125
+ * @param args - The parameters for the request.
126
+ * @param signal - An optional abort signal
127
+ * @returns {SuccessResponse} Successful response without specific data.
128
+ * @throws {ApiRequestError} When the API returns an error response.
129
+ *
130
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-an-api-wallet | Hyperliquid GitBook}
131
+ * @example
132
+ * ```ts
133
+ * import * as hl from "@nktkas/hyperliquid";
134
+ * import { privateKeyToAccount } from "viem/accounts";
135
+ *
136
+ * const wallet = privateKeyToAccount("0x...");
137
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
138
+ * const client = new hl.WalletClient({ wallet, transport });
139
+ *
140
+ * const result = await client.approveAgent({
141
+ * agentAddress: "0x...",
142
+ * agentName: "agentName",
143
+ * });
144
+ */
145
+ async approveAgent(args, signal) {
146
+ const action = {
147
+ ...args,
148
+ type: "approveAgent",
149
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
150
+ signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
151
+ nonce: args.nonce ?? Date.now(),
152
+ };
153
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
154
+ "HyperliquidTransaction:ApproveAgent": [
155
+ { name: "hyperliquidChain", type: "string" },
156
+ { name: "agentAddress", type: "address" },
157
+ { name: "agentName", type: "string" },
158
+ { name: "nonce", type: "uint64" },
159
+ ],
160
+ }, parseInt(action.signatureChainId, 16));
161
+ const request = {
162
+ action,
163
+ signature,
164
+ nonce: action.nonce,
165
+ };
166
+ const response = await this.transport.request("action", request, signal);
167
+ this._validateResponse(response);
168
+ return response;
169
+ }
170
+ /**
171
+ * Approve a max fee rate for a builder address.
172
+ * @param args - The parameters for the request.
173
+ * @param signal - An optional abort signal.
174
+ * @returns {SuccessResponse} Successful response without specific data.
175
+ * @throws {ApiRequestError} When the API returns an error response.
176
+ *
177
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-a-builder-fee | Hyperliquid GitBook}
178
+ * @example
179
+ * ```ts
180
+ * import * as hl from "@nktkas/hyperliquid";
181
+ * import { privateKeyToAccount } from "viem/accounts";
182
+ *
183
+ * const wallet = privateKeyToAccount("0x...");
184
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
185
+ * const client = new hl.WalletClient({ wallet, transport });
186
+ *
187
+ * const result = await client.approveBuilderFee({
188
+ * maxFeeRate: "0.01%",
189
+ * builder: "0x...",
190
+ * });
191
+ */
192
+ async approveBuilderFee(args, signal) {
193
+ const action = {
194
+ ...args,
195
+ type: "approveBuilderFee",
196
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
197
+ signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
198
+ nonce: args.nonce ?? Date.now(),
199
+ };
200
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
201
+ "HyperliquidTransaction:ApproveBuilderFee": [
202
+ { name: "hyperliquidChain", type: "string" },
203
+ { name: "maxFeeRate", type: "string" },
204
+ { name: "builder", type: "address" },
205
+ { name: "nonce", type: "uint64" },
206
+ ],
207
+ }, parseInt(action.signatureChainId, 16));
208
+ const request = {
209
+ action,
210
+ signature,
211
+ nonce: action.nonce,
212
+ };
213
+ const response = await this.transport.request("action", request, signal);
214
+ this._validateResponse(response);
215
+ return response;
216
+ }
217
+ /**
218
+ * Modify multiple orders.
219
+ * @param args - The parameters for the request.
220
+ * @param signal - An optional abort signal.
221
+ * @returns {OrderResponseSuccess} Successful variant of {@link OrderResponse} without error statuses.
222
+ * @throws {ApiRequestError} When the API returns an error response.
223
+ *
224
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders | Hyperliquid GitBook}
225
+ * @example
226
+ * ```ts
227
+ * import * as hl from "@nktkas/hyperliquid";
228
+ * import { privateKeyToAccount } from "viem/accounts";
229
+ *
230
+ * const wallet = privateKeyToAccount("0x...");
231
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
232
+ * const client = new hl.WalletClient({ wallet, transport });
233
+ *
234
+ * const result = await client.batchModify({
235
+ * modifies: [{
236
+ * oid: 123, // Order ID
237
+ * order: {
238
+ * a: 0, // Asset index
239
+ * b: true, // Buy order
240
+ * p: "31000", // New price
241
+ * s: "0.2", // New size
242
+ * r: false, // Not reduce-only
243
+ * t: {
244
+ * limit: {
245
+ * tif: "Gtc", // Good-til-cancelled
246
+ * },
247
+ * },
248
+ * c: "0x...", // Optional: Client Order ID
249
+ * },
250
+ * }],
251
+ * });
252
+ * ```
253
+ */
254
+ async batchModify(args, signal) {
255
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
256
+ const sortedAction = key_sort_js_1.sorters.batchModify({ type: "batchModify", ...actionArgs });
257
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
258
+ const request = {
259
+ action: sortedAction,
260
+ signature,
261
+ nonce,
262
+ vaultAddress,
263
+ };
264
+ const response = await this.transport.request("action", request, signal);
265
+ this._validateResponse(response);
266
+ return response;
267
+ }
268
+ /**
269
+ * Cancel order(s).
270
+ * @param args - The parameters for the request.
271
+ * @param signal - An optional abort signal.
272
+ * @returns {CancelResponseSuccess} Successful variant of {@link CancelResponse} without error statuses.
273
+ * @throws {ApiRequestError} When the API returns an error response.
274
+ *
275
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s | Hyperliquid GitBook}
276
+ * @example
277
+ * ```ts
278
+ * import * as hl from "@nktkas/hyperliquid";
279
+ * import { privateKeyToAccount } from "viem/accounts";
280
+ *
281
+ * const wallet = privateKeyToAccount("0x...");
282
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
283
+ * const client = new hl.WalletClient({ wallet, transport });
284
+ *
285
+ * const result = await client.cancel({
286
+ * cancels: [{
287
+ * a: 0, // Asset index
288
+ * o: 123, // Order ID
289
+ * }],
290
+ * });
291
+ * ```
292
+ */
293
+ async cancel(args, signal) {
294
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
295
+ const sortedAction = key_sort_js_1.sorters.cancel({ type: "cancel", ...actionArgs });
296
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
297
+ const request = {
298
+ action: sortedAction,
299
+ signature,
300
+ nonce,
301
+ vaultAddress,
302
+ };
303
+ const response = await this.transport.request("action", request, signal);
304
+ this._validateResponse(response);
305
+ return response;
306
+ }
307
+ /**
308
+ * Cancel order(s) by Client Order ID.
309
+ * @param args - The parameters for the request.
310
+ * @param signal - An optional abort signal.
311
+ * @returns {CancelResponseSuccess} Successful variant of {@link CancelResponse} without error statuses.
312
+ * @throws {ApiRequestError} When the API returns an error response.
313
+ *
314
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid | Hyperliquid GitBook}
315
+ * @example
316
+ * ```ts
317
+ * import * as hl from "@nktkas/hyperliquid";
318
+ * import { privateKeyToAccount } from "viem/accounts";
319
+ *
320
+ * const wallet = privateKeyToAccount("0x...");
321
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
322
+ * const client = new hl.WalletClient({ wallet, transport });
323
+ *
324
+ * const result = await client.cancelByCloid({
325
+ * cancels: [{
326
+ * asset: 0,
327
+ * cloid: "0x...", // Client Order ID
328
+ * }],
329
+ * });
330
+ * ```
331
+ */
332
+ async cancelByCloid(args, signal) {
333
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
334
+ const sortedAction = key_sort_js_1.sorters.cancelByCloid({ type: "cancelByCloid", ...actionArgs });
335
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
336
+ const request = {
337
+ action: sortedAction,
338
+ signature,
339
+ nonce,
340
+ vaultAddress,
341
+ };
342
+ const response = await this.transport.request("action", request, signal);
343
+ this._validateResponse(response);
344
+ return response;
345
+ }
346
+ /**
347
+ * Create a sub-account.
348
+ * @param args - The parameters for the request.
349
+ * @param signal - An optional abort signal.
350
+ * @returns {CreateSubAccountResponse} Response for creating a sub-account.
351
+ * @throws {ApiRequestError} When the API returns an error response.
352
+ *
353
+ * @example
354
+ * ```ts
355
+ * import * as hl from "@nktkas/hyperliquid";
356
+ * import { privateKeyToAccount } from "viem/accounts";
357
+ *
358
+ * const wallet = privateKeyToAccount("0x...");
359
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
360
+ * const client = new hl.WalletClient({ wallet, transport });
361
+ *
362
+ * const result = await client.createSubAccount({
363
+ * name: "subAccountName",
364
+ * });
365
+ * ```
366
+ */
367
+ async createSubAccount(args, signal) {
368
+ const { nonce = Date.now(), ...actionArgs } = args;
369
+ const sortedAction = key_sort_js_1.sorters.createSubAccount({ type: "createSubAccount", ...actionArgs });
370
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce);
371
+ const request = {
372
+ action: sortedAction,
373
+ signature,
374
+ nonce,
375
+ };
376
+ const response = await this.transport.request("action", request, signal);
377
+ this._validateResponse(response);
378
+ return response;
379
+ }
380
+ /**
381
+ * Modify an order.
382
+ * @param args - The parameters for the request.
383
+ * @param signal - An optional abort signal.
384
+ * @returns {SuccessResponse} Successful response without specific data.
385
+ * @throws {ApiRequestError} When the API returns an error response.
386
+ *
387
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-an-order | Hyperliquid GitBook}
388
+ * @example
389
+ * ```ts
390
+ * import * as hl from "@nktkas/hyperliquid";
391
+ * import { privateKeyToAccount } from "viem/accounts";
392
+ *
393
+ * const wallet = privateKeyToAccount("0x...");
394
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
395
+ * const client = new hl.WalletClient({ wallet, transport });
396
+ *
397
+ * const result = await client.modify({
398
+ * oid: 123, // Order ID
399
+ * order: {
400
+ * a: 0, // Asset index
401
+ * b: true, // Buy order
402
+ * p: "31000", // New price
403
+ * s: "0.2", // New size
404
+ * r: false, // Not reduce-only
405
+ * t: {
406
+ * limit: {
407
+ * tif: "Gtc", // Good-til-cancelled
408
+ * },
409
+ * },
410
+ * c: "0x...", // Optional: Client Order ID
411
+ * },
412
+ * });
413
+ * ```
414
+ */
415
+ async modify(args, signal) {
416
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
417
+ const sortedAction = key_sort_js_1.sorters.modify({ type: "modify", ...actionArgs });
418
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
419
+ const request = {
420
+ action: sortedAction,
421
+ signature,
422
+ nonce,
423
+ vaultAddress,
424
+ };
425
+ const response = await this.transport.request("action", request, signal);
426
+ this._validateResponse(response);
427
+ return response;
428
+ }
429
+ /**
430
+ * Place an order(s).
431
+ * @param args - The parameters for the request.
432
+ * @param signal - An optional abort signal.
433
+ * @returns {OrderResponseSuccess} Successful variant of {@link OrderResponse} without error statuses.
434
+ * @throws {ApiRequestError} When the API returns an error response.
435
+ *
436
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order | Hyperliquid GitBook}
437
+ * @example
438
+ * ```ts
439
+ * import * as hl from "@nktkas/hyperliquid";
440
+ * import { privateKeyToAccount } from "viem/accounts";
441
+ *
442
+ * const wallet = privateKeyToAccount("0x...");
443
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
444
+ * const client = new hl.WalletClient({ wallet, transport });
445
+ *
446
+ * const result = await client.order({
447
+ * orders: [{
448
+ * a: 0, // Asset index
449
+ * b: true, // Buy order
450
+ * p: "30000", // Price
451
+ * s: "0.1", // Size
452
+ * r: false, // Not reduce-only
453
+ * t: {
454
+ * limit: {
455
+ * tif: "Gtc", // Good-til-cancelled
456
+ * },
457
+ * },
458
+ * c: "0x...", // Optional: Client Order ID
459
+ * }],
460
+ * grouping: "na", // No grouping
461
+ * });
462
+ * ```
463
+ */
464
+ async order(args, signal) {
465
+ const clonedArgs = structuredClone(args); // Clone to prevent mutation of original object
466
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = clonedArgs;
467
+ if (actionArgs.builder)
468
+ actionArgs.builder.b = actionArgs.builder.b.toLowerCase();
469
+ const sortedAction = key_sort_js_1.sorters.order({ type: "order", ...actionArgs });
470
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
471
+ const request = {
472
+ action: sortedAction,
473
+ signature,
474
+ nonce,
475
+ vaultAddress,
476
+ };
477
+ const response = await this.transport.request("action", request, signal);
478
+ this._validateResponse(response);
479
+ return response;
480
+ }
481
+ /**
482
+ * Schedule a time to cancel all open orders.
483
+ * @param args - The parameters for the request.
484
+ * @param signal - An optional abort signal.
485
+ * @returns {SuccessResponse} Successful response without specific data.
486
+ * @throws {ApiRequestError} When the API returns an error response.
487
+ *
488
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#schedule-cancel-dead-mans-switch | Hyperliquid GitBook}
489
+ * @example
490
+ * ```ts
491
+ * import * as hl from "@nktkas/hyperliquid";
492
+ * import { privateKeyToAccount } from "viem/accounts";
493
+ *
494
+ * const wallet = privateKeyToAccount("0x...");
495
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
496
+ * const client = new hl.WalletClient({ wallet, transport });
497
+ *
498
+ * const result = await client.scheduleCancel({
499
+ * time: Date.now() + 3600000, // Schedule cancellation 1 hour from now
500
+ * });
501
+ * ```
502
+ */
503
+ async scheduleCancel(args = {}, signal) {
504
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
505
+ const sortedAction = key_sort_js_1.sorters.scheduleCancel({ type: "scheduleCancel", ...actionArgs });
506
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
507
+ const request = {
508
+ action: sortedAction,
509
+ signature,
510
+ nonce,
511
+ vaultAddress,
512
+ };
513
+ const response = await this.transport.request("action", request, signal);
514
+ this._validateResponse(response);
515
+ return response;
516
+ }
517
+ /**
518
+ * Set a referral code.
519
+ * @param args - The parameters for the request.
520
+ * @param signal - An optional abort signal.
521
+ * @returns {SuccessResponse} Successful response without specific data.
522
+ * @throws {ApiRequestError} When the API returns an error response.
523
+ *
524
+ * @example
525
+ * ```ts
526
+ * import * as hl from "@nktkas/hyperliquid";
527
+ * import { privateKeyToAccount } from "viem/accounts";
528
+ *
529
+ * const wallet = privateKeyToAccount("0x...");
530
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
531
+ * const client = new hl.WalletClient({ wallet, transport });
532
+ *
533
+ * const result = await client.setReferrer({
534
+ * code: "TEST",
535
+ * });
536
+ * ```
537
+ */
538
+ async setReferrer(args, signal) {
539
+ const { nonce = Date.now(), ...actionArgs } = args;
540
+ const sortedAction = key_sort_js_1.sorters.setReferrer({ type: "setReferrer", ...actionArgs });
541
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce);
542
+ const request = {
543
+ action: sortedAction,
544
+ signature,
545
+ nonce,
546
+ };
547
+ const response = await this.transport.request("action", request, signal);
548
+ this._validateResponse(response);
549
+ return response;
550
+ }
551
+ /**
552
+ * Transfer a spot asset on L1 to another address.
553
+ * @param args - The parameters for the request.
554
+ * @param signal - An optional abort signal.
555
+ * @returns {SuccessResponse} Successful response without specific data.
556
+ * @throws {ApiRequestError} When the API returns an error response.
557
+ *
558
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#l1-spot-transfer | Hyperliquid GitBook}
559
+ * @example
560
+ * ```ts
561
+ * import * as hl from "@nktkas/hyperliquid";
562
+ * import { privateKeyToAccount } from "viem/accounts";
563
+ *
564
+ * const wallet = privateKeyToAccount("0x...");
565
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
566
+ * const client = new hl.WalletClient({ wallet, transport });
567
+ *
568
+ * const result = await client.spotSend({
569
+ * destination: "0x...",
570
+ * token: "USDC:0xeb62eee3685fc4c43992febcd9e75443",
571
+ * amount: "1",
572
+ * });
573
+ * ```
574
+ */
575
+ async spotSend(args, signal) {
576
+ const action = {
577
+ ...args,
578
+ type: "spotSend",
579
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
580
+ signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
581
+ time: args.time ?? Date.now(),
582
+ };
583
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
584
+ "HyperliquidTransaction:SpotSend": [
585
+ { name: "hyperliquidChain", type: "string" },
586
+ { name: "destination", type: "string" },
587
+ { name: "token", type: "string" },
588
+ { name: "amount", type: "string" },
589
+ { name: "time", type: "uint64" },
590
+ ],
591
+ }, parseInt(action.signatureChainId, 16));
592
+ const request = {
593
+ action,
594
+ signature,
595
+ nonce: action.time,
596
+ };
597
+ const response = await this.transport.request("action", request, signal);
598
+ this._validateResponse(response);
599
+ return response;
600
+ }
601
+ /**
602
+ * Transfer between sub-accounts.
603
+ * @param args - The parameters for the request.
604
+ * @param signal - An optional abort signal.
605
+ * @returns {SuccessResponse} Successful response without specific data.
606
+ * @throws {ApiRequestError} When the API returns an error response.
607
+ *
608
+ * @example
609
+ * ```ts
610
+ * import * as hl from "@nktkas/hyperliquid";
611
+ * import { privateKeyToAccount } from "viem/accounts";
612
+ *
613
+ * const wallet = privateKeyToAccount("0x...");
614
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
615
+ * const client = new hl.WalletClient({ wallet, transport });
616
+ *
617
+ * const result = await client.subAccountTransfer({
618
+ * subAccountUser: "0x...",
619
+ * isDeposit: true,
620
+ * usd: 1000000, // 1 USD in raw units (float amount * 1e6)
621
+ * });
622
+ * ```
623
+ */
624
+ async subAccountTransfer(args, signal) {
625
+ const { nonce = Date.now(), ...actionArgs } = args;
626
+ const sortedAction = key_sort_js_1.sorters.subAccountTransfer({ type: "subAccountTransfer", ...actionArgs });
627
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce);
628
+ const request = {
629
+ action: sortedAction,
630
+ signature,
631
+ nonce,
632
+ };
633
+ const response = await this.transport.request("action", request, signal);
634
+ this._validateResponse(response);
635
+ return response;
636
+ }
637
+ /**
638
+ * Cancel a TWAP order.
639
+ * @param args - The parameters for the request.
640
+ * @param signal - An optional abort signal.
641
+ * @returns {TwapCancelResponseSuccess} Successful variant of {@link TwapCancelResponse} without error status.
642
+ * @throws {ApiRequestError} When the API returns an error response.
643
+ *
644
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-a-twap-order | Hyperliquid GitBook}
645
+ * @example
646
+ * ```ts
647
+ * import * as hl from "@nktkas/hyperliquid";
648
+ * import { privateKeyToAccount } from "viem/accounts";
649
+ *
650
+ * const wallet = privateKeyToAccount("0x...");
651
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
652
+ * const client = new hl.WalletClient({ wallet, transport });
653
+ *
654
+ * const result = await client.twapCancel({
655
+ * a: 0, // Asset index
656
+ * t: 1, // TWAP ID
657
+ * });
658
+ * ```
659
+ */
660
+ async twapCancel(args, signal) {
661
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
662
+ const sortedAction = key_sort_js_1.sorters.twapCancel({ type: "twapCancel", ...actionArgs });
663
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
664
+ const request = {
665
+ action: sortedAction,
666
+ signature,
667
+ nonce,
668
+ vaultAddress,
669
+ };
670
+ const response = await this.transport.request("action", request, signal);
671
+ this._validateResponse(response);
672
+ return response;
673
+ }
674
+ /**
675
+ * Place a TWAP order.
676
+ * @param args - The parameters for the request.
677
+ * @param signal - An optional abort signal.
678
+ * @returns {TwapOrderResponseSuccess} Successful variant of {@link TwapOrderResponse} without error status.
679
+ * @throws {ApiRequestError} When the API returns an error response.
680
+ *
681
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-a-twap-order | Hyperliquid GitBook}
682
+ * @example
683
+ * ```ts
684
+ * import * as hl from "@nktkas/hyperliquid";
685
+ * import { privateKeyToAccount } from "viem/accounts";
686
+ *
687
+ * const wallet = privateKeyToAccount("0x...");
688
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
689
+ * const client = new hl.WalletClient({ wallet, transport });
690
+ *
691
+ * const result = await client.twapOrder({
692
+ * a: 0, // Asset index
693
+ * b: true, // Buy order
694
+ * s: "1", // Size
695
+ * r: false, // Not reduce-only
696
+ * m: 10, // Duration in minutes
697
+ * t: true, // Randomize order timing
698
+ * });
699
+ * ```
700
+ */
701
+ async twapOrder(args, signal) {
702
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
703
+ const sortedAction = key_sort_js_1.sorters.twapOrder({ type: "twapOrder", twap: actionArgs });
704
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
705
+ const request = {
706
+ action: sortedAction,
707
+ signature,
708
+ nonce,
709
+ vaultAddress,
710
+ };
711
+ const response = await this.transport.request("action", request, signal);
712
+ this._validateResponse(response);
713
+ return response;
714
+ }
715
+ /**
716
+ * Update isolated margin for a position.
717
+ * @param args - The parameters for the request.
718
+ * @param signal - An optional abort signal.
719
+ * @returns {SuccessResponse} Successful response without specific data.
720
+ * @throws {ApiRequestError} When the API returns an error response.
721
+ *
722
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-isolated-margin | Hyperliquid GitBook}
723
+ * @example
724
+ * ```ts
725
+ * import * as hl from "@nktkas/hyperliquid";
726
+ * import { privateKeyToAccount } from "viem/accounts";
727
+ *
728
+ * const wallet = privateKeyToAccount("0x...");
729
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
730
+ * const client = new hl.WalletClient({ wallet, transport });
731
+ *
732
+ * const result = await client.updateIsolatedMargin({
733
+ * asset: 0,
734
+ * isBuy: true, // Add to long position
735
+ * ntli: 1000, // Add 1000 USD margin (integer only)
736
+ * });
737
+ * ```
738
+ */
739
+ async updateIsolatedMargin(args, signal) {
740
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
741
+ const sortedAction = key_sort_js_1.sorters.updateIsolatedMargin({ type: "updateIsolatedMargin", ...actionArgs });
742
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
743
+ const request = {
744
+ action: sortedAction,
745
+ signature,
746
+ nonce,
747
+ vaultAddress,
748
+ };
749
+ const response = await this.transport.request("action", request, signal);
750
+ this._validateResponse(response);
751
+ return response;
752
+ }
753
+ /**
754
+ * Update leverage for cross or isolated margin.
755
+ * @param args - The parameters for the request.
756
+ * @param signal - An optional abort signal.
757
+ * @returns {SuccessResponse} Successful response without specific data.
758
+ * @throws {ApiRequestError} When the API returns an error response.
759
+ *
760
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#update-leverage | Hyperliquid GitBook}
761
+ * @example
762
+ * ```ts
763
+ * import * as hl from "@nktkas/hyperliquid";
764
+ * import { privateKeyToAccount } from "viem/accounts";
765
+ *
766
+ * const wallet = privateKeyToAccount("0x...");
767
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
768
+ * const client = new hl.WalletClient({ wallet, transport });
769
+ *
770
+ * const result = await client.updateLeverage({
771
+ * asset: 0,
772
+ * isCross: true,
773
+ * leverage: 5,
774
+ * });
775
+ * ```
776
+ */
777
+ async updateLeverage(args, signal) {
778
+ const { vaultAddress = this.defaultVaultAddress, nonce = Date.now(), ...actionArgs } = args;
779
+ const sortedAction = key_sort_js_1.sorters.updateLeverage({ type: "updateLeverage", ...actionArgs });
780
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce, vaultAddress);
781
+ const request = {
782
+ action: sortedAction,
783
+ signature,
784
+ nonce,
785
+ vaultAddress,
786
+ };
787
+ const response = await this.transport.request("action", request, signal);
788
+ this._validateResponse(response);
789
+ return response;
790
+ }
791
+ /**
792
+ * Transfer funds between Spot and Perp accounts.
793
+ * @param args - The parameters for the request.
794
+ * @param signal - An optional abort signal.
795
+ * @returns {SuccessResponse} Successful response without specific data.
796
+ * @throws {ApiRequestError} When the API returns an error response.
797
+ *
798
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa | Hyperliquid GitBook}
799
+ * @example
800
+ * ```ts
801
+ * import * as hl from "@nktkas/hyperliquid";
802
+ * import { privateKeyToAccount } from "viem/accounts";
803
+ *
804
+ * const wallet = privateKeyToAccount("0x...");
805
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
806
+ * const client = new hl.WalletClient({ wallet, transport });
807
+ *
808
+ * const result = await client.usdClassTransfer({
809
+ * amount: "1000",
810
+ * toPerp: true, // Transfer from Spot to Perp
811
+ * });
812
+ * ```
813
+ */
814
+ async usdClassTransfer(args, signal) {
815
+ const action = {
816
+ ...args,
817
+ type: "usdClassTransfer",
818
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
819
+ signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
820
+ nonce: args.nonce ?? Date.now(),
821
+ };
822
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
823
+ "HyperliquidTransaction:UsdClassTransfer": [
824
+ { name: "hyperliquidChain", type: "string" },
825
+ { name: "amount", type: "string" },
826
+ { name: "toPerp", type: "bool" },
827
+ { name: "nonce", type: "uint64" },
828
+ ],
829
+ }, parseInt(action.signatureChainId, 16));
830
+ const request = {
831
+ action,
832
+ signature,
833
+ nonce: action.nonce,
834
+ };
835
+ const response = await this.transport.request("action", request, signal);
836
+ this._validateResponse(response);
837
+ return response;
838
+ }
839
+ /**
840
+ * Transfer USDC on L1 to another address.
841
+ * @param args - The parameters for the request.
842
+ * @param signal - An optional abort signal.
843
+ * @returns {SuccessResponse} Successful response without specific data.
844
+ * @throws {ApiRequestError} When the API returns an error response.
845
+ *
846
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#l1-usdc-transfer | Hyperliquid GitBook}
847
+ * @example
848
+ * ```ts
849
+ * import * as hl from "@nktkas/hyperliquid";
850
+ * import { privateKeyToAccount } from "viem/accounts";
851
+ *
852
+ * const wallet = privateKeyToAccount("0x...");
853
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
854
+ * const client = new hl.WalletClient({ wallet, transport });
855
+ *
856
+ * const result = await client.usdSend({
857
+ * destination: "0x...",
858
+ * amount: "1000",
859
+ * });
860
+ * ```
861
+ */
862
+ async usdSend(args, signal) {
863
+ const action = {
864
+ ...args,
865
+ type: "usdSend",
866
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
867
+ signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
868
+ time: args.time ?? Date.now(),
869
+ };
870
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
871
+ "HyperliquidTransaction:UsdSend": [
872
+ { name: "hyperliquidChain", type: "string" },
873
+ { name: "destination", type: "string" },
874
+ { name: "amount", type: "string" },
875
+ { name: "time", type: "uint64" },
876
+ ],
877
+ }, parseInt(action.signatureChainId, 16));
878
+ const request = {
879
+ action,
880
+ signature,
881
+ nonce: action.time,
882
+ };
883
+ const response = await this.transport.request("action", request, signal);
884
+ this._validateResponse(response);
885
+ return response;
886
+ }
887
+ /**
888
+ * Transfer funds to/from a vault.
889
+ * @param args - The parameters for the request.
890
+ * @param signal - An optional abort signal.
891
+ * @returns {SuccessResponse} Successful response without specific data.
892
+ * @throws {ApiRequestError} When the API returns an error response.
893
+ *
894
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-or-withdraw-from-a-vault | Hyperliquid GitBook}
895
+ * @example
896
+ * ```ts
897
+ * import * as hl from "@nktkas/hyperliquid";
898
+ * import { privateKeyToAccount } from "viem/accounts";
899
+ *
900
+ * const wallet = privateKeyToAccount("0x...");
901
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
902
+ * const client = new hl.WalletClient({ wallet, transport });
903
+ *
904
+ * const result = await client.vaultTransfer({
905
+ * vaultAddress: "0x...",
906
+ * isDeposit: true,
907
+ * usd: 1000000, // 1 USD in raw units (float amount * 1e6)
908
+ * });
909
+ * ```
910
+ */
911
+ async vaultTransfer(args, signal) {
912
+ const { nonce = Date.now(), ...actionArgs } = args;
913
+ const sortedAction = key_sort_js_1.sorters.vaultTransfer({ type: "vaultTransfer", ...actionArgs });
914
+ const signature = await (0, signing_js_1.signL1Action)(this.wallet, this.isTestnet, sortedAction, nonce);
915
+ const request = {
916
+ action: sortedAction,
917
+ signature,
918
+ nonce,
919
+ };
920
+ const response = await this.transport.request("action", request, signal);
921
+ this._validateResponse(response);
922
+ return response;
923
+ }
924
+ /**
925
+ * Initiate a withdrawal request.
926
+ * @param args - The parameters for the request.
927
+ * @param signal - An optional abort signal.
928
+ * @returns {SuccessResponse} Successful response without specific data.
929
+ * @throws {ApiRequestError} When the API returns an error response.
930
+ *
931
+ * @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#initiate-a-withdrawal-request | Hyperliquid GitBook}
932
+ * @example
933
+ * ```ts
934
+ * import * as hl from "@nktkas/hyperliquid";
935
+ * import { privateKeyToAccount } from "viem/accounts";
936
+ *
937
+ * const wallet = privateKeyToAccount("0x...");
938
+ * const transport = new hl.HttpTransport(); // or WebSocketTransport
939
+ * const client = new hl.WalletClient({ wallet, transport });
940
+ *
941
+ * const result = await client.withdraw3({
942
+ * destination: "0x...",
943
+ * amount: "1000",
944
+ * });
945
+ * ```
946
+ */
947
+ async withdraw3(args, signal) {
948
+ const action = {
949
+ ...args,
950
+ type: "withdraw3",
951
+ hyperliquidChain: this.isTestnet ? "Testnet" : "Mainnet",
952
+ signatureChainId: args.signatureChainId ?? this.isTestnet ? "0x66eee" : "0xa4b1",
953
+ time: args.time ?? Date.now(),
954
+ };
955
+ const signature = await (0, signing_js_1.signUserSignedAction)(this.wallet, action, {
956
+ "HyperliquidTransaction:Withdraw": [
957
+ { name: "hyperliquidChain", type: "string" },
958
+ { name: "destination", type: "string" },
959
+ { name: "amount", type: "string" },
960
+ { name: "time", type: "uint64" },
961
+ ],
962
+ }, parseInt(action.signatureChainId, 16));
963
+ const request = {
964
+ action,
965
+ signature,
966
+ nonce: action.time,
967
+ };
968
+ const response = await this.transport.request("action", request, signal);
969
+ this._validateResponse(response);
970
+ return response;
971
+ }
972
+ /** Validate a response from the API. */
973
+ _validateResponse(response) {
974
+ if (response.status === "err") {
975
+ throw new ApiRequestError(response);
976
+ }
977
+ else if (response.response.type === "order" || response.response.type === "cancel") {
978
+ if (response.response.data.statuses.some((status) => typeof status === "object" && "error" in status)) {
979
+ throw new ApiRequestError(response);
980
+ }
981
+ }
982
+ else if (response.response.type === "twapOrder" || response.response.type === "twapCancel") {
983
+ if (typeof response.response.data.status === "object" && "error" in response.response.data.status) {
984
+ throw new ApiRequestError(response);
985
+ }
986
+ }
987
+ }
988
+ }
989
+ exports.WalletClient = WalletClient;