@nktkas/hyperliquid 0.22.1 → 0.23.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 +125 -70
- package/esm/mod.d.ts +1 -1
- package/esm/mod.d.ts.map +1 -1
- package/esm/src/base.d.ts +4 -1
- package/esm/src/base.d.ts.map +1 -1
- package/esm/src/base.js +2 -2
- package/esm/src/clients/exchange.d.ts +420 -368
- package/esm/src/clients/exchange.d.ts.map +1 -1
- package/esm/src/clients/exchange.js +721 -551
- package/esm/src/clients/info.d.ts +137 -41
- package/esm/src/clients/info.d.ts.map +1 -1
- package/esm/src/clients/info.js +124 -28
- package/esm/src/clients/multiSign.d.ts +406 -484
- package/esm/src/clients/multiSign.d.ts.map +1 -1
- package/esm/src/clients/multiSign.js +639 -734
- package/esm/src/clients/subscription.d.ts +38 -3
- package/esm/src/clients/subscription.d.ts.map +1 -1
- package/esm/src/clients/subscription.js +36 -3
- package/esm/src/signing/_signTypedData/ethers.d.ts +33 -0
- package/esm/src/signing/_signTypedData/ethers.d.ts.map +1 -0
- package/esm/src/signing/_signTypedData/ethers.js +12 -0
- package/esm/src/signing/_signTypedData/mod.d.ts +35 -0
- package/esm/src/signing/_signTypedData/mod.d.ts.map +1 -0
- package/esm/src/signing/_signTypedData/mod.js +59 -0
- package/esm/src/signing/_signTypedData/private_key.d.ts +26 -0
- package/esm/src/signing/_signTypedData/private_key.d.ts.map +1 -0
- package/esm/src/signing/_signTypedData/private_key.js +144 -0
- package/esm/src/signing/_signTypedData/viem.d.ts +23 -0
- package/esm/src/signing/_signTypedData/viem.d.ts.map +1 -0
- package/esm/src/signing/_signTypedData/viem.js +6 -0
- package/esm/src/signing/_signTypedData/window.d.ts +29 -0
- package/esm/src/signing/_signTypedData/window.d.ts.map +1 -0
- package/esm/src/signing/_signTypedData/window.js +30 -0
- package/esm/src/signing/_sorter.d.ts +128 -0
- package/esm/src/signing/_sorter.d.ts.map +1 -0
- package/esm/src/{signing.js → signing/_sorter.js} +36 -449
- package/esm/src/signing/mod.d.ts +272 -0
- package/esm/src/signing/mod.d.ts.map +1 -0
- package/esm/src/signing/mod.js +306 -0
- package/esm/src/transports/base.d.ts +1 -1
- package/esm/src/transports/base.d.ts.map +1 -1
- package/esm/src/transports/base.js +2 -2
- package/esm/src/transports/http/http_transport.d.ts +9 -15
- package/esm/src/transports/http/http_transport.d.ts.map +1 -1
- package/esm/src/transports/http/http_transport.js +64 -59
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts +1 -1
- package/esm/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
- package/esm/src/transports/websocket/_hyperliquid_event_target.js +1 -1
- package/esm/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
- package/esm/src/transports/websocket/_reconnecting_websocket.js +17 -4
- package/esm/src/transports/websocket/_websocket_async_request.d.ts +0 -9
- package/esm/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
- package/esm/src/transports/websocket/_websocket_async_request.js +14 -20
- package/esm/src/transports/websocket/websocket_transport.d.ts +8 -4
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/esm/src/transports/websocket/websocket_transport.js +83 -54
- package/esm/src/types/exchange/requests.d.ts +27 -51
- package/esm/src/types/exchange/requests.d.ts.map +1 -1
- package/esm/src/types/explorer/requests.d.ts +5 -8
- package/esm/src/types/explorer/requests.d.ts.map +1 -1
- package/esm/src/types/info/accounts.d.ts +5 -5
- package/esm/src/types/info/accounts.d.ts.map +1 -1
- package/esm/src/types/info/delegations.d.ts +1 -1
- package/esm/src/types/info/delegations.d.ts.map +1 -1
- package/esm/src/types/info/orders.d.ts +1 -1
- package/esm/src/types/info/orders.d.ts.map +1 -1
- package/esm/src/types/info/requests.d.ts +49 -96
- package/esm/src/types/info/requests.d.ts.map +1 -1
- package/esm/src/types/subscriptions/requests.d.ts +72 -18
- package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
- package/esm/src/types/subscriptions/responses.d.ts +2 -0
- package/esm/src/types/subscriptions/responses.d.ts.map +1 -1
- package/package.json +12 -8
- package/script/mod.d.ts +1 -1
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +13 -23
- package/script/src/base.d.ts +4 -1
- package/script/src/base.d.ts.map +1 -1
- package/script/src/base.js +10 -20
- package/script/src/clients/exchange.d.ts +420 -368
- package/script/src/clients/exchange.d.ts.map +1 -1
- package/script/src/clients/exchange.js +2000 -1840
- package/script/src/clients/info.d.ts +137 -41
- package/script/src/clients/info.d.ts.map +1 -1
- package/script/src/clients/info.js +1296 -1210
- package/script/src/clients/multiSign.d.ts +406 -484
- package/script/src/clients/multiSign.d.ts.map +1 -1
- package/script/src/clients/multiSign.js +2043 -2148
- package/script/src/clients/subscription.d.ts +38 -3
- package/script/src/clients/subscription.d.ts.map +1 -1
- package/script/src/clients/subscription.js +568 -545
- package/script/src/signing/_signTypedData/ethers.d.ts +33 -0
- package/script/src/signing/_signTypedData/ethers.d.ts.map +1 -0
- package/script/src/signing/_signTypedData/ethers.js +16 -0
- package/script/src/signing/_signTypedData/mod.d.ts +35 -0
- package/script/src/signing/_signTypedData/mod.d.ts.map +1 -0
- package/script/src/signing/_signTypedData/mod.js +67 -0
- package/script/src/signing/_signTypedData/private_key.d.ts +26 -0
- package/script/src/signing/_signTypedData/private_key.d.ts.map +1 -0
- package/script/src/signing/_signTypedData/private_key.js +148 -0
- package/script/src/signing/_signTypedData/viem.d.ts +23 -0
- package/script/src/signing/_signTypedData/viem.d.ts.map +1 -0
- package/script/src/signing/_signTypedData/viem.js +9 -0
- package/script/src/signing/_signTypedData/window.d.ts +29 -0
- package/script/src/signing/_signTypedData/window.d.ts.map +1 -0
- package/script/src/signing/_signTypedData/window.js +34 -0
- package/script/src/signing/_sorter.d.ts +128 -0
- package/script/src/signing/_sorter.d.ts.map +1 -0
- package/script/src/signing/_sorter.js +698 -0
- package/script/src/signing/mod.d.ts +272 -0
- package/script/src/signing/mod.d.ts.map +1 -0
- package/script/src/signing/mod.js +331 -0
- package/script/src/transports/base.d.ts +1 -1
- package/script/src/transports/base.d.ts.map +1 -1
- package/script/src/transports/base.js +11 -21
- package/script/src/transports/http/http_transport.d.ts +9 -15
- package/script/src/transports/http/http_transport.d.ts.map +1 -1
- package/script/src/transports/http/http_transport.js +97 -102
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts +1 -1
- package/script/src/transports/websocket/_hyperliquid_event_target.d.ts.map +1 -1
- package/script/src/transports/websocket/_hyperliquid_event_target.js +51 -61
- package/script/src/transports/websocket/_reconnecting_websocket.d.ts.map +1 -1
- package/script/src/transports/websocket/_reconnecting_websocket.js +271 -268
- package/script/src/transports/websocket/_websocket_async_request.d.ts +0 -9
- package/script/src/transports/websocket/_websocket_async_request.d.ts.map +1 -1
- package/script/src/transports/websocket/_websocket_async_request.js +157 -174
- package/script/src/transports/websocket/websocket_transport.d.ts +8 -4
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/script/src/transports/websocket/websocket_transport.js +209 -189
- package/script/src/types/exchange/requests.d.ts +27 -51
- package/script/src/types/exchange/requests.d.ts.map +1 -1
- package/script/src/types/exchange/requests.js +2 -12
- package/script/src/types/exchange/responses.js +2 -12
- package/script/src/types/explorer/requests.d.ts +5 -8
- package/script/src/types/explorer/requests.d.ts.map +1 -1
- package/script/src/types/explorer/requests.js +2 -12
- package/script/src/types/explorer/responses.js +2 -12
- package/script/src/types/info/accounts.d.ts +5 -5
- package/script/src/types/info/accounts.d.ts.map +1 -1
- package/script/src/types/info/accounts.js +2 -12
- package/script/src/types/info/assets.js +2 -12
- package/script/src/types/info/delegations.d.ts +1 -1
- package/script/src/types/info/delegations.d.ts.map +1 -1
- package/script/src/types/info/delegations.js +2 -12
- package/script/src/types/info/markets.js +2 -12
- package/script/src/types/info/orders.d.ts +1 -1
- package/script/src/types/info/orders.d.ts.map +1 -1
- package/script/src/types/info/orders.js +2 -12
- package/script/src/types/info/requests.d.ts +49 -96
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/info/requests.js +2 -12
- package/script/src/types/info/vaults.js +2 -12
- package/script/src/types/mod.js +2 -12
- package/script/src/types/subscriptions/requests.d.ts +72 -18
- package/script/src/types/subscriptions/requests.d.ts.map +1 -1
- package/script/src/types/subscriptions/requests.js +2 -12
- package/script/src/types/subscriptions/responses.d.ts +2 -0
- package/script/src/types/subscriptions/responses.d.ts.map +1 -1
- package/script/src/types/subscriptions/responses.js +2 -12
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +0 -2
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +0 -1
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +0 -1
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +0 -95
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +0 -1
- package/esm/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +0 -10
- package/esm/deps/jsr.io/@std/async/1.0.13/delay.d.ts +0 -43
- package/esm/deps/jsr.io/@std/async/1.0.13/delay.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/async/1.0.13/delay.js +0 -63
- package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts +0 -9
- package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/bytes/1.0.6/_types.js +0 -2
- package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts +0 -21
- package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/bytes/1.0.6/concat.js +0 -32
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -51
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -13
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -2
- package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
- package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -87
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +0 -9
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +0 -2
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts +0 -39
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.js +0 -237
- package/esm/src/signing.d.ts +0 -463
- package/esm/src/signing.d.ts.map +0 -1
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts +0 -2
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.d.ts.map +0 -1
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +0 -27
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts +0 -95
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.d.ts.map +0 -1
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +0 -24
- package/script/deps/jsr.io/@std/async/1.0.13/delay.d.ts +0 -43
- package/script/deps/jsr.io/@std/async/1.0.13/delay.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/async/1.0.13/delay.js +0 -76
- package/script/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts +0 -9
- package/script/deps/jsr.io/@std/bytes/1.0.6/_types.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/bytes/1.0.6/_types.js +0 -13
- package/script/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts +0 -21
- package/script/deps/jsr.io/@std/bytes/1.0.6/concat.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/bytes/1.0.6/concat.js +0 -45
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts +0 -23
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common16.js +0 -67
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts +0 -4
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/_common_detach.js +0 -26
- package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts +0 -9
- package/script/deps/jsr.io/@std/encoding/1.0.10/_types.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/_types.js +0 -13
- package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts +0 -39
- package/script/deps/jsr.io/@std/encoding/1.0.10/hex.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.10/hex.js +0 -101
- package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +0 -9
- package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +0 -13
- package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts +0 -39
- package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +0 -250
- package/script/src/signing.d.ts +0 -463
- package/script/src/signing.d.ts.map +0 -1
- package/script/src/signing.js +0 -1129
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# Hyperliquid API TypeScript SDK
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/@nktkas/hyperliquid)
|
|
4
|
+
[](https://jsr.io/@nktkas/hyperliquid)
|
|
5
|
+
[](https://coveralls.io/github/nktkas/hyperliquid)
|
|
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
9
|
runtimes, written in TypeScript and provided with tests.
|
|
@@ -44,10 +44,71 @@ deno add jsr:@nktkas/hyperliquid
|
|
|
44
44
|
```html
|
|
45
45
|
<script type="module">
|
|
46
46
|
import * as hl from "https://esm.sh/jsr/@nktkas/hyperliquid";
|
|
47
|
-
// Use hl.InfoClient, hl.ExchangeClient, etc.
|
|
48
47
|
</script>
|
|
49
48
|
```
|
|
50
49
|
|
|
50
|
+
### React Native
|
|
51
|
+
|
|
52
|
+
<details>
|
|
53
|
+
<summary>For React Native, you need to import polyfills before importing the SDK:</summary>
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
// React Native v0.76.3 / Expo v52
|
|
57
|
+
// Issues:
|
|
58
|
+
// - signing: does not support private keys directly, use viem or ethers
|
|
59
|
+
import { Event, EventTarget } from "event-target-shim";
|
|
60
|
+
|
|
61
|
+
if (!globalThis.EventTarget || !globalThis.Event) {
|
|
62
|
+
globalThis.EventTarget = EventTarget;
|
|
63
|
+
globalThis.Event = Event;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!globalThis.CustomEvent) {
|
|
67
|
+
globalThis.CustomEvent = function (type, params) {
|
|
68
|
+
params = params || {};
|
|
69
|
+
const event = new Event(type, params);
|
|
70
|
+
event.detail = params.detail || null;
|
|
71
|
+
return event;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!AbortSignal.timeout) {
|
|
76
|
+
AbortSignal.timeout = function (delay) {
|
|
77
|
+
const controller = new AbortController();
|
|
78
|
+
setTimeout(() => controller.abort(), delay);
|
|
79
|
+
return controller.signal;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!Promise.withResolvers) {
|
|
84
|
+
Promise.withResolvers = function () {
|
|
85
|
+
let resolve, reject;
|
|
86
|
+
const promise = new Promise((res, rej) => {
|
|
87
|
+
resolve = res;
|
|
88
|
+
reject = rej;
|
|
89
|
+
});
|
|
90
|
+
return { promise, resolve, reject };
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (!ArrayBuffer.prototype.transfer) {
|
|
95
|
+
ArrayBuffer.prototype.transfer = function (newByteLength) {
|
|
96
|
+
const length = newByteLength ?? this.byteLength;
|
|
97
|
+
const newBuffer = new ArrayBuffer(length);
|
|
98
|
+
const oldView = new Uint8Array(this);
|
|
99
|
+
const newView = new Uint8Array(newBuffer);
|
|
100
|
+
|
|
101
|
+
newView.set(oldView.subarray(0, Math.min(oldView.length, length)));
|
|
102
|
+
|
|
103
|
+
Object.defineProperty(this, "byteLength", { value: 0 });
|
|
104
|
+
|
|
105
|
+
return newBuffer;
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
</details>
|
|
111
|
+
|
|
51
112
|
## Quick Start
|
|
52
113
|
|
|
53
114
|
#### Info endpoint
|
|
@@ -65,12 +126,11 @@ const openOrders = await infoClient.openOrders({ user: "0x..." });
|
|
|
65
126
|
|
|
66
127
|
```ts
|
|
67
128
|
import * as hl from "@nktkas/hyperliquid";
|
|
68
|
-
import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
|
|
69
129
|
|
|
70
|
-
const
|
|
130
|
+
const privateKey = "0x..."; // or `viem`, `ethers`
|
|
71
131
|
|
|
72
132
|
const transport = new hl.HttpTransport();
|
|
73
|
-
const exchClient = new hl.ExchangeClient({ wallet, transport });
|
|
133
|
+
const exchClient = new hl.ExchangeClient({ wallet: privateKey, transport });
|
|
74
134
|
|
|
75
135
|
const result = await exchClient.order({
|
|
76
136
|
orders: [{
|
|
@@ -108,27 +168,22 @@ await sub.unsubscribe(); // unsubscribe from the event
|
|
|
108
168
|
|
|
109
169
|
```ts
|
|
110
170
|
import * as hl from "@nktkas/hyperliquid";
|
|
111
|
-
import { privateKeyToAccount } from "viem/accounts"; // or other wallet libraries
|
|
112
171
|
|
|
113
172
|
const multiSignAddress = "0x...";
|
|
114
173
|
const signers = [
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
// ...
|
|
118
|
-
privateKeyToAccount("0x..."),
|
|
119
|
-
];
|
|
174
|
+
"0x...", // Private key; or any other wallet libraries
|
|
175
|
+
] as const;
|
|
120
176
|
|
|
121
177
|
const transport = new hl.HttpTransport();
|
|
122
|
-
const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends ExchangeClient
|
|
178
|
+
const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends `ExchangeClient`
|
|
123
179
|
|
|
124
|
-
const data = await multiSignClient.approveAgent({ // same API as ExchangeClient
|
|
180
|
+
const data = await multiSignClient.approveAgent({ // same API as `ExchangeClient`
|
|
125
181
|
agentAddress: "0x...",
|
|
126
182
|
agentName: "agentName",
|
|
127
183
|
});
|
|
128
184
|
```
|
|
129
185
|
|
|
130
|
-
|
|
131
|
-
<summary><h2>Usage</h2></summary>
|
|
186
|
+
## Usage
|
|
132
187
|
|
|
133
188
|
### 1) Initialize Transport
|
|
134
189
|
|
|
@@ -137,11 +192,11 @@ First, choose and configure your transport layer (more details in the [API Refer
|
|
|
137
192
|
```ts
|
|
138
193
|
import * as hl from "@nktkas/hyperliquid";
|
|
139
194
|
|
|
140
|
-
// HTTP Transport
|
|
141
|
-
const httpTransport = new hl.HttpTransport(); // Accepts optional parameters
|
|
195
|
+
// 1. HTTP Transport: suitable for one-time requests or serverless environments
|
|
196
|
+
const httpTransport = new hl.HttpTransport(); // Accepts optional parameters (e.g. isTestnet, timeout, etc.)
|
|
142
197
|
|
|
143
|
-
// WebSocket Transport
|
|
144
|
-
const wsTransport = new hl.WebSocketTransport(); // Accepts optional parameters
|
|
198
|
+
// 2. WebSocket Transport: has better network latency than HTTP transport
|
|
199
|
+
const wsTransport = new hl.WebSocketTransport(); // Accepts optional parameters (e.g. url, timeout, reconnect, etc.)
|
|
145
200
|
```
|
|
146
201
|
|
|
147
202
|
### 2) Initialize Client
|
|
@@ -153,7 +208,7 @@ Next, initialize a client with the transport layer (more details in the [API Ref
|
|
|
153
208
|
```ts
|
|
154
209
|
import * as hl from "@nktkas/hyperliquid";
|
|
155
210
|
|
|
156
|
-
const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
211
|
+
const transport = new hl.HttpTransport(); // or `WebSocketTransport`
|
|
157
212
|
const infoClient = new hl.InfoClient({ transport });
|
|
158
213
|
```
|
|
159
214
|
|
|
@@ -165,22 +220,26 @@ import { createWalletClient, custom } from "viem";
|
|
|
165
220
|
import { privateKeyToAccount } from "viem/accounts";
|
|
166
221
|
import { ethers } from "ethers";
|
|
167
222
|
|
|
168
|
-
const transport = new hl.HttpTransport(); // or WebSocketTransport
|
|
223
|
+
const transport = new hl.HttpTransport(); // or `WebSocketTransport`
|
|
224
|
+
|
|
225
|
+
// 1. Using private key directly
|
|
226
|
+
const privateKey = "0x...";
|
|
227
|
+
const exchClient_privateKey = new hl.ExchangeClient({ wallet: privateKey, transport });
|
|
169
228
|
|
|
170
|
-
//
|
|
229
|
+
// 2. Using Viem
|
|
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)
|
|
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`
|
|
184
243
|
const exchClient_windowMetamask = new hl.ExchangeClient({ wallet: window.ethereum, transport });
|
|
185
244
|
```
|
|
186
245
|
|
|
@@ -189,7 +248,7 @@ const exchClient_windowMetamask = new hl.ExchangeClient({ wallet: window.ethereu
|
|
|
189
248
|
```ts
|
|
190
249
|
import * as hl from "@nktkas/hyperliquid";
|
|
191
250
|
|
|
192
|
-
const transport = new hl.WebSocketTransport(); // only WebSocketTransport
|
|
251
|
+
const transport = new hl.WebSocketTransport(); // only `WebSocketTransport`
|
|
193
252
|
const subsClient = new hl.SubscriptionClient({ transport });
|
|
194
253
|
```
|
|
195
254
|
|
|
@@ -203,8 +262,9 @@ 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
|
-
signTypedData(params: {
|
|
267
|
+
async signTypedData(params: {
|
|
208
268
|
domain: {
|
|
209
269
|
name: string;
|
|
210
270
|
version: string;
|
|
@@ -221,15 +281,14 @@ const signers = [
|
|
|
221
281
|
message: Record<string, unknown>;
|
|
222
282
|
}): Promise<Hex> {
|
|
223
283
|
// Custom signer logic
|
|
224
|
-
return "0x..."; // return signature
|
|
284
|
+
return "0x..."; // return hex 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();
|
|
232
|
-
const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends ExchangeClient
|
|
291
|
+
const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers }); // extends `ExchangeClient`
|
|
233
292
|
```
|
|
234
293
|
|
|
235
294
|
### 3) Use Client
|
|
@@ -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,15 +381,16 @@ 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 });
|
|
333
392
|
|
|
334
|
-
// Interaction is the same as with ExchangeClient
|
|
393
|
+
// Interaction is the same as with `ExchangeClient`
|
|
335
394
|
|
|
336
395
|
// Place an orders
|
|
337
396
|
const result = await multiSignClient.order({
|
|
@@ -363,10 +422,7 @@ const result = await multiSignClient.withdraw3({
|
|
|
363
422
|
});
|
|
364
423
|
```
|
|
365
424
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
<details>
|
|
369
|
-
<summary><h2>API Reference</h2></summary>
|
|
425
|
+
## API Reference
|
|
370
426
|
|
|
371
427
|
### Clients
|
|
372
428
|
|
|
@@ -454,6 +510,7 @@ class ExchangeClient {
|
|
|
454
510
|
constructor(args: {
|
|
455
511
|
transport: HttpTransport | WebSocketTransport;
|
|
456
512
|
wallet:
|
|
513
|
+
| Hex // Private key directly
|
|
457
514
|
| AbstractViemWalletClient // viem
|
|
458
515
|
| AbstractEthersSigner // ethers
|
|
459
516
|
| AbstractEthersV5Signer // ethers v5
|
|
@@ -491,6 +548,7 @@ class ExchangeClient {
|
|
|
491
548
|
|
|
492
549
|
// Transfer
|
|
493
550
|
perpDexClassTransfer(args: PerpDexClassTransferParameters): Promise<SuccessResponse>;
|
|
551
|
+
perpDexTransfer(args: PerpDexTransferParameters): Promise<SuccessResponse>;
|
|
494
552
|
spotSend(args: SpotSendParameters): Promise<SuccessResponse>;
|
|
495
553
|
subAccountSpotTransfer(args: SubAccountSpotTransferParameters): Promise<SuccessResponse>;
|
|
496
554
|
subAccountTransfer(args: SubAccountTransferParameters): Promise<SuccessResponse>;
|
|
@@ -555,7 +613,7 @@ class SubscriptionClient {
|
|
|
555
613
|
|
|
556
614
|
// Explorer
|
|
557
615
|
explorerBlock(listener: (data: WsBlockDetails[]) => void): Promise<Subscription>;
|
|
558
|
-
|
|
616
|
+
explorerTxs(listener: (data: TxDetails[]) => void): Promise<Subscription>;
|
|
559
617
|
}
|
|
560
618
|
```
|
|
561
619
|
<!-- deno-fmt-ignore-end -->
|
|
@@ -576,16 +634,18 @@ class MultiSignClient extends ExchangeClient {
|
|
|
576
634
|
},
|
|
577
635
|
);
|
|
578
636
|
|
|
579
|
-
// Same methods as ExchangeClient
|
|
637
|
+
// Same methods as `ExchangeClient`
|
|
580
638
|
}
|
|
581
639
|
```
|
|
582
640
|
|
|
583
641
|
### Transports
|
|
584
642
|
|
|
585
|
-
Transport acts as a layer between
|
|
643
|
+
Transport acts as a layer between class requests and Hyperliquid servers.
|
|
586
644
|
|
|
587
645
|
#### HTTP Transport
|
|
588
646
|
|
|
647
|
+
HTTP transport is suitable for one-off requests or serverless environments.
|
|
648
|
+
|
|
589
649
|
```ts
|
|
590
650
|
class HttpTransport {
|
|
591
651
|
constructor(options?: {
|
|
@@ -604,6 +664,8 @@ class HttpTransport {
|
|
|
604
664
|
|
|
605
665
|
#### WebSocket Transport
|
|
606
666
|
|
|
667
|
+
WebSocket transport has better network latency than HTTP transport.
|
|
668
|
+
|
|
607
669
|
```ts
|
|
608
670
|
class WebSocketTransport {
|
|
609
671
|
constructor(options?: {
|
|
@@ -627,10 +689,7 @@ class WebSocketTransport {
|
|
|
627
689
|
}
|
|
628
690
|
```
|
|
629
691
|
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
<details>
|
|
633
|
-
<summary><h2>Additional Import Points</h2></summary>
|
|
692
|
+
## Additional Import Points
|
|
634
693
|
|
|
635
694
|
### `/types`
|
|
636
695
|
|
|
@@ -647,26 +706,25 @@ 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
|
|
|
712
|
+
const nonce = Date.now();
|
|
654
713
|
const action = {
|
|
655
714
|
type: "cancel",
|
|
656
715
|
cancels: [
|
|
657
716
|
{ a: 0, o: 12345 },
|
|
658
717
|
],
|
|
659
|
-
};
|
|
660
|
-
const nonce = Date.now();
|
|
718
|
+
} as const;
|
|
661
719
|
|
|
662
720
|
const signature = await signL1Action({
|
|
663
|
-
wallet,
|
|
664
|
-
action: actionSorter[action.type](action),
|
|
721
|
+
wallet: privateKey,
|
|
722
|
+
action: actionSorter[action.type](action),
|
|
665
723
|
nonce,
|
|
666
|
-
isTestnet: true, // change to `false` for mainnet
|
|
667
724
|
});
|
|
668
725
|
|
|
669
|
-
|
|
726
|
+
// Send the signed action to the Hyperliquid API
|
|
727
|
+
const response = await fetch("https://api.hyperliquid.xyz/exchange", {
|
|
670
728
|
method: "POST",
|
|
671
729
|
headers: { "Content-Type": "application/json" },
|
|
672
730
|
body: JSON.stringify({ action, signature, nonce }),
|
|
@@ -678,27 +736,26 @@ 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",
|
|
687
|
-
signatureChainId: "0x66eee",
|
|
688
|
-
hyperliquidChain: "
|
|
744
|
+
signatureChainId: "0x66eee",
|
|
745
|
+
hyperliquidChain: "Mainnet",
|
|
689
746
|
agentAddress: "0x...",
|
|
690
747
|
agentName: "Agent",
|
|
691
748
|
nonce: Date.now(),
|
|
692
|
-
};
|
|
749
|
+
} as const;
|
|
693
750
|
|
|
694
751
|
const signature = await signUserSignedAction({
|
|
695
|
-
wallet,
|
|
752
|
+
wallet: privateKey,
|
|
696
753
|
action,
|
|
697
|
-
types: userSignedActionEip712Types[action.type],
|
|
698
|
-
chainId: parseInt(action.signatureChainId, 16),
|
|
754
|
+
types: userSignedActionEip712Types[action.type],
|
|
699
755
|
});
|
|
700
756
|
|
|
701
|
-
|
|
757
|
+
// Send the signed action to the Hyperliquid API
|
|
758
|
+
const response = await fetch("https://api.hyperliquid.xyz/exchange", {
|
|
702
759
|
method: "POST",
|
|
703
760
|
headers: { "Content-Type": "application/json" },
|
|
704
761
|
body: JSON.stringify({ action, signature, nonce: action.nonce }),
|
|
@@ -706,8 +763,6 @@ const response = await fetch("https://api.hyperliquid-testnet.xyz/exchange", {
|
|
|
706
763
|
const body = await response.json();
|
|
707
764
|
```
|
|
708
765
|
|
|
709
|
-
</details>
|
|
710
|
-
|
|
711
766
|
## Contributing
|
|
712
767
|
|
|
713
768
|
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 { 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"}
|
package/esm/src/base.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
export type Hex = `0x${string}`;
|
|
2
2
|
export type MaybePromise<T> = T | Promise<T>;
|
|
3
|
+
export type DeepImmutable<T> = {
|
|
4
|
+
readonly [K in keyof T]: DeepImmutable<T[K]>;
|
|
5
|
+
};
|
|
3
6
|
/** Base error class for all SDK errors. */
|
|
4
7
|
export declare class HyperliquidError extends Error {
|
|
5
|
-
constructor(message?: string);
|
|
8
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
6
9
|
}
|
|
7
10
|
//# 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,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC;AAEhC,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;
|
|
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;AAG7C,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI;IAC3B,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAC/C,CAAC;AAEF,2CAA2C;AAC3C,qBAAa,gBAAiB,SAAQ,KAAK;gBAC3B,OAAO,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIvD"}
|
package/esm/src/base.js
CHANGED