@nktkas/hyperliquid 0.22.2 → 0.23.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/README.md +79 -67
- 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 +289 -207
- package/esm/src/clients/exchange.d.ts.map +1 -1
- package/esm/src/clients/exchange.js +605 -410
- package/esm/src/clients/info.d.ts +264 -48
- package/esm/src/clients/info.d.ts.map +1 -1
- package/esm/src/clients/info.js +273 -31
- package/esm/src/clients/multiSign.d.ts +285 -204
- package/esm/src/clients/multiSign.d.ts.map +1 -1
- package/esm/src/clients/multiSign.js +521 -493
- 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/{_ethers.d.ts → _signTypedData/ethers.d.ts} +1 -1
- package/esm/src/signing/_signTypedData/ethers.d.ts.map +1 -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/{_viem.d.ts → _signTypedData/viem.d.ts} +2 -2
- package/esm/src/signing/_signTypedData/viem.d.ts.map +1 -0
- package/{script/src/signing/_window.d.ts → esm/src/signing/_signTypedData/window.d.ts} +18 -12
- package/esm/src/signing/_signTypedData/window.d.ts.map +1 -0
- package/esm/src/signing/{_window.js → _signTypedData/window.js} +3 -2
- package/esm/src/signing/_sorter.d.ts +56 -82
- package/esm/src/signing/_sorter.d.ts.map +1 -1
- package/esm/src/signing/_sorter.js +36 -49
- package/esm/src/signing/mod.d.ts +151 -130
- package/esm/src/signing/mod.d.ts.map +1 -1
- package/esm/src/signing/mod.js +182 -228
- 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/markets.d.ts +6 -0
- package/esm/src/types/info/markets.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 +129 -124
- package/esm/src/types/info/requests.d.ts.map +1 -1
- package/{script/src/types/info/delegations.d.ts → esm/src/types/info/validators.d.ts} +2 -2
- package/esm/src/types/info/validators.d.ts.map +1 -0
- package/esm/src/types/info/vaults.d.ts +7 -0
- 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 +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 +8 -5
- 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 +289 -207
- package/script/src/clients/exchange.d.ts.map +1 -1
- package/script/src/clients/exchange.js +2000 -1815
- package/script/src/clients/info.d.ts +264 -48
- package/script/src/clients/info.d.ts.map +1 -1
- package/script/src/clients/info.js +1442 -1210
- package/script/src/clients/multiSign.d.ts +285 -204
- package/script/src/clients/multiSign.d.ts.map +1 -1
- package/script/src/clients/multiSign.js +2043 -2025
- 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/{_ethers.d.ts → _signTypedData/ethers.d.ts} +1 -1
- 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/{_viem.d.ts → _signTypedData/viem.d.ts} +2 -2
- package/script/src/signing/_signTypedData/viem.d.ts.map +1 -0
- package/script/src/signing/_signTypedData/viem.js +9 -0
- package/{esm/src/signing/_window.d.ts → script/src/signing/_signTypedData/window.d.ts} +18 -12
- 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 +56 -82
- package/script/src/signing/_sorter.d.ts.map +1 -1
- package/script/src/signing/_sorter.js +655 -678
- package/script/src/signing/mod.d.ts +151 -130
- package/script/src/signing/mod.d.ts.map +1 -1
- package/script/src/signing/mod.js +286 -342
- 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/markets.d.ts +6 -0
- package/script/src/types/info/markets.d.ts.map +1 -1
- 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 +129 -124
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/info/requests.js +2 -12
- package/{esm/src/types/info/delegations.d.ts → script/src/types/info/validators.d.ts} +2 -2
- package/script/src/types/info/validators.d.ts.map +1 -0
- package/script/src/types/info/validators.js +2 -0
- package/script/src/types/info/vaults.d.ts +7 -0
- package/script/src/types/info/vaults.d.ts.map +1 -1
- package/script/src/types/info/vaults.js +2 -12
- package/script/src/types/mod.d.ts +1 -1
- package/script/src/types/mod.d.ts.map +1 -1
- 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/_ethers.d.ts.map +0 -1
- package/esm/src/signing/_private_key.d.ts +0 -22
- package/esm/src/signing/_private_key.d.ts.map +0 -1
- package/esm/src/signing/_private_key.js +0 -124
- package/esm/src/signing/_viem.d.ts.map +0 -1
- package/esm/src/signing/_window.d.ts.map +0 -1
- package/esm/src/types/info/delegations.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/_ethers.d.ts.map +0 -1
- package/script/src/signing/_ethers.js +0 -26
- package/script/src/signing/_private_key.d.ts +0 -22
- package/script/src/signing/_private_key.d.ts.map +0 -1
- package/script/src/signing/_private_key.js +0 -138
- package/script/src/signing/_viem.d.ts.map +0 -1
- package/script/src/signing/_viem.js +0 -19
- package/script/src/signing/_window.d.ts.map +0 -1
- package/script/src/signing/_window.js +0 -43
- package/script/src/types/info/delegations.d.ts.map +0 -1
- package/script/src/types/info/delegations.js +0 -12
- /package/esm/src/signing/{_ethers.js → _signTypedData/ethers.js} +0 -0
- /package/esm/src/signing/{_viem.js → _signTypedData/viem.js} +0 -0
- /package/esm/src/types/info/{delegations.js → validators.js} +0 -0
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
import { TransportError } from "../base.js";
|
|
2
|
-
/**
|
|
3
|
-
* Error thrown when an HTTP response is deemed invalid:
|
|
4
|
-
* - Non-200 status code
|
|
5
|
-
* - Unexpected content type
|
|
6
|
-
*/
|
|
2
|
+
/** Error thrown when an HTTP request fails. */
|
|
7
3
|
export class HttpRequestError extends TransportError {
|
|
8
4
|
response;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
5
|
+
body;
|
|
6
|
+
constructor(args, options) {
|
|
7
|
+
const { response, body } = args ?? {};
|
|
8
|
+
let message;
|
|
9
|
+
if (response) {
|
|
10
|
+
message = `${response.status} ${response.statusText}`.trim();
|
|
11
|
+
if (body)
|
|
12
|
+
message += ` - ${body}`;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
message = "Unknown error while making an HTTP request. See the `cause` for more details.";
|
|
16
|
+
}
|
|
17
|
+
super(message, options);
|
|
22
18
|
this.name = "HttpRequestError";
|
|
19
|
+
this.response = response;
|
|
20
|
+
this.body = body;
|
|
23
21
|
}
|
|
24
22
|
}
|
|
25
23
|
/** HTTP implementation of the REST transport interface. */
|
|
@@ -57,51 +55,58 @@ export class HttpTransport {
|
|
|
57
55
|
* @param payload - The payload to send with the request.
|
|
58
56
|
* @param signal - An optional abort signal.
|
|
59
57
|
* @returns A promise that resolves with parsed JSON response body.
|
|
60
|
-
*
|
|
61
|
-
* @throws
|
|
58
|
+
*
|
|
59
|
+
* @throws {HttpRequestError} Thrown when the HTTP request fails.
|
|
62
60
|
*/
|
|
63
61
|
async request(endpoint, payload, signal) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
62
|
+
try {
|
|
63
|
+
// Construct a Request
|
|
64
|
+
const url = new URL(endpoint, this.server[this.isTestnet ? "testnet" : "mainnet"][endpoint === "explorer" ? "rpc" : "api"]);
|
|
65
|
+
const init = mergeRequestInit({
|
|
66
|
+
body: JSON.stringify(payload),
|
|
67
|
+
headers: {
|
|
68
|
+
"Accept-Encoding": "gzip, deflate, br, zstd",
|
|
69
|
+
"Content-Type": "application/json",
|
|
70
|
+
},
|
|
71
|
+
keepalive: true,
|
|
72
|
+
method: "POST",
|
|
73
|
+
signal: this.timeout ? AbortSignal.timeout(this.timeout) : undefined,
|
|
74
|
+
}, this.fetchOptions, { signal });
|
|
75
|
+
let request = new Request(url, init);
|
|
76
|
+
// Call the onRequest callback, if provided
|
|
77
|
+
if (this.onRequest) {
|
|
78
|
+
const customRequest = await this.onRequest(request);
|
|
79
|
+
if (customRequest instanceof Request)
|
|
80
|
+
request = customRequest;
|
|
81
|
+
}
|
|
82
|
+
// Send the Request and wait for a Response
|
|
83
|
+
let response = await fetch(request);
|
|
84
|
+
// Call the onResponse callback, if provided
|
|
85
|
+
if (this.onResponse) {
|
|
86
|
+
const customResponse = await this.onResponse(response);
|
|
87
|
+
if (customResponse instanceof Response)
|
|
88
|
+
response = customResponse;
|
|
89
|
+
}
|
|
90
|
+
// Validate the Response
|
|
91
|
+
if (!response.ok || !response.headers.get("Content-Type")?.includes("application/json")) {
|
|
92
|
+
// Unload the response body to prevent memory leaks
|
|
93
|
+
const body = await response.text().catch(() => undefined);
|
|
94
|
+
throw new HttpRequestError({ response, body });
|
|
95
|
+
}
|
|
96
|
+
// Parse the response body
|
|
97
|
+
const body = await response.json();
|
|
98
|
+
// Check if the response is an error
|
|
99
|
+
if (body?.type === "error") {
|
|
100
|
+
throw new HttpRequestError({ response, body: body?.message });
|
|
101
|
+
}
|
|
102
|
+
// Return the response body
|
|
103
|
+
return body;
|
|
96
104
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
throw new HttpRequestError(response, body?.message);
|
|
105
|
+
catch (error) {
|
|
106
|
+
if (error instanceof HttpRequestError)
|
|
107
|
+
throw error; // Re-throw known errors
|
|
108
|
+
throw new HttpRequestError(undefined, { cause: error });
|
|
102
109
|
}
|
|
103
|
-
// Return the response body
|
|
104
|
-
return body;
|
|
105
110
|
}
|
|
106
111
|
}
|
|
107
112
|
/** Merges multiple {@linkcode HeadersInit} into one {@linkcode Headers}. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TypedEventTarget } from "
|
|
1
|
+
import { TypedEventTarget } from "typescript-event-target";
|
|
2
2
|
import type { BlockDetails, TxDetails } from "../../types/explorer/responses.js";
|
|
3
3
|
import type { BaseExchangeResponse } from "../../types/exchange/responses.js";
|
|
4
4
|
/** Response to subscribe to or unsubscribe from an event. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_hyperliquid_event_target.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/_hyperliquid_event_target.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"_hyperliquid_event_target.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/_hyperliquid_event_target.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AAU9E,6DAA6D;AAC7D,UAAU,oBAAoB;IAC1B,sCAAsC;IACtC,MAAM,EAAE,WAAW,GAAG,aAAa,CAAC;IACpC,qCAAqC;IACrC,YAAY,EAAE,OAAO,CAAC;CACzB;AAED,gCAAgC;AAChC,UAAU,YAAY;IAClB,iCAAiC;IACjC,EAAE,EAAE,MAAM,CAAC;IACX,uBAAuB;IACvB,QAAQ;IACJ,iDAAiD;IAC/C;QACE,wDAAwD;QACxD,IAAI,EAAE,MAAM,CAAC;QACb,qCAAqC;QACrC,OAAO,EAAE;YACL,0CAA0C;YAC1C,IAAI,EAAE,MAAM,CAAC;YACb,iCAAiC;YACjC,IAAI,EAAE,OAAO,CAAC;SACjB,CAAC;KACL;IACD,yCAAyC;OACvC;QACE,iDAAiD;QACjD,IAAI,EAAE,QAAQ,CAAC;QACf,qBAAqB;QACrB,OAAO,EAAE,oBAAoB,CAAC;KACjC,CAAC;CACT;AAED,mFAAmF;AACnF,UAAU,mBAAmB;IACzB,oBAAoB,EAAE,WAAW,CAAC,oBAAoB,CAAC,CAAC;IACxD,IAAI,EAAE,WAAW,CAAC,YAAY,CAAC,CAAC;IAChC,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC3B,IAAI,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;IAC7B,cAAc,EAAE,WAAW,CAAC,IAAI,CAAC,YAAY,EAAE,KAAK,CAAC,EAAE,CAAC,CAAC;IACzD,YAAY,EAAE,WAAW,CAAC,SAAS,EAAE,CAAC,CAAC;IAEvC,CAAC,GAAG,EAAE,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;CACnC;AAED,iFAAiF;AACjF,qBAAa,sBAAuB,SAAQ,gBAAgB,CAAC,mBAAmB,CAAC;gBACjE,MAAM,EAAE,SAAS;CAiBhC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TypedEventTarget } from "
|
|
1
|
+
import { TypedEventTarget } from "typescript-event-target";
|
|
2
2
|
/** Listens for WebSocket messages and sends them as Hyperliquid typed events. */
|
|
3
3
|
export class HyperliquidEventTarget extends TypedEventTarget {
|
|
4
4
|
constructor(socket) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_reconnecting_websocket.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/_reconnecting_websocket.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"_reconnecting_websocket.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/_reconnecting_websocket.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C,6DAA6D;AAC7D,MAAM,WAAW,4BAA4B;IACzC;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAElC;;;;;OAKG;IACH,eAAe,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,KAAK,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC;IAE5F;;;;;OAKG;IACH,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,KAAK,YAAY,CAAC,OAAO,CAAC,CAAC;IAEpF;;;OAGG;IACH,aAAa,CAAC,EAAE,qBAAqB,CAAC;CACzC;AAED,yCAAyC;AACzC,MAAM,WAAW,qBAAqB;IAClC,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI,GAAG,eAAe,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC;IAC1F,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,MAAM,GAAG,eAAe,GAAG,IAAI,GAAG,eAAe,CAAC,CAAC;CACpF;AAsBD,qDAAqD;AACrD,qBAAa,0BAA2B,SAAQ,cAAc;IAE/C,IAAI,EACL,4BAA4B,GAC5B,8BAA8B,GAC9B,sBAAsB,GACtB,eAAe;gBAJd,IAAI,EACL,4BAA4B,GAC5B,8BAA8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} from "../../../deps/jsr.io/@std/async/1.0.13/delay.js";
|
|
3
1
|
import { TransportError } from "../base.js";
|
|
4
2
|
/** Simple FIFO (First In, First Out) buffer implementation. */
|
|
5
3
|
class FIFOMessageBuffer {
|
|
@@ -110,7 +108,7 @@ export class ReconnectingWebSocket {
|
|
|
110
108
|
: await this.reconnectOptions.connectionDelay(this._attempt, this.reconnectAbortController.signal);
|
|
111
109
|
if (this.reconnectAbortController.signal.aborted)
|
|
112
110
|
return;
|
|
113
|
-
await delay(reconnectDelay,
|
|
111
|
+
await delay(reconnectDelay, this.reconnectAbortController.signal);
|
|
114
112
|
// Create a new WebSocket instance
|
|
115
113
|
const { onclose, onerror, onmessage, onopen } = this._socket;
|
|
116
114
|
this._socket = this._createSocket(this._socket.url, this._protocols);
|
|
@@ -268,10 +266,25 @@ export class ReconnectingWebSocket {
|
|
|
268
266
|
return this._socket.dispatchEvent(event);
|
|
269
267
|
}
|
|
270
268
|
}
|
|
271
|
-
/** Check if two event listeners are the same (just like EventTarget). */
|
|
272
269
|
function listenersMatch(a, b) {
|
|
273
270
|
// EventTarget only compares capture in options, even if one is an object and the other is boolean
|
|
274
271
|
const aCapture = Boolean(typeof a.options === "object" ? a.options.capture : a.options);
|
|
275
272
|
const bCapture = Boolean(typeof b.options === "object" ? b.options.capture : b.options);
|
|
276
273
|
return a.type === b.type && a.listener === b.listener && aCapture === bCapture;
|
|
277
274
|
}
|
|
275
|
+
function delay(ms, signal) {
|
|
276
|
+
if (signal?.aborted)
|
|
277
|
+
return Promise.reject(signal.reason);
|
|
278
|
+
return new Promise((resolve, reject) => {
|
|
279
|
+
const onAbort = () => {
|
|
280
|
+
clearTimeout(timer);
|
|
281
|
+
reject(signal?.reason);
|
|
282
|
+
};
|
|
283
|
+
const onTimeout = () => {
|
|
284
|
+
signal?.removeEventListener("abort", onAbort);
|
|
285
|
+
resolve();
|
|
286
|
+
};
|
|
287
|
+
const timer = setTimeout(onTimeout, ms);
|
|
288
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
289
|
+
});
|
|
290
|
+
}
|
|
@@ -1,14 +1,5 @@
|
|
|
1
|
-
import { TransportError } from "../base.js";
|
|
2
1
|
import type { ReconnectingWebSocket } from "./_reconnecting_websocket.js";
|
|
3
2
|
import type { HyperliquidEventTarget } from "./_hyperliquid_event_target.js";
|
|
4
|
-
/**
|
|
5
|
-
* Error thrown when a WebSocket request fails:
|
|
6
|
-
* - When the WebSocket connection is closed
|
|
7
|
-
* - When the server responds with an error message
|
|
8
|
-
*/
|
|
9
|
-
export declare class WebSocketRequestError extends TransportError {
|
|
10
|
-
constructor(message: string);
|
|
11
|
-
}
|
|
12
3
|
/**
|
|
13
4
|
* Manages WebSocket requests to the Hyperliquid API.
|
|
14
5
|
* Handles request creation, sending, and mapping responses to their corresponding requests.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_websocket_async_request.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/_websocket_async_request.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"_websocket_async_request.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/_websocket_async_request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AAkB7E;;;GAGG;AACH,qBAAa,qBAAqB;IAgBlB,SAAS,CAAC,MAAM,EAAE,qBAAqB;IAfnD,SAAS,CAAC,MAAM,EAAE,MAAM,CAAK;IAC7B,SAAS,CAAC,KAAK,EAAE;QACb,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC;QAEpB,OAAO,EAAE,CAAC,KAAK,CAAC,EAAE,GAAG,KAAK,IAAI,CAAC;QAE/B,MAAM,EAAE,CAAC,MAAM,CAAC,EAAE,GAAG,KAAK,IAAI,CAAC;KAClC,EAAE,CAAM;IACT,eAAe,EAAE,MAAM,CAAK;IAE5B;;;;OAIG;gBACmB,MAAM,EAAE,qBAAqB,EAAE,QAAQ,EAAE,sBAAsB;IAiFrF;;;OAGG;IACG,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAC5D,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,WAAW,GAAG,aAAa,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC;IA6ClH,6DAA6D;IAC7D,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM;CAK7C"}
|
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
/**
|
|
3
|
-
* Error thrown when a WebSocket request fails:
|
|
4
|
-
* - When the WebSocket connection is closed
|
|
5
|
-
* - When the server responds with an error message
|
|
6
|
-
*/
|
|
7
|
-
export class WebSocketRequestError extends TransportError {
|
|
8
|
-
constructor(message) {
|
|
9
|
-
super(message);
|
|
10
|
-
this.name = "WebSocketRequestError";
|
|
11
|
-
}
|
|
12
|
-
}
|
|
1
|
+
import { WebSocketRequestError } from "./websocket_transport.js";
|
|
13
2
|
/**
|
|
14
3
|
* Manages WebSocket requests to the Hyperliquid API.
|
|
15
4
|
* Handles request creation, sending, and mapping responses to their corresponding requests.
|
|
@@ -50,37 +39,42 @@ export class WebSocketAsyncRequest {
|
|
|
50
39
|
const parsedRequest = JSON.parse(request);
|
|
51
40
|
// For `post` requests
|
|
52
41
|
if ("id" in parsedRequest && typeof parsedRequest.id === "number") {
|
|
53
|
-
this.queue.findLast((item) => item.id === parsedRequest.id)
|
|
42
|
+
this.queue.findLast((item) => item.id === parsedRequest.id)
|
|
43
|
+
?.reject(new WebSocketRequestError(`Server error: ${event.detail}`, { cause: event.detail }));
|
|
54
44
|
return;
|
|
55
45
|
}
|
|
56
46
|
// For `subscribe` and `unsubscribe` requests
|
|
57
47
|
if ("subscription" in parsedRequest &&
|
|
58
48
|
typeof parsedRequest.subscription === "object" && parsedRequest.subscription !== null) {
|
|
59
49
|
const id = WebSocketAsyncRequest.requestToId(parsedRequest);
|
|
60
|
-
this.queue.findLast((item) => item.id === id)
|
|
50
|
+
this.queue.findLast((item) => item.id === id)
|
|
51
|
+
?.reject(new WebSocketRequestError(`Server error: ${event.detail}`, { cause: event.detail }));
|
|
61
52
|
return;
|
|
62
53
|
}
|
|
63
|
-
// For
|
|
54
|
+
// For `Already subscribed` and `Invalid subscription` requests
|
|
64
55
|
if (event.detail.startsWith("Already subscribed") || event.detail.startsWith("Invalid subscription")) {
|
|
65
56
|
const id = WebSocketAsyncRequest.requestToId({
|
|
66
57
|
method: "subscribe",
|
|
67
58
|
subscription: parsedRequest,
|
|
68
59
|
});
|
|
69
|
-
this.queue.findLast((item) => item.id === id)
|
|
60
|
+
this.queue.findLast((item) => item.id === id)
|
|
61
|
+
?.reject(new WebSocketRequestError(`Server error: ${event.detail}`, { cause: event.detail }));
|
|
70
62
|
return;
|
|
71
63
|
}
|
|
72
|
-
// For
|
|
64
|
+
// For `Already unsubscribed` requests
|
|
73
65
|
if (event.detail.startsWith("Already unsubscribed")) {
|
|
74
66
|
const id = WebSocketAsyncRequest.requestToId({
|
|
75
67
|
method: "unsubscribe",
|
|
76
68
|
subscription: parsedRequest,
|
|
77
69
|
});
|
|
78
|
-
this.queue.findLast((item) => item.id === id)
|
|
70
|
+
this.queue.findLast((item) => item.id === id)
|
|
71
|
+
?.reject(new WebSocketRequestError(`Server error: ${event.detail}`, { cause: event.detail }));
|
|
79
72
|
return;
|
|
80
73
|
}
|
|
81
74
|
// For unknown requests
|
|
82
75
|
const id = WebSocketAsyncRequest.requestToId(parsedRequest);
|
|
83
|
-
this.queue.findLast((item) => item.id === id)
|
|
76
|
+
this.queue.findLast((item) => item.id === id)
|
|
77
|
+
?.reject(new WebSocketRequestError(`Server error: ${event.detail}`, { cause: event.detail }));
|
|
84
78
|
}
|
|
85
79
|
catch {
|
|
86
80
|
// Ignore JSON parsing errors
|
|
@@ -89,7 +83,7 @@ export class WebSocketAsyncRequest {
|
|
|
89
83
|
// Throws all pending requests if the connection is dropped
|
|
90
84
|
socket.addEventListener("close", () => {
|
|
91
85
|
this.queue.forEach(({ reject }) => {
|
|
92
|
-
reject(new WebSocketRequestError("
|
|
86
|
+
reject(new WebSocketRequestError("WebSocket connection closed."));
|
|
93
87
|
});
|
|
94
88
|
this.queue = [];
|
|
95
89
|
});
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
import { type IRequestTransport, type ISubscriptionTransport, type Subscription, TransportError } from "../base.js";
|
|
1
2
|
import { type MessageBufferStrategy, ReconnectingWebSocket, ReconnectingWebSocketError, type ReconnectingWebSocketOptions } from "./_reconnecting_websocket.js";
|
|
2
3
|
import { HyperliquidEventTarget } from "./_hyperliquid_event_target.js";
|
|
3
|
-
import { WebSocketAsyncRequest
|
|
4
|
-
import type { IRequestTransport, ISubscriptionTransport, Subscription } from "../base.js";
|
|
5
|
-
export { WebSocketRequestError };
|
|
4
|
+
import { WebSocketAsyncRequest } from "./_websocket_async_request.js";
|
|
6
5
|
export { type MessageBufferStrategy, ReconnectingWebSocketError, type ReconnectingWebSocketOptions };
|
|
7
6
|
/** Configuration options for the WebSocket transport layer. */
|
|
8
7
|
export interface WebSocketTransportOptions {
|
|
@@ -46,6 +45,10 @@ export interface WebSocketTransportOptions {
|
|
|
46
45
|
*/
|
|
47
46
|
autoResubscribe?: boolean;
|
|
48
47
|
}
|
|
48
|
+
/** Error thrown when a WebSocket request fails. */
|
|
49
|
+
export declare class WebSocketRequestError extends TransportError {
|
|
50
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
51
|
+
}
|
|
49
52
|
/** WebSocket implementation of the REST and Subscription transport interfaces. */
|
|
50
53
|
export declare class WebSocketTransport implements IRequestTransport, ISubscriptionTransport, AsyncDisposable {
|
|
51
54
|
protected _wsRequester: WebSocketAsyncRequest;
|
|
@@ -95,18 +98,19 @@ export declare class WebSocketTransport implements IRequestTransport, ISubscript
|
|
|
95
98
|
* @param payload - The payload to send with the request.
|
|
96
99
|
* @param signal - An optional abort signal.
|
|
97
100
|
* @returns A promise that resolves with parsed JSON response body.
|
|
101
|
+
*
|
|
98
102
|
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
|
|
99
103
|
*/
|
|
100
104
|
request<T>(type: "info" | "exchange" | "explorer", payload: unknown, signal?: AbortSignal): Promise<T>;
|
|
101
105
|
/**
|
|
102
106
|
* Subscribes to a Hyperliquid event channel.
|
|
103
|
-
*
|
|
104
107
|
* Sends a subscription request to the server and listens for events.
|
|
105
108
|
*
|
|
106
109
|
* @param channel - The event channel to listen to.
|
|
107
110
|
* @param payload - A payload to send with the subscription request.
|
|
108
111
|
* @param listener - A function to call when the event is dispatched.
|
|
109
112
|
* @returns A promise that resolves with a {@link Subscription} object to manage the subscription lifecycle.
|
|
113
|
+
*
|
|
110
114
|
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
|
|
111
115
|
*/
|
|
112
116
|
subscribe<T>(channel: string, payload: unknown, listener: (data: CustomEvent<T>) => void): Promise<Subscription>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"websocket_transport.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/websocket_transport.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,qBAAqB,EAC1B,qBAAqB,EACrB,0BAA0B,EAC1B,KAAK,4BAA4B,EACpC,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AACxE,OAAO,EAAE,qBAAqB,EAAE,
|
|
1
|
+
{"version":3,"file":"websocket_transport.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/websocket_transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,iBAAiB,EAAE,KAAK,sBAAsB,EAAE,KAAK,YAA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AI,IAAI;IA4BjC,SAAS,CAAC,cAAc,IAAI,IAAI;IAOhC,gFAAgF;IAChF,SAAS,CAAC,iBAAiB,IAAI,IAAI;IAUnC,SAAS,CAAC,gBAAgB,IAAI,IAAI;IAU5B,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;CAG/C"}
|
|
@@ -1,8 +1,15 @@
|
|
|
1
|
+
import { TransportError } from "../base.js";
|
|
1
2
|
import { ReconnectingWebSocket, ReconnectingWebSocketError, } from "./_reconnecting_websocket.js";
|
|
2
3
|
import { HyperliquidEventTarget } from "./_hyperliquid_event_target.js";
|
|
3
|
-
import { WebSocketAsyncRequest
|
|
4
|
-
export { WebSocketRequestError };
|
|
4
|
+
import { WebSocketAsyncRequest } from "./_websocket_async_request.js";
|
|
5
5
|
export { ReconnectingWebSocketError };
|
|
6
|
+
/** Error thrown when a WebSocket request fails. */
|
|
7
|
+
export class WebSocketRequestError extends TransportError {
|
|
8
|
+
constructor(message, options) {
|
|
9
|
+
super(message, options);
|
|
10
|
+
this.name = "WebSocketRequestError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
6
13
|
/** WebSocket implementation of the REST and Subscription transport interfaces. */
|
|
7
14
|
export class WebSocketTransport {
|
|
8
15
|
_wsRequester;
|
|
@@ -55,75 +62,97 @@ export class WebSocketTransport {
|
|
|
55
62
|
* @param payload - The payload to send with the request.
|
|
56
63
|
* @param signal - An optional abort signal.
|
|
57
64
|
* @returns A promise that resolves with parsed JSON response body.
|
|
65
|
+
*
|
|
58
66
|
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
|
|
59
67
|
*/
|
|
60
|
-
request(type, payload, signal) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
68
|
+
async request(type, payload, signal) {
|
|
69
|
+
try {
|
|
70
|
+
const timeoutSignal = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
|
|
71
|
+
const combinedSignal = signal && timeoutSignal
|
|
72
|
+
? AbortSignal.any([signal, timeoutSignal])
|
|
73
|
+
: signal ?? timeoutSignal;
|
|
74
|
+
return await this._wsRequester.request("post", {
|
|
75
|
+
type: type === "exchange" ? "action" : type,
|
|
76
|
+
payload,
|
|
77
|
+
}, combinedSignal);
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
if (error instanceof WebSocketRequestError)
|
|
81
|
+
throw error; // Re-throw known errors
|
|
82
|
+
throw new WebSocketRequestError("Unknown error while making a WebSocket request. See the `cause` for more details.", { cause: error });
|
|
83
|
+
}
|
|
69
84
|
}
|
|
70
85
|
/**
|
|
71
86
|
* Subscribes to a Hyperliquid event channel.
|
|
72
|
-
*
|
|
73
87
|
* Sends a subscription request to the server and listens for events.
|
|
74
88
|
*
|
|
75
89
|
* @param channel - The event channel to listen to.
|
|
76
90
|
* @param payload - A payload to send with the subscription request.
|
|
77
91
|
* @param listener - A function to call when the event is dispatched.
|
|
78
92
|
* @returns A promise that resolves with a {@link Subscription} object to manage the subscription lifecycle.
|
|
93
|
+
*
|
|
79
94
|
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
|
|
80
95
|
*/
|
|
81
96
|
async subscribe(channel, payload, listener) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
97
|
+
try {
|
|
98
|
+
// Create a unique identifier for the subscription
|
|
99
|
+
const id = WebSocketAsyncRequest.requestToId(payload);
|
|
100
|
+
// Initialize new subscription, if it doesn't exist
|
|
101
|
+
let subscription = this._subscriptions.get(id);
|
|
102
|
+
if (!subscription) {
|
|
103
|
+
// Send subscription request
|
|
104
|
+
const promise = this._wsRequester.request("subscribe", payload);
|
|
105
|
+
// Cache subscription info
|
|
106
|
+
subscription = {
|
|
107
|
+
listeners: new Map(),
|
|
108
|
+
promise,
|
|
109
|
+
resubscribeAbortController: new AbortController(),
|
|
110
|
+
};
|
|
111
|
+
this._subscriptions.set(id, subscription);
|
|
112
|
+
}
|
|
113
|
+
// Initialize new listener, if it doesn't exist
|
|
114
|
+
let unsubscribe = subscription.listeners.get(listener);
|
|
115
|
+
if (!unsubscribe) {
|
|
116
|
+
// Create new unsubscribe function
|
|
117
|
+
unsubscribe = async () => {
|
|
118
|
+
try {
|
|
119
|
+
// Remove listener and cleanup
|
|
120
|
+
this._hlEvents.removeEventListener(channel, listener);
|
|
121
|
+
const subscription = this._subscriptions.get(id);
|
|
122
|
+
subscription?.listeners.delete(listener);
|
|
123
|
+
// If no listeners remain, remove subscription entirely
|
|
124
|
+
if (subscription?.listeners.size === 0) {
|
|
125
|
+
// Cleanup subscription
|
|
126
|
+
this._subscriptions.delete(id);
|
|
127
|
+
// If the socket is open, send unsubscription request
|
|
128
|
+
if (this.socket.readyState === ReconnectingWebSocket.OPEN) {
|
|
129
|
+
await this._wsRequester.request("unsubscribe", payload);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
113
132
|
}
|
|
114
|
-
|
|
133
|
+
catch (error) {
|
|
134
|
+
if (error instanceof WebSocketRequestError)
|
|
135
|
+
throw error; // Re-throw known errors
|
|
136
|
+
throw new WebSocketRequestError("Unknown error while unsubscribing from a WebSocket channel. See the `cause` for more details.", { cause: error });
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
// Add listener and cache unsubscribe function
|
|
140
|
+
this._hlEvents.addEventListener(channel, listener);
|
|
141
|
+
subscription.listeners.set(listener, unsubscribe);
|
|
142
|
+
}
|
|
143
|
+
// Wait for the initial subscription request to complete
|
|
144
|
+
await subscription.promise;
|
|
145
|
+
// Return subscription control object
|
|
146
|
+
return {
|
|
147
|
+
unsubscribe,
|
|
148
|
+
resubscribeSignal: subscription.resubscribeAbortController?.signal,
|
|
115
149
|
};
|
|
116
|
-
// Add listener and cache unsubscribe function
|
|
117
|
-
this._hlEvents.addEventListener(channel, listener);
|
|
118
|
-
subscription.listeners.set(listener, unsubscribe);
|
|
119
150
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
resubscribeSignal: subscription.resubscribeAbortController?.signal,
|
|
126
|
-
};
|
|
151
|
+
catch (error) {
|
|
152
|
+
if (error instanceof WebSocketRequestError)
|
|
153
|
+
throw error; // Re-throw known errors
|
|
154
|
+
throw new WebSocketRequestError("Unknown error while subscribing to a WebSocket channel. See the `cause` for more details.", { cause: error });
|
|
155
|
+
}
|
|
127
156
|
}
|
|
128
157
|
/**
|
|
129
158
|
* Waits until the WebSocket connection is ready.
|