@nktkas/hyperliquid 0.13.1 → 0.13.2

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