@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.
- package/CONTRIBUTING.md +3 -2
- package/README.md +154 -170
- package/esm/mod.d.ts +1 -1
- package/esm/mod.d.ts.map +1 -1
- package/esm/src/base.d.ts +0 -5
- package/esm/src/base.d.ts.map +1 -1
- package/esm/src/clients/exchange.d.ts +430 -393
- package/esm/src/clients/exchange.d.ts.map +1 -1
- package/esm/src/clients/exchange.js +467 -927
- package/esm/src/clients/info.d.ts +228 -203
- package/esm/src/clients/info.d.ts.map +1 -1
- package/esm/src/clients/info.js +236 -368
- package/esm/src/clients/multiSign.d.ts +14 -1184
- package/esm/src/clients/multiSign.d.ts.map +1 -1
- package/esm/src/clients/multiSign.js +37 -2004
- package/esm/src/clients/subscription.d.ts +95 -93
- package/esm/src/clients/subscription.d.ts.map +1 -1
- package/esm/src/clients/subscription.js +101 -146
- package/esm/src/signing/_signTypedData/ethers.d.ts +16 -4
- package/esm/src/signing/_signTypedData/ethers.d.ts.map +1 -1
- package/esm/src/signing/_signTypedData/ethers.js +1 -1
- package/esm/src/signing/_signTypedData/mod.d.ts +12 -12
- package/esm/src/signing/_signTypedData/mod.d.ts.map +1 -1
- package/esm/src/signing/_signTypedData/mod.js +52 -24
- package/esm/src/signing/_signTypedData/private_key.d.ts +6 -5
- package/esm/src/signing/_signTypedData/private_key.d.ts.map +1 -1
- package/esm/src/signing/_signTypedData/private_key.js +40 -19
- package/esm/src/signing/_signTypedData/viem.d.ts +27 -6
- package/esm/src/signing/_signTypedData/viem.d.ts.map +1 -1
- package/esm/src/signing/_signTypedData/viem.js +1 -1
- package/esm/src/signing/_sorter.d.ts +12 -21
- package/esm/src/signing/_sorter.d.ts.map +1 -1
- package/esm/src/signing/_sorter.js +30 -63
- package/esm/src/signing/mod.d.ts +177 -127
- package/esm/src/signing/mod.d.ts.map +1 -1
- package/esm/src/signing/mod.js +182 -130
- package/esm/src/transports/base.d.ts +2 -1
- package/esm/src/transports/base.d.ts.map +1 -1
- package/esm/src/transports/http/http_transport.d.ts +3 -2
- package/esm/src/transports/http/http_transport.d.ts.map +1 -1
- package/esm/src/transports/http/http_transport.js +4 -4
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +14 -6
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
- package/esm/src/transports/websocket/_hyperliquid_event_target.js +1 -2
- package/esm/src/transports/websocket/_reconnecting_websocket.d.ts +2 -1
- package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
- package/esm/src/transports/websocket/_reconnecting_websocket.js +1 -0
- package/esm/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
- package/esm/src/transports/websocket/_websocket_async_request.js +17 -21
- package/esm/src/transports/websocket/websocket_transport.d.ts +4 -4
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/esm/src/transports/websocket/websocket_transport.js +6 -6
- package/esm/src/types/exchange/requests.d.ts +492 -306
- package/esm/src/types/exchange/requests.d.ts.map +1 -1
- package/esm/src/types/exchange/responses.d.ts +105 -25
- package/esm/src/types/exchange/responses.d.ts.map +1 -1
- package/esm/src/types/explorer/requests.d.ts +3 -3
- package/esm/src/types/explorer/requests.d.ts.map +1 -1
- package/esm/src/types/explorer/responses.d.ts +1 -1
- package/esm/src/types/explorer/responses.d.ts.map +1 -1
- package/esm/src/types/info/accounts.d.ts +403 -98
- package/esm/src/types/info/accounts.d.ts.map +1 -1
- package/esm/src/types/info/assets.d.ts +131 -35
- package/esm/src/types/info/assets.d.ts.map +1 -1
- package/esm/src/types/info/markets.d.ts +29 -8
- package/esm/src/types/info/markets.d.ts.map +1 -1
- package/esm/src/types/info/orders.d.ts +62 -17
- package/esm/src/types/info/orders.d.ts.map +1 -1
- package/esm/src/types/info/requests.d.ts +117 -51
- package/esm/src/types/info/requests.d.ts.map +1 -1
- package/esm/src/types/info/validators.d.ts +44 -14
- package/esm/src/types/info/validators.d.ts.map +1 -1
- package/esm/src/types/info/vaults.d.ts +25 -10
- package/esm/src/types/info/vaults.d.ts.map +1 -1
- package/esm/src/types/mod.d.ts +1 -1
- package/esm/src/types/mod.d.ts.map +1 -1
- package/esm/src/types/subscriptions/requests.d.ts +21 -66
- package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
- package/esm/src/types/subscriptions/responses.d.ts +46 -55
- package/esm/src/types/subscriptions/responses.d.ts.map +1 -1
- package/package.json +2 -3
- package/script/mod.d.ts +1 -1
- package/script/mod.d.ts.map +1 -1
- package/script/src/base.d.ts +0 -5
- package/script/src/base.d.ts.map +1 -1
- package/script/src/clients/exchange.d.ts +430 -393
- package/script/src/clients/exchange.d.ts.map +1 -1
- package/script/src/clients/exchange.js +466 -926
- package/script/src/clients/info.d.ts +228 -203
- package/script/src/clients/info.d.ts.map +1 -1
- package/script/src/clients/info.js +236 -368
- package/script/src/clients/multiSign.d.ts +14 -1184
- package/script/src/clients/multiSign.d.ts.map +1 -1
- package/script/src/clients/multiSign.js +38 -2005
- package/script/src/clients/subscription.d.ts +95 -93
- package/script/src/clients/subscription.d.ts.map +1 -1
- package/script/src/clients/subscription.js +101 -146
- package/script/src/signing/_signTypedData/ethers.d.ts +16 -4
- package/script/src/signing/_signTypedData/ethers.d.ts.map +1 -1
- package/script/src/signing/_signTypedData/ethers.js +2 -2
- package/script/src/signing/_signTypedData/mod.d.ts +12 -12
- package/script/src/signing/_signTypedData/mod.d.ts.map +1 -1
- package/script/src/signing/_signTypedData/mod.js +51 -26
- package/script/src/signing/_signTypedData/private_key.d.ts +6 -5
- package/script/src/signing/_signTypedData/private_key.d.ts.map +1 -1
- package/script/src/signing/_signTypedData/private_key.js +40 -18
- package/script/src/signing/_signTypedData/viem.d.ts +27 -6
- package/script/src/signing/_signTypedData/viem.d.ts.map +1 -1
- package/script/src/signing/_signTypedData/viem.js +2 -2
- package/script/src/signing/_sorter.d.ts +12 -21
- package/script/src/signing/_sorter.d.ts.map +1 -1
- package/script/src/signing/_sorter.js +30 -63
- package/script/src/signing/mod.d.ts +177 -127
- package/script/src/signing/mod.d.ts.map +1 -1
- package/script/src/signing/mod.js +185 -148
- package/script/src/transports/base.d.ts +2 -1
- package/script/src/transports/base.d.ts.map +1 -1
- package/script/src/transports/http/http_transport.d.ts +3 -2
- package/script/src/transports/http/http_transport.d.ts.map +1 -1
- package/script/src/transports/http/http_transport.js +4 -4
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +14 -6
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
- package/script/src/transports/websocket/_hyperliquid_event_target.js +1 -2
- package/script/src/transports/websocket/_reconnecting_websocket.d.ts +2 -1
- package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
- package/script/src/transports/websocket/_reconnecting_websocket.js +1 -0
- package/script/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
- package/script/src/transports/websocket/_websocket_async_request.js +17 -21
- package/script/src/transports/websocket/websocket_transport.d.ts +4 -4
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/script/src/transports/websocket/websocket_transport.js +6 -6
- package/script/src/types/exchange/requests.d.ts +492 -306
- package/script/src/types/exchange/requests.d.ts.map +1 -1
- package/script/src/types/exchange/responses.d.ts +105 -25
- package/script/src/types/exchange/responses.d.ts.map +1 -1
- package/script/src/types/explorer/requests.d.ts +3 -3
- package/script/src/types/explorer/requests.d.ts.map +1 -1
- package/script/src/types/explorer/responses.d.ts +1 -1
- package/script/src/types/explorer/responses.d.ts.map +1 -1
- package/script/src/types/info/accounts.d.ts +403 -98
- package/script/src/types/info/accounts.d.ts.map +1 -1
- package/script/src/types/info/assets.d.ts +131 -35
- package/script/src/types/info/assets.d.ts.map +1 -1
- package/script/src/types/info/markets.d.ts +29 -8
- package/script/src/types/info/markets.d.ts.map +1 -1
- package/script/src/types/info/orders.d.ts +62 -17
- package/script/src/types/info/orders.d.ts.map +1 -1
- package/script/src/types/info/requests.d.ts +117 -51
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/info/validators.d.ts +44 -14
- package/script/src/types/info/validators.d.ts.map +1 -1
- package/script/src/types/info/vaults.d.ts +25 -10
- package/script/src/types/info/vaults.d.ts.map +1 -1
- package/script/src/types/mod.d.ts +1 -1
- package/script/src/types/mod.d.ts.map +1 -1
- package/script/src/types/subscriptions/requests.d.ts +21 -66
- package/script/src/types/subscriptions/requests.d.ts.map +1 -1
- package/script/src/types/subscriptions/responses.d.ts +46 -55
- package/script/src/types/subscriptions/responses.d.ts.map +1 -1
- package/esm/src/signing/_signTypedData/window.d.ts +0 -29
- package/esm/src/signing/_signTypedData/window.d.ts.map +0 -1
- package/esm/src/signing/_signTypedData/window.js +0 -30
- package/script/src/signing/_signTypedData/window.d.ts +0 -29
- package/script/src/signing/_signTypedData/window.d.ts.map +0 -1
- 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.
|
|
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
|
[](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
|
|
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
|
|
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)
|
|
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
|
-
|
|
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
|
-
|
|
106
|
-
const infoClient = new hl.InfoClient({
|
|
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
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
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
|
-
|
|
144
|
-
const subsClient = new hl.SubscriptionClient({
|
|
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
|
-
|
|
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
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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
|
|
198
|
-
|
|
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
|
|
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
|
|
238
|
-
|
|
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
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
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
|
-
|
|
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
|
|
290
|
-
|
|
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
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
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
|
|
348
|
-
|
|
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
|
-
|
|
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
|
|
372
|
-
|
|
373
|
-
"0x...",
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
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
|
-
//
|
|
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?:
|
|
514
|
-
signatureChainId?:
|
|
515
|
-
nonceManager?: () => MaybePromise<number>; // Function to get the next nonce (default:
|
|
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:
|
|
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"> //
|
|
588
|
+
& Omit<ExchangeClientParameters, "wallet"> // instead of `wallet`, you should specify the following parameters:
|
|
622
589
|
& {
|
|
623
|
-
multiSignAddress:
|
|
624
|
-
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
|
-
|
|
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
|
-
|
|
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..."; //
|
|
685
|
+
const privateKey = "0x..."; // `viem`, `ethers`, or private key directly
|
|
705
686
|
|
|
706
|
-
const
|
|
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..."; //
|
|
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
|
-
}
|
|
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
|
|
775
|
-
with `tif: "Ioc"` and
|
|
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 {
|
|
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,
|
|
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);
|
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,2CAA2C;AAC3C,qBAAa,gBAAiB,SAAQ,KAAK;gBAC3B,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIvD"}
|