@nktkas/hyperliquid 0.13.0 → 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.
Files changed (206) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +42 -17
  3. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  4. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  5. package/esm/deps/jsr.io/@std/bytes/1.0.5/_types.js +2 -0
  6. package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  7. package/esm/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  8. package/esm/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.js +1 -1
  9. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  10. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@std/encoding/1.0.7/_types.js +2 -0
  12. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  13. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.js +1 -1
  14. package/{script/deps/jsr.io/@std/encoding/1.0.6 → esm/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  15. package/esm/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  16. package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/hex.js +1 -1
  17. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  18. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  19. package/esm/deps/jsr.io/@std/msgpack/1.0.3/_types.js +2 -0
  20. package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  21. package/esm/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  22. package/esm/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.js +2 -2
  23. package/esm/mod.d.ts +15 -14
  24. package/esm/mod.d.ts.map +1 -1
  25. package/esm/src/clients/event.d.ts +64 -21
  26. package/esm/src/clients/event.d.ts.map +1 -1
  27. package/esm/src/clients/event.js +76 -18
  28. package/esm/src/clients/public.d.ts +244 -52
  29. package/esm/src/clients/public.d.ts.map +1 -1
  30. package/esm/src/clients/public.js +243 -47
  31. package/esm/src/clients/wallet.d.ts +122 -53
  32. package/esm/src/clients/wallet.d.ts.map +1 -1
  33. package/esm/src/clients/wallet.js +201 -65
  34. package/esm/src/transports/http/http_transport.d.ts.map +1 -1
  35. package/esm/src/transports/http/http_transport.js +4 -1
  36. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts +27 -21
  37. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -1
  38. package/esm/src/transports/websocket/hyperliquid_event_target.js +37 -0
  39. package/esm/src/transports/websocket/websocket_request_dispatcher.d.ts.map +1 -1
  40. package/esm/src/transports/websocket/websocket_request_dispatcher.js +4 -4
  41. package/esm/src/transports/websocket/websocket_transport.d.ts +7 -10
  42. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  43. package/esm/src/transports/websocket/websocket_transport.js +41 -35
  44. package/esm/src/types/common.d.ts +3 -0
  45. package/esm/src/types/common.d.ts.map +1 -0
  46. package/esm/src/types/common.js +1 -0
  47. package/esm/src/types/exchange/common.d.ts +36 -0
  48. package/esm/src/types/exchange/common.d.ts.map +1 -0
  49. package/esm/src/types/exchange/common.js +1 -0
  50. package/esm/src/types/exchange/requests.d.ts +502 -0
  51. package/esm/src/types/exchange/requests.d.ts.map +1 -0
  52. package/esm/src/types/exchange/requests.js +1 -0
  53. package/esm/src/types/exchange/responses.d.ts +141 -0
  54. package/esm/src/types/exchange/responses.d.ts.map +1 -0
  55. package/esm/src/types/exchange/responses.js +1 -0
  56. package/esm/src/types/explorer/common.d.ts +37 -0
  57. package/esm/src/types/explorer/common.d.ts.map +1 -0
  58. package/esm/src/types/explorer/common.js +1 -0
  59. package/esm/src/types/explorer/requests.d.ts +35 -0
  60. package/esm/src/types/explorer/requests.d.ts.map +1 -0
  61. package/esm/src/types/explorer/requests.js +1 -0
  62. package/esm/src/types/explorer/responses.d.ts +23 -0
  63. package/esm/src/types/explorer/responses.d.ts.map +1 -0
  64. package/esm/src/types/explorer/responses.js +1 -0
  65. package/esm/src/types/info/accounts.d.ts +489 -0
  66. package/esm/src/types/info/accounts.d.ts.map +1 -0
  67. package/esm/src/types/info/accounts.js +1 -0
  68. package/esm/src/types/info/assets.d.ts +275 -0
  69. package/esm/src/types/info/assets.d.ts.map +1 -0
  70. package/esm/src/types/info/assets.js +1 -0
  71. package/esm/src/types/info/delegations.d.ts +117 -0
  72. package/esm/src/types/info/delegations.d.ts.map +1 -0
  73. package/esm/src/types/info/delegations.js +1 -0
  74. package/esm/src/types/info/orders.d.ts +209 -0
  75. package/esm/src/types/info/orders.d.ts.map +1 -0
  76. package/esm/src/types/info/orders.js +1 -0
  77. package/esm/src/types/info/requests.d.ts +445 -0
  78. package/esm/src/types/info/requests.d.ts.map +1 -0
  79. package/esm/src/types/info/requests.js +1 -0
  80. package/esm/src/types/info/vaults.d.ts +90 -0
  81. package/esm/src/types/info/vaults.d.ts.map +1 -0
  82. package/esm/src/types/info/vaults.js +1 -0
  83. package/esm/src/types/subscriptions/common.d.ts +208 -0
  84. package/esm/src/types/subscriptions/common.d.ts.map +1 -0
  85. package/esm/src/types/subscriptions/common.js +1 -0
  86. package/esm/src/types/subscriptions/requests.d.ts +134 -0
  87. package/esm/src/types/subscriptions/requests.d.ts.map +1 -0
  88. package/esm/src/types/subscriptions/requests.js +1 -0
  89. package/esm/src/utils/key_sort.d.ts +3 -3
  90. package/esm/src/utils/key_sort.d.ts.map +1 -1
  91. package/esm/src/utils/signing.d.ts +1 -1
  92. package/esm/src/utils/signing.js +2 -2
  93. package/package.json +2 -2
  94. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +13 -3
  95. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +23 -13
  96. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +55 -45
  97. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +97 -87
  98. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +14 -4
  99. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +288 -278
  100. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +238 -228
  101. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  102. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  103. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.js +13 -0
  104. package/script/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  105. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  106. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.js +45 -0
  107. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  108. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  109. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.js +13 -0
  110. package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  111. package/script/deps/jsr.io/@std/encoding/1.0.7/_validate_binary_like.js +39 -0
  112. package/{esm/deps/jsr.io/@std/encoding/1.0.6 → script/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  113. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  114. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.js +123 -0
  115. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  116. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  117. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +13 -0
  118. package/script/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  119. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  120. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +250 -0
  121. package/script/mod.d.ts +15 -14
  122. package/script/mod.d.ts.map +1 -1
  123. package/script/mod.js +23 -13
  124. package/script/src/clients/event.d.ts +64 -21
  125. package/script/src/clients/event.d.ts.map +1 -1
  126. package/script/src/clients/event.js +551 -483
  127. package/script/src/clients/public.d.ts +244 -52
  128. package/script/src/clients/public.d.ts.map +1 -1
  129. package/script/src/clients/public.js +914 -708
  130. package/script/src/clients/wallet.d.ts +122 -53
  131. package/script/src/clients/wallet.d.ts.map +1 -1
  132. package/script/src/clients/wallet.js +1121 -975
  133. package/script/src/transports/base.js +25 -15
  134. package/script/src/transports/http/http_transport.d.ts.map +1 -1
  135. package/script/src/transports/http/http_transport.js +174 -161
  136. package/script/src/transports/websocket/hyperliquid_event_target.d.ts +27 -21
  137. package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -1
  138. package/script/src/transports/websocket/hyperliquid_event_target.js +80 -33
  139. package/script/src/transports/websocket/reconnecting_websocket.js +364 -354
  140. package/script/src/transports/websocket/websocket_request_dispatcher.d.ts.map +1 -1
  141. package/script/src/transports/websocket/websocket_request_dispatcher.js +196 -186
  142. package/script/src/transports/websocket/websocket_transport.d.ts +7 -10
  143. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  144. package/script/src/transports/websocket/websocket_transport.js +236 -220
  145. package/script/src/types/common.d.ts +3 -0
  146. package/script/src/types/common.d.ts.map +1 -0
  147. package/script/src/types/common.js +12 -0
  148. package/script/src/types/exchange/common.d.ts +36 -0
  149. package/script/src/types/exchange/common.d.ts.map +1 -0
  150. package/script/src/types/exchange/common.js +12 -0
  151. package/script/src/types/exchange/requests.d.ts +502 -0
  152. package/script/src/types/exchange/requests.d.ts.map +1 -0
  153. package/script/src/types/exchange/requests.js +12 -0
  154. package/script/src/types/exchange/responses.d.ts +141 -0
  155. package/script/src/types/exchange/responses.d.ts.map +1 -0
  156. package/script/src/types/exchange/responses.js +12 -0
  157. package/script/src/types/explorer/common.d.ts +37 -0
  158. package/script/src/types/explorer/common.d.ts.map +1 -0
  159. package/script/src/types/explorer/common.js +12 -0
  160. package/script/src/types/explorer/requests.d.ts +35 -0
  161. package/script/src/types/explorer/requests.d.ts.map +1 -0
  162. package/script/src/types/explorer/requests.js +12 -0
  163. package/script/src/types/explorer/responses.d.ts +23 -0
  164. package/script/src/types/explorer/responses.d.ts.map +1 -0
  165. package/script/src/types/explorer/responses.js +12 -0
  166. package/script/src/types/info/accounts.d.ts +489 -0
  167. package/script/src/types/info/accounts.d.ts.map +1 -0
  168. package/script/src/types/info/accounts.js +12 -0
  169. package/script/src/types/info/assets.d.ts +275 -0
  170. package/script/src/types/info/assets.d.ts.map +1 -0
  171. package/script/src/types/info/assets.js +12 -0
  172. package/script/src/types/info/delegations.d.ts +117 -0
  173. package/script/src/types/info/delegations.d.ts.map +1 -0
  174. package/script/src/types/info/delegations.js +12 -0
  175. package/script/src/types/info/orders.d.ts +209 -0
  176. package/script/src/types/info/orders.d.ts.map +1 -0
  177. package/script/src/types/info/orders.js +12 -0
  178. package/script/src/types/info/requests.d.ts +445 -0
  179. package/script/src/types/info/requests.d.ts.map +1 -0
  180. package/script/src/types/info/requests.js +12 -0
  181. package/script/src/types/info/vaults.d.ts +90 -0
  182. package/script/src/types/info/vaults.d.ts.map +1 -0
  183. package/script/src/types/info/vaults.js +12 -0
  184. package/script/src/types/subscriptions/common.d.ts +208 -0
  185. package/script/src/types/subscriptions/common.d.ts.map +1 -0
  186. package/script/src/types/subscriptions/common.js +12 -0
  187. package/script/src/types/subscriptions/requests.d.ts +134 -0
  188. package/script/src/types/subscriptions/requests.d.ts.map +1 -0
  189. package/script/src/types/subscriptions/requests.js +12 -0
  190. package/script/src/utils/key_sort.d.ts +3 -3
  191. package/script/src/utils/key_sort.d.ts.map +1 -1
  192. package/script/src/utils/key_sort.js +133 -123
  193. package/script/src/utils/signing.d.ts +1 -1
  194. package/script/src/utils/signing.js +172 -162
  195. package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  196. package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  197. package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  198. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  199. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.js +0 -35
  200. package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +0 -29
  201. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  202. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.js +0 -113
  203. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  204. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.js +0 -240
  205. /package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
  206. /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
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WebSocketTransport = void 0;
4
- const reconnecting_websocket_js_1 = require("./reconnecting_websocket.js");
5
- const hyperliquid_event_target_js_1 = require("./hyperliquid_event_target.js");
6
- const websocket_request_dispatcher_js_1 = require("./websocket_request_dispatcher.js");
7
- /**
8
- * WebSocket implementation of the REST and Subscription transport interfaces.
9
- */
10
- class WebSocketTransport {
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
- * Creates a new WebSocket transport instance.
13
- * @param options - Configuration options for the WebSocket transport layer.
17
+ * WebSocket implementation of the REST and Subscription transport interfaces.
14
18
  */
15
- constructor(options) {
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
- * Map of active subscriptions.
39
- * - Key: Unique subscription identifier (stringified payload)
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
- Object.defineProperty(this, "_subscriptions", {
43
- enumerable: true,
44
- configurable: true,
45
- writable: true,
46
- value: new Map()
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
- * Request timeout in ms.
50
- * Set to `null` to disable.
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
- Object.defineProperty(this, "timeout", {
53
- enumerable: true,
54
- configurable: true,
55
- writable: true,
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
- this.socket.addEventListener("close", () => {
89
- // Clear keep-alive timer
90
- if (this._keepAliveTimer) {
91
- clearInterval(this._keepAliveTimer);
92
- this._keepAliveTimer = undefined;
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
- // Clear all subscriptions
95
- for (const [_, { channel, listeners }] of this._subscriptions.entries()) {
96
- for (const [listener] of listeners) {
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
- this._subscriptions.clear();
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
- const isDeleted = subscriptionInfo.listeners.delete(listener);
189
+ subscriptionInfo.listeners.delete(listener);
152
190
  // If no listeners remain, remove subscription entirely
153
- // `isDeleted` means that the map had a listener before and became 0 after that
154
- if (subscriptionInfo.listeners.size === 0 && isDeleted) {
191
+ if (subscriptionInfo.listeners.size === 0)
155
192
  this._subscriptions.delete(id);
156
- await this._wsRequester.request("unsubscribe", payload, this.combineSignals(signal));
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
- // Wait for the initial subscription request to complete
164
- await subscriptionInfo.requestPromise.catch((error) => {
165
- // Cleanup the subscription and rethrow the error
166
- this._hlEvents.removeEventListener(channel, listener);
167
- subscriptionInfo.listeners.delete(listener);
168
- throw error;
169
- });
170
- // Return subscription control object
171
- return { unsubscribe };
172
- }
173
- /**
174
- * Waits until the WebSocket connection is ready.
175
- * @param signal - An optional abort signal.
176
- * @returns A promise that resolves when the connection is ready.
177
- */
178
- ready(signal) {
179
- return new Promise((resolve, reject) => {
180
- const combinedSignal = signal
181
- ? AbortSignal.any([this.socket.terminationSignal, signal])
182
- : this.socket.terminationSignal;
183
- if (combinedSignal.aborted)
184
- return reject(combinedSignal.reason);
185
- if (this.socket.readyState === WebSocket.OPEN)
186
- return resolve();
187
- const handleOpen = () => {
188
- combinedSignal.removeEventListener("abort", handleAbort);
189
- resolve();
190
- };
191
- const handleAbort = () => {
192
- this.socket.removeEventListener("open", handleOpen);
193
- reject(combinedSignal.reason);
194
- };
195
- this.socket.addEventListener("open", handleOpen, { once: true });
196
- combinedSignal.addEventListener("abort", handleAbort, { once: true });
197
- });
198
- }
199
- /**
200
- * Closes the WebSocket connection and waits until it is fully closed.
201
- * @param signal - An optional abort signal.
202
- * @returns A promise that resolves when the connection is fully closed.
203
- */
204
- close(signal) {
205
- return new Promise((resolve, reject) => {
206
- if (signal?.aborted)
207
- return reject(signal.reason);
208
- if (this.socket.readyState === WebSocket.CLOSED)
209
- return resolve();
210
- const handleClose = () => {
211
- signal?.removeEventListener("abort", handleAbort);
212
- resolve();
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
- exports.WebSocketTransport = WebSocketTransport;
252
+ exports.WebSocketTransport = WebSocketTransport;
253
+ });
@@ -0,0 +1,3 @@
1
+ /** Hexadecimal string starting with `0x`. */
2
+ export type Hex = `0x${string}`;
3
+ //# sourceMappingURL=common.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../../src/src/types/common.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,MAAM,MAAM,GAAG,GAAG,KAAK,MAAM,EAAE,CAAC"}
@@ -0,0 +1,12 @@
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
+ });
@@ -0,0 +1,36 @@
1
+ import type { Hex } from "../common.js";
2
+ import type { TIF } from "../info/orders.js";
3
+ /** Order parameters. */
4
+ export type OrderParams = {
5
+ /** Asset ID. */
6
+ a: number;
7
+ /** Position side (`true` for long, `false` for short). */
8
+ b: boolean;
9
+ /** Price. */
10
+ p: string;
11
+ /** Size (in base currency units). */
12
+ s: string;
13
+ /** Is reduce-only? */
14
+ r: boolean;
15
+ /** Order type. */
16
+ t: {
17
+ /** Limit order parameters. */
18
+ limit: {
19
+ /** Time-in-force. */
20
+ tif: TIF;
21
+ };
22
+ } | {
23
+ /** Trigger order parameters. */
24
+ trigger: {
25
+ /** Is market order? */
26
+ isMarket: boolean;
27
+ /** Trigger price. */
28
+ triggerPx: string;
29
+ /** Indicates whether it is take profit or stop loss. */
30
+ tpsl: "tp" | "sl";
31
+ };
32
+ };
33
+ /** Client Order ID. */
34
+ c?: Hex;
35
+ };
36
+ //# sourceMappingURL=common.d.ts.map
@@ -0,0 +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,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"}
@@ -0,0 +1,12 @@
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
+ });