@nktkas/hyperliquid 0.19.2 → 0.21.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.
- package/README.md +61 -79
- package/esm/mod.d.ts +5 -3
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +4 -3
- package/esm/src/base.d.ts +1 -47
- package/esm/src/base.d.ts.map +1 -1
- package/esm/src/base.js +1 -8
- package/{script/src/clients/wallet.d.ts → esm/src/clients/exchange.d.ts} +111 -112
- package/esm/src/clients/exchange.d.ts.map +1 -0
- package/esm/src/clients/{wallet.js → exchange.js} +96 -306
- package/esm/src/clients/{public.d.ts → info.d.ts} +121 -102
- package/esm/src/clients/info.d.ts.map +1 -0
- package/esm/src/clients/{public.js → info.js} +73 -57
- package/{script/src/clients/event.d.ts → esm/src/clients/subscription.d.ts} +61 -80
- package/esm/src/clients/subscription.d.ts.map +1 -0
- package/esm/src/clients/{event.js → subscription.js} +106 -136
- package/esm/src/signing.d.ts +4 -0
- package/esm/src/signing.d.ts.map +1 -1
- package/esm/src/signing.js +23 -5
- package/esm/src/transports/base.d.ts +47 -0
- package/esm/src/transports/base.d.ts.map +1 -0
- package/esm/src/transports/base.js +8 -0
- package/esm/src/transports/http/http_transport.d.ts +8 -5
- package/esm/src/transports/http/http_transport.d.ts.map +1 -1
- package/esm/src/transports/http/http_transport.js +15 -62
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +36 -39
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
- package/esm/src/transports/websocket/_reconnecting_websocket.d.ts +21 -27
- package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
- package/esm/src/transports/websocket/_reconnecting_websocket.js +89 -182
- package/{script/src/transports/websocket/_websocket_request_dispatcher.d.ts → esm/src/transports/websocket/_websocket_async_request.d.ts} +17 -19
- package/esm/src/transports/websocket/_websocket_async_request.d.ts.map +1 -0
- package/esm/src/transports/websocket/_websocket_async_request.js +177 -0
- package/esm/src/transports/websocket/websocket_transport.d.ts +54 -41
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/esm/src/transports/websocket/websocket_transport.js +101 -113
- package/esm/src/types/info/accounts.d.ts +1 -0
- package/esm/src/types/info/accounts.d.ts.map +1 -1
- package/esm/src/types/info/assets.d.ts +0 -48
- package/esm/src/types/info/assets.d.ts.map +1 -1
- package/esm/src/types/info/markets.d.ts +52 -0
- package/esm/src/types/info/markets.d.ts.map +1 -0
- package/esm/src/types/info/markets.js +1 -0
- package/esm/src/types/info/requests.d.ts +9 -0
- package/esm/src/types/info/requests.d.ts.map +1 -1
- package/esm/src/types/mod.d.ts +3 -0
- package/esm/src/types/mod.d.ts.map +1 -1
- package/esm/src/types/mod.js +2 -0
- package/package.json +12 -9
- package/script/mod.d.ts +5 -3
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +5 -4
- package/script/src/base.d.ts +1 -47
- package/script/src/base.d.ts.map +1 -1
- package/script/src/base.js +2 -10
- package/{esm/src/clients/wallet.d.ts → script/src/clients/exchange.d.ts} +111 -112
- package/script/src/clients/exchange.d.ts.map +1 -0
- package/script/src/clients/{wallet.js → exchange.js} +98 -308
- package/script/src/clients/{public.d.ts → info.d.ts} +121 -102
- package/script/src/clients/info.d.ts.map +1 -0
- package/script/src/clients/{public.js → info.js} +75 -59
- package/{esm/src/clients/event.d.ts → script/src/clients/subscription.d.ts} +61 -80
- package/script/src/clients/subscription.d.ts.map +1 -0
- package/script/src/clients/{event.js → subscription.js} +108 -138
- package/script/src/signing.d.ts +4 -0
- package/script/src/signing.d.ts.map +1 -1
- package/script/src/signing.js +25 -7
- package/script/src/transports/base.d.ts +47 -0
- package/script/src/transports/base.d.ts.map +1 -0
- package/script/src/transports/base.js +22 -0
- package/script/src/transports/http/http_transport.d.ts +8 -5
- package/script/src/transports/http/http_transport.d.ts.map +1 -1
- package/script/src/transports/http/http_transport.js +16 -63
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +36 -39
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
- package/script/src/transports/websocket/_reconnecting_websocket.d.ts +21 -27
- package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
- package/script/src/transports/websocket/_reconnecting_websocket.js +90 -183
- package/{esm/src/transports/websocket/_websocket_request_dispatcher.d.ts → script/src/transports/websocket/_websocket_async_request.d.ts} +17 -19
- package/script/src/transports/websocket/_websocket_async_request.d.ts.map +1 -0
- package/script/src/transports/websocket/_websocket_async_request.js +192 -0
- package/script/src/transports/websocket/websocket_transport.d.ts +54 -41
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/script/src/transports/websocket/websocket_transport.js +103 -115
- package/script/src/types/info/accounts.d.ts +1 -0
- package/script/src/types/info/accounts.d.ts.map +1 -1
- package/script/src/types/info/assets.d.ts +0 -48
- package/script/src/types/info/assets.d.ts.map +1 -1
- package/script/src/types/info/markets.d.ts +52 -0
- package/script/src/types/info/markets.d.ts.map +1 -0
- package/script/{deps/jsr.io/@noble/hashes/1.8.0/src/crypto.js → src/types/info/markets.js} +0 -2
- package/script/src/types/info/requests.d.ts +9 -0
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/mod.d.ts +3 -0
- package/script/src/types/mod.d.ts.map +1 -1
- package/script/src/types/mod.js +2 -0
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.d.ts +0 -55
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.d.ts.map +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.js +0 -66
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.d.ts +0 -2
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.d.ts.map +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.js +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.d.ts +0 -53
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.d.ts.map +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.js +0 -294
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/utils.d.ts +0 -161
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/utils.d.ts.map +0 -1
- package/esm/deps/jsr.io/@noble/hashes/1.8.0/src/utils.js +0 -280
- package/esm/src/clients/event.d.ts.map +0 -1
- package/esm/src/clients/public.d.ts.map +0 -1
- package/esm/src/clients/wallet.d.ts.map +0 -1
- package/esm/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +0 -1
- package/esm/src/transports/websocket/_websocket_request_dispatcher.js +0 -191
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.d.ts +0 -55
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/_u64.js +0 -99
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.d.ts +0 -2
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/crypto.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.d.ts +0 -53
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/sha3.js +0 -309
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/utils.d.ts +0 -161
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/utils.d.ts.map +0 -1
- package/script/deps/jsr.io/@noble/hashes/1.8.0/src/utils.js +0 -322
- package/script/src/clients/event.d.ts.map +0 -1
- package/script/src/clients/public.d.ts.map +0 -1
- package/script/src/clients/wallet.d.ts.map +0 -1
- package/script/src/transports/websocket/_websocket_request_dispatcher.d.ts.map +0 -1
- package/script/src/transports/websocket/_websocket_request_dispatcher.js +0 -206
package/README.md
CHANGED
|
@@ -20,21 +20,23 @@ runtimes, written in TypeScript and provided with tests.
|
|
|
20
20
|
|
|
21
21
|
## Installation
|
|
22
22
|
|
|
23
|
+
> [!NOTE]
|
|
24
|
+
> While this library is in TypeScript, it can also be used in JavaScript and there is support for ESM/CommonJS.
|
|
25
|
+
|
|
26
|
+
### Node.js (choose your package manager)
|
|
27
|
+
|
|
23
28
|
```
|
|
24
|
-
# npm
|
|
25
29
|
npm i @nktkas/hyperliquid
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
deno add jsr:@nktkas/hyperliquid
|
|
31
|
+
pnpm add @nktkas/hyperliquid
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
yarn add @nktkas/hyperliquid
|
|
34
|
+
```
|
|
32
35
|
|
|
33
|
-
|
|
34
|
-
yarn add jsr:@nktkas/hyperliquid
|
|
36
|
+
### Deno
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
```
|
|
39
|
+
deno add jsr:@nktkas/hyperliquid
|
|
38
40
|
```
|
|
39
41
|
|
|
40
42
|
## Quick Start
|
|
@@ -43,9 +45,9 @@ bun i @nktkas/hyperliquid
|
|
|
43
45
|
import * as hl from "@nktkas/hyperliquid";
|
|
44
46
|
|
|
45
47
|
const transport = new hl.HttpTransport();
|
|
46
|
-
const
|
|
48
|
+
const infoClient = new hl.InfoClient({ transport });
|
|
47
49
|
|
|
48
|
-
const openOrders = await
|
|
50
|
+
const openOrders = await infoClient.openOrders({ user: "0x..." }); // Change to your address
|
|
49
51
|
```
|
|
50
52
|
|
|
51
53
|
```ts
|
|
@@ -55,9 +57,9 @@ import { privateKeyToAccount } from "viem/accounts";
|
|
|
55
57
|
const wallet = privateKeyToAccount("0x..."); // Your private key
|
|
56
58
|
|
|
57
59
|
const transport = new hl.HttpTransport();
|
|
58
|
-
const
|
|
60
|
+
const exchClient = new hl.ExchangeClient({ wallet, transport });
|
|
59
61
|
|
|
60
|
-
const result = await
|
|
62
|
+
const result = await exchClient.order({
|
|
61
63
|
orders: [{
|
|
62
64
|
a: 0, // Asset index
|
|
63
65
|
b: true, // Buy order
|
|
@@ -78,11 +80,9 @@ const result = await client.order({
|
|
|
78
80
|
import * as hl from "@nktkas/hyperliquid";
|
|
79
81
|
|
|
80
82
|
const transport = new hl.WebSocketTransport();
|
|
81
|
-
const
|
|
83
|
+
const subsClient = new hl.SubscriptionClient({ transport });
|
|
82
84
|
|
|
83
|
-
|
|
84
|
-
const sub = await client.allMids((event) => {
|
|
85
|
-
// Handle the event
|
|
85
|
+
const sub = await subsClient.allMids((event) => {
|
|
86
86
|
console.log(event);
|
|
87
87
|
});
|
|
88
88
|
|
|
@@ -96,7 +96,7 @@ await sub.unsubscribe(); // Unsubscribe from the event
|
|
|
96
96
|
First, choose and configure your transport layer (more details in the [API Reference](#transports)):
|
|
97
97
|
|
|
98
98
|
```ts
|
|
99
|
-
import * as hl from "@nktkas/hyperliquid";
|
|
99
|
+
import * as hl from "@nktkas/hyperliquid";
|
|
100
100
|
|
|
101
101
|
// HTTP Transport
|
|
102
102
|
const httpTransport = new hl.HttpTransport(); // Accepts optional parameters
|
|
@@ -109,19 +109,19 @@ const wsTransport = new hl.WebSocketTransport(); // Accepts optional parameters
|
|
|
109
109
|
|
|
110
110
|
Next, initialize a client with the transport layer (more details in the [API Reference](#clients)):
|
|
111
111
|
|
|
112
|
-
#### Create
|
|
112
|
+
#### Create InfoClient
|
|
113
113
|
|
|
114
114
|
```ts
|
|
115
|
-
import * as hl from "@nktkas/hyperliquid";
|
|
115
|
+
import * as hl from "@nktkas/hyperliquid";
|
|
116
116
|
|
|
117
117
|
const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
118
|
-
const
|
|
118
|
+
const infoClient = new hl.InfoClient({ transport });
|
|
119
119
|
```
|
|
120
120
|
|
|
121
|
-
#### Create
|
|
121
|
+
#### Create ExchangeClient
|
|
122
122
|
|
|
123
123
|
```ts
|
|
124
|
-
import * as hl from "@nktkas/hyperliquid";
|
|
124
|
+
import * as hl from "@nktkas/hyperliquid";
|
|
125
125
|
import { createWalletClient, custom } from "viem";
|
|
126
126
|
import { privateKeyToAccount } from "viem/accounts";
|
|
127
127
|
import { ethers } from "ethers";
|
|
@@ -130,53 +130,53 @@ const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
|
130
130
|
|
|
131
131
|
// 1. Using Viem with private key
|
|
132
132
|
const viemAccount = privateKeyToAccount("0x...");
|
|
133
|
-
const
|
|
133
|
+
const ExchangeClient_Viem = new hl.ExchangeClient({ wallet: viemAccount, transport });
|
|
134
134
|
|
|
135
135
|
// 2. Using Ethers (or Ethers V5) with private key
|
|
136
136
|
const ethersWallet = new ethers.Wallet("0x...");
|
|
137
|
-
const
|
|
137
|
+
const ExchangeClient_Ethers = new hl.ExchangeClient({ wallet: ethersWallet, transport });
|
|
138
138
|
|
|
139
139
|
// 3. Using external wallet (e.g. MetaMask) via Viem
|
|
140
140
|
const [account] = await window.ethereum.request({ method: "eth_requestAccounts" });
|
|
141
141
|
const externalWallet = createWalletClient({ account, transport: custom(window.ethereum) });
|
|
142
|
-
const
|
|
142
|
+
const ExchangeClient_ViemMetamask = new hl.ExchangeClient({ wallet: externalWallet, transport });
|
|
143
143
|
|
|
144
144
|
// 4. Using external wallet (e.g. MetaMask) via `window.ethereum` directly
|
|
145
|
-
const
|
|
145
|
+
const ExchangeClient_WindowMetamask = new hl.ExchangeClient({ wallet: window.ethereum, transport });
|
|
146
146
|
```
|
|
147
147
|
|
|
148
|
-
#### Create
|
|
148
|
+
#### Create SubscriptionClient
|
|
149
149
|
|
|
150
150
|
```ts
|
|
151
|
-
import * as hl from "@nktkas/hyperliquid";
|
|
151
|
+
import * as hl from "@nktkas/hyperliquid";
|
|
152
152
|
|
|
153
153
|
const transport = new hl.WebSocketTransport(); // only WebSocketTransport
|
|
154
|
-
const
|
|
154
|
+
const subsClient = new hl.SubscriptionClient({ transport });
|
|
155
155
|
```
|
|
156
156
|
|
|
157
157
|
### 3) Use Client
|
|
158
158
|
|
|
159
159
|
Finally, use client methods to interact with the Hyperliquid API (more details in the [API Reference](#clients)):
|
|
160
160
|
|
|
161
|
-
#### Example of using
|
|
161
|
+
#### Example of using an InfoClient
|
|
162
162
|
|
|
163
163
|
```ts
|
|
164
164
|
import * as hl from "@nktkas/hyperliquid";
|
|
165
165
|
|
|
166
166
|
const transport = new hl.HttpTransport();
|
|
167
|
-
const
|
|
167
|
+
const infoClient = new hl.InfoClient({ transport });
|
|
168
168
|
|
|
169
169
|
// L2 Book
|
|
170
|
-
const l2Book = await
|
|
170
|
+
const l2Book = await infoClient.l2Book({ coin: "BTC" });
|
|
171
171
|
|
|
172
172
|
// Account clearinghouse state
|
|
173
|
-
const clearinghouseState = await
|
|
173
|
+
const clearinghouseState = await infoClient.clearinghouseState({ user: "0x..." });
|
|
174
174
|
|
|
175
175
|
// Open orders
|
|
176
|
-
const openOrders = await
|
|
176
|
+
const openOrders = await infoClient.openOrders({ user: "0x..." });
|
|
177
177
|
```
|
|
178
178
|
|
|
179
|
-
#### Example of using
|
|
179
|
+
#### Example of using an ExchangeClient
|
|
180
180
|
|
|
181
181
|
```ts
|
|
182
182
|
import * as hl from "@nktkas/hyperliquid";
|
|
@@ -184,10 +184,10 @@ import { privateKeyToAccount } from "viem/accounts";
|
|
|
184
184
|
|
|
185
185
|
const account = privateKeyToAccount("0x...");
|
|
186
186
|
const transport = new hl.HttpTransport();
|
|
187
|
-
const
|
|
187
|
+
const exchClient = new hl.ExchangeClient({ wallet: account, transport });
|
|
188
188
|
|
|
189
189
|
// Place an orders
|
|
190
|
-
const result = await
|
|
190
|
+
const result = await exchClient.order({
|
|
191
191
|
orders: [{
|
|
192
192
|
a: 0,
|
|
193
193
|
b: true,
|
|
@@ -204,43 +204,40 @@ const result = await client.order({
|
|
|
204
204
|
});
|
|
205
205
|
|
|
206
206
|
// Approve an agent
|
|
207
|
-
const result = await
|
|
207
|
+
const result = await exchClient.approveAgent({
|
|
208
208
|
agentAddress: "0x...",
|
|
209
209
|
agentName: "agentName",
|
|
210
210
|
});
|
|
211
211
|
|
|
212
212
|
// Withdraw funds
|
|
213
|
-
const result = await
|
|
213
|
+
const result = await exchClient.withdraw3({
|
|
214
214
|
destination: account.address,
|
|
215
215
|
amount: "100",
|
|
216
216
|
});
|
|
217
217
|
```
|
|
218
218
|
|
|
219
|
-
#### Example of using
|
|
219
|
+
#### Example of using a SubscriptionClient
|
|
220
220
|
|
|
221
221
|
```ts
|
|
222
222
|
import * as hl from "@nktkas/hyperliquid";
|
|
223
223
|
|
|
224
224
|
const transport = new hl.WebSocketTransport();
|
|
225
|
-
const
|
|
225
|
+
const subsClient = new hl.SubscriptionClient({ transport });
|
|
226
226
|
|
|
227
227
|
// L2 Book updates
|
|
228
|
-
|
|
228
|
+
await client.l2Book({ coin: "BTC" }, (data) => {
|
|
229
229
|
console.log(data);
|
|
230
230
|
});
|
|
231
|
-
await sub.unsubscribe();
|
|
232
231
|
|
|
233
232
|
// User fills
|
|
234
|
-
|
|
233
|
+
await client.userFills({ user: "0x..." }, (data) => {
|
|
235
234
|
console.log(data);
|
|
236
235
|
});
|
|
237
|
-
await sub.unsubscribe();
|
|
238
236
|
|
|
239
237
|
// Explorer block updates
|
|
240
|
-
|
|
238
|
+
await client.explorerBlock((data) => {
|
|
241
239
|
console.log(data);
|
|
242
240
|
});
|
|
243
|
-
await sub.unsubscribe();
|
|
244
241
|
```
|
|
245
242
|
|
|
246
243
|
## API Reference
|
|
@@ -249,16 +246,16 @@ await sub.unsubscribe();
|
|
|
249
246
|
|
|
250
247
|
A Client provides access to the Hyperliquid API endpoints.
|
|
251
248
|
|
|
252
|
-
####
|
|
249
|
+
#### Info Client
|
|
253
250
|
|
|
254
|
-
|
|
251
|
+
An Info Client which provides access to
|
|
255
252
|
[Info API](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint) and Explorer API, such as
|
|
256
253
|
`l2Book` and `clearinghouseState`.
|
|
257
254
|
|
|
258
|
-
The
|
|
255
|
+
The Info Client class sets up with a given [Transport](#transports).
|
|
259
256
|
|
|
260
257
|
```ts
|
|
261
|
-
class
|
|
258
|
+
class InfoClient {
|
|
262
259
|
constructor(args: {
|
|
263
260
|
transport: HttpTransport | WebSocketTransport;
|
|
264
261
|
});
|
|
@@ -270,6 +267,7 @@ class PublicClient {
|
|
|
270
267
|
l2Book(args: L2BookParameters): Promise<Book>;
|
|
271
268
|
meta(): Promise<PerpsMeta>;
|
|
272
269
|
metaAndAssetCtxs(): Promise<PerpsMetaAndAssetCtxs>;
|
|
270
|
+
perpDeployAuctionStatus(): Promise<DeployAuctionStatus>;
|
|
273
271
|
perpDexs(): Promise<(PerpDex | null)[]>;
|
|
274
272
|
perpsAtOpenInterestCap(): Promise<string[]>;
|
|
275
273
|
predictedFundings(): Promise<PredictedFunding[]>;
|
|
@@ -326,18 +324,18 @@ class PublicClient {
|
|
|
326
324
|
}
|
|
327
325
|
```
|
|
328
326
|
|
|
329
|
-
####
|
|
327
|
+
#### Exchange Client
|
|
330
328
|
|
|
331
|
-
|
|
329
|
+
An Exchange Client which provides access to
|
|
332
330
|
[Exchange API](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint), such as `order`
|
|
333
331
|
and `withdraw3`.
|
|
334
332
|
|
|
335
|
-
The
|
|
333
|
+
The Exchange Client class sets up with a given [Transport](#transports) and a wallet instance, which can be a
|
|
336
334
|
[viem](https://viem.sh/docs/clients/wallet), [ethers.js](https://docs.ethers.org/v6/api/providers/#Signer) or other
|
|
337
335
|
wallet libraries.
|
|
338
336
|
|
|
339
337
|
```ts
|
|
340
|
-
class
|
|
338
|
+
class ExchangeClient {
|
|
341
339
|
constructor(args: {
|
|
342
340
|
transport: HttpTransport | WebSocketTransport;
|
|
343
341
|
wallet:
|
|
@@ -410,17 +408,17 @@ class WalletClient {
|
|
|
410
408
|
}
|
|
411
409
|
```
|
|
412
410
|
|
|
413
|
-
####
|
|
411
|
+
#### Subscription Client
|
|
414
412
|
|
|
415
|
-
A
|
|
413
|
+
A Subscription Client which provides access to
|
|
416
414
|
[Subscriptions API](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions), such as
|
|
417
415
|
real-time updates for `l2Book` and `userFills`.
|
|
418
416
|
|
|
419
|
-
The
|
|
417
|
+
The Subscription Client class sets up with a given [WebSocket Transport](#websocket-transport).
|
|
420
418
|
|
|
421
419
|
<!-- deno-fmt-ignore-start -->
|
|
422
420
|
```ts
|
|
423
|
-
class
|
|
421
|
+
class SubscriptionClient {
|
|
424
422
|
constructor(args: {
|
|
425
423
|
transport: WebSocketTransport;
|
|
426
424
|
});
|
|
@@ -471,13 +469,10 @@ class HttpTransport {
|
|
|
471
469
|
constructor(options?: {
|
|
472
470
|
isTestnet?: boolean; // Whether to use testnet url (default: false)
|
|
473
471
|
timeout?: number; // Request timeout in ms (default: 10_000)
|
|
474
|
-
server?: "api" | "api2" | "api-ui"; // Server URL (default: "api" = "https://api.hyperliquid.xyz")
|
|
475
472
|
fetchOptions?: RequestInit; // A custom fetch options
|
|
476
473
|
onRequest?: (request: Request) => MaybePromise<Request | void | null | undefined>; // A callback before request is sent
|
|
477
474
|
onResponse?: (response: Response) => MaybePromise<Response | void | null | undefined>; // A callback after response is received
|
|
478
475
|
});
|
|
479
|
-
|
|
480
|
-
request(endpoint: "info" | "exchange" | "explorer", payload: unknown, signal?: AbortSignal): Promise<unknown>;
|
|
481
476
|
}
|
|
482
477
|
```
|
|
483
478
|
|
|
@@ -492,7 +487,8 @@ class WebSocketTransport {
|
|
|
492
487
|
url?: string | URL; // WebSocket URL (default: "wss://api.hyperliquid.xyz/ws")
|
|
493
488
|
timeout?: number; // Request timeout in ms (default: 10_000)
|
|
494
489
|
keepAlive?: { // Keep-alive configuration
|
|
495
|
-
interval?: number; // Ping interval in ms (default:
|
|
490
|
+
interval?: number; // Ping interval in ms (default: 30_000)
|
|
491
|
+
timeout?: number; // Pong timeout in ms (default: same as `timeout` for requests)
|
|
496
492
|
};
|
|
497
493
|
reconnect?: { // Reconnection policy configuration for closed connections
|
|
498
494
|
maxRetries?: number; // Maximum number of reconnection attempts (default: 3)
|
|
@@ -501,16 +497,8 @@ class WebSocketTransport {
|
|
|
501
497
|
shouldReconnect?: (event: CloseEvent) => boolean | Promise<boolean>; // Custom reconnection logic (default: Always reconnect)
|
|
502
498
|
messageBuffer?: MessageBufferStrategy; // Message buffering strategy between reconnection (default: FIFO buffer)
|
|
503
499
|
};
|
|
500
|
+
autoResubscribe?: boolean; // Whether to automatically resubscribe to events after reconnection (default: true)
|
|
504
501
|
});
|
|
505
|
-
|
|
506
|
-
request(endpoint: "info" | "exchange", payload: unknown, signal?: AbortSignal): Promise<unknown>;
|
|
507
|
-
subscribe(
|
|
508
|
-
channel: string,
|
|
509
|
-
payload: unknown,
|
|
510
|
-
listener: (data: CustomEvent) => void,
|
|
511
|
-
signal?: AbortSignal,
|
|
512
|
-
): Promise<Subscription>;
|
|
513
|
-
|
|
514
502
|
ready(signal?: AbortSignal): Promise<void>;
|
|
515
503
|
close(signal?: AbortSignal): Promise<void>;
|
|
516
504
|
}
|
|
@@ -518,21 +506,15 @@ class WebSocketTransport {
|
|
|
518
506
|
|
|
519
507
|
## Additional Import Points
|
|
520
508
|
|
|
521
|
-
The SDK exports additional import points to access internal functions.
|
|
522
|
-
|
|
523
509
|
### `/types`
|
|
524
510
|
|
|
525
511
|
The import point gives access to all Hyperliquid-related types, including the base types on which class methods are
|
|
526
512
|
based.
|
|
527
513
|
|
|
528
|
-
Useful if you want to get all Hyperliquid types.
|
|
529
|
-
|
|
530
514
|
### `/signing`
|
|
531
515
|
|
|
532
516
|
The import point gives access to functions that generate signatures for Hyperliquid transactions.
|
|
533
517
|
|
|
534
|
-
Useful if you want to sign a Hyperliquid transaction yourself.
|
|
535
|
-
|
|
536
518
|
### Examples
|
|
537
519
|
|
|
538
520
|
#### Cancel an order without a client
|
package/esm/mod.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export * from "./src/base.js";
|
|
2
|
+
export * from "./src/transports/base.js";
|
|
2
3
|
export type { AbstractEthersSigner, AbstractEthersV5Signer, AbstractExtendedViemWalletClient, AbstractViemWalletClient, AbstractWindowEthereum, } from "./src/signing.js";
|
|
3
|
-
export * from "./src/clients/
|
|
4
|
-
export * from "./src/clients/
|
|
5
|
-
export * from "./src/clients/
|
|
4
|
+
export * from "./src/clients/subscription.js";
|
|
5
|
+
export * from "./src/clients/info.js";
|
|
6
|
+
export * from "./src/clients/exchange.js";
|
|
6
7
|
export * from "./src/transports/http/http_transport.js";
|
|
7
8
|
export * from "./src/transports/websocket/websocket_transport.js";
|
|
8
9
|
export type * from "./src/types/exchange/responses.js";
|
|
@@ -10,6 +11,7 @@ export type * from "./src/types/explorer/responses.js";
|
|
|
10
11
|
export type * from "./src/types/info/accounts.js";
|
|
11
12
|
export type * from "./src/types/info/assets.js";
|
|
12
13
|
export type * from "./src/types/info/delegations.js";
|
|
14
|
+
export type * from "./src/types/info/markets.js";
|
|
13
15
|
export type * from "./src/types/info/orders.js";
|
|
14
16
|
export type * from "./src/types/info/vaults.js";
|
|
15
17
|
export type * from "./src/types/subscriptions/responses.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;
|
|
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,gCAAgC,EAChC,wBAAwB,EACxB,sBAAsB,GACzB,MAAM,kBAAkB,CAAC;AAG1B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,uBAAuB,CAAC;AACtC,cAAc,2BAA2B,CAAC;AAG1C,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,iCAAiC,CAAC;AACrD,mBAAmB,6BAA6B,CAAC;AACjD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,wCAAwC,CAAC"}
|
package/esm/mod.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// Base interfaces
|
|
2
2
|
export * from "./src/base.js";
|
|
3
|
+
export * from "./src/transports/base.js";
|
|
3
4
|
// Clients
|
|
4
|
-
export * from "./src/clients/
|
|
5
|
-
export * from "./src/clients/
|
|
6
|
-
export * from "./src/clients/
|
|
5
|
+
export * from "./src/clients/subscription.js";
|
|
6
|
+
export * from "./src/clients/info.js";
|
|
7
|
+
export * from "./src/clients/exchange.js";
|
|
7
8
|
// Transports
|
|
8
9
|
export * from "./src/transports/http/http_transport.js";
|
|
9
10
|
export * from "./src/transports/websocket/websocket_transport.js";
|
package/esm/src/base.d.ts
CHANGED
|
@@ -1,53 +1,7 @@
|
|
|
1
|
-
/** Hexadecimal string starting with `0x`. */
|
|
2
1
|
export type Hex = `0x${string}`;
|
|
3
2
|
export type MaybePromise<T> = T | Promise<T>;
|
|
4
|
-
/**
|
|
5
|
-
* Interface representing a REST transport.
|
|
6
|
-
* Handles communication with Hyperliquid API endpoints.
|
|
7
|
-
* @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint | Info endpoint}
|
|
8
|
-
* @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint | Exchange endpoint}
|
|
9
|
-
* @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/post-requests | Websocket post requests}
|
|
10
|
-
*/
|
|
11
|
-
export interface IRequestTransport extends Partial<AsyncDisposable> {
|
|
12
|
-
/**
|
|
13
|
-
* Sends a request to the Hyperliquid API.
|
|
14
|
-
* @param endpoint - The API endpoint to send the request to.
|
|
15
|
-
* @param payload - The payload to send with the request.
|
|
16
|
-
* @param signal - An ptional abort signal.
|
|
17
|
-
* @returns A promise that resolves with parsed JSON response body.
|
|
18
|
-
*/
|
|
19
|
-
request(endpoint: "info" | "exchange" | "explorer", payload: unknown, signal?: AbortSignal): Promise<unknown>;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Interface representing an event subscription transport.
|
|
23
|
-
* Handles WebSocket subscriptions for real-time updates.
|
|
24
|
-
* @see {@link https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions | Websocket subscriptions}
|
|
25
|
-
*/
|
|
26
|
-
export interface ISubscriptionTransport extends Partial<AsyncDisposable> {
|
|
27
|
-
/**
|
|
28
|
-
* Subscribes to a Hyperliquid event channel.
|
|
29
|
-
* @param channel - The event channel to listen to.
|
|
30
|
-
* @param payload - The payload to send with the subscription request.
|
|
31
|
-
* @param listener - The function to call when the event is dispatched.
|
|
32
|
-
* @param signal - An optional abort signal for canceling the subscription request.
|
|
33
|
-
* @returns A promise that resolves with a {@link Subscription} object to manage the subscription lifecycle.
|
|
34
|
-
*/
|
|
35
|
-
subscribe(channel: string, payload: unknown, listener: (data: CustomEvent) => void, signal?: AbortSignal): Promise<Subscription>;
|
|
36
|
-
}
|
|
37
|
-
/** Controls event subscription lifecycle. */
|
|
38
|
-
export interface Subscription {
|
|
39
|
-
/**
|
|
40
|
-
* Unsubscribes from the event and sends an unsubscribe request to the server.
|
|
41
|
-
* @param signal - An optional abort signal for canceling the unsubscribe request.
|
|
42
|
-
*/
|
|
43
|
-
unsubscribe(signal?: AbortSignal): Promise<void>;
|
|
44
|
-
}
|
|
45
|
-
/** Base class for all Hyperliquid SDK errors. */
|
|
3
|
+
/** Base error class for all SDK errors. */
|
|
46
4
|
export declare class HyperliquidError extends Error {
|
|
47
5
|
constructor(message?: string);
|
|
48
6
|
}
|
|
49
|
-
/** Base class for all transport-related errors. */
|
|
50
|
-
export declare class TransportError extends HyperliquidError {
|
|
51
|
-
constructor(message?: string);
|
|
52
|
-
}
|
|
53
7
|
//# sourceMappingURL=base.d.ts.map
|
package/esm/src/base.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/src/base.ts"],"names":[],"mappings":"AAAA,
|
|
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;AAE7C,2CAA2C;AAC3C,qBAAa,gBAAiB,SAAQ,KAAK;gBAC3B,OAAO,CAAC,EAAE,MAAM;CAI/B"}
|
package/esm/src/base.js
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
/** Base class for all
|
|
1
|
+
/** Base error class for all SDK errors. */
|
|
2
2
|
export class HyperliquidError extends Error {
|
|
3
3
|
constructor(message) {
|
|
4
4
|
super(message);
|
|
5
5
|
this.name = "HyperliquidError";
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
|
-
/** Base class for all transport-related errors. */
|
|
9
|
-
export class TransportError extends HyperliquidError {
|
|
10
|
-
constructor(message) {
|
|
11
|
-
super(message);
|
|
12
|
-
this.name = "TransportError";
|
|
13
|
-
}
|
|
14
|
-
}
|