@nktkas/hyperliquid 0.13.1 → 0.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -20
- package/README.md +33 -17
- package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
- package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.js +2 -0
- package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
- package/esm/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.js +1 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.js +2 -0
- package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
- package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.js +1 -1
- package/{script/deps/jsr.io/@std/encoding/1.0.6 → esm/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/hex.js +1 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +2 -0
- package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.js +2 -2
- package/esm/mod.d.ts +1 -0
- package/esm/mod.d.ts.map +1 -1
- package/esm/src/clients/event.d.ts +57 -16
- package/esm/src/clients/event.d.ts.map +1 -1
- package/esm/src/clients/event.js +72 -16
- package/esm/src/clients/public.d.ts +241 -49
- package/esm/src/clients/public.d.ts.map +1 -1
- package/esm/src/clients/public.js +243 -47
- package/esm/src/clients/wallet.d.ts +120 -51
- package/esm/src/clients/wallet.d.ts.map +1 -1
- package/esm/src/clients/wallet.js +192 -58
- package/esm/src/transports/websocket/hyperliquid_event_target.d.ts +27 -21
- package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -1
- package/esm/src/transports/websocket/hyperliquid_event_target.js +37 -0
- package/esm/src/transports/websocket/websocket_request_dispatcher.js +3 -3
- package/esm/src/transports/websocket/websocket_transport.d.ts +7 -10
- package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/esm/src/transports/websocket/websocket_transport.js +41 -35
- package/esm/src/types/exchange/common.d.ts +6 -6
- package/esm/src/types/exchange/common.d.ts.map +1 -1
- package/esm/src/types/exchange/requests.d.ts +131 -64
- package/esm/src/types/exchange/requests.d.ts.map +1 -1
- package/esm/src/types/exchange/responses.d.ts +7 -7
- package/esm/src/types/exchange/responses.d.ts.map +1 -1
- package/esm/src/types/explorer/common.d.ts +15 -15
- package/esm/src/types/explorer/common.d.ts.map +1 -1
- package/esm/src/types/explorer/requests.d.ts +19 -6
- package/esm/src/types/explorer/requests.d.ts.map +1 -1
- package/esm/src/types/explorer/responses.d.ts +9 -2
- package/esm/src/types/explorer/responses.d.ts.map +1 -1
- package/esm/src/types/info/accounts.d.ts +189 -124
- package/esm/src/types/info/accounts.d.ts.map +1 -1
- package/esm/src/types/info/assets.d.ts +82 -78
- package/esm/src/types/info/assets.d.ts.map +1 -1
- package/esm/src/types/info/delegations.d.ts +117 -0
- package/esm/src/types/info/delegations.d.ts.map +1 -0
- package/esm/src/types/info/delegations.js +1 -0
- package/esm/src/types/info/orders.d.ts +67 -67
- package/esm/src/types/info/orders.d.ts.map +1 -1
- package/esm/src/types/info/requests.d.ts +128 -63
- package/esm/src/types/info/requests.d.ts.map +1 -1
- package/esm/src/types/info/vaults.d.ts +41 -84
- package/esm/src/types/info/vaults.d.ts.map +1 -1
- package/esm/src/types/subscriptions/common.d.ts +3 -0
- package/esm/src/types/subscriptions/common.d.ts.map +1 -1
- package/esm/src/types/subscriptions/requests.d.ts +10 -0
- package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
- package/esm/src/utils/key_sort.d.ts +2 -2
- package/esm/src/utils/key_sort.d.ts.map +1 -1
- package/esm/src/utils/signing.d.ts +1 -1
- package/esm/src/utils/signing.js +2 -2
- package/package.json +1 -1
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +13 -3
- package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +23 -13
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +55 -45
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +97 -87
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +14 -4
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +288 -278
- package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +238 -228
- package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
- package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/bytes/1.0.5/_types.js +13 -0
- package/script/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
- package/script/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/bytes/1.0.5/concat.js +45 -0
- package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
- package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/encoding/1.0.7/_types.js +13 -0
- package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
- package/script/deps/jsr.io/@std/encoding/1.0.7/_validate_binary_like.js +39 -0
- package/{esm/deps/jsr.io/@std/encoding/1.0.6 → script/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
- package/script/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/encoding/1.0.7/hex.js +123 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +13 -0
- package/script/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
- package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +250 -0
- package/script/mod.d.ts +1 -0
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +23 -13
- package/script/src/clients/event.d.ts +57 -16
- package/script/src/clients/event.d.ts.map +1 -1
- package/script/src/clients/event.js +551 -485
- package/script/src/clients/public.d.ts +241 -49
- package/script/src/clients/public.d.ts.map +1 -1
- package/script/src/clients/public.js +914 -708
- package/script/src/clients/wallet.d.ts +120 -51
- package/script/src/clients/wallet.d.ts.map +1 -1
- package/script/src/clients/wallet.js +1121 -977
- package/script/src/transports/base.js +25 -15
- package/script/src/transports/http/http_transport.js +174 -164
- package/script/src/transports/websocket/hyperliquid_event_target.d.ts +27 -21
- package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -1
- package/script/src/transports/websocket/hyperliquid_event_target.js +80 -33
- package/script/src/transports/websocket/reconnecting_websocket.js +364 -354
- package/script/src/transports/websocket/websocket_request_dispatcher.js +196 -186
- package/script/src/transports/websocket/websocket_transport.d.ts +7 -10
- package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
- package/script/src/transports/websocket/websocket_transport.js +236 -220
- package/script/src/types/common.js +12 -2
- package/script/src/types/exchange/common.d.ts +6 -6
- package/script/src/types/exchange/common.d.ts.map +1 -1
- package/script/src/types/exchange/common.js +12 -2
- package/script/src/types/exchange/requests.d.ts +131 -64
- package/script/src/types/exchange/requests.d.ts.map +1 -1
- package/script/src/types/exchange/requests.js +12 -2
- package/script/src/types/exchange/responses.d.ts +7 -7
- package/script/src/types/exchange/responses.d.ts.map +1 -1
- package/script/src/types/exchange/responses.js +12 -2
- package/script/src/types/explorer/common.d.ts +15 -15
- package/script/src/types/explorer/common.d.ts.map +1 -1
- package/script/src/types/explorer/common.js +12 -2
- package/script/src/types/explorer/requests.d.ts +19 -6
- package/script/src/types/explorer/requests.d.ts.map +1 -1
- package/script/src/types/explorer/requests.js +12 -2
- package/script/src/types/explorer/responses.d.ts +9 -2
- package/script/src/types/explorer/responses.d.ts.map +1 -1
- package/script/src/types/explorer/responses.js +12 -2
- package/script/src/types/info/accounts.d.ts +189 -124
- package/script/src/types/info/accounts.d.ts.map +1 -1
- package/script/src/types/info/accounts.js +12 -2
- package/script/src/types/info/assets.d.ts +82 -78
- package/script/src/types/info/assets.d.ts.map +1 -1
- package/script/src/types/info/assets.js +12 -2
- package/script/src/types/info/delegations.d.ts +117 -0
- package/script/src/types/info/delegations.d.ts.map +1 -0
- package/script/src/types/info/delegations.js +12 -0
- package/script/src/types/info/orders.d.ts +67 -67
- package/script/src/types/info/orders.d.ts.map +1 -1
- package/script/src/types/info/orders.js +12 -2
- package/script/src/types/info/requests.d.ts +128 -63
- package/script/src/types/info/requests.d.ts.map +1 -1
- package/script/src/types/info/requests.js +12 -2
- package/script/src/types/info/vaults.d.ts +41 -84
- package/script/src/types/info/vaults.d.ts.map +1 -1
- package/script/src/types/info/vaults.js +12 -2
- package/script/src/types/subscriptions/common.d.ts +3 -0
- package/script/src/types/subscriptions/common.d.ts.map +1 -1
- package/script/src/types/subscriptions/common.js +12 -2
- package/script/src/types/subscriptions/requests.d.ts +10 -0
- package/script/src/types/subscriptions/requests.d.ts.map +1 -1
- package/script/src/types/subscriptions/requests.js +12 -2
- package/script/src/utils/key_sort.d.ts +2 -2
- package/script/src/utils/key_sort.d.ts.map +1 -1
- package/script/src/utils/key_sort.js +133 -123
- package/script/src/utils/signing.d.ts +1 -1
- package/script/src/utils/signing.js +172 -162
- package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
- package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/bytes/1.0.4/concat.js +0 -35
- package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +0 -29
- package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/encoding/1.0.6/hex.js +0 -113
- package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
- package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.js +0 -240
- /package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
- /package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
|
@@ -1,237 +1,253 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports", "./reconnecting_websocket.js", "./hyperliquid_event_target.js", "./websocket_request_dispatcher.js"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.WebSocketTransport = void 0;
|
|
13
|
+
const reconnecting_websocket_js_1 = require("./reconnecting_websocket.js");
|
|
14
|
+
const hyperliquid_event_target_js_1 = require("./hyperliquid_event_target.js");
|
|
15
|
+
const websocket_request_dispatcher_js_1 = require("./websocket_request_dispatcher.js");
|
|
11
16
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @param options - Configuration options for the WebSocket transport layer.
|
|
17
|
+
* WebSocket implementation of the REST and Subscription transport interfaces.
|
|
14
18
|
*/
|
|
15
|
-
|
|
16
|
-
/** The interval timer ID for keep-alive messages. */
|
|
17
|
-
Object.defineProperty(this, "_keepAliveTimer", {
|
|
18
|
-
enumerable: true,
|
|
19
|
-
configurable: true,
|
|
20
|
-
writable: true,
|
|
21
|
-
value: void 0
|
|
22
|
-
});
|
|
23
|
-
/** The WebSocket request dispatcher instance. */
|
|
24
|
-
Object.defineProperty(this, "_wsRequester", {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
configurable: true,
|
|
27
|
-
writable: true,
|
|
28
|
-
value: void 0
|
|
29
|
-
});
|
|
30
|
-
/** The Hyperliquid event target instance. */
|
|
31
|
-
Object.defineProperty(this, "_hlEvents", {
|
|
32
|
-
enumerable: true,
|
|
33
|
-
configurable: true,
|
|
34
|
-
writable: true,
|
|
35
|
-
value: void 0
|
|
36
|
-
});
|
|
19
|
+
class WebSocketTransport {
|
|
37
20
|
/**
|
|
38
|
-
*
|
|
39
|
-
* -
|
|
40
|
-
* - Value: Subscription info containing payload and listener mappings
|
|
21
|
+
* Creates a new WebSocket transport instance.
|
|
22
|
+
* @param options - Configuration options for the WebSocket transport layer.
|
|
41
23
|
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
24
|
+
constructor(options) {
|
|
25
|
+
/** The interval timer ID for keep-alive messages. */
|
|
26
|
+
Object.defineProperty(this, "_keepAliveTimer", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true,
|
|
30
|
+
value: void 0
|
|
31
|
+
});
|
|
32
|
+
/** The WebSocket request dispatcher instance. */
|
|
33
|
+
Object.defineProperty(this, "_wsRequester", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: void 0
|
|
38
|
+
});
|
|
39
|
+
/** The Hyperliquid event target instance. */
|
|
40
|
+
Object.defineProperty(this, "_hlEvents", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
value: void 0
|
|
45
|
+
});
|
|
46
|
+
/**
|
|
47
|
+
* Map of active subscriptions.
|
|
48
|
+
* - Key: Unique subscription identifier based on payload
|
|
49
|
+
* - Value: Subscription info containing the subscription request promise
|
|
50
|
+
* and a map of listeners to their metadata (channel + unsubscribe function).
|
|
51
|
+
*/
|
|
52
|
+
Object.defineProperty(this, "_subscriptions", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
configurable: true,
|
|
55
|
+
writable: true,
|
|
56
|
+
value: new Map()
|
|
57
|
+
});
|
|
58
|
+
/**
|
|
59
|
+
* Request timeout in ms.
|
|
60
|
+
* Set to `null` to disable.
|
|
61
|
+
*/
|
|
62
|
+
Object.defineProperty(this, "timeout", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: void 0
|
|
67
|
+
});
|
|
68
|
+
/** Keep-alive configuration settings. */
|
|
69
|
+
Object.defineProperty(this, "keepAlive", {
|
|
70
|
+
enumerable: true,
|
|
71
|
+
configurable: true,
|
|
72
|
+
writable: true,
|
|
73
|
+
value: void 0
|
|
74
|
+
});
|
|
75
|
+
/** The WebSocket that is used for communication. */
|
|
76
|
+
Object.defineProperty(this, "socket", {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
configurable: true,
|
|
79
|
+
writable: true,
|
|
80
|
+
value: void 0
|
|
81
|
+
});
|
|
82
|
+
this.socket = new reconnecting_websocket_js_1.ReconnectingWebSocket(options?.url ?? "wss://api.hyperliquid.xyz/ws", undefined, options?.reconnect);
|
|
83
|
+
this._hlEvents = new hyperliquid_event_target_js_1.HyperliquidEventTarget(this.socket);
|
|
84
|
+
this._wsRequester = new websocket_request_dispatcher_js_1.WebSocketRequestDispatcher(this.socket, this._hlEvents);
|
|
85
|
+
this.timeout = options?.timeout === undefined ? 10_000 : options.timeout;
|
|
86
|
+
this.keepAlive = {
|
|
87
|
+
interval: options?.keepAlive?.interval === undefined ? 20_000 : options.keepAlive.interval,
|
|
88
|
+
};
|
|
89
|
+
// Initialize listeners
|
|
90
|
+
this.socket.addEventListener("open", () => {
|
|
91
|
+
// Start keep-alive timer
|
|
92
|
+
if (this.keepAlive.interval && !this._keepAliveTimer) {
|
|
93
|
+
this._keepAliveTimer = setInterval(() => {
|
|
94
|
+
this.socket.send(JSON.stringify({ method: "ping" }));
|
|
95
|
+
}, this.keepAlive.interval);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
this.socket.addEventListener("close", () => {
|
|
99
|
+
// Clear keep-alive timer
|
|
100
|
+
if (this._keepAliveTimer) {
|
|
101
|
+
clearInterval(this._keepAliveTimer);
|
|
102
|
+
this._keepAliveTimer = undefined;
|
|
103
|
+
}
|
|
104
|
+
// Clear all subscriptions
|
|
105
|
+
for (const subscriptionInfo of this._subscriptions.values()) {
|
|
106
|
+
for (const [listener, { channel }] of subscriptionInfo.listeners) {
|
|
107
|
+
this._hlEvents.removeEventListener(channel, listener);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
this._subscriptions.clear();
|
|
111
|
+
});
|
|
112
|
+
}
|
|
48
113
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
114
|
+
* Sends a request to the Hyperliquid API via WebSocket.
|
|
115
|
+
* @param endpoint - The API endpoint to send the request to.
|
|
116
|
+
* @param payload - The payload to send with the request.
|
|
117
|
+
* @param signal - An optional abort signal.
|
|
118
|
+
* @returns A promise that resolves with parsed JSON response body.
|
|
119
|
+
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
|
|
120
|
+
* @note Explorer requests are not supported in the Hyperliquid WebSocket API.
|
|
51
121
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
value: void 0
|
|
57
|
-
});
|
|
58
|
-
/** Keep-alive configuration settings. */
|
|
59
|
-
Object.defineProperty(this, "keepAlive", {
|
|
60
|
-
enumerable: true,
|
|
61
|
-
configurable: true,
|
|
62
|
-
writable: true,
|
|
63
|
-
value: void 0
|
|
64
|
-
});
|
|
65
|
-
/** The WebSocket that is used for communication. */
|
|
66
|
-
Object.defineProperty(this, "socket", {
|
|
67
|
-
enumerable: true,
|
|
68
|
-
configurable: true,
|
|
69
|
-
writable: true,
|
|
70
|
-
value: void 0
|
|
71
|
-
});
|
|
72
|
-
this.socket = new reconnecting_websocket_js_1.ReconnectingWebSocket(options?.url ?? "wss://api.hyperliquid.xyz/ws", undefined, options?.reconnect);
|
|
73
|
-
this._hlEvents = new hyperliquid_event_target_js_1.HyperliquidEventTarget(this.socket);
|
|
74
|
-
this._wsRequester = new websocket_request_dispatcher_js_1.WebSocketRequestDispatcher(this.socket, this._hlEvents);
|
|
75
|
-
this.timeout = options?.timeout === undefined ? 10_000 : options.timeout;
|
|
76
|
-
this.keepAlive = {
|
|
77
|
-
interval: options?.keepAlive?.interval === undefined ? 20_000 : options.keepAlive.interval,
|
|
78
|
-
};
|
|
79
|
-
// Initialize listeners
|
|
80
|
-
this.socket.addEventListener("open", () => {
|
|
81
|
-
// Start keep-alive timer
|
|
82
|
-
if (this.keepAlive.interval && !this._keepAliveTimer) {
|
|
83
|
-
this._keepAliveTimer = setInterval(() => {
|
|
84
|
-
this.socket.send(JSON.stringify({ method: "ping" }));
|
|
85
|
-
}, this.keepAlive.interval);
|
|
122
|
+
request(type, payload, signal) {
|
|
123
|
+
// Reject explorer requests because they are not supported by the Hyperliquid WebSocket API
|
|
124
|
+
if (type === "explorer") {
|
|
125
|
+
throw new Error("Explorer requests are not supported in the Hyperliquid WebSocket API.");
|
|
86
126
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
127
|
+
// Send the request and wait for a response
|
|
128
|
+
const timeoutSignal = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
|
|
129
|
+
const combinedSignal = timeoutSignal && signal
|
|
130
|
+
? AbortSignal.any([timeoutSignal, signal])
|
|
131
|
+
: timeoutSignal ?? signal;
|
|
132
|
+
return this._wsRequester.request("post", { type, payload }, combinedSignal);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Subscribes to a Hyperliquid event channel.
|
|
136
|
+
* @param channel - The event channel to listen to.
|
|
137
|
+
* @param payload - A payload to send with the subscription request.
|
|
138
|
+
* @param listener - A function to call when the event is dispatched.
|
|
139
|
+
* @param signal - An optional abort signal for canceling the subscription request.
|
|
140
|
+
* @returns A promise that resolves with a {@link Subscription} object to manage the subscription lifecycle.
|
|
141
|
+
*/
|
|
142
|
+
async subscribe(channel, payload, listener, signal) {
|
|
143
|
+
// Create a unique identifier for the subscription
|
|
144
|
+
const id = websocket_request_dispatcher_js_1.WebSocketRequestDispatcher.requestToId(payload);
|
|
145
|
+
// Initialize new subscription, if it doesn't exist
|
|
146
|
+
let subscriptionInfo = this._subscriptions.get(id);
|
|
147
|
+
if (!subscriptionInfo) {
|
|
148
|
+
// Send subscription request
|
|
149
|
+
const timeoutSignal = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
|
|
150
|
+
const combinedSignal = timeoutSignal && signal
|
|
151
|
+
? AbortSignal.any([timeoutSignal, signal])
|
|
152
|
+
: timeoutSignal ?? signal;
|
|
153
|
+
const requestPromise = this._wsRequester.request("subscribe", payload, combinedSignal);
|
|
154
|
+
// Cache subscription info
|
|
155
|
+
subscriptionInfo = { listeners: new Map(), requestPromise };
|
|
156
|
+
this._subscriptions.set(id, subscriptionInfo);
|
|
93
157
|
}
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
158
|
+
// Initialize new listener, if it doesn't exist
|
|
159
|
+
let listenerInfo = subscriptionInfo.listeners.get(listener);
|
|
160
|
+
if (!listenerInfo) {
|
|
161
|
+
// Create new unsubscribe function
|
|
162
|
+
const unsubscribe = async (signal) => {
|
|
163
|
+
// Remove listener and cleanup
|
|
97
164
|
this._hlEvents.removeEventListener(channel, listener);
|
|
98
|
-
|
|
165
|
+
const isDeleted = subscriptionInfo.listeners.delete(listener);
|
|
166
|
+
// If no listeners remain, remove subscription entirely
|
|
167
|
+
// `isDeleted` means that the map had a listener before and became 0 after that
|
|
168
|
+
if (subscriptionInfo.listeners.size === 0 && isDeleted) {
|
|
169
|
+
// Cleanup subscription
|
|
170
|
+
this._subscriptions.delete(id);
|
|
171
|
+
// Send unsubscription request
|
|
172
|
+
const timeoutSignal = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
|
|
173
|
+
const combinedSignal = timeoutSignal && signal
|
|
174
|
+
? AbortSignal.any([timeoutSignal, signal])
|
|
175
|
+
: timeoutSignal ?? signal;
|
|
176
|
+
await this._wsRequester.request("unsubscribe", payload, combinedSignal);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
// Cache listener info
|
|
180
|
+
listenerInfo = { channel, unsubscribe };
|
|
181
|
+
subscriptionInfo.listeners.set(listener, listenerInfo);
|
|
182
|
+
// Add event listener
|
|
183
|
+
this._hlEvents.addEventListener(channel, listener);
|
|
99
184
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Sends a request to the Hyperliquid API via WebSocket.
|
|
105
|
-
* @param endpoint - The API endpoint to send the request to.
|
|
106
|
-
* @param payload - The payload to send with the request.
|
|
107
|
-
* @param signal - An optional abort signal.
|
|
108
|
-
* @returns A promise that resolves with parsed JSON response body.
|
|
109
|
-
* @throws {WebSocketRequestError} - An error that occurs when a WebSocket request fails.
|
|
110
|
-
* @note Explorer requests are not supported in the Hyperliquid WebSocket API.
|
|
111
|
-
*/
|
|
112
|
-
request(type, payload, signal) {
|
|
113
|
-
// Reject explorer requests because they are not supported by the Hyperliquid WebSocket API
|
|
114
|
-
if (type === "explorer") {
|
|
115
|
-
throw new Error("Explorer requests are not supported in the Hyperliquid WebSocket API.");
|
|
116
|
-
}
|
|
117
|
-
// Send the request and wait for a response
|
|
118
|
-
return this._wsRequester.request("post", { type, payload }, this.combineSignals(signal));
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Subscribes to a Hyperliquid event channel.
|
|
122
|
-
* @param channel - The event channel to listen to.
|
|
123
|
-
* @param payload - A payload to send with the subscription request.
|
|
124
|
-
* @param listener - A function to call when the event is dispatched.
|
|
125
|
-
* @param signal - An optional abort signal for canceling the subscription request.
|
|
126
|
-
* @returns A promise that resolves with a {@link Subscription} object to manage the subscription lifecycle.
|
|
127
|
-
*/
|
|
128
|
-
async subscribe(channel, payload, listener, signal) {
|
|
129
|
-
// Create a unique identifier for the subscription
|
|
130
|
-
const id = `${channel}::${websocket_request_dispatcher_js_1.WebSocketRequestDispatcher.requestToId(payload)}`;
|
|
131
|
-
// Initialize new subscription, if it doesn't exist
|
|
132
|
-
let subscriptionInfo = this._subscriptions.get(id);
|
|
133
|
-
if (!subscriptionInfo) {
|
|
134
|
-
// Send subscription request
|
|
135
|
-
const requestPromise = this._wsRequester.request("subscribe", payload, this.combineSignals(signal))
|
|
136
|
-
.catch((error) => {
|
|
137
|
-
this._subscriptions.delete(id);
|
|
138
|
-
throw error;
|
|
139
|
-
});
|
|
140
|
-
// Cache subscription info
|
|
141
|
-
subscriptionInfo = { channel, listeners: new Map(), requestPromise };
|
|
142
|
-
this._subscriptions.set(id, subscriptionInfo);
|
|
143
|
-
}
|
|
144
|
-
// Check if we already have an unsubscribe function for this listener
|
|
145
|
-
let unsubscribe = subscriptionInfo.listeners.get(listener);
|
|
146
|
-
if (!unsubscribe) {
|
|
147
|
-
// Create new unsubscribe function if none exists
|
|
148
|
-
unsubscribe = async (signal) => {
|
|
185
|
+
// Wait for the initial subscription request to complete
|
|
186
|
+
await subscriptionInfo.requestPromise.catch((error) => {
|
|
149
187
|
// Remove listener and cleanup
|
|
150
188
|
this._hlEvents.removeEventListener(channel, listener);
|
|
151
|
-
|
|
189
|
+
subscriptionInfo.listeners.delete(listener);
|
|
152
190
|
// If no listeners remain, remove subscription entirely
|
|
153
|
-
|
|
154
|
-
if (subscriptionInfo.listeners.size === 0 && isDeleted) {
|
|
191
|
+
if (subscriptionInfo.listeners.size === 0)
|
|
155
192
|
this._subscriptions.delete(id);
|
|
156
|
-
|
|
157
|
-
|
|
193
|
+
// Rethrow the error
|
|
194
|
+
throw error;
|
|
195
|
+
});
|
|
196
|
+
// Return subscription control object
|
|
197
|
+
return {
|
|
198
|
+
unsubscribe: listenerInfo.unsubscribe,
|
|
158
199
|
};
|
|
159
|
-
// Add event listener & Cache unsubscribe function
|
|
160
|
-
this._hlEvents.addEventListener(channel, listener);
|
|
161
|
-
subscriptionInfo.listeners.set(listener, unsubscribe);
|
|
162
200
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
};
|
|
214
|
-
const handleAbort = () => {
|
|
215
|
-
this.socket.removeEventListener("close", handleClose);
|
|
216
|
-
reject(signal?.reason);
|
|
217
|
-
};
|
|
218
|
-
this.socket.addEventListener("close", handleClose, { once: true });
|
|
219
|
-
signal?.addEventListener("abort", handleAbort, { once: true });
|
|
220
|
-
this.socket.close();
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Combines timeout and user-provided signal.
|
|
225
|
-
* @param signal - A user-provided signal.
|
|
226
|
-
* @returns An AbortSignal that triggers when either the timeout or user-provided signal aborts.
|
|
227
|
-
*/
|
|
228
|
-
combineSignals(signal) {
|
|
229
|
-
const signals = [];
|
|
230
|
-
if (this.timeout)
|
|
231
|
-
signals.push(AbortSignal.timeout(this.timeout));
|
|
232
|
-
if (signal)
|
|
233
|
-
signals.push(signal);
|
|
234
|
-
return signals.length > 1 ? AbortSignal.any(signals) : signals[0];
|
|
201
|
+
/**
|
|
202
|
+
* Waits until the WebSocket connection is ready.
|
|
203
|
+
* @param signal - An optional abort signal.
|
|
204
|
+
* @returns A promise that resolves when the connection is ready.
|
|
205
|
+
*/
|
|
206
|
+
ready(signal) {
|
|
207
|
+
return new Promise((resolve, reject) => {
|
|
208
|
+
const combinedSignal = signal
|
|
209
|
+
? AbortSignal.any([this.socket.terminationSignal, signal])
|
|
210
|
+
: this.socket.terminationSignal;
|
|
211
|
+
if (combinedSignal.aborted)
|
|
212
|
+
return reject(combinedSignal.reason);
|
|
213
|
+
if (this.socket.readyState === WebSocket.OPEN)
|
|
214
|
+
return resolve();
|
|
215
|
+
const handleOpen = () => {
|
|
216
|
+
combinedSignal.removeEventListener("abort", handleAbort);
|
|
217
|
+
resolve();
|
|
218
|
+
};
|
|
219
|
+
const handleAbort = () => {
|
|
220
|
+
this.socket.removeEventListener("open", handleOpen);
|
|
221
|
+
reject(combinedSignal.reason);
|
|
222
|
+
};
|
|
223
|
+
this.socket.addEventListener("open", handleOpen, { once: true });
|
|
224
|
+
combinedSignal.addEventListener("abort", handleAbort, { once: true });
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Closes the WebSocket connection and waits until it is fully closed.
|
|
229
|
+
* @param signal - An optional abort signal.
|
|
230
|
+
* @returns A promise that resolves when the connection is fully closed.
|
|
231
|
+
*/
|
|
232
|
+
close(signal) {
|
|
233
|
+
return new Promise((resolve, reject) => {
|
|
234
|
+
if (signal?.aborted)
|
|
235
|
+
return reject(signal.reason);
|
|
236
|
+
if (this.socket.readyState === WebSocket.CLOSED)
|
|
237
|
+
return resolve();
|
|
238
|
+
const handleClose = () => {
|
|
239
|
+
signal?.removeEventListener("abort", handleAbort);
|
|
240
|
+
resolve();
|
|
241
|
+
};
|
|
242
|
+
const handleAbort = () => {
|
|
243
|
+
this.socket.removeEventListener("close", handleClose);
|
|
244
|
+
reject(signal?.reason);
|
|
245
|
+
};
|
|
246
|
+
this.socket.addEventListener("close", handleClose, { once: true });
|
|
247
|
+
signal?.addEventListener("abort", handleAbort, { once: true });
|
|
248
|
+
this.socket.close();
|
|
249
|
+
});
|
|
250
|
+
}
|
|
235
251
|
}
|
|
236
|
-
|
|
237
|
-
|
|
252
|
+
exports.WebSocketTransport = WebSocketTransport;
|
|
253
|
+
});
|
|
@@ -1,2 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
});
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { Hex } from "../common.js";
|
|
2
2
|
import type { TIF } from "../info/orders.js";
|
|
3
3
|
/** Order parameters. */
|
|
4
|
-
export type
|
|
5
|
-
/**
|
|
4
|
+
export type OrderParams = {
|
|
5
|
+
/** Asset ID. */
|
|
6
6
|
a: number;
|
|
7
|
-
/** Position side (true for long, false for short). */
|
|
7
|
+
/** Position side (`true` for long, `false` for short). */
|
|
8
8
|
b: boolean;
|
|
9
9
|
/** Price. */
|
|
10
10
|
p: string;
|
|
@@ -12,7 +12,7 @@ export type OrderParms = {
|
|
|
12
12
|
s: string;
|
|
13
13
|
/** Is reduce-only? */
|
|
14
14
|
r: boolean;
|
|
15
|
-
/** Order type
|
|
15
|
+
/** Order type. */
|
|
16
16
|
t: {
|
|
17
17
|
/** Limit order parameters. */
|
|
18
18
|
limit: {
|
|
@@ -22,11 +22,11 @@ export type OrderParms = {
|
|
|
22
22
|
} | {
|
|
23
23
|
/** Trigger order parameters. */
|
|
24
24
|
trigger: {
|
|
25
|
-
/** Is
|
|
25
|
+
/** Is market order? */
|
|
26
26
|
isMarket: boolean;
|
|
27
27
|
/** Trigger price. */
|
|
28
28
|
triggerPx: string;
|
|
29
|
-
/** Indicates
|
|
29
|
+
/** Indicates whether it is take profit or stop loss. */
|
|
30
30
|
tpsl: "tp" | "sl";
|
|
31
31
|
};
|
|
32
32
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../../../src/src/types/exchange/common.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACxC,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAE7C,wBAAwB;AACxB,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../../../src/src/types/exchange/common.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACxC,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AAE7C,wBAAwB;AACxB,MAAM,MAAM,WAAW,GAAG;IACtB,gBAAgB;IAChB,CAAC,EAAE,MAAM,CAAC;IACV,0DAA0D;IAC1D,CAAC,EAAE,OAAO,CAAC;IACX,aAAa;IACb,CAAC,EAAE,MAAM,CAAC;IACV,qCAAqC;IACrC,CAAC,EAAE,MAAM,CAAC;IACV,sBAAsB;IACtB,CAAC,EAAE,OAAO,CAAC;IACX,kBAAkB;IAClB,CAAC,EACK;QACE,8BAA8B;QAC9B,KAAK,EAAE;YACH,qBAAqB;YACrB,GAAG,EAAE,GAAG,CAAC;SACZ,CAAC;KACL,GACC;QACE,gCAAgC;QAChC,OAAO,EAAE;YACL,uBAAuB;YACvB,QAAQ,EAAE,OAAO,CAAC;YAClB,qBAAqB;YACrB,SAAS,EAAE,MAAM,CAAC;YAClB,wDAAwD;YACxD,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;SACrB,CAAC;KACL,CAAC;IACN,uBAAuB;IACvB,CAAC,CAAC,EAAE,GAAG,CAAC;CACX,CAAC"}
|
|
@@ -1,2 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
});
|