@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.
Files changed (183) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +33 -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 +1 -0
  24. package/esm/mod.d.ts.map +1 -1
  25. package/esm/src/clients/event.d.ts +57 -16
  26. package/esm/src/clients/event.d.ts.map +1 -1
  27. package/esm/src/clients/event.js +72 -16
  28. package/esm/src/clients/public.d.ts +241 -49
  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 +120 -51
  32. package/esm/src/clients/wallet.d.ts.map +1 -1
  33. package/esm/src/clients/wallet.js +192 -58
  34. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts +27 -21
  35. package/esm/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -1
  36. package/esm/src/transports/websocket/hyperliquid_event_target.js +37 -0
  37. package/esm/src/transports/websocket/websocket_request_dispatcher.js +3 -3
  38. package/esm/src/transports/websocket/websocket_transport.d.ts +7 -10
  39. package/esm/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  40. package/esm/src/transports/websocket/websocket_transport.js +41 -35
  41. package/esm/src/types/exchange/common.d.ts +6 -6
  42. package/esm/src/types/exchange/common.d.ts.map +1 -1
  43. package/esm/src/types/exchange/requests.d.ts +131 -64
  44. package/esm/src/types/exchange/requests.d.ts.map +1 -1
  45. package/esm/src/types/exchange/responses.d.ts +7 -7
  46. package/esm/src/types/exchange/responses.d.ts.map +1 -1
  47. package/esm/src/types/explorer/common.d.ts +15 -15
  48. package/esm/src/types/explorer/common.d.ts.map +1 -1
  49. package/esm/src/types/explorer/requests.d.ts +19 -6
  50. package/esm/src/types/explorer/requests.d.ts.map +1 -1
  51. package/esm/src/types/explorer/responses.d.ts +9 -2
  52. package/esm/src/types/explorer/responses.d.ts.map +1 -1
  53. package/esm/src/types/info/accounts.d.ts +189 -124
  54. package/esm/src/types/info/accounts.d.ts.map +1 -1
  55. package/esm/src/types/info/assets.d.ts +82 -78
  56. package/esm/src/types/info/assets.d.ts.map +1 -1
  57. package/esm/src/types/info/delegations.d.ts +117 -0
  58. package/esm/src/types/info/delegations.d.ts.map +1 -0
  59. package/esm/src/types/info/delegations.js +1 -0
  60. package/esm/src/types/info/orders.d.ts +67 -67
  61. package/esm/src/types/info/orders.d.ts.map +1 -1
  62. package/esm/src/types/info/requests.d.ts +128 -63
  63. package/esm/src/types/info/requests.d.ts.map +1 -1
  64. package/esm/src/types/info/vaults.d.ts +41 -84
  65. package/esm/src/types/info/vaults.d.ts.map +1 -1
  66. package/esm/src/types/subscriptions/common.d.ts +3 -0
  67. package/esm/src/types/subscriptions/common.d.ts.map +1 -1
  68. package/esm/src/types/subscriptions/requests.d.ts +10 -0
  69. package/esm/src/types/subscriptions/requests.d.ts.map +1 -1
  70. package/esm/src/utils/key_sort.d.ts +2 -2
  71. package/esm/src/utils/key_sort.d.ts.map +1 -1
  72. package/esm/src/utils/signing.d.ts +1 -1
  73. package/esm/src/utils/signing.js +2 -2
  74. package/package.json +1 -1
  75. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/mod.js +13 -3
  76. package/script/deps/jsr.io/@derzade/typescript-event-target/1.1.1/src/TypedEventTarget.js +23 -13
  77. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_assert.js +55 -45
  78. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/_u64.js +97 -87
  79. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/crypto.js +14 -4
  80. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/sha3.js +288 -278
  81. package/script/deps/jsr.io/@noble/hashes/1.7.1/src/utils.js +238 -228
  82. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts +9 -0
  83. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.d.ts.map +1 -0
  84. package/script/deps/jsr.io/@std/bytes/1.0.5/_types.js +13 -0
  85. package/script/deps/jsr.io/@std/bytes/{1.0.4 → 1.0.5}/concat.d.ts +3 -1
  86. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.d.ts.map +1 -0
  87. package/script/deps/jsr.io/@std/bytes/1.0.5/concat.js +45 -0
  88. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts +9 -0
  89. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.d.ts.map +1 -0
  90. package/script/deps/jsr.io/@std/encoding/1.0.7/_types.js +13 -0
  91. package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts.map +1 -1
  92. package/script/deps/jsr.io/@std/encoding/1.0.7/_validate_binary_like.js +39 -0
  93. package/{esm/deps/jsr.io/@std/encoding/1.0.6 → script/deps/jsr.io/@std/encoding/1.0.7}/hex.d.ts +3 -1
  94. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.d.ts.map +1 -0
  95. package/script/deps/jsr.io/@std/encoding/1.0.7/hex.js +123 -0
  96. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts +9 -0
  97. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.d.ts.map +1 -0
  98. package/script/deps/jsr.io/@std/msgpack/1.0.3/_types.js +13 -0
  99. package/script/deps/jsr.io/@std/msgpack/{1.0.2 → 1.0.3}/encode.d.ts +3 -1
  100. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.d.ts.map +1 -0
  101. package/script/deps/jsr.io/@std/msgpack/1.0.3/encode.js +250 -0
  102. package/script/mod.d.ts +1 -0
  103. package/script/mod.d.ts.map +1 -1
  104. package/script/mod.js +23 -13
  105. package/script/src/clients/event.d.ts +57 -16
  106. package/script/src/clients/event.d.ts.map +1 -1
  107. package/script/src/clients/event.js +551 -485
  108. package/script/src/clients/public.d.ts +241 -49
  109. package/script/src/clients/public.d.ts.map +1 -1
  110. package/script/src/clients/public.js +914 -708
  111. package/script/src/clients/wallet.d.ts +120 -51
  112. package/script/src/clients/wallet.d.ts.map +1 -1
  113. package/script/src/clients/wallet.js +1121 -977
  114. package/script/src/transports/base.js +25 -15
  115. package/script/src/transports/http/http_transport.js +174 -164
  116. package/script/src/transports/websocket/hyperliquid_event_target.d.ts +27 -21
  117. package/script/src/transports/websocket/hyperliquid_event_target.d.ts.map +1 -1
  118. package/script/src/transports/websocket/hyperliquid_event_target.js +80 -33
  119. package/script/src/transports/websocket/reconnecting_websocket.js +364 -354
  120. package/script/src/transports/websocket/websocket_request_dispatcher.js +196 -186
  121. package/script/src/transports/websocket/websocket_transport.d.ts +7 -10
  122. package/script/src/transports/websocket/websocket_transport.d.ts.map +1 -1
  123. package/script/src/transports/websocket/websocket_transport.js +236 -220
  124. package/script/src/types/common.js +12 -2
  125. package/script/src/types/exchange/common.d.ts +6 -6
  126. package/script/src/types/exchange/common.d.ts.map +1 -1
  127. package/script/src/types/exchange/common.js +12 -2
  128. package/script/src/types/exchange/requests.d.ts +131 -64
  129. package/script/src/types/exchange/requests.d.ts.map +1 -1
  130. package/script/src/types/exchange/requests.js +12 -2
  131. package/script/src/types/exchange/responses.d.ts +7 -7
  132. package/script/src/types/exchange/responses.d.ts.map +1 -1
  133. package/script/src/types/exchange/responses.js +12 -2
  134. package/script/src/types/explorer/common.d.ts +15 -15
  135. package/script/src/types/explorer/common.d.ts.map +1 -1
  136. package/script/src/types/explorer/common.js +12 -2
  137. package/script/src/types/explorer/requests.d.ts +19 -6
  138. package/script/src/types/explorer/requests.d.ts.map +1 -1
  139. package/script/src/types/explorer/requests.js +12 -2
  140. package/script/src/types/explorer/responses.d.ts +9 -2
  141. package/script/src/types/explorer/responses.d.ts.map +1 -1
  142. package/script/src/types/explorer/responses.js +12 -2
  143. package/script/src/types/info/accounts.d.ts +189 -124
  144. package/script/src/types/info/accounts.d.ts.map +1 -1
  145. package/script/src/types/info/accounts.js +12 -2
  146. package/script/src/types/info/assets.d.ts +82 -78
  147. package/script/src/types/info/assets.d.ts.map +1 -1
  148. package/script/src/types/info/assets.js +12 -2
  149. package/script/src/types/info/delegations.d.ts +117 -0
  150. package/script/src/types/info/delegations.d.ts.map +1 -0
  151. package/script/src/types/info/delegations.js +12 -0
  152. package/script/src/types/info/orders.d.ts +67 -67
  153. package/script/src/types/info/orders.d.ts.map +1 -1
  154. package/script/src/types/info/orders.js +12 -2
  155. package/script/src/types/info/requests.d.ts +128 -63
  156. package/script/src/types/info/requests.d.ts.map +1 -1
  157. package/script/src/types/info/requests.js +12 -2
  158. package/script/src/types/info/vaults.d.ts +41 -84
  159. package/script/src/types/info/vaults.d.ts.map +1 -1
  160. package/script/src/types/info/vaults.js +12 -2
  161. package/script/src/types/subscriptions/common.d.ts +3 -0
  162. package/script/src/types/subscriptions/common.d.ts.map +1 -1
  163. package/script/src/types/subscriptions/common.js +12 -2
  164. package/script/src/types/subscriptions/requests.d.ts +10 -0
  165. package/script/src/types/subscriptions/requests.d.ts.map +1 -1
  166. package/script/src/types/subscriptions/requests.js +12 -2
  167. package/script/src/utils/key_sort.d.ts +2 -2
  168. package/script/src/utils/key_sort.d.ts.map +1 -1
  169. package/script/src/utils/key_sort.js +133 -123
  170. package/script/src/utils/signing.d.ts +1 -1
  171. package/script/src/utils/signing.js +172 -162
  172. package/esm/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  173. package/esm/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  174. package/esm/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  175. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.d.ts.map +0 -1
  176. package/script/deps/jsr.io/@std/bytes/1.0.4/concat.js +0 -35
  177. package/script/deps/jsr.io/@std/encoding/1.0.6/_validate_binary_like.js +0 -29
  178. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.d.ts.map +0 -1
  179. package/script/deps/jsr.io/@std/encoding/1.0.6/hex.js +0 -113
  180. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.d.ts.map +0 -1
  181. package/script/deps/jsr.io/@std/msgpack/1.0.2/encode.js +0 -240
  182. /package/esm/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
  183. /package/script/deps/jsr.io/@std/encoding/{1.0.6 → 1.0.7}/_validate_binary_like.d.ts +0 -0
