@nktkas/hyperliquid 0.21.1 → 0.22.1
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 +19 -34
- package/README.md +212 -87
- package/esm/mod.d.ts +4 -3
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +3 -2
- package/esm/src/clients/exchange.d.ts +102 -59
- package/esm/src/clients/exchange.d.ts.map +1 -1
- package/esm/src/clients/exchange.js +234 -517
- package/esm/src/clients/info.d.ts +55 -55
- package/esm/src/clients/info.d.ts.map +1 -1
- package/esm/src/clients/info.js +57 -54
- package/esm/src/clients/multiSign.d.ts +1299 -0
- package/esm/src/clients/multiSign.d.ts.map +1 -0
- package/esm/src/clients/multiSign.js +2158 -0
- package/esm/src/clients/subscription.d.ts +19 -19
- package/esm/src/clients/subscription.d.ts.map +1 -1
- package/esm/src/clients/subscription.js +17 -17
- package/esm/src/signing.d.ts +164 -40
- package/esm/src/signing.d.ts.map +1 -1
- package/esm/src/signing.js +710 -9
- package/esm/src/types/exchange/requests.d.ts +241 -246
- package/esm/src/types/exchange/requests.d.ts.map +1 -1
- package/package.json +2 -1
- package/script/mod.d.ts +4 -3
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +4 -3
- package/script/src/clients/exchange.d.ts +102 -59
- package/script/src/clients/exchange.d.ts.map +1 -1
- package/script/src/clients/exchange.js +233 -516
- package/script/src/clients/info.d.ts +55 -55
- package/script/src/clients/info.d.ts.map +1 -1
- package/script/src/clients/info.js +57 -54
- package/script/src/clients/multiSign.d.ts +1299 -0
- package/script/src/clients/multiSign.d.ts.map +1 -0
- package/script/src/clients/multiSign.js +2172 -0
- package/script/src/clients/subscription.d.ts +19 -19
- package/script/src/clients/subscription.d.ts.map +1 -1
- package/script/src/clients/subscription.js +17 -17
- package/script/src/signing.d.ts +164 -40
- package/script/src/signing.d.ts.map +1 -1
- package/script/src/signing.js +711 -10
- package/script/src/types/exchange/requests.d.ts +241 -246
- package/script/src/types/exchange/requests.d.ts.map +1 -1
package/CONTRIBUTING.md
CHANGED
|
@@ -1,46 +1,38 @@
|
|
|
1
1
|
# Contributing to @nktkas/hyperliquid
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Welcome, and thank you for taking time in contributing to SDK! You can contribute to SDK in different ways:
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
- Submit new features
|
|
6
|
+
- Report and fix bugs
|
|
7
|
+
- Review code
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
## Development Setup
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
possible to help us understand and address the problem.
|
|
11
|
+
You will need [Deno](https://deno.land/) 2.0+.
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
1. Fork this repository to your own GitHub account.
|
|
14
|
+
2. Clone the repository to your local device.
|
|
15
|
+
3. Create a new branch `git checkout -b BRANCH_NAME`.
|
|
16
|
+
4. Change code.
|
|
17
|
+
5. [Push your branch to Github after all tests passed.](#Testing)
|
|
18
|
+
6. Make a [pull request](https://github.com/nktkas/hyperliquid/pulls).
|
|
19
|
+
7. Merge to master branch by our maintainers.
|
|
13
20
|
|
|
14
|
-
|
|
15
|
-
- **Clone Your Fork**: Clone your forked repository to your local machine.
|
|
21
|
+
## Testing
|
|
16
22
|
|
|
17
|
-
|
|
18
|
-
git clone https://github.com/your-username/hyperliquid.git
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
- **Create a Branch**: Create a new branch for your feature or bug fix.
|
|
23
|
+
You can run most tests with the following command:
|
|
22
24
|
|
|
23
25
|
```bash
|
|
24
|
-
|
|
26
|
+
deno test -A
|
|
25
27
|
```
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
- **Commit Your Changes**:
|
|
29
|
+
However, for complete testing, you will need a private key from a testnet account with funds:
|
|
29
30
|
|
|
30
31
|
```bash
|
|
31
|
-
|
|
32
|
+
deno test -A -- YOUR_PRIVATE_KEY
|
|
32
33
|
```
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
```bash
|
|
37
|
-
git push origin feature/your-feature-name
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
- **Create a Pull Request**: Go to the original repository and click on "New Pull Request". Select your branch and
|
|
41
|
-
submit the pull request.
|
|
42
|
-
|
|
43
|
-
#### Coding Guidelines
|
|
35
|
+
## Coding Guidelines
|
|
44
36
|
|
|
45
37
|
- **TypeScript**: Ensure your code passes TypeScript compilation without errors. Try not to ignore typescript errors and
|
|
46
38
|
avoid creating unsafe types.
|
|
@@ -49,10 +41,3 @@ git push origin feature/your-feature-name
|
|
|
49
41
|
- **Dependencies**: Try to use trusted small dependencies (e.g. [@noble](https://github.com/paulmillr/noble-hashes) or
|
|
50
42
|
[deno @std](https://github.com/denoland/std)).
|
|
51
43
|
- **Docs**: Update or add JSDoc comments where appropriate.
|
|
52
|
-
|
|
53
|
-
#### Testing
|
|
54
|
-
|
|
55
|
-
Before submitting your pull request:
|
|
56
|
-
|
|
57
|
-
- Run existing tests to ensure nothing breaks.
|
|
58
|
-
- If you add new features, consider adding tests for them.
|
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ runtimes, written in TypeScript and provided with tests.
|
|
|
21
21
|
## Installation
|
|
22
22
|
|
|
23
23
|
> [!NOTE]
|
|
24
|
-
> While this library is in TypeScript, it can also be used in JavaScript and
|
|
24
|
+
> While this library is in TypeScript, it can also be used in JavaScript and supports ESM/CommonJS.
|
|
25
25
|
|
|
26
26
|
### Node.js (choose your package manager)
|
|
27
27
|
|
|
@@ -39,43 +39,58 @@ yarn add @nktkas/hyperliquid
|
|
|
39
39
|
deno add jsr:@nktkas/hyperliquid
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
+
### Web
|
|
43
|
+
|
|
44
|
+
```html
|
|
45
|
+
<script type="module">
|
|
46
|
+
import * as hl from "https://esm.sh/jsr/@nktkas/hyperliquid";
|
|
47
|
+
// Use hl.InfoClient, hl.ExchangeClient, etc.
|
|
48
|
+
</script>
|
|
49
|
+
```
|
|
50
|
+
|
|
42
51
|
## Quick Start
|
|
43
52
|
|
|
53
|
+
#### Info endpoint
|
|
54
|
+
|
|
44
55
|
```ts
|
|
45
56
|
import * as hl from "@nktkas/hyperliquid";
|
|
46
57
|
|
|
47
58
|
const transport = new hl.HttpTransport();
|
|
48
59
|
const infoClient = new hl.InfoClient({ transport });
|
|
49
60
|
|
|
50
|
-
const openOrders = await infoClient.openOrders({ user: "0x..." });
|
|
61
|
+
const openOrders = await infoClient.openOrders({ user: "0x..." });
|
|
51
62
|
```
|
|
52
63
|
|
|
64
|
+
#### Exchange endpoint
|
|
65
|
+
|
|
53
66
|
```ts
|
|
54
67
|
import * as hl from "@nktkas/hyperliquid";
|
|
55
|
-
import { privateKeyToAccount } from "viem/accounts";
|
|
68
|
+
import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
|
|
56
69
|
|
|
57
|
-
const wallet = privateKeyToAccount("0x...");
|
|
70
|
+
const wallet = privateKeyToAccount("0x...");
|
|
58
71
|
|
|
59
72
|
const transport = new hl.HttpTransport();
|
|
60
73
|
const exchClient = new hl.ExchangeClient({ wallet, transport });
|
|
61
74
|
|
|
62
75
|
const result = await exchClient.order({
|
|
63
76
|
orders: [{
|
|
64
|
-
a: 0,
|
|
65
|
-
b: true,
|
|
66
|
-
p: "30000",
|
|
67
|
-
s: "0.1",
|
|
68
|
-
r: false,
|
|
77
|
+
a: 0,
|
|
78
|
+
b: true,
|
|
79
|
+
p: "30000",
|
|
80
|
+
s: "0.1",
|
|
81
|
+
r: false,
|
|
69
82
|
t: {
|
|
70
83
|
limit: {
|
|
71
|
-
tif: "Gtc",
|
|
84
|
+
tif: "Gtc",
|
|
72
85
|
},
|
|
73
86
|
},
|
|
74
87
|
}],
|
|
75
|
-
grouping: "na",
|
|
88
|
+
grouping: "na",
|
|
76
89
|
});
|
|
77
90
|
```
|
|
78
91
|
|
|
92
|
+
#### Subscription
|
|
93
|
+
|
|
79
94
|
```ts
|
|
80
95
|
import * as hl from "@nktkas/hyperliquid";
|
|
81
96
|
|
|
@@ -86,10 +101,34 @@ const sub = await subsClient.allMids((event) => {
|
|
|
86
101
|
console.log(event);
|
|
87
102
|
});
|
|
88
103
|
|
|
89
|
-
await sub.unsubscribe(); //
|
|
104
|
+
await sub.unsubscribe(); // unsubscribe from the event
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
#### Multi-Sign
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import * as hl from "@nktkas/hyperliquid";
|
|
111
|
+
import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
|
|
112
|
+
|
|
113
|
+
const multiSignAddress = "0x...";
|
|
114
|
+
const signers = [
|
|
115
|
+
privateKeyToAccount("0x..."), // first is leader
|
|
116
|
+
privateKeyToAccount("0x..."), // can be a custom async wallet
|
|
117
|
+
// ...
|
|
118
|
+
privateKeyToAccount("0x..."),
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
const transport = new hl.HttpTransport();
|
|
122
|
+
const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends ExchangeClient
|
|
123
|
+
|
|
124
|
+
const data = await multiSignClient.approveAgent({ // same API as ExchangeClient
|
|
125
|
+
agentAddress: "0x...",
|
|
126
|
+
agentName: "agentName",
|
|
127
|
+
});
|
|
90
128
|
```
|
|
91
129
|
|
|
92
|
-
|
|
130
|
+
<details>
|
|
131
|
+
<summary><h2>Usage</h2></summary>
|
|
93
132
|
|
|
94
133
|
### 1) Initialize Transport
|
|
95
134
|
|
|
@@ -130,19 +169,19 @@ const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
|
130
169
|
|
|
131
170
|
// 1. Using Viem with private key
|
|
132
171
|
const viemAccount = privateKeyToAccount("0x...");
|
|
133
|
-
const
|
|
172
|
+
const exchClient_viem = new hl.ExchangeClient({ wallet: viemAccount, transport });
|
|
134
173
|
|
|
135
174
|
// 2. Using Ethers (or Ethers V5) with private key
|
|
136
175
|
const ethersWallet = new ethers.Wallet("0x...");
|
|
137
|
-
const
|
|
176
|
+
const exchClient_ethers = new hl.ExchangeClient({ wallet: ethersWallet, transport });
|
|
138
177
|
|
|
139
178
|
// 3. Using external wallet (e.g. MetaMask) via Viem
|
|
140
179
|
const [account] = await window.ethereum.request({ method: "eth_requestAccounts" });
|
|
141
180
|
const externalWallet = createWalletClient({ account, transport: custom(window.ethereum) });
|
|
142
|
-
const
|
|
181
|
+
const exchClient_viemMetamask = new hl.ExchangeClient({ wallet: externalWallet, transport });
|
|
143
182
|
|
|
144
|
-
// 4. Using external wallet (e.g. MetaMask) via `window.ethereum`
|
|
145
|
-
const
|
|
183
|
+
// 4. Using external wallet (e.g. MetaMask) via `window.ethereum` (EIP-1193)
|
|
184
|
+
const exchClient_windowMetamask = new hl.ExchangeClient({ wallet: window.ethereum, transport });
|
|
146
185
|
```
|
|
147
186
|
|
|
148
187
|
#### Create SubscriptionClient
|
|
@@ -154,6 +193,45 @@ const transport = new hl.WebSocketTransport(); // only WebSocketTransport
|
|
|
154
193
|
const subsClient = new hl.SubscriptionClient({ transport });
|
|
155
194
|
```
|
|
156
195
|
|
|
196
|
+
#### Create MultiSignClient
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
import * as hl from "@nktkas/hyperliquid";
|
|
200
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
201
|
+
import { ethers } from "ethers";
|
|
202
|
+
|
|
203
|
+
const multiSignAddress = "0x...";
|
|
204
|
+
const signers = [
|
|
205
|
+
privateKeyToAccount("0x..."), // first is leader for multi-sign transaction, must contain own address
|
|
206
|
+
{ // can be a custom async wallet
|
|
207
|
+
signTypedData(params: {
|
|
208
|
+
domain: {
|
|
209
|
+
name: string;
|
|
210
|
+
version: string;
|
|
211
|
+
chainId: number;
|
|
212
|
+
verifyingContract: Hex;
|
|
213
|
+
};
|
|
214
|
+
types: {
|
|
215
|
+
[key: string]: {
|
|
216
|
+
name: string;
|
|
217
|
+
type: string;
|
|
218
|
+
}[];
|
|
219
|
+
};
|
|
220
|
+
primaryType: string;
|
|
221
|
+
message: Record<string, unknown>;
|
|
222
|
+
}): Promise<Hex> {
|
|
223
|
+
// Custom signer logic
|
|
224
|
+
return "0x..."; // return signature
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
// ...
|
|
228
|
+
new ethers.Wallet("0x..."),
|
|
229
|
+
];
|
|
230
|
+
|
|
231
|
+
const transport = new hl.HttpTransport();
|
|
232
|
+
const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends ExchangeClient
|
|
233
|
+
```
|
|
234
|
+
|
|
157
235
|
### 3) Use Client
|
|
158
236
|
|
|
159
237
|
Finally, use client methods to interact with the Hyperliquid API (more details in the [API Reference](#clients)):
|
|
@@ -183,6 +261,7 @@ import * as hl from "@nktkas/hyperliquid";
|
|
|
183
261
|
import { privateKeyToAccount } from "viem/accounts";
|
|
184
262
|
|
|
185
263
|
const account = privateKeyToAccount("0x...");
|
|
264
|
+
|
|
186
265
|
const transport = new hl.HttpTransport();
|
|
187
266
|
const exchClient = new hl.ExchangeClient({ wallet: account, transport });
|
|
188
267
|
|
|
@@ -225,34 +304,78 @@ const transport = new hl.WebSocketTransport();
|
|
|
225
304
|
const subsClient = new hl.SubscriptionClient({ transport });
|
|
226
305
|
|
|
227
306
|
// L2 Book updates
|
|
228
|
-
await
|
|
307
|
+
await subsClient.l2Book({ coin: "BTC" }, (data) => {
|
|
229
308
|
console.log(data);
|
|
230
309
|
});
|
|
231
310
|
|
|
232
311
|
// User fills
|
|
233
|
-
await
|
|
312
|
+
await subsClient.userFills({ user: "0x..." }, (data) => {
|
|
234
313
|
console.log(data);
|
|
235
314
|
});
|
|
236
315
|
|
|
237
|
-
//
|
|
238
|
-
await
|
|
316
|
+
// Candle updates
|
|
317
|
+
const sub = await subsClient.candle({ coin: "BTC", interval: "1h" }, (data) => {
|
|
239
318
|
console.log(data);
|
|
240
319
|
});
|
|
241
320
|
```
|
|
242
321
|
|
|
243
|
-
|
|
322
|
+
#### Example of using a MultiSignClient
|
|
244
323
|
|
|
245
|
-
|
|
324
|
+
```ts
|
|
325
|
+
import * as hl from "@nktkas/hyperliquid";
|
|
326
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
327
|
+
|
|
328
|
+
const multiSignAddress = "0x...";
|
|
329
|
+
const signers = [privateKeyToAccount("0x...")];
|
|
330
|
+
|
|
331
|
+
const transport = new hl.HttpTransport();
|
|
332
|
+
const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
|
|
333
|
+
|
|
334
|
+
// Interaction is the same as with ExchangeClient
|
|
335
|
+
|
|
336
|
+
// Place an orders
|
|
337
|
+
const result = await multiSignClient.order({
|
|
338
|
+
orders: [{
|
|
339
|
+
a: 0,
|
|
340
|
+
b: true,
|
|
341
|
+
p: "30000",
|
|
342
|
+
s: "0.1",
|
|
343
|
+
r: false,
|
|
344
|
+
t: {
|
|
345
|
+
limit: {
|
|
346
|
+
tif: "Gtc",
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
}],
|
|
350
|
+
grouping: "na",
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
// Approve an agent
|
|
354
|
+
const result = await multiSignClient.approveAgent({
|
|
355
|
+
agentAddress: "0x...",
|
|
356
|
+
agentName: "agentName",
|
|
357
|
+
});
|
|
246
358
|
|
|
247
|
-
|
|
359
|
+
// Withdraw funds
|
|
360
|
+
const result = await multiSignClient.withdraw3({
|
|
361
|
+
destination: account.address,
|
|
362
|
+
amount: "100",
|
|
363
|
+
});
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
</details>
|
|
367
|
+
|
|
368
|
+
<details>
|
|
369
|
+
<summary><h2>API Reference</h2></summary>
|
|
370
|
+
|
|
371
|
+
### Clients
|
|
248
372
|
|
|
249
|
-
|
|
373
|
+
A client is an interface through which you can interact with the Hyperliquid API.
|
|
250
374
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
`l2Book` and `clearinghouseState`.
|
|
375
|
+
The client is responsible for formatting an action, creating a signature correctly, sending a request, and validating a
|
|
376
|
+
response.
|
|
254
377
|
|
|
255
|
-
|
|
378
|
+
#### InfoClient
|
|
256
379
|
|
|
257
380
|
```ts
|
|
258
381
|
class InfoClient {
|
|
@@ -317,22 +440,14 @@ class InfoClient {
|
|
|
317
440
|
vaultDetails(args: VaultDetailsParameters): Promise<VaultDetails | null>;
|
|
318
441
|
vaultSummaries(): Promise<VaultSummary[]>;
|
|
319
442
|
|
|
320
|
-
// Explorer
|
|
321
|
-
blockDetails(args: BlockDetailsParameters): Promise<
|
|
322
|
-
txDetails(args: TxDetailsParameters): Promise<
|
|
323
|
-
userDetails(args: UserDetailsParameters): Promise<
|
|
443
|
+
// Explorer (RPC endpoint)
|
|
444
|
+
blockDetails(args: BlockDetailsParameters): Promise<BlockDetails>;
|
|
445
|
+
txDetails(args: TxDetailsParameters): Promise<TxDetails>;
|
|
446
|
+
userDetails(args: UserDetailsParameters): Promise<TxDetails[]>;
|
|
324
447
|
}
|
|
325
448
|
```
|
|
326
449
|
|
|
327
|
-
####
|
|
328
|
-
|
|
329
|
-
An Exchange Client which provides access to
|
|
330
|
-
[Exchange API](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint), such as `order`
|
|
331
|
-
and `withdraw3`.
|
|
332
|
-
|
|
333
|
-
The Exchange Client class sets up with a given [Transport](#transports) and a wallet instance, which can be a
|
|
334
|
-
[viem](https://viem.sh/docs/clients/wallet), [ethers.js](https://docs.ethers.org/v6/api/providers/#Signer) or other
|
|
335
|
-
wallet libraries.
|
|
450
|
+
#### ExchangeClient
|
|
336
451
|
|
|
337
452
|
```ts
|
|
338
453
|
class ExchangeClient {
|
|
@@ -342,8 +457,7 @@ class ExchangeClient {
|
|
|
342
457
|
| AbstractViemWalletClient // viem
|
|
343
458
|
| AbstractEthersSigner // ethers
|
|
344
459
|
| AbstractEthersV5Signer // ethers v5
|
|
345
|
-
|
|
|
346
|
-
| AbstractWindowEthereum; // window.ethereum (EIP-1193) directly
|
|
460
|
+
| AbstractWindowEthereum; // window.ethereum (EIP-1193)
|
|
347
461
|
isTestnet?: boolean; // Whether to use testnet (default: false)
|
|
348
462
|
defaultVaultAddress?: Hex; // Vault address used by default if not provided in method call
|
|
349
463
|
signatureChainId?: Hex | (() => MaybePromise<Hex>); // Chain ID used for signing (default: trying to guess based on wallet and isTestnet)
|
|
@@ -375,7 +489,7 @@ class ExchangeClient {
|
|
|
375
489
|
setReferrer(args: SetReferrerParameters): Promise<SuccessResponse>;
|
|
376
490
|
spotUser(args: SpotUserParameters): Promise<SuccessResponse>;
|
|
377
491
|
|
|
378
|
-
//
|
|
492
|
+
// Transfer
|
|
379
493
|
perpDexClassTransfer(args: PerpDexClassTransferParameters): Promise<SuccessResponse>;
|
|
380
494
|
spotSend(args: SpotSendParameters): Promise<SuccessResponse>;
|
|
381
495
|
subAccountSpotTransfer(args: SubAccountSpotTransferParameters): Promise<SuccessResponse>;
|
|
@@ -408,13 +522,7 @@ class ExchangeClient {
|
|
|
408
522
|
}
|
|
409
523
|
```
|
|
410
524
|
|
|
411
|
-
####
|
|
412
|
-
|
|
413
|
-
A Subscription Client which provides access to
|
|
414
|
-
[Subscriptions API](https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/websocket/subscriptions), such as
|
|
415
|
-
real-time updates for `l2Book` and `userFills`.
|
|
416
|
-
|
|
417
|
-
The Subscription Client class sets up with a given [WebSocket Transport](#websocket-transport).
|
|
525
|
+
#### SubscriptionClient
|
|
418
526
|
|
|
419
527
|
<!-- deno-fmt-ignore-start -->
|
|
420
528
|
```ts
|
|
@@ -452,23 +560,41 @@ class SubscriptionClient {
|
|
|
452
560
|
```
|
|
453
561
|
<!-- deno-fmt-ignore-end -->
|
|
454
562
|
|
|
455
|
-
|
|
563
|
+
#### MultiSignClient
|
|
456
564
|
|
|
457
|
-
|
|
458
|
-
|
|
565
|
+
```ts
|
|
566
|
+
class MultiSignClient extends ExchangeClient {
|
|
567
|
+
constructor(
|
|
568
|
+
args:
|
|
569
|
+
& Omit<ExchangeClientParameters, "wallet"> // Instead of `wallet`, you should specify the following parameters:
|
|
570
|
+
& {
|
|
571
|
+
multiSignAddress: Hex; // Multi-signature address
|
|
572
|
+
signers: [ // Array of signers
|
|
573
|
+
AbstractWalletWithAddress, // First signer is the leader of a multi-sign transaction
|
|
574
|
+
...AbstractWallet[], // Any number of additional signers
|
|
575
|
+
];
|
|
576
|
+
},
|
|
577
|
+
);
|
|
578
|
+
|
|
579
|
+
// Same methods as ExchangeClient
|
|
580
|
+
}
|
|
581
|
+
```
|
|
459
582
|
|
|
460
|
-
|
|
583
|
+
### Transports
|
|
461
584
|
|
|
462
|
-
|
|
585
|
+
Transport acts as a layer between the class and Hyperliquid servers.
|
|
463
586
|
|
|
464
|
-
|
|
465
|
-
API.
|
|
587
|
+
#### HTTP Transport
|
|
466
588
|
|
|
467
589
|
```ts
|
|
468
590
|
class HttpTransport {
|
|
469
591
|
constructor(options?: {
|
|
470
592
|
isTestnet?: boolean; // Whether to use testnet url (default: false)
|
|
471
593
|
timeout?: number; // Request timeout in ms (default: 10_000)
|
|
594
|
+
server?: { // Custom server URLs
|
|
595
|
+
mainnet?: { api?: string | URL; rpc?: string | URL };
|
|
596
|
+
testnet?: { api?: string | URL; rpc?: string | URL };
|
|
597
|
+
};
|
|
472
598
|
fetchOptions?: RequestInit; // A custom fetch options
|
|
473
599
|
onRequest?: (request: Request) => MaybePromise<Request | void | null | undefined>; // A callback before request is sent
|
|
474
600
|
onResponse?: (response: Response) => MaybePromise<Response | void | null | undefined>; // A callback after response is received
|
|
@@ -478,19 +604,16 @@ class HttpTransport {
|
|
|
478
604
|
|
|
479
605
|
#### WebSocket Transport
|
|
480
606
|
|
|
481
|
-
A WebSocket Transport that executes requests and subscribes to events via a
|
|
482
|
-
[WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) connection.
|
|
483
|
-
|
|
484
607
|
```ts
|
|
485
608
|
class WebSocketTransport {
|
|
486
609
|
constructor(options?: {
|
|
487
610
|
url?: string | URL; // WebSocket URL (default: "wss://api.hyperliquid.xyz/ws")
|
|
488
611
|
timeout?: number; // Request timeout in ms (default: 10_000)
|
|
489
|
-
keepAlive?: {
|
|
612
|
+
keepAlive?: {
|
|
490
613
|
interval?: number; // Ping interval in ms (default: 30_000)
|
|
491
614
|
timeout?: number; // Pong timeout in ms (default: same as `timeout` for requests)
|
|
492
615
|
};
|
|
493
|
-
reconnect?: {
|
|
616
|
+
reconnect?: {
|
|
494
617
|
maxRetries?: number; // Maximum number of reconnection attempts (default: 3)
|
|
495
618
|
connectionTimeout?: number; // Connection timeout in ms (default: 10_000)
|
|
496
619
|
connectionDelay?: number | ((attempt: number) => number | Promise<number>); // Delay between reconnection (default: Exponential backoff (max 10s))
|
|
@@ -504,7 +627,10 @@ class WebSocketTransport {
|
|
|
504
627
|
}
|
|
505
628
|
```
|
|
506
629
|
|
|
507
|
-
|
|
630
|
+
</details>
|
|
631
|
+
|
|
632
|
+
<details>
|
|
633
|
+
<summary><h2>Additional Import Points</h2></summary>
|
|
508
634
|
|
|
509
635
|
### `/types`
|
|
510
636
|
|
|
@@ -517,13 +643,13 @@ The import point gives access to functions that generate signatures for Hyperliq
|
|
|
517
643
|
|
|
518
644
|
### Examples
|
|
519
645
|
|
|
520
|
-
#### Cancel
|
|
646
|
+
#### Cancel order yourself
|
|
521
647
|
|
|
522
648
|
```ts
|
|
523
|
-
import { signL1Action } from "@nktkas/hyperliquid/signing";
|
|
524
|
-
import { privateKeyToAccount } from "viem/accounts";
|
|
649
|
+
import { actionSorter, signL1Action } from "@nktkas/hyperliquid/signing";
|
|
650
|
+
import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
|
|
525
651
|
|
|
526
|
-
const wallet = privateKeyToAccount("0x...");
|
|
652
|
+
const wallet = privateKeyToAccount("0x...");
|
|
527
653
|
|
|
528
654
|
const action = {
|
|
529
655
|
type: "cancel",
|
|
@@ -535,9 +661,9 @@ const nonce = Date.now();
|
|
|
535
661
|
|
|
536
662
|
const signature = await signL1Action({
|
|
537
663
|
wallet,
|
|
538
|
-
action,
|
|
664
|
+
action: actionSorter[action.type](action), // key order affects signature
|
|
539
665
|
nonce,
|
|
540
|
-
isTestnet: true, //
|
|
666
|
+
isTestnet: true, // change to `false` for mainnet
|
|
541
667
|
});
|
|
542
668
|
|
|
543
669
|
const response = await fetch("https://api.hyperliquid-testnet.xyz/exchange", {
|
|
@@ -548,34 +674,27 @@ const response = await fetch("https://api.hyperliquid-testnet.xyz/exchange", {
|
|
|
548
674
|
const body = await response.json();
|
|
549
675
|
```
|
|
550
676
|
|
|
551
|
-
#### Approve
|
|
677
|
+
#### Approve agent yourself
|
|
552
678
|
|
|
553
679
|
```ts
|
|
554
|
-
import { signUserSignedAction } from "@nktkas/hyperliquid/signing";
|
|
555
|
-
import { privateKeyToAccount } from "viem/accounts";
|
|
680
|
+
import { signUserSignedAction, userSignedActionEip712Types } from "@nktkas/hyperliquid/signing";
|
|
681
|
+
import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
|
|
556
682
|
|
|
557
|
-
const wallet = privateKeyToAccount("0x...");
|
|
683
|
+
const wallet = privateKeyToAccount("0x...");
|
|
558
684
|
|
|
559
685
|
const action = {
|
|
560
686
|
type: "approveAgent",
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
nonce: Date.now(),
|
|
687
|
+
signatureChainId: "0x66eee", // must match the current wallet network
|
|
688
|
+
hyperliquidChain: "Testnet", // Mainnet | Testnet
|
|
564
689
|
agentAddress: "0x...",
|
|
565
690
|
agentName: "Agent",
|
|
691
|
+
nonce: Date.now(),
|
|
566
692
|
};
|
|
567
693
|
|
|
568
694
|
const signature = await signUserSignedAction({
|
|
569
695
|
wallet,
|
|
570
696
|
action,
|
|
571
|
-
types:
|
|
572
|
-
"HyperliquidTransaction:ApproveAgent": [
|
|
573
|
-
{ name: "hyperliquidChain", type: "string" },
|
|
574
|
-
{ name: "agentAddress", type: "address" },
|
|
575
|
-
{ name: "agentName", type: "string" },
|
|
576
|
-
{ name: "nonce", type: "uint64" },
|
|
577
|
-
],
|
|
578
|
-
},
|
|
697
|
+
types: userSignedActionEip712Types[action.type], // key order affects signature
|
|
579
698
|
chainId: parseInt(action.signatureChainId, 16),
|
|
580
699
|
});
|
|
581
700
|
|
|
@@ -586,3 +705,9 @@ const response = await fetch("https://api.hyperliquid-testnet.xyz/exchange", {
|
|
|
586
705
|
});
|
|
587
706
|
const body = await response.json();
|
|
588
707
|
```
|
|
708
|
+
|
|
709
|
+
</details>
|
|
710
|
+
|
|
711
|
+
## Contributing
|
|
712
|
+
|
|
713
|
+
We appreciate your help! To contribute, please read the [contributing instructions](CONTRIBUTING.md).
|
package/esm/mod.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export * from "./src/base.js";
|
|
2
2
|
export * from "./src/transports/base.js";
|
|
3
|
-
export type { AbstractEthersSigner, AbstractEthersV5Signer,
|
|
4
|
-
export * from "./src/clients/subscription.js";
|
|
5
|
-
export * from "./src/clients/info.js";
|
|
3
|
+
export type { AbstractEthersSigner, AbstractEthersV5Signer, AbstractViemWalletClient, AbstractWindowEthereum, } from "./src/signing.js";
|
|
6
4
|
export * from "./src/clients/exchange.js";
|
|
5
|
+
export * from "./src/clients/info.js";
|
|
6
|
+
export * from "./src/clients/multiSign.js";
|
|
7
|
+
export * from "./src/clients/subscription.js";
|
|
7
8
|
export * from "./src/transports/http/http_transport.js";
|
|
8
9
|
export * from "./src/transports/websocket/websocket_transport.js";
|
|
9
10
|
export type * from "./src/types/exchange/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;AAC9B,cAAc,0BAA0B,CAAC;AAGzC,YAAY,EACR,oBAAoB,EACpB,sBAAsB,EACtB,
|
|
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,kBAAkB,CAAC;AAG1B,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,iCAAiC,CAAC;AACrD,mBAAmB,6BAA6B,CAAC;AACjD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,wCAAwC,CAAC"}
|
package/esm/mod.js
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
export * from "./src/base.js";
|
|
3
3
|
export * from "./src/transports/base.js";
|
|
4
4
|
// Clients
|
|
5
|
-
export * from "./src/clients/subscription.js";
|
|
6
|
-
export * from "./src/clients/info.js";
|
|
7
5
|
export * from "./src/clients/exchange.js";
|
|
6
|
+
export * from "./src/clients/info.js";
|
|
7
|
+
export * from "./src/clients/multiSign.js";
|
|
8
|
+
export * from "./src/clients/subscription.js";
|
|
8
9
|
// Transports
|
|
9
10
|
export * from "./src/transports/http/http_transport.js";
|
|
10
11
|
export * from "./src/transports/websocket/websocket_transport.js";
|