@polymarket/clob-client-v2 1.0.3 → 1.0.5
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/dist/index.cjs +51 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -9
- package/dist/index.d.ts +33 -9
- package/dist/index.js +51 -32
- package/dist/index.js.map +1 -1
- package/package.json +4 -1
package/dist/index.cjs
CHANGED
|
@@ -257,20 +257,49 @@ var buildClobEip712Signature = async (signer, chainId, timestamp, nonce, address
|
|
|
257
257
|
};
|
|
258
258
|
|
|
259
259
|
// src/signing/hmac.ts
|
|
260
|
-
var
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
260
|
+
var decodeBase64Secret = (secret) => {
|
|
261
|
+
const normalized = secret.replace(/-/g, "+").replace(/_/g, "/");
|
|
262
|
+
const padded = normalized + "=".repeat((4 - normalized.length % 4) % 4);
|
|
263
|
+
const binary = atob(padded);
|
|
264
|
+
const buffer = new ArrayBuffer(binary.length);
|
|
265
|
+
const bytes = new Uint8Array(buffer);
|
|
266
|
+
for (let i = 0; i < binary.length; i++) {
|
|
267
|
+
bytes[i] = binary.charCodeAt(i);
|
|
268
|
+
}
|
|
269
|
+
return bytes;
|
|
270
|
+
};
|
|
271
|
+
var buildPolyHmacSignature = async (secret, timestamp, method, requestPath, body) => {
|
|
272
|
+
if (!secret) {
|
|
273
|
+
throw new Error("buildPolyHmacSignature: secret is empty");
|
|
274
|
+
}
|
|
275
|
+
if (!globalThis.crypto?.subtle) {
|
|
276
|
+
throw new Error(
|
|
277
|
+
"buildPolyHmacSignature: globalThis.crypto.subtle is unavailable. Requires Node >=20 or a secure browser context (HTTPS or localhost)."
|
|
278
|
+
);
|
|
279
|
+
}
|
|
265
280
|
let message = timestamp + method + requestPath;
|
|
266
281
|
if (body !== void 0) {
|
|
267
282
|
message += body;
|
|
268
283
|
}
|
|
269
|
-
const
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
284
|
+
const keyBytes = decodeBase64Secret(secret);
|
|
285
|
+
const key = await globalThis.crypto.subtle.importKey(
|
|
286
|
+
"raw",
|
|
287
|
+
keyBytes,
|
|
288
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
289
|
+
false,
|
|
290
|
+
["sign"]
|
|
291
|
+
);
|
|
292
|
+
const sigBuffer = await globalThis.crypto.subtle.sign(
|
|
293
|
+
"HMAC",
|
|
294
|
+
key,
|
|
295
|
+
new TextEncoder().encode(message)
|
|
296
|
+
);
|
|
297
|
+
const sigBytes = new Uint8Array(sigBuffer);
|
|
298
|
+
let binary = "";
|
|
299
|
+
for (let i = 0; i < sigBytes.length; i++) {
|
|
300
|
+
binary += String.fromCharCode(sigBytes[i]);
|
|
301
|
+
}
|
|
302
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_");
|
|
274
303
|
};
|
|
275
304
|
|
|
276
305
|
// src/headers/index.ts
|
|
@@ -299,7 +328,7 @@ var createL2Headers = async (signer, creds, l2HeaderArgs, timestamp) => {
|
|
|
299
328
|
ts = timestamp;
|
|
300
329
|
}
|
|
301
330
|
const address = await getSignerAddress(signer);
|
|
302
|
-
const sig = buildPolyHmacSignature(
|
|
331
|
+
const sig = await buildPolyHmacSignature(
|
|
303
332
|
creds.secret,
|
|
304
333
|
ts,
|
|
305
334
|
l2HeaderArgs.method,
|
|
@@ -351,7 +380,6 @@ var post = async (endpoint, options, retryOnError = false) => {
|
|
|
351
380
|
return resp.data;
|
|
352
381
|
} catch (err) {
|
|
353
382
|
if (retryOnError && isTransientAxiosError(err)) {
|
|
354
|
-
console.log("[CLOB Client-v2] transient error, retrying once after 30 ms");
|
|
355
383
|
await sleep(30);
|
|
356
384
|
try {
|
|
357
385
|
const resp = await request(
|
|
@@ -394,15 +422,6 @@ var del = async (endpoint, options) => {
|
|
|
394
422
|
var errorHandling = (err) => {
|
|
395
423
|
if (import_axios.default.isAxiosError(err)) {
|
|
396
424
|
if (err.response) {
|
|
397
|
-
console.error(
|
|
398
|
-
"[CLOB Client] request error",
|
|
399
|
-
JSON.stringify({
|
|
400
|
-
status: err.response?.status,
|
|
401
|
-
statusText: err.response?.statusText,
|
|
402
|
-
data: err.response?.data,
|
|
403
|
-
config: err.response?.config
|
|
404
|
-
})
|
|
405
|
-
);
|
|
406
425
|
if (err.response?.data) {
|
|
407
426
|
if (typeof err.response?.data === "string" || err.response?.data instanceof String) {
|
|
408
427
|
return { error: err.response?.data, status: err.response?.status };
|
|
@@ -414,16 +433,9 @@ var errorHandling = (err) => {
|
|
|
414
433
|
}
|
|
415
434
|
}
|
|
416
435
|
if (err.message) {
|
|
417
|
-
console.error(
|
|
418
|
-
"[CLOB Client] request error",
|
|
419
|
-
JSON.stringify({
|
|
420
|
-
error: err.message
|
|
421
|
-
})
|
|
422
|
-
);
|
|
423
436
|
return { error: err.message };
|
|
424
437
|
}
|
|
425
438
|
}
|
|
426
|
-
console.error("[CLOB Client] request error", err);
|
|
427
439
|
return { error: err };
|
|
428
440
|
};
|
|
429
441
|
var isTransientAxiosError = (err) => {
|
|
@@ -1052,7 +1064,6 @@ function isV2Order(order) {
|
|
|
1052
1064
|
}
|
|
1053
1065
|
|
|
1054
1066
|
// src/utilities.ts
|
|
1055
|
-
var import_node_crypto2 = require("crypto");
|
|
1056
1067
|
var roundNormal = (num, decimals) => {
|
|
1057
1068
|
if (decimalPlaces(num) <= decimals) {
|
|
1058
1069
|
return num;
|
|
@@ -1081,9 +1092,17 @@ var decimalPlaces = (num) => {
|
|
|
1081
1092
|
}
|
|
1082
1093
|
return arr[1].length;
|
|
1083
1094
|
};
|
|
1084
|
-
var generateOrderBookSummaryHash = (orderbook) => {
|
|
1095
|
+
var generateOrderBookSummaryHash = async (orderbook) => {
|
|
1096
|
+
if (!globalThis.crypto?.subtle) {
|
|
1097
|
+
throw new Error(
|
|
1098
|
+
"generateOrderBookSummaryHash: globalThis.crypto.subtle is unavailable. Requires Node >=20 or a secure browser context (HTTPS or localhost)."
|
|
1099
|
+
);
|
|
1100
|
+
}
|
|
1085
1101
|
orderbook.hash = "";
|
|
1086
|
-
const
|
|
1102
|
+
const data = new TextEncoder().encode(JSON.stringify(orderbook));
|
|
1103
|
+
const hashBuffer = await globalThis.crypto.subtle.digest("SHA-1", data);
|
|
1104
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
1105
|
+
const hash = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
1087
1106
|
orderbook.hash = hash;
|
|
1088
1107
|
return hash;
|
|
1089
1108
|
};
|
|
@@ -1591,7 +1610,7 @@ var ClobClient = class {
|
|
|
1591
1610
|
* @param orderbook
|
|
1592
1611
|
* @returns
|
|
1593
1612
|
*/
|
|
1594
|
-
getOrderBookHash(orderbook) {
|
|
1613
|
+
async getOrderBookHash(orderbook) {
|
|
1595
1614
|
return generateOrderBookSummaryHash(orderbook);
|
|
1596
1615
|
}
|
|
1597
1616
|
async getMidpoint(tokenID) {
|