@nktkas/hyperliquid 0.22.2 → 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 +47 -49
- 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 +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 +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/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 +8 -5
- 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 +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 +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/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/_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/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/esm/src/signing/{_ethers.js → _signTypedData/ethers.js} +0 -0
- /package/esm/src/signing/{_viem.js → _signTypedData/viem.js} +0 -0
|
@@ -1,96 +1,104 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebSocketTransport = exports.WebSocketRequestError = exports.ReconnectingWebSocketError = void 0;
|
|
4
|
+
const base_js_1 = require("../base.js");
|
|
5
|
+
const _reconnecting_websocket_js_1 = require("./_reconnecting_websocket.js");
|
|
6
|
+
Object.defineProperty(exports, "ReconnectingWebSocketError", { enumerable: true, get: function () { return _reconnecting_websocket_js_1.ReconnectingWebSocketError; } });
|
|
7
|
+
const _hyperliquid_event_target_js_1 = require("./_hyperliquid_event_target.js");
|
|
8
|
+
const _websocket_async_request_js_1 = require("./_websocket_async_request.js");
|
|
9
|
+
/** Error thrown when a WebSocket request fails. */
|
|
10
|
+
class WebSocketRequestError extends base_js_1.TransportError {
|
|
11
|
+
constructor(message, options) {
|
|
12
|
+
super(message, options);
|
|
13
|
+
this.name = "WebSocketRequestError";
|
|
5
14
|
}
|
|
6
|
-
|
|
7
|
-
|
|
15
|
+
}
|
|
16
|
+
exports.WebSocketRequestError = WebSocketRequestError;
|
|
17
|
+
/** WebSocket implementation of the REST and Subscription transport interfaces. */
|
|
18
|
+
class WebSocketTransport {
|
|
19
|
+
_wsRequester;
|
|
20
|
+
_hlEvents;
|
|
21
|
+
_keepAliveTimeout = null;
|
|
22
|
+
_subscriptions = new Map();
|
|
23
|
+
_isReconnecting = false;
|
|
24
|
+
/**
|
|
25
|
+
* Request timeout in ms.
|
|
26
|
+
* Set to `null` to disable.
|
|
27
|
+
*/
|
|
28
|
+
timeout;
|
|
29
|
+
/** Keep-alive configuration. */
|
|
30
|
+
keepAlive;
|
|
31
|
+
/** Enable automatic resubscription after reconnection. */
|
|
32
|
+
autoResubscribe;
|
|
33
|
+
/** The WebSocket that is used for communication. */
|
|
34
|
+
socket;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new WebSocket transport instance.
|
|
37
|
+
* @param options - Configuration options for the WebSocket transport layer.
|
|
38
|
+
*/
|
|
39
|
+
constructor(options) {
|
|
40
|
+
this.socket = new _reconnecting_websocket_js_1.ReconnectingWebSocket(options?.url ?? "wss://api.hyperliquid.xyz/ws", undefined, options?.reconnect);
|
|
41
|
+
this._hlEvents = new _hyperliquid_event_target_js_1.HyperliquidEventTarget(this.socket);
|
|
42
|
+
this._wsRequester = new _websocket_async_request_js_1.WebSocketAsyncRequest(this.socket, this._hlEvents);
|
|
43
|
+
this.timeout = options?.timeout === undefined ? 10_000 : options.timeout;
|
|
44
|
+
this.keepAlive = {
|
|
45
|
+
interval: options?.keepAlive?.interval === undefined ? 30_000 : options.keepAlive?.interval,
|
|
46
|
+
timeout: options?.keepAlive?.timeout === undefined ? this.timeout : options.keepAlive?.timeout,
|
|
47
|
+
};
|
|
48
|
+
this.autoResubscribe = options?.autoResubscribe ?? true;
|
|
49
|
+
// Initialize listeners
|
|
50
|
+
this.socket.addEventListener("open", () => {
|
|
51
|
+
this._keepAliveStart();
|
|
52
|
+
this._resubscribeStart();
|
|
53
|
+
});
|
|
54
|
+
this.socket.addEventListener("close", () => {
|
|
55
|
+
this._keepAliveStop();
|
|
56
|
+
this._resubscribeStop();
|
|
57
|
+
this._isReconnecting = true;
|
|
58
|
+
});
|
|
8
59
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
_subscriptions = new Map();
|
|
24
|
-
_isReconnecting = false;
|
|
25
|
-
/**
|
|
26
|
-
* Request timeout in ms.
|
|
27
|
-
* Set to `null` to disable.
|
|
28
|
-
*/
|
|
29
|
-
timeout;
|
|
30
|
-
/** Keep-alive configuration. */
|
|
31
|
-
keepAlive;
|
|
32
|
-
/** Enable automatic resubscription after reconnection. */
|
|
33
|
-
autoResubscribe;
|
|
34
|
-
/** The WebSocket that is used for communication. */
|
|
35
|
-
socket;
|
|
36
|
-
/**
|
|
37
|
-
* Creates a new WebSocket transport instance.
|
|
38
|
-
* @param options - Configuration options for the WebSocket transport layer.
|
|
39
|
-
*/
|
|
40
|
-
constructor(options) {
|
|
41
|
-
this.socket = new _reconnecting_websocket_js_1.ReconnectingWebSocket(options?.url ?? "wss://api.hyperliquid.xyz/ws", undefined, options?.reconnect);
|
|
42
|
-
this._hlEvents = new _hyperliquid_event_target_js_1.HyperliquidEventTarget(this.socket);
|
|
43
|
-
this._wsRequester = new _websocket_async_request_js_1.WebSocketAsyncRequest(this.socket, this._hlEvents);
|
|
44
|
-
this.timeout = options?.timeout === undefined ? 10_000 : options.timeout;
|
|
45
|
-
this.keepAlive = {
|
|
46
|
-
interval: options?.keepAlive?.interval === undefined ? 30_000 : options.keepAlive?.interval,
|
|
47
|
-
timeout: options?.keepAlive?.timeout === undefined ? this.timeout : options.keepAlive?.timeout,
|
|
48
|
-
};
|
|
49
|
-
this.autoResubscribe = options?.autoResubscribe ?? true;
|
|
50
|
-
// Initialize listeners
|
|
51
|
-
this.socket.addEventListener("open", () => {
|
|
52
|
-
this._keepAliveStart();
|
|
53
|
-
this._resubscribeStart();
|
|
54
|
-
});
|
|
55
|
-
this.socket.addEventListener("close", () => {
|
|
56
|
-
this._keepAliveStop();
|
|
57
|
-
this._resubscribeStop();
|
|
58
|
-
this._isReconnecting = true;
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Sends a request to the Hyperliquid API via WebSocket.
|
|
63
|
-
*
|
|
64
|
-
* Note: Explorer requests are not supported in the Hyperliquid WebSocket API.
|
|
65
|
-
*
|
|
66
|
-
* @param endpoint - The API endpoint to send the request to (`explorer` requests are not supported).
|
|
67
|
-
* @param payload - The payload to send with the request.
|
|
68
|
-
* @param signal - An optional abort signal.
|
|
69
|
-
* @returns A promise that resolves with parsed JSON response body.
|
|
70
|
-
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
|
|
71
|
-
*/
|
|
72
|
-
request(type, payload, signal) {
|
|
60
|
+
/**
|
|
61
|
+
* Sends a request to the Hyperliquid API via WebSocket.
|
|
62
|
+
*
|
|
63
|
+
* Note: Explorer requests are not supported in the Hyperliquid WebSocket API.
|
|
64
|
+
*
|
|
65
|
+
* @param endpoint - The API endpoint to send the request to (`explorer` requests are not supported).
|
|
66
|
+
* @param payload - The payload to send with the request.
|
|
67
|
+
* @param signal - An optional abort signal.
|
|
68
|
+
* @returns A promise that resolves with parsed JSON response body.
|
|
69
|
+
*
|
|
70
|
+
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
|
|
71
|
+
*/
|
|
72
|
+
async request(type, payload, signal) {
|
|
73
|
+
try {
|
|
73
74
|
const timeoutSignal = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
|
|
74
75
|
const combinedSignal = signal && timeoutSignal
|
|
75
76
|
? AbortSignal.any([signal, timeoutSignal])
|
|
76
77
|
: signal ?? timeoutSignal;
|
|
77
|
-
return this._wsRequester.request("post", {
|
|
78
|
+
return await this._wsRequester.request("post", {
|
|
78
79
|
type: type === "exchange" ? "action" : type,
|
|
79
80
|
payload,
|
|
80
81
|
}, combinedSignal);
|
|
81
82
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
83
|
+
catch (error) {
|
|
84
|
+
if (error instanceof WebSocketRequestError)
|
|
85
|
+
throw error; // Re-throw known errors
|
|
86
|
+
throw new WebSocketRequestError("Unknown error while making a WebSocket request. See the `cause` for more details.", { cause: error });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Subscribes to a Hyperliquid event channel.
|
|
91
|
+
* Sends a subscription request to the server and listens for events.
|
|
92
|
+
*
|
|
93
|
+
* @param channel - The event channel to listen to.
|
|
94
|
+
* @param payload - A payload to send with the subscription request.
|
|
95
|
+
* @param listener - A function to call when the event is dispatched.
|
|
96
|
+
* @returns A promise that resolves with a {@link Subscription} object to manage the subscription lifecycle.
|
|
97
|
+
*
|
|
98
|
+
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
|
|
99
|
+
*/
|
|
100
|
+
async subscribe(channel, payload, listener) {
|
|
101
|
+
try {
|
|
94
102
|
// Create a unique identifier for the subscription
|
|
95
103
|
const id = _websocket_async_request_js_1.WebSocketAsyncRequest.requestToId(payload);
|
|
96
104
|
// Initialize new subscription, if it doesn't exist
|
|
@@ -111,19 +119,26 @@
|
|
|
111
119
|
if (!unsubscribe) {
|
|
112
120
|
// Create new unsubscribe function
|
|
113
121
|
unsubscribe = async () => {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
122
|
+
try {
|
|
123
|
+
// Remove listener and cleanup
|
|
124
|
+
this._hlEvents.removeEventListener(channel, listener);
|
|
125
|
+
const subscription = this._subscriptions.get(id);
|
|
126
|
+
subscription?.listeners.delete(listener);
|
|
127
|
+
// If no listeners remain, remove subscription entirely
|
|
128
|
+
if (subscription?.listeners.size === 0) {
|
|
129
|
+
// Cleanup subscription
|
|
130
|
+
this._subscriptions.delete(id);
|
|
131
|
+
// If the socket is open, send unsubscription request
|
|
132
|
+
if (this.socket.readyState === _reconnecting_websocket_js_1.ReconnectingWebSocket.OPEN) {
|
|
133
|
+
await this._wsRequester.request("unsubscribe", payload);
|
|
134
|
+
}
|
|
125
135
|
}
|
|
126
136
|
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
if (error instanceof WebSocketRequestError)
|
|
139
|
+
throw error; // Re-throw known errors
|
|
140
|
+
throw new WebSocketRequestError("Unknown error while unsubscribing from a WebSocket channel. See the `cause` for more details.", { cause: error });
|
|
141
|
+
}
|
|
127
142
|
};
|
|
128
143
|
// Add listener and cache unsubscribe function
|
|
129
144
|
this._hlEvents.addEventListener(channel, listener);
|
|
@@ -137,108 +152,113 @@
|
|
|
137
152
|
resubscribeSignal: subscription.resubscribeAbortController?.signal,
|
|
138
153
|
};
|
|
139
154
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
*/
|
|
145
|
-
ready(signal) {
|
|
146
|
-
return new Promise((resolve, reject) => {
|
|
147
|
-
const combinedSignal = signal
|
|
148
|
-
? AbortSignal.any([this.socket.reconnectAbortController.signal, signal])
|
|
149
|
-
: this.socket.reconnectAbortController.signal;
|
|
150
|
-
if (combinedSignal.aborted)
|
|
151
|
-
return reject(combinedSignal.reason);
|
|
152
|
-
if (this.socket.readyState === _reconnecting_websocket_js_1.ReconnectingWebSocket.OPEN)
|
|
153
|
-
return resolve();
|
|
154
|
-
const handleOpen = () => {
|
|
155
|
-
combinedSignal.removeEventListener("abort", handleAbort);
|
|
156
|
-
resolve();
|
|
157
|
-
};
|
|
158
|
-
const handleAbort = () => {
|
|
159
|
-
this.socket.removeEventListener("open", handleOpen);
|
|
160
|
-
reject(combinedSignal.reason);
|
|
161
|
-
};
|
|
162
|
-
this.socket.addEventListener("open", handleOpen, { once: true });
|
|
163
|
-
combinedSignal.addEventListener("abort", handleAbort, { once: true });
|
|
164
|
-
});
|
|
155
|
+
catch (error) {
|
|
156
|
+
if (error instanceof WebSocketRequestError)
|
|
157
|
+
throw error; // Re-throw known errors
|
|
158
|
+
throw new WebSocketRequestError("Unknown error while subscribing to a WebSocket channel. See the `cause` for more details.", { cause: error });
|
|
165
159
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
};
|
|
185
|
-
this.socket.addEventListener("close", handleClose, { once: true });
|
|
186
|
-
signal?.addEventListener("abort", handleAbort, { once: true });
|
|
187
|
-
this.socket.close();
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
/** Keep the connection alive. Sends ping only when necessary. */
|
|
191
|
-
_keepAliveStart() {
|
|
192
|
-
if (this.keepAlive.interval === null || this._keepAliveTimeout)
|
|
193
|
-
return;
|
|
194
|
-
const tick = async () => {
|
|
195
|
-
if (this.socket.readyState !== _reconnecting_websocket_js_1.ReconnectingWebSocket.OPEN || !this._keepAliveTimeout ||
|
|
196
|
-
this.keepAlive.interval === null)
|
|
197
|
-
return;
|
|
198
|
-
// Check if the last request was sent more than the keep-alive interval ago
|
|
199
|
-
if (Date.now() - this._wsRequester.lastRequestTime >= this.keepAlive.interval) {
|
|
200
|
-
const timeoutSignal = this.keepAlive.timeout ? AbortSignal.timeout(this.keepAlive.timeout) : undefined;
|
|
201
|
-
await this._wsRequester.request("ping", timeoutSignal)
|
|
202
|
-
.catch(() => undefined); // Ignore errors
|
|
203
|
-
}
|
|
204
|
-
// Schedule the next ping
|
|
205
|
-
if (this.socket.readyState === _reconnecting_websocket_js_1.ReconnectingWebSocket.OPEN && this._keepAliveTimeout &&
|
|
206
|
-
this.keepAlive.interval !== null) {
|
|
207
|
-
const nextDelay = this.keepAlive.interval - (Date.now() - this._wsRequester.lastRequestTime);
|
|
208
|
-
this._keepAliveTimeout = setTimeout(tick, nextDelay);
|
|
209
|
-
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Waits until the WebSocket connection is ready.
|
|
163
|
+
* @param signal - An optional abort signal.
|
|
164
|
+
* @returns A promise that resolves when the connection is ready.
|
|
165
|
+
*/
|
|
166
|
+
ready(signal) {
|
|
167
|
+
return new Promise((resolve, reject) => {
|
|
168
|
+
const combinedSignal = signal
|
|
169
|
+
? AbortSignal.any([this.socket.reconnectAbortController.signal, signal])
|
|
170
|
+
: this.socket.reconnectAbortController.signal;
|
|
171
|
+
if (combinedSignal.aborted)
|
|
172
|
+
return reject(combinedSignal.reason);
|
|
173
|
+
if (this.socket.readyState === _reconnecting_websocket_js_1.ReconnectingWebSocket.OPEN)
|
|
174
|
+
return resolve();
|
|
175
|
+
const handleOpen = () => {
|
|
176
|
+
combinedSignal.removeEventListener("abort", handleAbort);
|
|
177
|
+
resolve();
|
|
210
178
|
};
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
179
|
+
const handleAbort = () => {
|
|
180
|
+
this.socket.removeEventListener("open", handleOpen);
|
|
181
|
+
reject(combinedSignal.reason);
|
|
182
|
+
};
|
|
183
|
+
this.socket.addEventListener("open", handleOpen, { once: true });
|
|
184
|
+
combinedSignal.addEventListener("abort", handleAbort, { once: true });
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Closes the WebSocket connection and waits until it is fully closed.
|
|
189
|
+
* @param signal - An optional abort signal.
|
|
190
|
+
* @returns A promise that resolves when the connection is fully closed.
|
|
191
|
+
*/
|
|
192
|
+
close(signal) {
|
|
193
|
+
return new Promise((resolve, reject) => {
|
|
194
|
+
if (signal?.aborted)
|
|
195
|
+
return reject(signal.reason);
|
|
196
|
+
if (this.socket.readyState === _reconnecting_websocket_js_1.ReconnectingWebSocket.CLOSED)
|
|
197
|
+
return resolve();
|
|
198
|
+
const handleClose = () => {
|
|
199
|
+
signal?.removeEventListener("abort", handleAbort);
|
|
200
|
+
resolve();
|
|
201
|
+
};
|
|
202
|
+
const handleAbort = () => {
|
|
203
|
+
this.socket.removeEventListener("close", handleClose);
|
|
204
|
+
reject(signal?.reason);
|
|
205
|
+
};
|
|
206
|
+
this.socket.addEventListener("close", handleClose, { once: true });
|
|
207
|
+
signal?.addEventListener("abort", handleAbort, { once: true });
|
|
208
|
+
this.socket.close();
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/** Keep the connection alive. Sends ping only when necessary. */
|
|
212
|
+
_keepAliveStart() {
|
|
213
|
+
if (this.keepAlive.interval === null || this._keepAliveTimeout)
|
|
214
|
+
return;
|
|
215
|
+
const tick = async () => {
|
|
216
|
+
if (this.socket.readyState !== _reconnecting_websocket_js_1.ReconnectingWebSocket.OPEN || !this._keepAliveTimeout ||
|
|
217
|
+
this.keepAlive.interval === null)
|
|
218
|
+
return;
|
|
219
|
+
// Check if the last request was sent more than the keep-alive interval ago
|
|
220
|
+
if (Date.now() - this._wsRequester.lastRequestTime >= this.keepAlive.interval) {
|
|
221
|
+
const timeoutSignal = this.keepAlive.timeout ? AbortSignal.timeout(this.keepAlive.timeout) : undefined;
|
|
222
|
+
await this._wsRequester.request("ping", timeoutSignal)
|
|
223
|
+
.catch(() => undefined); // Ignore errors
|
|
224
|
+
}
|
|
225
|
+
// Schedule the next ping
|
|
226
|
+
if (this.socket.readyState === _reconnecting_websocket_js_1.ReconnectingWebSocket.OPEN && this._keepAliveTimeout &&
|
|
227
|
+
this.keepAlive.interval !== null) {
|
|
228
|
+
const nextDelay = this.keepAlive.interval - (Date.now() - this._wsRequester.lastRequestTime);
|
|
229
|
+
this._keepAliveTimeout = setTimeout(tick, nextDelay);
|
|
217
230
|
}
|
|
231
|
+
};
|
|
232
|
+
this._keepAliveTimeout = setTimeout(tick, this.keepAlive.interval);
|
|
233
|
+
}
|
|
234
|
+
_keepAliveStop() {
|
|
235
|
+
if (this._keepAliveTimeout !== null) {
|
|
236
|
+
clearTimeout(this._keepAliveTimeout);
|
|
237
|
+
this._keepAliveTimeout = null;
|
|
218
238
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
239
|
+
}
|
|
240
|
+
/** Resubscribe to all existing subscriptions if auto-resubscribe is enabled. */
|
|
241
|
+
_resubscribeStart() {
|
|
242
|
+
if (this.autoResubscribe && this._isReconnecting) {
|
|
243
|
+
for (const [id, subscriptionInfo] of this._subscriptions.entries()) {
|
|
244
|
+
subscriptionInfo.promise = this._wsRequester.request("subscribe", JSON.parse(id))
|
|
245
|
+
.catch((error) => {
|
|
246
|
+
subscriptionInfo.resubscribeAbortController?.abort(error);
|
|
247
|
+
});
|
|
228
248
|
}
|
|
229
249
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
250
|
+
}
|
|
251
|
+
_resubscribeStop() {
|
|
252
|
+
if (!this.autoResubscribe || this.socket.reconnectAbortController.signal.aborted) {
|
|
253
|
+
for (const subscriptionInfo of this._subscriptions.values()) {
|
|
254
|
+
for (const [_, unsubscribe] of subscriptionInfo.listeners) {
|
|
255
|
+
unsubscribe(); // does not cause an error if used when the connection is closed
|
|
236
256
|
}
|
|
237
257
|
}
|
|
238
258
|
}
|
|
239
|
-
async [Symbol.asyncDispose]() {
|
|
240
|
-
await this.close();
|
|
241
|
-
}
|
|
242
259
|
}
|
|
243
|
-
|
|
244
|
-
|
|
260
|
+
async [Symbol.asyncDispose]() {
|
|
261
|
+
await this.close();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
exports.WebSocketTransport = WebSocketTransport;
|