@@ -1,206 +1,216 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WebSocketRequestDispatcher = exports.WebSocketRequestError = void 0;
4
- const base_js_1 = require("../base.js");
5
- /**
6
- * Error thrown when a WebSocket request fails:
7
- * - When the WebSocket connection is closed
8
- * - When the server responds with an error message
9
- */
10
- class WebSocketRequestError extends base_js_1.TransportError {
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", "../base.js"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.WebSocketRequestDispatcher = exports.WebSocketRequestError = void 0;
13
+ const base_js_1 = require("../base.js");
11
14
  /**
12
- * Creates a new WebSocket request error.
13
- * @param message - The error message.
15
+ * Error thrown when a WebSocket request fails:
16
+ * - When the WebSocket connection is closed
17
+ * - When the server responds with an error message
14
18
  */
15
- constructor(message) {
16
- super(message);
17
- this.name = "WebSocketRequestError";
19
+ class WebSocketRequestError extends base_js_1.TransportError {
20
+ /**
21
+ * Creates a new WebSocket request error.
22
+ * @param message - The error message.
23
+ */
24
+ constructor(message) {
25
+ super(message);
26
+ this.name = "WebSocketRequestError";
27
+ }
18
28
  }
19
- }
20
- exports.WebSocketRequestError = WebSocketRequestError;
21
- /**
22
- * Manages WebSocket requests to the Hyperliquid API.
23
- * Handles request creation, sending, and mapping responses to their corresponding requests.
24
- */
25
- class WebSocketRequestDispatcher {
29
+ exports.WebSocketRequestError = WebSocketRequestError;
26
30
  /**
27
- * Creates a new WebSocket request dispatcher.
28
- * @param socket - WebSocket connection instance for sending requests to the Hyperliquid WebSocket API
29
- * @param hlEvents - Used to recognize Hyperliquid responses and match them with sent requests
31
+ * Manages WebSocket requests to the Hyperliquid API.
32
+ * Handles request creation, sending, and mapping responses to their corresponding requests.
30
33
  */
31
- constructor(socket, hlEvents) {
32
- Object.defineProperty(this, "socket", {
33
- enumerable: true,
34
- configurable: true,
35
- writable: true,
36
- value: socket
37
- });
38
- /** Last used request ID */
39
- Object.defineProperty(this, "lastId", {
40
- enumerable: true,
41
- configurable: true,
42
- writable: true,
43
- value: 0
44
- });
45
- /** Map of pending requests waiting for responses */
46
- Object.defineProperty(this, "pending", {
47
- enumerable: true,
48
- configurable: true,
49
- writable: true,
50
- value: new Map()
51
- });
52
- // Monitor responses and match the pending request
53
- hlEvents.addEventListener("subscriptionResponse", (event) => {
54
- // Use a stringified request as an id
55
- const id = WebSocketRequestDispatcher.requestToId(event.detail.subscription);
56
- this.resolve(id, event.detail);
57
- });
58
- hlEvents.addEventListener("post", (event) => {
59
- const data = event.detail.response.type === "info"
60
- ? event.detail.response.payload.data
61
- : event.detail.response.payload;
62
- this.resolve(event.detail.id, data);
63
- });
64
- hlEvents.addEventListener("error", (event) => {
65
- try {
66
- // Error event doesn't have an id, use original request to match
67
- const request = event.detail.match(/{.*}/)?.[0];
68
- if (request) {
69
- const parsedRequest = JSON.parse(request);
70
- if ("id" in parsedRequest && typeof parsedRequest.id === "number") {
71
- // If a post request was sent, it is possible to get the id from the request
72
- this.reject(parsedRequest.id, new WebSocketRequestError(`Cannot process WebSocket request: ${event.detail}`));
73
- }
74
- else if ("subscription" in parsedRequest &&
75
- typeof parsedRequest.subscription === "object" &&
76
- parsedRequest.subscription !== null) {
77
- // If a subscription/unsubscribe request was sent, use the request as an id
78
- const id = WebSocketRequestDispatcher.requestToId(parsedRequest.subscription);
79
- this.reject(id, new WebSocketRequestError(`Cannot process subscription request: ${event.detail}`));
80
- }
81
- else {
82
- // If the request is not recognized, use the parsed request as an id
83
- const id = WebSocketRequestDispatcher.requestToId(parsedRequest);
84
- this.reject(id, new WebSocketRequestError(`Cannot process unrecognized request: ${event.detail}`));
34
+ class WebSocketRequestDispatcher {
35
+ /**
36
+ * Creates a new WebSocket request dispatcher.
37
+ * @param socket - WebSocket connection instance for sending requests to the Hyperliquid WebSocket API
38
+ * @param hlEvents - Used to recognize Hyperliquid responses and match them with sent requests
39
+ */
40
+ constructor(socket, hlEvents) {
41
+ Object.defineProperty(this, "socket", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: socket
46
+ });
47
+ /** Last used request ID */
48
+ Object.defineProperty(this, "lastId", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: 0
53
+ });
54
+ /** Map of pending requests waiting for responses */
55
+ Object.defineProperty(this, "pending", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: new Map()
60
+ });
61
+ // Monitor responses and match the pending request
62
+ hlEvents.addEventListener("subscriptionResponse", (event) => {
63
+ // Use a stringified request as an id
64
+ const id = WebSocketRequestDispatcher.requestToId(event.detail.subscription);
65
+ this.resolve(id, event.detail);
66
+ });
67
+ hlEvents.addEventListener("post", (event) => {
68
+ const data = event.detail.response.type === "info"
69
+ ? event.detail.response.payload.data
70
+ : event.detail.response.payload;
71
+ this.resolve(event.detail.id, data);
72
+ });
73
+ hlEvents.addEventListener("error", (event) => {
74
+ try {
75
+ // Error event doesn't have an id, use original request to match
76
+ const request = event.detail.match(/{.*}/)?.[0];
77
+ if (request) {
78
+ const parsedRequest = JSON.parse(request);
79
+ if ("id" in parsedRequest && typeof parsedRequest.id === "number") {
80
+ // If a post request was sent, it is possible to get the id from the request
81
+ this.reject(parsedRequest.id, new WebSocketRequestError(`Cannot complete WebSocket request: ${event.detail}`));
82
+ }
83
+ else if ("subscription" in parsedRequest &&
84
+ typeof parsedRequest.subscription === "object" &&
85
+ parsedRequest.subscription !== null) {
86
+ // If a subscription/unsubscribe request was sent, use the request as an id
87
+ const id = WebSocketRequestDispatcher.requestToId(parsedRequest.subscription);
88
+ this.reject(id, new WebSocketRequestError(`Cannot complete WebSocket request: ${event.detail}`));
89
+ }
90
+ else {
91
+ // If the request is not recognized, use the parsed request as an id
92
+ const id = WebSocketRequestDispatcher.requestToId(parsedRequest);
93
+ this.reject(id, new WebSocketRequestError(`Cannot complete WebSocket request: ${event.detail}`));
94
+ }
85
95
  }
86
96
  }
97
+ catch {
98
+ // Ignore JSON parsing errors
99
+ }
100
+ });
101
+ // Throws all pending requests if the connection is dropped
102
+ socket.addEventListener("close", () => {
103
+ this.pending.forEach(({ reject }) => {
104
+ reject(new WebSocketRequestError("Cannot complete WebSocket request: connection is closed"));
105
+ });
106
+ this.pending.clear();
107
+ });
108
+ }
109
+ /** Gets the next request ID */
110
+ get nextId() {
111
+ return ++this.lastId;
112
+ }
113
+ /**
114
+ * Sends a request to the Hyperliquid API.
115
+ * @param method - The method of websocket request.
116
+ * @param payload - The payload to send with the request.
117
+ * @param signal - An optional abort signal.
118
+ * @returns A promise that resolves with the parsed JSON response body.
119
+ */
120
+ request(method, payload, signal) {
121
+ signal?.throwIfAborted();
122
+ // Create a request object
123
+ let id;
124
+ let request;
125
+ if (method === "post") {
126
+ id = this.nextId;
127
+ request = { method, id, request: payload };
87
128
  }
88
- catch {
89
- // Ignore JSON parsing errors
129
+ else {
130
+ id = WebSocketRequestDispatcher.requestToId(payload);
131
+ request = { method, subscription: payload };
90
132
  }
91
- });
92
- // Throws all pending requests if the connection is dropped
93
- socket.addEventListener("close", () => {
94
- this.pending.forEach(({ reject }) => {
95
- reject(new WebSocketRequestError("Cannot complete WebSocket request: connection is closed"));
133
+ // Send the request
134
+ this.socket.send(JSON.stringify(request));
135
+ // Wait for a response
136
+ let onAbort;
137
+ return new Promise((resolve, reject) => {
138
+ // Add an abort listener
139
+ onAbort = () => reject(signal?.reason);
140
+ signal?.addEventListener("abort", onAbort, { once: true });
141
+ // Add the promise to the pending list
142
+ this.pending.set(id, { resolve, reject });
143
+ }).finally(() => {
144
+ // Remove the abort listener when the promise is settled
145
+ signal?.removeEventListener("abort", onAbort);
96
146
  });
97
- this.pending.clear();
98
- });
99
- }
100
- /** Gets the next request ID */
101
- get nextId() {
102
- return ++this.lastId;
147
+ }
148
+ /**
149
+ * Resolves a pending request.
150
+ * @param id - A request ID or a stringified request.
151
+ * @param value - A resolution value.
152
+ */
153
+ resolve(id, value) {
154
+ this.pending.get(id)?.resolve(value);
155
+ this.pending.delete(id);
156
+ }
157
+ /**
158
+ * Rejects a pending request.
159
+ * @param id - A request ID or a stringified request.
160
+ * @param reason - A rejection reason.
161
+ */
162
+ reject(id, reason) {
163
+ this.pending.get(id)?.reject(reason);
164
+ this.pending.delete(id);
165
+ }
166
+ /**
167
+ * Normalizes a request object to an ID.
168
+ * @param value - A request object.
169
+ * @returns A stringified request.
170
+ */
171
+ static requestToId(value) {
172
+ const lowerHex = deepLowerHex(value);
173
+ const sorted = deepSortKeys(lowerHex);
174
+ return JSON.stringify(sorted);
175
+ }
103
176
  }
177
+ exports.WebSocketRequestDispatcher = WebSocketRequestDispatcher;
104
178
  /**
105
- * Sends a request to the Hyperliquid API.
106
- * @param method - The method of websocket request.
107
- * @param payload - The payload to send with the request.
108
- * @param signal - An optional abort signal.
109
- * @returns A promise that resolves with the parsed JSON response body.
179
+ * Convert all hexadecimal strings to lowercase in an object/array.
180
+ * @param obj - The object/array to convert hexadecimal strings to lowercase.
181
+ * @returns A new object/array with hexadecimal strings converted to lowercase.
110
182
  */
111
- request(method, payload, signal) {
112
- signal?.throwIfAborted();
113
- // Create a request object
114
- let id;
115
- let request;
116
- if (method === "post") {
117
- id = this.nextId;
118
- request = { method, id, request: payload };
183
+ function deepLowerHex(obj) {
184
+ if (Array.isArray(obj)) {
185
+ return obj.map(deepLowerHex);
186
+ }
187
+ else if (obj && typeof obj === "object") {
188
+ return Object.entries(obj).reduce((acc, [key, val]) => ({
189
+ ...acc,
190
+ [key]: deepLowerHex(val),
191
+ }), {});
192
+ }
193
+ else if (typeof obj === "string" && /^0x[0-9A-Fa-f]+$/.test(obj)) {
194
+ return obj.toLowerCase();
119
195
  }
120
196
  else {
121
- id = WebSocketRequestDispatcher.requestToId(payload);
122
- request = { method, subscription: payload };
197
+ return obj;
123
198
  }
124
- // Send the request
125
- this.socket.send(JSON.stringify(request));
126
- // Wait for a response
127
- let onAbort;
128
- return new Promise((resolve, reject) => {
129
- // Add an abort listener
130
- onAbort = () => reject(signal?.reason);
131
- signal?.addEventListener("abort", onAbort, { once: true });
132
- // Add the promise to the pending list
133
- this.pending.set(id, { resolve, reject });
134
- }).finally(() => {
135
- // Remove the abort listener when the promise is settled
136
- signal?.removeEventListener("abort", onAbort);
137
- });
138
- }
139
- /**
140
- * Resolves a pending request.
141
- * @param id - A request ID or a stringified request.
142
- * @param value - A resolution value.
143
- */
144
- resolve(id, value) {
145
- this.pending.get(id)?.resolve(value);
146
- this.pending.delete(id);
147
- }
148
- /**
149
- * Rejects a pending request.
150
- * @param id - A request ID or a stringified request.
151
- * @param reason - A rejection reason.
152
- */
153
- reject(id, reason) {
154
- this.pending.get(id)?.reject(reason);
155
- this.pending.delete(id);
156
199
  }
157
200
  /**
158
- * Normalizes a request object to an ID.
159
- * @param value - A request object.
160
- * @returns A stringified request.
201
+ * Deeply sort the keys of an object.
202
+ * @param obj - An object to sort the keys of.
203
+ * @returns A new object with sorted keys.
161
204
  */
162
- static requestToId(value) {
163
- const lowerHex = deepLowerHex(value);
164
- const sorted = deepSortKeys(lowerHex);
165
- return JSON.stringify(sorted);
166
- }
167
- }
168
- exports.WebSocketRequestDispatcher = WebSocketRequestDispatcher;
169
- /**
170
- * Convert all hexadecimal strings to lowercase in an object/array.
171
- * @param obj - The object/array to convert hexadecimal strings to lowercase.
172
- * @returns A new object/array with hexadecimal strings converted to lowercase.
173
- */
174
- function deepLowerHex(obj) {
175
- if (Array.isArray(obj)) {
176
- return obj.map(deepLowerHex);
177
- }
178
- else if (obj && typeof obj === "object") {
179
- return Object.entries(obj).reduce((acc, [key, val]) => ({
180
- ...acc,
181
- [key]: deepLowerHex(val),
182
- }), {});
183
- }
184
- else if (typeof obj === "string" && /^0x[0-9A-Fa-f]+$/.test(obj)) {
185
- return obj.toLowerCase();
186
- }
187
- else {
188
- return obj;
189
- }
190
- }
191
- /**
192
- * Deeply sort the keys of an object.
193
- * @param obj - An object to sort the keys of.
194
- * @returns A new object with sorted keys.
195
- */
196
- function deepSortKeys(obj) {
197
- if (obj === null || typeof obj !== "object") {
198
- return obj;
199
- }
200
- if (Array.isArray(obj)) {
201
- return obj.map(deepSortKeys);
205
+ function deepSortKeys(obj) {
206
+ if (obj === null || typeof obj !== "object") {
207
+ return obj;
208
+ }
209
+ if (Array.isArray(obj)) {
210
+ return obj.map(deepSortKeys);
211
+ }
212
+ return Object.fromEntries(Object.entries(obj)
213
+ .sort(([a], [b]) => a.localeCompare(b))
214
+ .map(([k, v]) => [k, deepSortKeys(v)]));
202
215
  }
203
- return Object.fromEntries(Object.entries(obj)
204
- .sort(([a], [b]) => a.localeCompare(b))
205
- .map(([k, v]) => [k, deepSortKeys(v)]));
206
- }
216
+ });
@@ -48,12 +48,15 @@ export declare class WebSocketTransport implements IRequestTransport, ISubscript
48
48
  protected _hlEvents: HyperliquidEventTarget;
49
49
  /**
50
50
  * Map of active subscriptions.
51
- * - Key: Unique subscription identifier (stringified payload)
52
- * - Value: Subscription info containing payload and listener mappings
51
+ * - Key: Unique subscription identifier based on payload
52
+ * - Value: Subscription info containing the subscription request promise
53
+ * and a map of listeners to their metadata (channel + unsubscribe function).
53
54
  */
54
55
  protected _subscriptions: Map<string, {
55
- channel: string;
56
- listeners: Map<(data: CustomEvent) => void, () => Promise<void>>;
56
+ listeners: Map<(data: CustomEvent) => void, {
57
+ channel: string;
58
+ unsubscribe: (signal?: AbortSignal) => Promise<void>;
59
+ }>;
57
60
  requestPromise: Promise<unknown>;
58
61
  }>;
59
62
  /**
@@ -107,11 +110,5 @@ export declare class WebSocketTransport implements IRequestTransport, ISubscript
107
110
  * @returns A promise that resolves when the connection is fully closed.
108
111
  */
109
112
  close(signal?: AbortSignal): Promise<void>;
110
- /**
111
- * Combines timeout and user-provided signal.
112
- * @param signal - A user-provided signal.
113
- * @returns An AbortSignal that triggers when either the timeout or user-provided signal aborts.
114
- */
115
- protected combineSignals(signal?: AbortSignal): AbortSignal;
116
113
  }
117
114
  //# sourceMappingURL=websocket_transport.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"websocket_transport.d.ts","sourceRoot":"","sources":["../../../../src/src/transports/websocket/websocket_transport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,KAAK,4BAA4B,EAAE,MAAM,6BAA6B,CAAC;AACvG,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,0BAA0B,EAAE,MAAM,mCAAmC,CAAC;AAC/E,OAAO,KAAK,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1F;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACtC;;;;;OAKG;IACH,GAAG,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;IAEnB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAExB;;;OAGG;IACH,SAAS,CAAC,EAAE;QACR;;;;WAIG;QACH,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;KAC5B,CAAC;IAEF;;OAEG;IACH,SAAS,CAAC,EAAE,4BAA4B,CAAC;CAC5C;AAED;;GAEG;AACH,qBAAa,kBAAmB,YAAW,iBAAiB,EAAE,sBAAsB;IAChF,qDAAqD;IACrD,SAAS,CAAC,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;IAE9C,iDAAiD;IACjD,SAAS,CAAC,YAAY,EAAE,0BAA0B,CAAC;IAEnD,6CAA6C;IAC7C,SAAS,CAAC,SAAS,EAAE,sBAAsB,CAAC;IAE5C;;;;OAIG;IACH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