@nktkas/hyperliquid 0.16.0 โ†’ 0.17.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 (50) hide show
  1. package/README.md +44 -92
  2. package/esm/src/clients/wallet.d.ts +117 -90
  3. package/esm/src/clients/wallet.d.ts.map +1 -1
  4. package/esm/src/clients/wallet.js +278 -222
  5. package/esm/src/signing.d.ts +105 -32
  6. package/esm/src/signing.d.ts.map +1 -1
  7. package/esm/src/signing.js +79 -15
  8. package/esm/src/transports/http/http_transport.d.ts +14 -24
  9. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  10. package/esm/src/transports/http/http_transport.js +20 -15
  11. package/esm/src/transports/websocket/websocket_transport.d.ts +6 -2
  12. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  13. package/esm/src/types/exchange/requests.d.ts +28 -14
  14. package/esm/src/types/exchange/requests.d.ts.map +1 -1
  15. package/esm/src/types/info/accounts.d.ts +4 -0
  16. package/esm/src/types/info/accounts.d.ts.map +1 -1
  17. package/esm/src/types/info/assets.d.ts +2 -0
  18. package/esm/src/types/info/assets.d.ts.map +1 -1
  19. package/esm/src/types/info/orders.d.ts +28 -20
  20. package/esm/src/types/info/orders.d.ts.map +1 -1
  21. package/esm/src/types/info/vaults.d.ts +2 -0
  22. package/esm/src/types/info/vaults.d.ts.map +1 -1
  23. package/esm/src/types/mod.d.ts +23 -0
  24. package/esm/src/types/mod.d.ts.map +1 -1
  25. package/esm/src/types/mod.js +23 -0
  26. package/package.json +4 -1
  27. package/script/src/clients/wallet.d.ts +117 -90
  28. package/script/src/clients/wallet.d.ts.map +1 -1
  29. package/script/src/clients/wallet.js +278 -222
  30. package/script/src/signing.d.ts +105 -32
  31. package/script/src/signing.d.ts.map +1 -1
  32. package/script/src/signing.js +80 -16
  33. package/script/src/transports/http/http_transport.d.ts +14 -24
  34. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  35. package/script/src/transports/http/http_transport.js +20 -15
  36. package/script/src/transports/websocket/websocket_transport.d.ts +6 -2
  37. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  38. package/script/src/types/exchange/requests.d.ts +28 -14
  39. package/script/src/types/exchange/requests.d.ts.map +1 -1
  40. package/script/src/types/info/accounts.d.ts +4 -0
  41. package/script/src/types/info/accounts.d.ts.map +1 -1
  42. package/script/src/types/info/assets.d.ts +2 -0
  43. package/script/src/types/info/assets.d.ts.map +1 -1
  44. package/script/src/types/info/orders.d.ts +28 -20
  45. package/script/src/types/info/orders.d.ts.map +1 -1
  46. package/script/src/types/info/vaults.d.ts +2 -0
  47. package/script/src/types/info/vaults.d.ts.map +1 -1
  48. package/script/src/types/mod.d.ts +23 -0
  49. package/script/src/types/mod.d.ts.map +1 -1
  50. package/script/src/types/mod.js +23 -0
package/README.md CHANGED
@@ -11,12 +11,11 @@ runtimes, written in TypeScript and provided with tests.
11
11
  ## Features
12
12
 
13
13
  - ๐Ÿ–‹๏ธ **Typed**: Source code is 100% TypeScript.
