@nktkas/hyperliquid 0.19.0 → 0.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -134,44 +134,58 @@ export class WebSocketRequestDispatcher {
134
134
  * @returns A stringified request.
135
135
  */
136
136
  static requestToId(value) {
137
- const lowerHex = deepLowerHex(value);
137
+ const lowerHex = containsUppercaseHex(value) ? deepLowerHex(value) : value;
138
138
  const sorted = deepSortKeys(lowerHex);
139
139
  return JSON.stringify(sorted);
140
140
  }
141
141
  }
142
142
  /**
143
- * Convert all hexadecimal strings to lowercase in an object/array.
143
+ * Deeply converts hexadecimal strings in an object/array to lowercase.
144
144
  * @param obj - The object/array to convert hexadecimal strings to lowercase.
145
145
  * @returns A new object/array with hexadecimal strings converted to lowercase.
146
146
  */
147
147
  function deepLowerHex(obj) {
148
+ if (typeof obj === "string") {
149
+ return /^(0X[0-9a-fA-F]*|0x[0-9a-fA-F]*[A-F][0-9a-fA-F]*)$/.test(obj) ? obj.toLowerCase() : obj;
150
+ }
148
151
  if (Array.isArray(obj)) {
149
152
  return obj.map(deepLowerHex);
150
153
  }
151
154
  if (typeof obj === "object" && obj !== null) {
152
- return Object.entries(obj).reduce((acc, [key, val]) => ({
153
- ...acc,
154
- [key]: deepLowerHex(val),
155
- }), {});
156
- }
157
- if (typeof obj === "string" && /^0x[0-9A-Fa-f]+$/.test(obj)) {
158
- return obj.toLowerCase();
155
+ const result = {};
156
+ const entries = Object.entries(obj);
157
+ for (const [key, value] of entries) {
158
+ result[key] = deepLowerHex(value);
159
+ }
160
+ return result;
159
161
  }
160
162
  return obj;
161
163
  }
164
+ /**
165
+ * Check if an object contains uppercase hexadecimal strings.
166
+ * @param obj - The object to check.
167
+ * @returns True if the object contains uppercase hexadecimal strings, false otherwise.
168
+ */
169
+ function containsUppercaseHex(obj) {
170
+ const str = JSON.stringify(obj);
171
+ return /0X[0-9a-fA-F]*|0x[0-9a-fA-F]*[A-F][0-9a-fA-F]*/.test(str);
172
+ }
162
173
  /**
163
174
  * Deeply sort the keys of an object.
164
175
  * @param obj - An object to sort the keys of.
165
176
  * @returns A new object with sorted keys.
166
177
  */
167
178
  function deepSortKeys(obj) {
168
- if (obj === null || typeof obj !== "object") {
179
+ if (typeof obj !== "object" || obj === null) {
169
180
  return obj;
170
181
  }
171
182
  if (Array.isArray(obj)) {
172
183
  return obj.map(deepSortKeys);
173
184
  }
174
- return Object.fromEntries(Object.entries(obj)
175
- .sort(([a], [b]) => a.localeCompare(b))
176
- .map(([k, v]) => [k, deepSortKeys(v)]));
185
+ const result = {};
186
+ const keys = Object.keys(obj).sort();
187
+ for (const key of keys) {
188
+ result[key] = deepSortKeys(obj[key]);
189
+ }
190
+ return result;
177
191
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nktkas/hyperliquid",
3
- "version": "0.19.0",
3
+ "version": "0.19.1",
4
4
  "description": "Unofficial Hyperliquid API SDK for all major JS runtimes, written in TypeScript and provided with tests",
5
5
  "keywords": [
6
6
  "api",
@@ -147,46 +147,60 @@
147
147
  * @returns A stringified request.
148
148
  */
149
149
  static requestToId(value) {
150
- const lowerHex = deepLowerHex(value);
150
+ const lowerHex = containsUppercaseHex(value) ? deepLowerHex(value) : value;
151
151
  const sorted = deepSortKeys(lowerHex);
152
152
  return JSON.stringify(sorted);
153
153
  }
154
154
  }
155
155
  exports.WebSocketRequestDispatcher = WebSocketRequestDispatcher;
156
156
  /**
157
- * Convert all hexadecimal strings to lowercase in an object/array.
157
+ * Deeply converts hexadecimal strings in an object/array to lowercase.
158
158
  * @param obj - The object/array to convert hexadecimal strings to lowercase.
159
159
  * @returns A new object/array with hexadecimal strings converted to lowercase.
160
160
  */
161
161
  function deepLowerHex(obj) {
162
+ if (typeof obj === "string") {
163
+ return /^(0X[0-9a-fA-F]*|0x[0-9a-fA-F]*[A-F][0-9a-fA-F]*)$/.test(obj) ? obj.toLowerCase() : obj;
164
+ }
162
165
  if (Array.isArray(obj)) {
163
166
  return obj.map(deepLowerHex);
164
167
  }
165
168
  if (typeof obj === "object" && obj !== null) {
166
- return Object.entries(obj).reduce((acc, [key, val]) => ({
167
- ...acc,
168
- [key]: deepLowerHex(val),
169
- }), {});
170
- }
171
- if (typeof obj === "string" && /^0x[0-9A-Fa-f]+$/.test(obj)) {
172
- return obj.toLowerCase();
169
+ const result = {};
170
+ const entries = Object.entries(obj);
171
+ for (const [key, value] of entries) {
172
+ result[key] = deepLowerHex(value);
173
+ }
174
+ return result;
173
175
  }
174
176
  return obj;
175
177
  }
178
+ /**
179
+ * Check if an object contains uppercase hexadecimal strings.
180
+ * @param obj - The object to check.
181
+ * @returns True if the object contains uppercase hexadecimal strings, false otherwise.
182
+ */
183
+ function containsUppercaseHex(obj) {
184
+ const str = JSON.stringify(obj);
185
+ return /0X[0-9a-fA-F]*|0x[0-9a-fA-F]*[A-F][0-9a-fA-F]*/.test(str);
186
+ }
176
187
  /**
177
188
  * Deeply sort the keys of an object.
178
189
  * @param obj - An object to sort the keys of.
179
190
  * @returns A new object with sorted keys.
180
191
  */
181
192
  function deepSortKeys(obj) {
182
- if (obj === null || typeof obj !== "object") {
193
+ if (typeof obj !== "object" || obj === null) {
183
194
  return obj;
184
195
  }
185
196
  if (Array.isArray(obj)) {
186
197
  return obj.map(deepSortKeys);
187
198
  }
188
- return Object.fromEntries(Object.entries(obj)
189
- .sort(([a], [b]) => a.localeCompare(b))
190
- .map(([k, v]) => [k, deepSortKeys(v)]));
199
+ const result = {};
200
+ const keys = Object.keys(obj).sort();
201
+ for (const key of keys) {
202
+ result[key] = deepSortKeys(obj[key]);
203
+ }
204
+ return result;
191
205
  }
192
206
  });