@nktkas/hyperliquid 0.23.1 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/CONTRIBUTING.md +3 -2
  2. package/README.md +154 -170
  3. package/esm/mod.d.ts +1 -1
  4. package/esm/mod.d.ts.map +1 -1
  5. package/esm/src/base.d.ts +0 -5
  6. package/esm/src/base.d.ts.map +1 -1
  7. package/esm/src/clients/exchange.d.ts +430 -393
  8. package/esm/src/clients/exchange.d.ts.map +1 -1
  9. package/esm/src/clients/exchange.js +467 -927
  10. package/esm/src/clients/info.d.ts +228 -203
  11. package/esm/src/clients/info.d.ts.map +1 -1
  12. package/esm/src/clients/info.js +236 -368
  13. package/esm/src/clients/multiSign.d.ts +14 -1184
  14. package/esm/src/clients/multiSign.d.ts.map +1 -1
  15. package/esm/src/clients/multiSign.js +37 -2004
  16. package/esm/src/clients/subscription.d.ts +95 -93
  17. package/esm/src/clients/subscription.d.ts.map +1 -1
  18. package/esm/src/clients/subscription.js +101 -146
  19. package/esm/src/signing/_signTypedData/ethers.d.ts +16 -4
  20. package/esm/src/signing/_signTypedData/ethers.d.ts.map +1 -1
  21. package/esm/src/signing/_signTypedData/ethers.js +1 -1
  22. package/esm/src/signing/_signTypedData/mod.d.ts +12 -12
  23. package/esm/src/signing/_signTypedData/mod.d.ts.map +1 -1
  24. package/esm/src/signing/_signTypedData/mod.js +52 -24
  25. package/esm/src/signing/_signTypedData/private_key.d.ts +6 -5
  26. package/esm/src/signing/_signTypedData/private_key.d.ts.map +1 -1
  27. package/esm/src/signing/_signTypedData/private_key.js +40 -19
  28. package/esm/src/signing/_signTypedData/viem.d.ts +27 -6
  29. package/esm/src/signing/_signTypedData/viem.d.ts.map +1 -1
  30. package/esm/src/signing/_signTypedData/viem.js +1 -1
  31. package/esm/src/signing/_sorter.d.ts +12 -21
  32. package/esm/src/signing/_sorter.d.ts.map +1 -1
  33. package/esm/src/signing/_sorter.js +30 -63
  34. package/esm/src/signing/mod.d.ts +177 -127
  35. package/esm/src/signing/mod.d.ts.map +1 -1
  36. package/esm/src/signing/mod.js +182 -130
  37. package/esm/src/transports/base.d.ts +2 -1
  38. package/esm/src/transports/base.d.ts.map +1 -1
  39. package/esm/src/transports/http/http_transport.d.ts +3 -2
  40. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  41. package/esm/src/transports/http/http_transport.js +4 -4
  42. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +14 -6
  43. package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
  44. package/esm/src/transports/websocket/_hyperliquid_event_target.js +1 -2
  45. package/esm/src/transports/websocket/_reconnecting_websocket.d.ts +2 -1
  46. package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
  47. package/esm/src/transports/websocket/_reconnecting_websocket.js +1 -0
  48. package/esm/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
  49. package/esm/src/transports/websocket/_websocket_async_request.js +17 -21
  50. package/esm/src/transports/websocket/websocket_transport.d.ts +4 -4
  51. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  52. package/esm/src/transports/websocket/websocket_transport.js +6 -6
  53. package/esm/src/types/exchange/requests.d.ts +492 -306
  54. package/esm/src/types/exchange/requests.d.ts.map +1 -1
  55. package/esm/src/types/exchange/responses.d.ts +105 -25
  56. package/esm/src/types/exchange/responses.d.ts.map +1 -1
  57. package/esm/src/types/explorer/requests.d.ts +3 -3
  58. package/esm/src/types/explorer/requests.d.ts.map +1 -1
  59. package/esm/src/types/explorer/responses.d.ts +1 -1
  60. package/esm/src/types/explorer/responses.d.ts.map +1 -1
  61. package/esm/src/types/info/accounts.d.ts +403 -98
  62. package/esm/src/types/info/accounts.d.ts.map +1 -1
  63. package/esm/src/types/info/assets.d.ts +131 -35
  64. package/esm/src/types/info/assets.d.ts.map +1 -1
  65. package/esm/src/types/info/markets.d.ts +29 -8
  66. package/esm/src/types/info/markets.d.ts.map +1 -1
  67. package/esm/src/types/info/orders.d.ts +62 -17
  68. package/esm/src/types/info/orders.d.ts.map +1 -1
  69. package/esm/src/types/info/requests.d.ts +117 -51
  70. package/esm/src/types/info/requests.d.ts.map +1 -1
  71. package/esm/src/types/info/validators.d.ts +44 -14
  72. package/esm/src/types/info/validators.d.ts.map +1 -1
  73. package/esm/src/types/info/vaults.d.ts +25 -10
  74. package/esm/src/types/info/vaults.d.ts.map +1 -1
  75. package/esm/src/types/mod.d.ts +1 -1
  76. package/esm/src/types/mod.d.ts.map +1 -1
  77. package/esm/src/types/subscriptions/requests.d.ts +21 -66
  78. package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
  79. package/esm/src/types/subscriptions/responses.d.ts +46 -55
  80. package/esm/src/types/subscriptions/responses.d.ts.map +1 -1
  81. package/package.json +2 -3
  82. package/script/mod.d.ts +1 -1
  83. package/script/mod.d.ts.map +1 -1
  84. package/script/src/base.d.ts +0 -5
  85. package/script/src/base.d.ts.map +1 -1
  86. package/script/src/clients/exchange.d.ts +430 -393
  87. package/script/src/clients/exchange.d.ts.map +1 -1
  88. package/script/src/clients/exchange.js +466 -926
  89. package/script/src/clients/info.d.ts +228 -203
  90. package/script/src/clients/info.d.ts.map +1 -1
  91. package/script/src/clients/info.js +236 -368
  92. package/script/src/clients/multiSign.d.ts +14 -1184
  93. package/script/src/clients/multiSign.d.ts.map +1 -1
  94. package/script/src/clients/multiSign.js +38 -2005
  95. package/script/src/clients/subscription.d.ts +95 -93
  96. package/script/src/clients/subscription.d.ts.map +1 -1
  97. package/script/src/clients/subscription.js +101 -146
  98. package/script/src/signing/_signTypedData/ethers.d.ts +16 -4
  99. package/script/src/signing/_signTypedData/ethers.d.ts.map +1 -1
  100. package/script/src/signing/_signTypedData/ethers.js +2 -2
  101. package/script/src/signing/_signTypedData/mod.d.ts +12 -12
  102. package/script/src/signing/_signTypedData/mod.d.ts.map +1 -1
  103. package/script/src/signing/_signTypedData/mod.js +51 -26
  104. package/script/src/signing/_signTypedData/private_key.d.ts +6 -5
  105. package/script/src/signing/_signTypedData/private_key.d.ts.map +1 -1
  106. package/script/src/signing/_signTypedData/private_key.js +40 -18
  107. package/script/src/signing/_signTypedData/viem.d.ts +27 -6
  108. package/script/src/signing/_signTypedData/viem.d.ts.map +1 -1
  109. package/script/src/signing/_signTypedData/viem.js +2 -2
  110. package/script/src/signing/_sorter.d.ts +12 -21
  111. package/script/src/signing/_sorter.d.ts.map +1 -1
  112. package/script/src/signing/_sorter.js +30 -63
  113. package/script/src/signing/mod.d.ts +177 -127
  114. package/script/src/signing/mod.d.ts.map +1 -1
  115. package/script/src/signing/mod.js +185 -148
  116. package/script/src/transports/base.d.ts +2 -1
  117. package/script/src/transports/base.d.ts.map +1 -1
  118. package/script/src/transports/http/http_transport.d.ts +3 -2
  119. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  120. package/script/src/transports/http/http_transport.js +4 -4
  121. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +14 -6
  122. package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
  123. package/script/src/transports/websocket/_hyperliquid_event_target.js +1 -2
  124. package/script/src/transports/websocket/_reconnecting_websocket.d.ts +2 -1
  125. package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
  126. package/script/src/transports/websocket/_reconnecting_websocket.js +1 -0
  127. package/script/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
  128. package/script/src/transports/websocket/_websocket_async_request.js +17 -21
  129. package/script/src/transports/websocket/websocket_transport.d.ts +4 -4
  130. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  131. package/script/src/transports/websocket/websocket_transport.js +6 -6
  132. package/script/src/types/exchange/requests.d.ts +492 -306
  133. package/script/src/types/exchange/requests.d.ts.map +1 -1
  134. package/script/src/types/exchange/responses.d.ts +105 -25
  135. package/script/src/types/exchange/responses.d.ts.map +1 -1
  136. package/script/src/types/explorer/requests.d.ts +3 -3
  137. package/script/src/types/explorer/requests.d.ts.map +1 -1
  138. package/script/src/types/explorer/responses.d.ts +1 -1
  139. package/script/src/types/explorer/responses.d.ts.map +1 -1
  140. package/script/src/types/info/accounts.d.ts +403 -98
  141. package/script/src/types/info/accounts.d.ts.map +1 -1
  142. package/script/src/types/info/assets.d.ts +131 -35
  143. package/script/src/types/info/assets.d.ts.map +1 -1
  144. package/script/src/types/info/markets.d.ts +29 -8
  145. package/script/src/types/info/markets.d.ts.map +1 -1
  146. package/script/src/types/info/orders.d.ts +62 -17
  147. package/script/src/types/info/orders.d.ts.map +1 -1
  148. package/script/src/types/info/requests.d.ts +117 -51
  149. package/script/src/types/info/requests.d.ts.map +1 -1
  150. package/script/src/types/info/validators.d.ts +44 -14
  151. package/script/src/types/info/validators.d.ts.map +1 -1
  152. package/script/src/types/info/vaults.d.ts +25 -10
  153. package/script/src/types/info/vaults.d.ts.map +1 -1
  154. package/script/src/types/mod.d.ts +1 -1
  155. package/script/src/types/mod.d.ts.map +1 -1
  156. package/script/src/types/subscriptions/requests.d.ts +21 -66
  157. package/script/src/types/subscriptions/requests.d.ts.map +1 -1
  158. package/script/src/types/subscriptions/responses.d.ts +46 -55
  159. package/script/src/types/subscriptions/responses.d.ts.map +1 -1
  160. package/esm/src/signing/_signTypedData/window.d.ts +0 -29
  161. package/esm/src/signing/_signTypedData/window.d.ts.map +0 -1
  162. package/esm/src/signing/_signTypedData/window.js +0 -30
  163. package/script/src/signing/_signTypedData/window.d.ts +0 -29
  164. package/script/src/signing/_signTypedData/window.d.ts.map +0 -1
  165. package/script/src/signing/_signTypedData/window.js +0 -34