14
- - ๐Ÿงช **Tested**: Good code coverage and type testing.
15
- - ๐Ÿ“ฆ **Minimal dependencies**: Few small dependencies, standard JS is favored.
16
- - ๐ŸŒ **Cross-Environment Support**: Compatible with all major JS runtimes, including Node.js, Deno, Bun, and browser
17
- environments.
14
+ - ๐Ÿงช **Tested**: Good code coverage and guarantee of type validity.
15
+ - ๐Ÿ“ฆ **Minimal dependencies**: A few small trusted dependencies.
16
+ - ๐ŸŒ **Cross-Environment Support**: Compatible with all major JS runtimes.
18
17
  - ๐Ÿ”ง **Integratable**: Easy to use with [viem](https://github.com/wevm/viem),
19
- [ethers](https://github.com/ethers-io/ethers.js) and web3 wallets.
18
+ [ethers](https://github.com/ethers-io/ethers.js) and other wallet libraries.
20
19
  - ๐Ÿ“š **Documented**: Comprehensive documentation and usage examples, provided directly in JSDoc annotations within the
21
20
  source code.
22
21
 
@@ -26,7 +25,7 @@ runtimes, written in TypeScript and provided with tests.
26
25
  # npm
27
26
  npm i @nktkas/hyperliquid
28
27
 
29
- # yarn
28
+ # yarn
30
29
  yarn add @nktkas/hyperliquid
31
30
 
32
31
  # pnpm
@@ -44,7 +43,7 @@ import * as hl from "https://esm.sh/jsr/@nktkas/hyperliquid"
44
43
 
45
44
  ## Usage
46
45
 
47
- ### Initialize Transport
46
+ ### 1) Initialize Transport
48
47
 
49
48
  First, choose and configure your transport layer (more details in the [API Reference](#transports)):
50
49
 
@@ -52,13 +51,13 @@ First, choose and configure your transport layer (more details in the [API Refer
52
51
  import * as hl from "@nktkas/hyperliquid"; // ESM & Common.js
53
52
 
54
53
  // HTTP Transport
55
- const httpTransport = new hl.HttpTransport(); // Accepts optional parameters (e.g. url, timeout, fetchOptions)
54
+ const httpTransport = new hl.HttpTransport(); // Accepts optional parameters
56
55
 
57
- // or WebSocket Transport
58
- const wsTransport = new hl.WebSocketTransport(); // Accepts optional parameters (e.g. url, timeout, keepAlive, reconnect)
56
+ // WebSocket Transport
57
+ const wsTransport = new hl.WebSocketTransport(); // Accepts optional parameters
59
58
  ```
60
59
 
61
- ### Initialize Client
60
+ ### 2) Initialize Client
62
61
 
63
62
  Next, initialize a client with the transport layer (more details in the [API Reference](#clients)):
64
63
 
@@ -103,11 +102,11 @@ const windowMetamaskClient = new hl.WalletClient({ wallet: window.ethereum, tran
103
102
  ```typescript
104
103
  import * as hl from "@nktkas/hyperliquid"; // ESM & Common.js
105
104
 
106
- const transport = new hl.WebSocketTransport(); // Only WebSocketTransport is supported
105
+ const transport = new hl.WebSocketTransport(); // Only WebSocketTransport
107
106
  const client = new hl.EventClient({ transport });
108
107
  ```
109
108
 
110
- ### Use Client
109
+ ### 3) Use Client
111
110
 
112
111
  Finally, use client methods to interact with the Hyperliquid API (more details in the [API Reference](#clients)):
113
112
 
@@ -178,33 +177,29 @@ const transport = new hl.WebSocketTransport();
178
177
  const client = new hl.EventClient({ transport });
179
178
 
180
179
  // L2 Book updates
181
- // Promise is resolved when the subscription is set up
182
180
  const sub = await client.l2Book({ coin: "BTC" }, (data) => {
183
181
  console.log(data);
184
182
  });
185
- // Later, you can unsubscribe from receiving events
186
- await sub.unsubscribe();
183
+ await sub.unsubscribe(); // Unsubscribe from the event
187
184
 
188
185
  // User fills
189
186
  const sub = await client.userFills({ user: "0x..." }, (data) => {
190
187
  console.log(data);
191
188
  });
192
- await sub.unsubscribe();
189
+ await sub.unsubscribe(); // Unsubscribe from the event
193
190
 
194
191
  // Explorer block updates
195
192
  const sub = await client.explorerBlock((data) => {
196
193
  console.log(data);
197
194
  });
198
- await sub.unsubscribe();
195
+ await sub.unsubscribe(); // Unsubscribe from the event
199
196
  ```
200
197
 
201
198
  ## API Reference
202
199
 
203
200
  ### Clients
204
201
 
205
- A **Client** provides access to the Hyperliquid API endpoints.
206
-
207
- There are three types of **Clients** in the sdk:
202
+ A Client provides access to the Hyperliquid API endpoints.
208
203
 
209
204
  #### Public Client
210
205
 
@@ -289,10 +284,8 @@ A Wallet Client which provides access to
289
284
  and `withdraw3`.
290
285
 
291
286
  The Wallet Client class sets up with a given [Transport](#transports) and a wallet instance, which can be a
292
- [Viem Wallet](https://viem.sh/docs/clients/wallet) or an
293
- [Ethers Wallet](https://docs.ethers.org/v6/api/providers/#Signer).
294
-
295
- > NOTE: When using a web3 wallet, the wallet network must be set to 1337 for proper generation of L1 transactions.
287
+ [viem](https://viem.sh/docs/clients/wallet), [ethers.js](https://docs.ethers.org/v6/api/providers/#Signer) or other
288
+ wallet libraries.
296
289
 
297
290
  ```typescript
298
291
  class WalletClient {
@@ -300,13 +293,14 @@ class WalletClient {
300
293
  transport: HttpTransport | WebSocketTransport;
301
294
  wallet:
302
295
  | AbstractViemWalletClient // viem
303
- | AbstractExtendedViemWalletClient // extended viem (e.g. privy)
304
296
  | AbstractEthersSigner // ethers
305
297
  | AbstractEthersV5Signer // ethers v5
298
+ | AbstractExtendedViemWalletClient // privy
306
299
  | AbstractWindowEthereum; // window.ethereum (EIP-1193) directly
307
- isTestnet?: boolean; // Whether to use testnet API (default: false)
300
+ isTestnet?: boolean; // Whether to use testnet (default: false)
308
301
  defaultVaultAddress?: Hex; // Vault address used by default if not provided in method call
309
- signatureChainId?: Hex; // Chain ID used for signing (default: trying to guess based on wallet and isTestnet)
302
+ signatureChainId?: Hex | (() => MaybePromise<Hex>); // Chain ID used for signing (default: trying to guess based on wallet and isTestnet)
303
+ nonceManager?: () => MaybePromise<number>; // Function to get the next nonce (default: auto-incrementing Date.now())
310
304
  });
311
305
 
312
306
  // Order
@@ -324,9 +318,10 @@ class WalletClient {
324
318
  // Account
325
319
  approveAgent(args: ApproveAgentParameters): Promise<SuccessResponse>;
326
320
  approveBuilderFee(args: ApproveBuilderFeeParameters): Promise<SuccessResponse>;
327
- claimRewards(args: ClaimRewardsParameters): Promise<SuccessResponse>;
321
+ claimRewards(): Promise<SuccessResponse>;
328
322
  createSubAccount(args: CreateSubAccountParameters): Promise<CreateSubAccountResponse>;
329
323
  evmUserModify(args: EvmUserModifyParameters): Promise<SuccessResponse>;
324
+ reserveRequestWeight(args: ReserveRequestWeightParameters): Promise<SuccessResponse>;
330
325
  setDisplayName(args: SetDisplayNameParameters): Promise<SuccessResponse>;
331
326
  setReferrer(args: SetReferrerParameters): Promise<SuccessResponse>;
332
327
  spotUser(args: SpotUserParameters): Promise<SuccessResponse>;
@@ -400,10 +395,10 @@ class EventClient {
400
395
 
401
396
  ### Transports
402
397
 
403
- A [Client](#clients) is instantiated with a **Transport**, which is the intermediary layer that is responsible for
404
- executing outgoing requests (ie. API calls and event listeners).
398
+ A [Client](#clients) is instantiated with a Transport, which is the intermediary layer that is responsible for executing
399
+ outgoing requests (ie. API calls and event listeners).
405
400
 
406
- There are two types of **Transports** in the sdk:
401
+ There are two types of Transports in the sdk:
407
402
 
408
403
  #### HTTP Transport
409
404
 
@@ -413,7 +408,7 @@ API.
413
408
  ```typescript
414
409
  class HttpTransport {
415
410
  constructor(options?: {
416
- url?: string | URL; // Base URL for API endpoints (default: "https://hyperliquid.xyz")
411
+ isTestnet?: boolean; // Whether to use testnet url (default: false)
417
412
  timeout?: number; // Request timeout in ms (default: 10_000)
418
413
  fetchOptions?: RequestInit; // A custom fetch options
419
414
  onRequest?: (request: Request) => MaybePromise<Request | void | null | undefined>; // A callback before request is sent
@@ -482,70 +477,50 @@ Useful if you want to sign a Hyperliquid transaction yourself.
482
477
 
483
478
  ```typescript
484
479
  import { signL1Action } from "@nktkas/hyperliquid/signing";
485
- import type { CancelRequest, CancelResponse } from "@nktkas/hyperliquid/types";
486
480
  import { privateKeyToAccount } from "viem/accounts";
487
481
 
488
- // โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€” Prepare โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
489
-
490
482
  const wallet = privateKeyToAccount("0x..."); // Change to your private key
491
483
 
492
- // The CancelRequest["action"] type ensures that we collect the correct cancel request action
493
- const action: CancelRequest["action"] = {
484
+ const action = {
494
485
  type: "cancel",
495
486
  cancels: [
496
- { a: 0, o: 12345 },
487
+ { a: 0, o: 12345 }, // Asset index and order ID
497
488
  ],
498
489
  };
499
490
  const nonce = Date.now();
500
491
 
501
- // โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€” Signing โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
502
-
503
- const signature = await signL1Action({ wallet, action, nonce, isTestnet: true });
504
-
505
- // โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€” Request โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
506
-
507
- // The CancelRequest type guarantees us that the object we want to send is valid in terms of types
508
- const request: CancelRequest = { action, signature, nonce };
492
+ const signature = await signL1Action({
493
+ wallet,
494
+ action,
495
+ nonce,
496
+ isTestnet: true, // Change to false for mainnet
497
+ });
509
498
 
510
499
  const response = await fetch("https://api.hyperliquid-testnet.xyz/exchange", {
511
500
  method: "POST",
512
501
  headers: { "Content-Type": "application/json" },
513
- body: JSON.stringify(request),
502
+ body: JSON.stringify({ action, signature, nonce }),
514
503
  });
515
-
516
- if (!response.ok) {
517
- const body = await response.text();
518
- throw new Error(`Failed to cancel order: ${response.statusText} ${body}`);
519
- }
520
-
521
- // If we sent a cancel request and received a successful response, then we got exactly the CancelResponse type
522
- const body = await response.json() as CancelResponse;
523
- console.log("Order cancel response:", body);
504
+ const body = await response.json();
524
505
  ```
525
506
 
526
507
  #### Approve an agent without a client
527
508
 
528
509
  ```typescript
529
510
  import { signUserSignedAction } from "@nktkas/hyperliquid/signing";
530
- import type { ApproveAgentRequest, ErrorResponse, SuccessResponse } from "@nktkas/hyperliquid/types";
531
511
  import { privateKeyToAccount } from "viem/accounts";
532
512
 
533
- // โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€” Prepare โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
534
-
535
513
  const wallet = privateKeyToAccount("0x..."); // Change to your private key
536
514
 
537
- // The ApproveAgentRequest["action"] type ensures that we collect the correct approve agent request action
538
- const action: ApproveAgentRequest["action"] = {
515
+ const action = {
539
516
  type: "approveAgent",
540
- hyperliquidChain: "Testnet",
517
+ hyperliquidChain: "Testnet", // "Mainnet" or "Testnet"
541
518
  signatureChainId: "0x66eee",
542
519
  nonce: Date.now(),
543
- agentAddress: "0x...", // Change to the agent address you want to approve
544
- agentName: "TempAgent",
520
+ agentAddress: "0x...", // Change to your agent address
521
+ agentName: "Agent",
545
522
  };
546
523
 
547
- // โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€” Signing โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
548
-
549
524
  const signature = await signUserSignedAction({
550
525
  wallet,
551
526
  action,
@@ -560,33 +535,10 @@ const signature = await signUserSignedAction({
560
535
  chainId: parseInt(action.signatureChainId, 16),
561
536
  });
562
537
 
563
- // โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€” Request โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”
564
-
565
- // The ApproveAgentRequest type guarantees us that the object we want to send is valid in terms of types
566
- const request: ApproveAgentRequest = { action, signature, nonce: action.nonce };
567
-
568
538
  const response = await fetch("https://api.hyperliquid-testnet.xyz/exchange", {
569
539
  method: "POST",
570
540
  headers: { "Content-Type": "application/json" },
571
- body: JSON.stringify(request),
541
+ body: JSON.stringify({ action, signature, nonce: action.nonce }),
572
542
  });
573
-
574
- if (!response.ok) {
575
- const body = await response.text();
576
- throw new Error(`Failed to approve agent: ${response.statusText} ${body}`);
577
- }
578
-
579
- // If we sent a request for agent approval and received a successful response,
580
- // we will get either a SuccessResponse type or an ErrorResponse type
581
- const body = await response.json() as SuccessResponse | ErrorResponse;
582
- console.log("Agent approval response:", body);
543
+ const body = await response.json();
583
544
  ```
584
-
585
- ## Contributing
586
-
587
- Contributions are welcome! Please see the [CONTRIBUTING](./CONTRIBUTING.md) file for guidelines on how to contribute to
588
- this project.
589
-
590
- ## License
591
-
592
- This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.