@nktkas/hyperliquid 0.22.0 → 0.22.2
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 +84 -27
- package/esm/mod.d.ts +1 -1
- package/esm/mod.d.ts.map +1 -1
- package/esm/src/clients/exchange.d.ts +136 -166
- package/esm/src/clients/exchange.d.ts.map +1 -1
- package/esm/src/clients/exchange.js +122 -147
- package/esm/src/clients/multiSign.d.ts +129 -282
- package/esm/src/clients/multiSign.d.ts.map +1 -1
- package/esm/src/clients/multiSign.js +125 -246
- package/esm/src/signing/_ethers.d.ts +33 -0
- package/esm/src/signing/_ethers.d.ts.map +1 -0
- package/esm/src/signing/_ethers.js +12 -0
- package/esm/src/signing/_private_key.d.ts +22 -0
- package/esm/src/signing/_private_key.d.ts.map +1 -0
- package/esm/src/signing/_private_key.js +124 -0
- package/esm/src/signing/_sorter.d.ts +154 -0
- package/esm/src/signing/_sorter.d.ts.map +1 -0
- package/esm/src/{signing.js → signing/_sorter.js} +1 -401
- package/esm/src/signing/_viem.d.ts +23 -0
- package/esm/src/signing/_viem.d.ts.map +1 -0
- package/esm/src/signing/_viem.js +6 -0
- package/esm/src/signing/_window.d.ts +23 -0
- package/esm/src/signing/_window.d.ts.map +1 -0
- package/esm/src/signing/_window.js +29 -0
- package/esm/src/signing/mod.d.ts +251 -0
- package/esm/src/signing/mod.d.ts.map +1 -0
- package/esm/src/signing/mod.js +352 -0
- package/esm/src/types/exchange/requests.d.ts +1 -1
- package/esm/src/types/exchange/requests.d.ts.map +1 -1
- package/package.json +6 -5
- package/script/mod.d.ts +1 -1
- package/script/mod.d.ts.map +1 -1
- package/script/src/clients/exchange.d.ts +136 -166
- package/script/src/clients/exchange.d.ts.map +1 -1
- package/script/src/clients/exchange.js +206 -231
- package/script/src/clients/multiSign.d.ts +129 -282
- package/script/src/clients/multiSign.d.ts.map +1 -1
- package/script/src/clients/multiSign.js +170 -291
- package/script/src/signing/_ethers.d.ts +33 -0
- package/script/src/signing/_ethers.d.ts.map +1 -0
- package/script/src/signing/_ethers.js +26 -0
- package/script/src/signing/_private_key.d.ts +22 -0
- package/script/src/signing/_private_key.d.ts.map +1 -0
- package/script/src/signing/_private_key.js +138 -0
- package/script/src/signing/_sorter.d.ts +154 -0
- package/script/src/signing/_sorter.d.ts.map +1 -0
- package/script/src/{signing.js → signing/_sorter.js} +2 -410
- package/script/src/signing/_viem.d.ts +23 -0
- package/script/src/signing/_viem.d.ts.map +1 -0
- package/script/src/signing/_viem.js +19 -0
- package/script/src/signing/_window.d.ts +23 -0
- package/script/src/signing/_window.d.ts.map +1 -0
- package/script/src/signing/_window.js +43 -0
- package/script/src/signing/mod.d.ts +251 -0
- package/script/src/signing/mod.d.ts.map +1 -0
- package/script/src/signing/mod.js +387 -0
- package/script/src/types/exchange/requests.d.ts +1 -1
- package/script/src/types/exchange/requests.d.ts.map +1 -1
- package/esm/src/signing.d.ts +0 -463
- package/esm/src/signing.d.ts.map +0 -1
- package/script/src/signing.d.ts +0 -463
- package/script/src/signing.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -48,6 +48,66 @@ deno add jsr:@nktkas/hyperliquid
|
|
|
48
48
|
</script>
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
+
### React Native
|
|
52
|
+
|
|
53
|
+
<details>
|
|
54
|
+
<summary>For React Native, you need to import several polyfills before importing the SDK:</summary>
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
// React Native 0.76.3
|
|
58
|
+
import { Event, EventTarget } from "event-target-shim";
|
|
59
|
+
|
|
60
|
+
if (!globalThis.EventTarget || !globalThis.Event) {
|
|
61
|
+
globalThis.EventTarget = EventTarget;
|
|
62
|
+
globalThis.Event = Event;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!globalThis.CustomEvent) {
|
|
66
|
+
globalThis.CustomEvent = function (type, params) {
|
|
67
|
+
params = params || {};
|
|
68
|
+
const event = new Event(type, params);
|
|
69
|
+
event.detail = params.detail || null;
|
|
70
|
+
return event;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!AbortSignal.timeout) {
|
|
75
|
+
AbortSignal.timeout = function (delay) {
|
|
76
|
+
const controller = new AbortController();
|
|
77
|
+
setTimeout(() => controller.abort(), delay);
|
|
78
|
+
return controller.signal;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!Promise.withResolvers) {
|
|
83
|
+
Promise.withResolvers = function () {
|
|
84
|
+
let resolve, reject;
|
|
85
|
+
const promise = new Promise((res, rej) => {
|
|
86
|
+
resolve = res;
|
|
87
|
+
reject = rej;
|
|
88
|
+
});
|
|
89
|
+
return { promise, resolve, reject };
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (!ArrayBuffer.prototype.transfer) {
|
|
94
|
+
ArrayBuffer.prototype.transfer = function (newByteLength) {
|
|
95
|
+
const length = newByteLength ?? this.byteLength;
|
|
96
|
+
const newBuffer = new ArrayBuffer(length);
|
|
97
|
+
const oldView = new Uint8Array(this);
|
|
98
|
+
const newView = new Uint8Array(newBuffer);
|
|
99
|
+
|
|
100
|
+
newView.set(oldView.subarray(0, Math.min(oldView.length, length)));
|
|
101
|
+
|
|
102
|
+
Object.defineProperty(this, "byteLength", { value: 0 });
|
|
103
|
+
|
|
104
|
+
return newBuffer;
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
</details>
|
|
110
|
+
|
|
51
111
|
## Quick Start
|
|
52
112
|
|
|
53
113
|
#### Info endpoint
|
|
@@ -65,12 +125,11 @@ const openOrders = await infoClient.openOrders({ user: "0x..." });
|
|
|
65
125
|
|
|
66
126
|
```ts
|
|
67
127
|
import * as hl from "@nktkas/hyperliquid";
|
|
68
|
-
import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
|
|
69
128
|
|
|
70
|
-
const
|
|
129
|
+
const privateKey = "0x..."; // or `viem`, `ethers`
|
|
71
130
|
|
|
72
131
|
const transport = new hl.HttpTransport();
|
|
73
|
-
const exchClient = new hl.ExchangeClient({ wallet, transport });
|
|
132
|
+
const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
|
|
74
133
|
|
|
75
134
|
const result = await exchClient.order({
|
|
76
135
|
orders: [{
|
|
@@ -108,15 +167,11 @@ await sub.unsubscribe(); // unsubscribe from the event
|
|
|
108
167
|
|
|
109
168
|
```ts
|
|
110
169
|
import * as hl from "@nktkas/hyperliquid";
|
|
111
|
-
import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
|
|
112
170
|
|
|
113
171
|
const multiSignAddress = "0x...";
|
|
114
172
|
const signers = [
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
// ...
|
|
118
|
-
privateKeyToAccount("0x..."),
|
|
119
|
-
];
|
|
173
|
+
"0x...", // Private key; or any other wallet libraries
|
|
174
|
+
] as const;
|
|
120
175
|
|
|
121
176
|
const transport = new hl.HttpTransport();
|
|
122
177
|
const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends ExchangeClient
|
|
@@ -167,20 +222,24 @@ import { ethers } from "ethers";
|
|
|
167
222
|
|
|
168
223
|
const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
169
224
|
|
|
170
|
-
// 1. Using
|
|
225
|
+
// 1. Using private key
|
|
226
|
+
const privateKey = "0x...";
|
|
227
|
+
const exchClient_privateKey = new hl.ExchangeClient({ wallet: privateKey, transport });
|
|
228
|
+
|
|
229
|
+
// 2. Using Viem with private key
|
|
171
230
|
const viemAccount = privateKeyToAccount("0x...");
|
|
172
231
|
const exchClient_viem = new hl.ExchangeClient({ wallet: viemAccount, transport });
|
|
173
232
|
|
|
174
|
-
//
|
|
233
|
+
// 3. Using Ethers (or Ethers V5) with private key
|
|
175
234
|
const ethersWallet = new ethers.Wallet("0x...");
|
|
176
235
|
const exchClient_ethers = new hl.ExchangeClient({ wallet: ethersWallet, transport });
|
|
177
236
|
|
|
178
|
-
//
|
|
237
|
+
// 4. Using external wallet (e.g. MetaMask) via Viem
|
|
179
238
|
const [account] = await window.ethereum.request({ method: "eth_requestAccounts" });
|
|
180
239
|
const externalWallet = createWalletClient({ account, transport: custom(window.ethereum) });
|
|
181
240
|
const exchClient_viemMetamask = new hl.ExchangeClient({ wallet: externalWallet, transport });
|
|
182
241
|
|
|
183
|
-
//
|
|
242
|
+
// 5. Using external wallet (e.g. MetaMask) via `window.ethereum` (EIP-1193)
|
|
184
243
|
const exchClient_windowMetamask = new hl.ExchangeClient({ wallet: window.ethereum, transport });
|
|
185
244
|
```
|
|
186
245
|
|
|
@@ -203,6 +262,7 @@ import { ethers } from "ethers";
|
|
|
203
262
|
const multiSignAddress = "0x...";
|
|
204
263
|
const signers = [
|
|
205
264
|
privateKeyToAccount("0x..."), // first is leader for multi-sign transaction, must contain own address
|
|
265
|
+
new ethers.Wallet("0x..."),
|
|
206
266
|
{ // can be a custom async wallet
|
|
207
267
|
signTypedData(params: {
|
|
208
268
|
domain: {
|
|
@@ -224,8 +284,7 @@ const signers = [
|
|
|
224
284
|
return "0x..."; // return signature
|
|
225
285
|
},
|
|
226
286
|
},
|
|
227
|
-
//
|
|
228
|
-
new ethers.Wallet("0x..."),
|
|
287
|
+
"0x...", // private key directly
|
|
229
288
|
];
|
|
230
289
|
|
|
231
290
|
const transport = new hl.HttpTransport();
|
|
@@ -258,12 +317,11 @@ const openOrders = await infoClient.openOrders({ user: "0x..." });
|
|
|
258
317
|
|
|
259
318
|
```ts
|
|
260
319
|
import * as hl from "@nktkas/hyperliquid";
|
|
261
|
-
import { privateKeyToAccount } from "viem/accounts";
|
|
262
320
|
|
|
263
|
-
const
|
|
321
|
+
const privateKey = "0x..."; // or `viem`, `ethers`
|
|
264
322
|
|
|
265
323
|
const transport = new hl.HttpTransport();
|
|
266
|
-
const exchClient = new hl.ExchangeClient({ wallet:
|
|
324
|
+
const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
|
|
267
325
|
|
|
268
326
|
// Place an orders
|
|
269
327
|
const result = await exchClient.order({
|
|
@@ -323,10 +381,11 @@ const sub = await subsClient.candle({ coin: "BTC", interval: "1h" }, (data) => {
|
|
|
323
381
|
|
|
324
382
|
```ts
|
|
325
383
|
import * as hl from "@nktkas/hyperliquid";
|
|
326
|
-
import { privateKeyToAccount } from "viem/accounts";
|
|
327
384
|
|
|
328
385
|
const multiSignAddress = "0x...";
|
|
329
|
-
const signers = [
|
|
386
|
+
const signers = [
|
|
387
|
+
"0x...", // Private keys
|
|
388
|
+
] as const;
|
|
330
389
|
|
|
331
390
|
const transport = new hl.HttpTransport();
|
|
332
391
|
const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
|
|
@@ -555,7 +614,7 @@ class SubscriptionClient {
|
|
|
555
614
|
|
|
556
615
|
// Explorer
|
|
557
616
|
explorerBlock(listener: (data: WsBlockDetails[]) => void): Promise<Subscription>;
|
|
558
|
-
|
|
617
|
+
explorerTxs(listener: (data: TxDetails[]) => void): Promise<Subscription>;
|
|
559
618
|
}
|
|
560
619
|
```
|
|
561
620
|
<!-- deno-fmt-ignore-end -->
|
|
@@ -647,9 +706,8 @@ The import point gives access to functions that generate signatures for Hyperliq
|
|
|
647
706
|
|
|
648
707
|
```ts
|
|
649
708
|
import { actionSorter, signL1Action } from "@nktkas/hyperliquid/signing";
|
|
650
|
-
import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
|
|
651
709
|
|
|
652
|
-
const
|
|
710
|
+
const privateKey = "0x..."; // or `viem`, `ethers`
|
|
653
711
|
|
|
654
712
|
const action = {
|
|
655
713
|
type: "cancel",
|
|
@@ -660,7 +718,7 @@ const action = {
|
|
|
660
718
|
const nonce = Date.now();
|
|
661
719
|
|
|
662
720
|
const signature = await signL1Action({
|
|
663
|
-
wallet,
|
|
721
|
+
wallet: privateKey,
|
|
664
722
|
action: actionSorter[action.type](action), // key order affects signature
|
|
665
723
|
nonce,
|
|
666
724
|
isTestnet: true, // change to `false` for mainnet
|
|
@@ -678,9 +736,8 @@ const body = await response.json();
|
|
|
678
736
|
|
|
679
737
|
```ts
|
|
680
738
|
import { signUserSignedAction, userSignedActionEip712Types } from "@nktkas/hyperliquid/signing";
|
|
681
|
-
import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
|
|
682
739
|
|
|
683
|
-
const
|
|
740
|
+
const privateKey = "0x..."; // or `viem`, `ethers`
|
|
684
741
|
|
|
685
742
|
const action = {
|
|
686
743
|
type: "approveAgent",
|
|
@@ -692,7 +749,7 @@ const action = {
|
|
|
692
749
|
};
|
|
693
750
|
|
|
694
751
|
const signature = await signUserSignedAction({
|
|
695
|
-
wallet,
|
|
752
|
+
wallet: privateKey,
|
|
696
753
|
action,
|
|
697
754
|
types: userSignedActionEip712Types[action.type], // key order affects signature
|
|
698
755
|
chainId: parseInt(action.signatureChainId, 16),
|
package/esm/mod.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from "./src/base.js";
|
|
2
2
|
export * from "./src/transports/base.js";
|
|
3
|
-
export type { AbstractEthersSigner, AbstractEthersV5Signer, AbstractViemWalletClient, AbstractWindowEthereum, } from "./src/signing.js";
|
|
3
|
+
export type { AbstractEthersSigner, AbstractEthersV5Signer, AbstractViemWalletClient, AbstractWindowEthereum, } from "./src/signing/mod.js";
|
|
4
4
|
export * from "./src/clients/exchange.js";
|
|
5
5
|
export * from "./src/clients/info.js";
|
|
6
6
|
export * from "./src/clients/multiSign.js";
|
package/esm/mod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AACA,cAAc,eAAe,CAAC;AAC9B,cAAc,0BAA0B,CAAC;AAGzC,YAAY,EACR,oBAAoB,EACpB,sBAAsB,EACtB,wBAAwB,EACxB,sBAAsB,GACzB,MAAM,
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AACA,cAAc,eAAe,CAAC;AAC9B,cAAc,0BAA0B,CAAC;AAGzC,YAAY,EACR,oBAAoB,EACpB,sBAAsB,EACtB,wBAAwB,EACxB,sBAAsB,GACzB,MAAM,sBAAsB,CAAC;AAG9B,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAG9C,cAAc,yCAAyC,CAAC;AACxD,cAAc,mDAAmD,CAAC;AAGlE,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,mCAAmC,CAAC;AACvD,mBAAmB,8BAA8B,CAAC;AAClD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,iCAAiC,CAAC;AACrD,mBAAmB,6BAA6B,CAAC;AACjD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,4BAA4B,CAAC;AAChD,mBAAmB,wCAAwC,CAAC"}
|