package/CONTRIBUTING.md CHANGED
@@ -8,7 +8,7 @@ Welcome, and thank you for taking time in contributing to SDK! You can contribut
8
8
 
9
9
  ## Development Setup
10
10
 
11
- You will need [Deno](https://deno.land/) 2.0+.
11
+ You will need [Deno](https://deno.com) 2.0+.
12
12
 
13
13
  1. Fork this repository to your own GitHub account.
14
14
  2. Clone the repository to your local device.
@@ -26,7 +26,8 @@ You can run most tests with the following command:
26
26
  deno test -A
27
27
  ```
28
28
 
29
- However, for complete testing, you will need a private key from a testnet account with funds:
29
+ However, for complete testing, you will need a private key from a testnet account with funds (~100 usdc-perps, ~3
30
+ usdc-spot, ~0.0000001 hype-spot):
30
31
 
31
32
  ```bash
32
33
  deno test -A -- YOUR_PRIVATE_KEY
package/README.md CHANGED
@@ -6,16 +6,16 @@
6
6
  [![bundlephobia](https://img.shields.io/bundlephobia/minzip/@nktkas/hyperliquid?style=flat-square)](https://bundlephobia.com/package/@nktkas/hyperliquid)
7
7
 
8
8
  Unofficial [Hyperliquid API](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api) SDK for all major JS
9
- runtimes, written in TypeScript and provided with tests.
9
+ runtimes, written in TypeScript.
10
10
 
11
11
  ## Features
12
12
 
13
13
  - 🖋️ **Typed**: Source code is 100% TypeScript.
14
- - 🧪 **Tested**: Good code coverage and type-safe API responses.
14
+ - 🧪 **Tested**: Good code coverage and type relevance.
15
15
  - 📦 **Minimal dependencies**: A few small trusted dependencies.
16
16
  - 🌐 **Cross-Environment Support**: Compatible with all major JS runtimes.
17
- - 🔧 **Integratable**: Easy to use with [viem](https://github.com/wevm/viem),
18
- [ethers](https://github.com/ethers-io/ethers.js) and other wallet libraries.
17
+ - 🔧 **Integratable**: Easy to use with wallet providers ([viem](https://github.com/wevm/viem),
18
+ [ethers](https://github.com/ethers-io/ethers.js), private key directly).
19
19
  - 📚 **Documented**: JSDoc annotations with usage examples in source code.
20
20
 
21
21
  ## Installation
@@ -55,7 +55,7 @@ deno add jsr:@nktkas/hyperliquid
55
55
  ```js
56
56
  // React Native 0.76.3 / Expo v52
57
57
  // Issues:
58
- // - signing: does not support private keys directly, use viem or ethers
58
+ // - signing: does not support private keys directly, use `viem` or `ethers`
59
59
 
60
60
  import { Event, EventTarget } from "event-target-shim";
61
61
 
@@ -97,27 +97,34 @@ if (!Promise.withResolvers) {
97
97
 
98
98
  ## Quick Start
99
99
 
100
- #### Info endpoint
100
+ ### [Info endpoint](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint)
101
101
 
102
102
  ```ts
103
+ // 1. Import module
103
104
  import * as hl from "@nktkas/hyperliquid";
104
105
 
105
- const transport = new hl.HttpTransport();
106
- const infoClient = new hl.InfoClient({ transport });
106
+ // 1. Set up client with transport
107
+ const infoClient = new hl.InfoClient({
108
+ transport: new hl.HttpTransport(), // or `WebSocketTransport`
109
+ });
107
110
 
111
+ // 3. Query data
108
112
  const openOrders = await infoClient.openOrders({ user: "0x..." });
109
113
  ```
110
114
 
111
- #### Exchange endpoint
115
+ ### [Exchange endpoint](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint)
112
116
 
113
117
  ```ts
118
+ // 1. Import module
114
119
  import * as hl from "@nktkas/hyperliquid";
115
120
 
116
- const privateKey = "0x..."; // or `viem`, `ethers`
117
-
118
- const transport = new hl.HttpTransport();
119
- const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
121
+ // 2. Set up client with wallet and transport
122
+ const exchClient = new hl.ExchangeClient({
123
+ wallet: "0x...", // `viem`, `ethers`, or private key directly
124
+ transport: new hl.HttpTransport(), // or `WebSocketTransport`
125
+ });
120
126
 
127
+ // 3. Execute an action
121
128
  const result = await exchClient.order({
122
129
  orders: [{
123
130
  a: 0,
@@ -135,38 +142,41 @@ const result = await exchClient.order({
135
142
  });
136
143
  ```
137
144
 
138
- #### Subscription
145
+ ### [Subscription](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions)
139
146
 
140
147
  ```ts
148
+ // 1. Import module
141
149
  import * as hl from "@nktkas/hyperliquid";
142
150
 
143
- const transport = new hl.WebSocketTransport();
144
- const subsClient = new hl.SubscriptionClient({ transport });
151
+ // 2. Set up client with transport
152
+ const subsClient = new hl.SubscriptionClient({
153
+ transport: new hl.WebSocketTransport(),
154
+ });
145
155
 
156
+ // 3. Subscribe to events
146
157
  const sub = await subsClient.allMids((event) => {
147
158
  console.log(event);
148
159
  });
149
-
150
- await sub.unsubscribe(); // unsubscribe from the event
160
+ await sub.unsubscribe();
151
161
  ```
152
162
 
153
- #### Multi-Sign
163
+ ### [Multi-Sign](https://hyperliquid.gitbook.io/hyperliquid-docs/hypercore/multi-sig)
154
164
 
155
165
  ```ts
166
+ // 1. Import module
156
167
  import * as hl from "@nktkas/hyperliquid";
157
168
 
158
- const multiSignAddress = "0x...";
159
- const signers = [
160
- "0x...", // Private key; or any other wallet libraries
161
- ] as const;
162
-
163
- const transport = new hl.HttpTransport();
164
- const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends `ExchangeClient`
165
-
166
- const data = await multiSignClient.approveAgent({ // same API as `ExchangeClient`
167
- agentAddress: "0x...",
168
- agentName: "agentName",
169
+ // 2. Set up client with
170
+ const multiSignClient = new hl.MultiSignClient({
171
+ transport: new hl.HttpTransport(), // or `WebSocketTransport`
172
+ multiSignAddress: "0x...",
173
+ signers: [
174
+ "0x...", // `viem`, `ethers`, or private key directly
175
+ ],
169
176
  });
177
+
178
+ // 3. Execute an action
179
+ const data = await multiSignClient.approveAgent({ agentAddress: "0x..." });
170
180
  ```
171
181
 
172
182
  ## Usage
@@ -179,26 +189,27 @@ First, choose and configure your transport layer (more details in the [API Refer
179
189
  import * as hl from "@nktkas/hyperliquid";
180
190
 
181
191
  // 1. HTTP Transport: suitable for one-time requests or serverless environments
182
- const httpTransport = new hl.HttpTransport(); // Accepts optional parameters (e.g. isTestnet, timeout, etc.)
192
+ const httpTransport = new hl.HttpTransport({...}); // Accepts optional parameters (e.g. isTestnet, timeout, etc.)
183
193
 
184
194
  // 2. WebSocket Transport: has better network latency than HTTP transport
185
- const wsTransport = new hl.WebSocketTransport(); // Accepts optional parameters (e.g. url, timeout, reconnect, etc.)
195
+ const wsTransport = new hl.WebSocketTransport({...}); // Accepts optional parameters (e.g. url, timeout, reconnect, etc.)
186
196
  ```
187
197
 
188
198
  ### 2) Initialize Client
189
199
 
190
200
  Next, initialize a client with the transport layer (more details in the [API Reference](#clients)):
191
201
 
192
- #### Create InfoClient
202
+ #### Create [InfoClient](#infoclient)
193
203
 
194
204
  ```ts
195
205
  import * as hl from "@nktkas/hyperliquid";
196
206
 
197
- const transport = new hl.HttpTransport(); // or `WebSocketTransport`
198
- const infoClient = new hl.InfoClient({ transport });
207
+ const infoClient = new hl.InfoClient({
208
+ transport: new hl.HttpTransport(), // or `WebSocketTransport`
209
+ });
199
210
  ```
200
211
 
201
- #### Create ExchangeClient
212
+ #### Create [ExchangeClient](#exchangeclient)
202
213
 
203
214
  ```ts
204
215
  import * as hl from "@nktkas/hyperliquid";
@@ -216,7 +227,7 @@ const exchClient_privateKey = new hl.ExchangeClient({ wallet: privateKey, transp
216
227
  const viemAccount = privateKeyToAccount("0x...");
217
228
  const exchClient_viem = new hl.ExchangeClient({ wallet: viemAccount, transport });
218
229
 
219
- // 3. Using Ethers (or Ethers V5)
230
+ // 3. Using Ethers (V5 or V6)
220
231
  const ethersWallet = new ethers.Wallet("0x...");
221
232
  const exchClient_ethers = new hl.ExchangeClient({ wallet: ethersWallet, transport });
222
233
 
@@ -224,70 +235,69 @@ const exchClient_ethers = new hl.ExchangeClient({ wallet: ethersWallet, transpor
224
235
  const [account] = await window.ethereum.request({ method: "eth_requestAccounts" });
225
236
  const externalWallet = createWalletClient({ account, transport: custom(window.ethereum) });
226
237
  const exchClient_viemMetamask = new hl.ExchangeClient({ wallet: externalWallet, transport });
227
-
228
- // 5. Using external wallet (e.g. MetaMask) via `window.ethereum`
229
- const exchClient_windowMetamask = new hl.ExchangeClient({ wallet: window.ethereum, transport });
230
238
  ```
231
239
 
232
- #### Create SubscriptionClient
240
+ #### Create [SubscriptionClient](#subscriptionclient)
233
241
 
234
242
  ```ts
235
243
  import * as hl from "@nktkas/hyperliquid";
236
244
 
237
- const transport = new hl.WebSocketTransport(); // only `WebSocketTransport`
238
- const subsClient = new hl.SubscriptionClient({ transport });
245
+ const subsClient = new hl.SubscriptionClient({
246
+ transport: new hl.WebSocketTransport(),
247
+ });
239
248
  ```
240
249
 
241
- #### Create MultiSignClient
250
+ #### Create [MultiSignClient](#multisignclient)
242
251
 
243
252
  ```ts
244
253
  import * as hl from "@nktkas/hyperliquid";
245
254
  import { privateKeyToAccount } from "viem/accounts";
246
255
  import { ethers } from "ethers";
247
256
 
248
- const multiSignAddress = "0x...";
249
- const signers = [
250
- privateKeyToAccount("0x..."), // first is leader for multi-sign transaction, must contain own address
251
- new ethers.Wallet("0x..."),
252
- { // can be a custom async wallet
253
- async signTypedData(params: {
254
- domain: {
255
- name: string;
256
- version: string;
257
- chainId: number;
258
- verifyingContract: Hex;
259
- };
260
- types: {
261
- [key: string]: {
257
+ const multiSignClient = new hl.MultiSignClient({
258
+ transport: new hl.HttpTransport(), // or `WebSocketTransport`
259
+ multiSignAddress: "0x...",
260
+ signers: [
261
+ privateKeyToAccount("0x..."), // first is leader for multi-sign transaction (signs transaction 2 times)
262
+ new ethers.Wallet("0x..."),
263
+ { // can be a custom async wallet
264
+ async signTypedData(params: {
265
+ domain: {
262
266
  name: string;
263
- type: string;
264
- }[];
265
- };
266
- primaryType: string;
267
- message: Record<string, unknown>;
268
- }): Promise<Hex> {
269
- // Custom signer logic
270
- return "0x..."; // return hex signature
267
+ version: string;
268
+ chainId: number;
269
+ verifyingContract: `0x${string}`;
270
+ };
271
+ types: {
272
+ [key: string]: {
273
+ name: string;
274
+ type: string;
275
+ }[];
276
+ };
277
+ primaryType: string;
278
+ message: Record<string, unknown>;
279
+ }): Promise<`0x${string}`> {
280
+ // Custom signer logic
281
+ return "0x..."; // return hex signature
282
+ },
271
283
  },
272
- },
273
- "0x...", // private key directly
274
- ];
275
-
276
- const transport = new hl.HttpTransport();
277
- const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends `ExchangeClient`
284
+ "0x...", // private key directly
285
+ ],
286
+ });
278
287
  ```
279
288
 
280
289
  ### 3) Use Client
281
290
 
282
291
  Finally, use client methods to interact with the Hyperliquid API (more details in the [API Reference](#clients)):
283
292
 
284
- #### Example of using an InfoClient
293
+ #### Example of using an [InfoClient](#infoclient)
285
294
 
286
295
  ```ts
287
296
  import * as hl from "@nktkas/hyperliquid";
288
297
 
289
- const transport = new hl.HttpTransport();
290
- const infoClient = new hl.InfoClient({ transport });
298
+ const infoClient = new hl.InfoClient({
299
+ transport: new hl.HttpTransport(), // or `WebSocketTransport`
300
+ });
291
301
 
292
302
  // L2 Book
293
303
  const l2Book = await infoClient.l2Book({ coin: "BTC" });
@@ -299,15 +309,15 @@ const clearinghouseState = await infoClient.clearinghouseState({ user: "0x..." }
299
309
  const openOrders = await infoClient.openOrders({ user: "0x..." });
300
310
  ```
301
311
 
302
- #### Example of using an ExchangeClient
312
+ #### Example of using an [ExchangeClient](#exchangeclient)
303
313
 
304
314
  ```ts
305
315
  import * as hl from "@nktkas/hyperliquid";
306
316
 
307
- const privateKey = "0x..."; // or `viem`, `ethers`
308
-
309
- const transport = new hl.HttpTransport();
310
- const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
317
+ const exchClient = new hl.ExchangeClient({
318
+ wallet: "0x...", // `viem`, `ethers`, or private key directly
319
+ transport: new hl.HttpTransport(), // or `WebSocketTransport`
320
+ });
311
321
 
312
322
  // Place an orders
313
323
  const result = await exchClient.order({
@@ -327,25 +337,20 @@ const result = await exchClient.order({
327
337
  });
328
338
 
329
339
  // Approve an agent
330
- const result = await exchClient.approveAgent({
331
- agentAddress: "0x...",
332
- agentName: "agentName",
333
- });
340
+ const result = await exchClient.approveAgent({ agentAddress: "0x..." });
334
341
 
335
342
  // Withdraw funds
336
- const result = await exchClient.withdraw3({
337
- destination: account.address,
338
- amount: "100",
339
- });
343
+ const result = await exchClient.withdraw3({ destination: "0x...", amount: "100" });
340
344
  ```
341
345
 
342
- #### Example of using a SubscriptionClient
346
+ #### Example of using a [SubscriptionClient](#subscriptionclient)
343
347
 
344
348
  ```ts
345
349
  import * as hl from "@nktkas/hyperliquid";
346
350
 
347
- const transport = new hl.WebSocketTransport();
348
- const subsClient = new hl.SubscriptionClient({ transport });
351
+ const subsClient = new hl.SubscriptionClient({
352
+ transport: new hl.WebSocketTransport(),
353
+ });
349
354
 
350
355
  // L2 Book updates
351
356
  await subsClient.l2Book({ coin: "BTC" }, (data) => {
@@ -358,54 +363,25 @@ await subsClient.userFills({ user: "0x..." }, (data) => {
358
363
  });
359
364
 
360
365
  // Candle updates
361
- const sub = await subsClient.candle({ coin: "BTC", interval: "1h" }, (data) => {
366
+ await subsClient.candle({ coin: "BTC", interval: "1h" }, (data) => {
362
367
  console.log(data);
363
368
  });
364
369
  ```
365
370
 
366
- #### Example of using a MultiSignClient
371
+ #### Example of using a [MultiSignClient](#multisignclient)
367
372
 
368
373
  ```ts
369
374
  import * as hl from "@nktkas/hyperliquid";
370
375
 
371
- const multiSignAddress = "0x...";
372
- const signers = [
373
- "0x...", // Private keys
374
- ] as const;
375
-
376
- const transport = new hl.HttpTransport();
377
- const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
378
-
379
- // Interaction is the same as with `ExchangeClient`
380
-
381
- // Place an orders
382
- const result = await multiSignClient.order({
383
- orders: [{
384
- a: 0,
385
- b: true,
386
- p: "30000",
387
- s: "0.1",
388
- r: false,
389
- t: {
390
- limit: {
391
- tif: "Gtc",
392
- },
393
- },
394
- }],
395
- grouping: "na",
396
- });
397
-
398
- // Approve an agent
399
- const result = await multiSignClient.approveAgent({
400
- agentAddress: "0x...",
401
- agentName: "agentName",
376
+ const multiSignClient = new hl.MultiSignClient({
377
+ transport: new hl.HttpTransport(), // or `WebSocketTransport`
378
+ multiSignAddress: "0x...",
379
+ signers: [
380
+ "0x...", // `viem`, `ethers`, or private key directly
381
+ ],
402
382
  });
403
383
 
404
- // Withdraw funds
405
- const result = await multiSignClient.withdraw3({
406
- destination: account.address,
407
- amount: "100",
408
- });
384
+ // Interaction is the same as with `ExchangeClient`
409
385
  ```
410
386
 
411
387
  ## API Reference
@@ -414,9 +390,6 @@ const result = await multiSignClient.withdraw3({
414
390
 
415
391
  A client is an interface through which you can interact with the Hyperliquid API.
416
392
 
417
- The client is responsible for formatting an action, creating a signature correctly, sending a request, and validating a
418
- response.
419
-
420
393
  #### InfoClient
421
394
 
422
395
  ```ts
@@ -503,16 +476,11 @@ class InfoClient {
503
476
  class ExchangeClient {
504
477
  constructor(args: {
505
478
  transport: HttpTransport | WebSocketTransport;
506
- wallet:
507
- | Hex // Private key directly
508
- | AbstractViemWalletClient // viem
509
- | AbstractEthersSigner // ethers
510
- | AbstractEthersV5Signer // ethers v5
511
- | AbstractWindowEthereum; // window.ethereum (EIP-1193)
479
+ wallet: AbstractWallet; // `viem`, `ethers` (v5 or v6), or private key directly
512
480
  isTestnet?: boolean; // Whether to use testnet (default: false)
513
- defaultVaultAddress?: Hex; // Vault address used by default if not provided in method call
514
- signatureChainId?: Hex | (() => MaybePromise<Hex>); // Chain ID used for signing (default: trying to guess based on wallet and `isTestnet`)
515
- nonceManager?: () => MaybePromise<number>; // Function to get the next nonce (default: auto-incrementing `Date.now()`)
481
+ defaultVaultAddress?: `0x${string}`; // Vault address used by default if not provided in method call
482
+ signatureChainId?: `0x${string}` | (() => MaybePromise<`0x${string}`>); // Chain ID used for signing (default: get chain id from wallet otherwise `0x1`)
483
+ nonceManager?: () => MaybePromise<number>; // Function to get the next nonce (default: monotonically incrementing `Date.now()`)
516
484
  });
517
485
 
518
486
  // Order
@@ -537,11 +505,10 @@ class ExchangeClient {
537
505
  reserveRequestWeight(args: ReserveRequestWeightParameters): Promise<SuccessResponse>;
538
506
  setDisplayName(args: SetDisplayNameParameters): Promise<SuccessResponse>;
539
507
  setReferrer(args: SetReferrerParameters): Promise<SuccessResponse>;
508
+ subAccountModify(args: SubAccountModifyParameters): Promise<SuccessResponse>;
540
509
  spotUser(args: SpotUserParameters): Promise<SuccessResponse>;
541
510
 
542
511
  // Transfer
543
- perpDexClassTransfer(args: PerpDexClassTransferParameters): Promise<SuccessResponse>;
544
- perpDexTransfer(args: PerpDexTransferParameters): Promise<SuccessResponse>;
545
512
  spotSend(args: SpotSendParameters): Promise<SuccessResponse>;
546
513
  subAccountSpotTransfer(args: SubAccountSpotTransferParameters): Promise<SuccessResponse>;
547
514
  subAccountTransfer(args: SubAccountTransferParameters): Promise<SuccessResponse>;
@@ -585,7 +552,7 @@ class SubscriptionClient {
585
552
 
586
553
  // Market
587
554
  activeAssetCtx(args: EventActiveAssetCtxParameters, listener: (data: WsActiveAssetCtx | WsActiveSpotAssetCtx) => void): Promise<Subscription>;
588
- activeAssetData(args: EventActiveAssetDataParameters, listener: (data: WsActiveAssetData) => void): Promise<Subscription>;
555
+ activeAssetData(args: EventActiveAssetDataParameters, listener: (data: ActiveAssetData) => void): Promise<Subscription>;
589
556
  allMids(listener: (data: WsAllMids) => void): Promise<Subscription>;
590
557
  bbo(args: EventBboParameters, listener: (data: WsBbo) => void): Promise<Subscription>;
591
558
  candle(args: EventCandleParameters, listener: (data: Candle) => void): Promise<Subscription>;
@@ -605,7 +572,7 @@ class SubscriptionClient {
605
572
  userTwapHistory(args: EventUserTwapHistory, listener: (data: WsUserTwapHistory) => void): Promise<Subscription>;
606
573
  userTwapSliceFills(args: EventUserTwapSliceFills, listener: (data: WsUserTwapSliceFills) => void): Promise<Subscription>;
607
574
 
608
- // Explorer
575
+ // Explorer (RPC endpoint)
609
576
  explorerBlock(listener: (data: WsBlockDetails[]) => void): Promise<Subscription>;
610
577
  explorerTxs(listener: (data: TxDetails[]) => void): Promise<Subscription>;
611
578
  }
@@ -618,13 +585,10 @@ class SubscriptionClient {
618
585
  class MultiSignClient extends ExchangeClient {
619
586
  constructor(
620
587
  args:
621
- & Omit<ExchangeClientParameters, "wallet"> // Instead of `wallet`, you should specify the following parameters:
588
+ & Omit<ExchangeClientParameters, "wallet"> // instead of `wallet`, you should specify the following parameters:
622
589
  & {
623
- multiSignAddress: Hex; // Multi-signature address
624
- signers: [ // Array of signers
625
- AbstractWalletWithAddress, // First signer is the leader of a multi-sign transaction
626
- ...AbstractWallet[], // Any number of additional signers
627
- ];
590
+ multiSignAddress: `0x${string}`;
591
+ signers: [AbstractWallet, ...AbstractWallet[]];
628
592
  },
629
593
  );
630
594
 
@@ -638,7 +602,11 @@ Transport acts as a layer between class requests and Hyperliquid servers.
638
602
 
639
603
  #### HTTP Transport
640
604
 
641
- HTTP transport is suitable for one-off requests or serverless environments.
605
+ **Features:**
606
+
607
+ - Uses [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) for requests. Can be configured using
608
+ [`fetchOptions`](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit).
609
+ - Automatically determines the target URL based on the request + `isTestnet` flag.
642
610
 
643
611
  ```ts
644
612
  class HttpTransport {
@@ -658,7 +626,20 @@ class HttpTransport {
658
626
 
659
627
  #### WebSocket Transport
660
628
 
661
- WebSocket transport has better network latency than HTTP transport.
629
+ **Features:**
630
+
631
+ - Uses [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) for requests.
632
+ - Supports [subscriptions](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions)
633
+ and [post requests](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/post-requests).
634
+ - Automatically restores connection after loss and resubscribes to previous subscriptions.
635
+ - Smart keep alive (pings only when idle).
636
+ - Lazy initialization with message buffering during connection establishment.
637
+
638
+ **Limitations:**
639
+
640
+ - Cannot mix api/explorer endpoints or mainnet/testnet in single connection. Need to create separate instances for
641
+ different endpoints.
642
+ - Cannot send explorer post-requests via WebSocket. Use [HTTP transport](#http-transport).
662
643
 
663
644
  ```ts
664
645
  class WebSocketTransport {
@@ -701,27 +682,23 @@ The import point gives access to functions that generate signatures for Hyperliq
701
682
  ```ts
702
683
  import { actionSorter, signL1Action } from "@nktkas/hyperliquid/signing";
703
684
 
704
- const privateKey = "0x..."; // or `viem`, `ethers`
685
+ const privateKey = "0x..."; // `viem`, `ethers`, or private key directly
705
686
 
706
- const nonce = Date.now();
707
- const action = {
687
+ const action = actionSorter.cancel({
708
688
  type: "cancel",
709
689
  cancels: [
710
690
  { a: 0, o: 12345 },
711
691
  ],
712
- } as const;
713
-
714
- const signature = await signL1Action({
715
- wallet: privateKey,
716
- action: actionSorter[action.type](action),
717
- nonce,
718
692
  });
693
+ const nonce = Date.now();
694
+
695
+ const signature = await signL1Action({ wallet: privateKey, action, nonce });
719
696
 
720
697
  // Send the signed action to the Hyperliquid API
721
698
  const response = await fetch("https://api.hyperliquid.xyz/exchange", {
722
699
  method: "POST",
723
700
  headers: { "Content-Type": "application/json" },
724
- body: JSON.stringify({ action, signature, nonce }),
701
+ body: JSON.stringify({ action, signature, nonce }), // recommended to send the same formatted action
725
702
  });
726
703
  const body = await response.json();
727
704
  ```
@@ -729,18 +706,18 @@ const body = await response.json();
729
706
  #### Approve agent yourself
730
707
 
731
708
  ```ts
732
- import { signUserSignedAction, userSignedActionEip712Types } from "@nktkas/hyperliquid/signing";
709
+ import { actionSorter, signUserSignedAction, userSignedActionEip712Types } from "@nktkas/hyperliquid/signing";
733
710
 
734
- const privateKey = "0x..."; // or `viem`, `ethers`
711
+ const privateKey = "0x..."; // `viem`, `ethers`, or private key directly
735
712
 
736
- const action = {
713
+ const action = actionSorter.approveAgent({
737
714
  type: "approveAgent",
738
715
  signatureChainId: "0x66eee",
739
716
  hyperliquidChain: "Mainnet",
740
717
  agentAddress: "0x...",
741
718
  agentName: "Agent",
742
719
  nonce: Date.now(),
743
- } as const;
720
+ });
744
721
 
745
722
  const signature = await signUserSignedAction({
746
723
  wallet: privateKey,
@@ -752,7 +729,7 @@ const signature = await signUserSignedAction({
752
729
  const response = await fetch("https://api.hyperliquid.xyz/exchange", {
753
730
  method: "POST",
754
731
  headers: { "Content-Type": "application/json" },
755
- body: JSON.stringify({ action, signature, nonce: action.nonce }),
732
+ body: JSON.stringify({ action, signature, nonce: action.nonce }), // recommended to send the same formatted action
756
733
  });
757
734
  const body = await response.json();
758
735
  ```
@@ -771,12 +748,19 @@ L1 action through an external wallet:
771
748
 
772
749
  ### How to create a market order?
773
750
 
774
- Hyperliquid doesn't have traditional market orders, but you can achieve market-like execution by placing limit orders
775
- with `tif: "Ioc"` and prices that guarantee immediate execution:
751
+ Hyperliquid doesn't have traditional market orders, but you can achieve market-like execution by placing limit order
752
+ with `tif: "Ioc"` and price that guarantee immediate execution:
776
753
 
777
754
  - For buys: set limit price >= current best ask
778
755
  - For sells: set limit price <= current best bid
779
756
 
757
+ ### How to use the [Agent Wallet](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/nonces-and-api-wallets#api-wallets) / [Vault](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#subaccounts-and-vaults) / [Sub-Account](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#subaccounts-and-vaults) in `ExchangeClient`?
758
+
759
+ **Agent Wallet**: Use agent's private key in constructor instead of master account's private key.
760
+
761
+ **Vault and Sub-Account**: Pass vault or sub-account address via `vaultAddress` options to methods or set
762
+ `defaultVaultAddress` in constructor.
763
+
780
764
  ## Contributing
781
765
 
782
766
  We appreciate your help! To contribute, please read the [contributing instructions](CONTRIBUTING.md).
package/esm/mod.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from "./src/base.js";
2
2
  export * from "./src/transports/base.js";
3
- export type { AbstractEthersSigner, AbstractEthersV5Signer, AbstractViemWalletClient, AbstractWindowEthereum, } from "./src/signing/mod.js";
3
+ export type { AbstractWallet } from "./src/signing/mod.js";
4
4
  export * from "./src/clients/exchange.js";
5
5
  export * from "./src/clients/info.js";
6
6
  export * from "./src/clients/multiSign.js";
package/esm/mod.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AACA,cAAc,eAAe,CAAC;AAC9B,cAAc,0BAA0B,CAAC;AAGzC,YAAY,EACR,oBAAoB,EACpB,sBAAsB,EACtB,wBAAwB,EACxB,sBAAsB,GACzB,MAAM,sBAAsB,CAAC;AAG9B,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAG9C,cAAc,yCAAyC,CAAC;AACxD,cAAc,mDAAmD,CAAC;AAGlE,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,8BAA8B,CAAC;AAClD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,gCAAgC,CAAC;AACpD,mBAAmB,6BAA6B,CAAC;AACjD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,wCAAwC,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AACA,cAAc,eAAe,CAAC;AAC9B,cAAc,0BAA0B,CAAC;AAGzC,YAAY,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAG3D,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAG9C,cAAc,yCAAyC,CAAC;AACxD,cAAc,mDAAmD,CAAC;AAGlE,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,8BAA8B,CAAC;AAClD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,gCAAgC,CAAC;AACpD,mBAAmB,6BAA6B,CAAC;AACjD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,wCAAwC,CAAC"}
package/esm/src/base.d.ts CHANGED
@@ -1,8 +1,3 @@
1
- export type Hex = `0x${string}`;
2
- export type MaybePromise<T> = T | Promise<T>;
3
- export type DeepImmutable<T> = {
4
- readonly [K in keyof T]: DeepImmutable<T[K]>;
5
- };
6
1
  /** Base error class for all SDK errors. */
7
2
  export declare class HyperliquidError extends Error {
8
3
  constructor(message?: string, options?: ErrorOptions);
@@ -1 +1 @@
1
- {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/src/base.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AAEhC,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAG7C,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI;IAC3B,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAC/C,CAAC;AAEF,2CAA2C;AAC3C,qBAAa,gBAAiB,SAAQ,KAAK;gBAC3B,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIvD"}
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/src/base.ts"],"names":[],"mappings":"AAAA,2CAA2C;AAC3C,qBAAa,gBAAiB,SAAQ,KAAK;gBAC3B,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIvD"}