@liberfi.io/react-predict 0.1.20 → 0.1.22
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.d.mts +170 -21
- package/dist/index.d.ts +170 -21
- package/dist/index.js +478 -62
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +462 -63
- package/dist/index.mjs.map +1 -1
- package/dist/{server-CiGfmztS.d.mts → server-lP60FIU5.d.mts} +295 -27
- package/dist/{server-CiGfmztS.d.ts → server-lP60FIU5.d.ts} +295 -27
- package/dist/server.d.mts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +209 -40
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +208 -42
- package/dist/server.mjs.map +1 -1
- package/package.json +3 -3
package/dist/server.mjs
CHANGED
|
@@ -72,6 +72,14 @@ function matchQueryKey(id) {
|
|
|
72
72
|
async function fetchMatchesPage(client, params) {
|
|
73
73
|
return client.listMatches(params);
|
|
74
74
|
}
|
|
75
|
+
|
|
76
|
+
// src/hooks/predict/matchMarkets.params.ts
|
|
77
|
+
function matchMarketsQueryKey(params) {
|
|
78
|
+
return ["predict", "matchMarkets", params];
|
|
79
|
+
}
|
|
80
|
+
async function fetchMatchMarketsPage(client, params) {
|
|
81
|
+
return client.listMatchMarkets(params);
|
|
82
|
+
}
|
|
75
83
|
function buildQuery(params) {
|
|
76
84
|
const qs = new URLSearchParams();
|
|
77
85
|
for (const [key, value] of Object.entries(params)) {
|
|
@@ -174,13 +182,19 @@ var PredictClient = class {
|
|
|
174
182
|
// Positions
|
|
175
183
|
// -------------------------------------------------------------------------
|
|
176
184
|
/**
|
|
177
|
-
* Maps to `GET /api/v1/positions
|
|
185
|
+
* Maps to `GET /api/v1/positions`.
|
|
178
186
|
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
187
|
+
* Single-source: `getPositions("addr", "kalshi")`.
|
|
188
|
+
* Multi-wallet: `getPositions({ kalshi_user: "SOLaddr", polymarket_user: "EVMaddr" })`.
|
|
189
|
+
* Legacy agg: `getPositions("addr")` (same address for all providers).
|
|
181
190
|
*/
|
|
182
|
-
async getPositions(
|
|
183
|
-
|
|
191
|
+
async getPositions(userOrWallets, source) {
|
|
192
|
+
let query;
|
|
193
|
+
if (typeof userOrWallets === "string") {
|
|
194
|
+
query = buildQuery({ source, user: userOrWallets });
|
|
195
|
+
} else {
|
|
196
|
+
query = buildQuery(userOrWallets);
|
|
197
|
+
}
|
|
184
198
|
const url = `${this.endpoint}/api/v1/positions${query}`;
|
|
185
199
|
return await httpGet(url);
|
|
186
200
|
}
|
|
@@ -203,23 +217,46 @@ var PredictClient = class {
|
|
|
203
217
|
// -------------------------------------------------------------------------
|
|
204
218
|
// Orders
|
|
205
219
|
// -------------------------------------------------------------------------
|
|
206
|
-
/**
|
|
207
|
-
|
|
220
|
+
/**
|
|
221
|
+
* Maps to `GET /api/v1/orders?source=...&wallet_address=...`.
|
|
222
|
+
*
|
|
223
|
+
* @param params - Query parameters (source, wallet_address, etc.).
|
|
224
|
+
* @param headers - Optional extra headers (e.g. Polymarket POLY_* HMAC headers).
|
|
225
|
+
*/
|
|
226
|
+
async listOrders(params, headers) {
|
|
208
227
|
const query = buildQuery(params);
|
|
209
228
|
const url = `${this.endpoint}/api/v1/orders${query}`;
|
|
210
|
-
return await httpGet(
|
|
229
|
+
return await httpGet(
|
|
230
|
+
url,
|
|
231
|
+
headers ? { headers } : void 0
|
|
232
|
+
);
|
|
211
233
|
}
|
|
212
|
-
/**
|
|
213
|
-
|
|
234
|
+
/**
|
|
235
|
+
* Maps to `GET /api/v1/orders/:id?source=...`.
|
|
236
|
+
*
|
|
237
|
+
* @param id - Order ID.
|
|
238
|
+
* @param source - Provider source.
|
|
239
|
+
* @param headers - Optional extra headers (e.g. Polymarket POLY_* HMAC headers).
|
|
240
|
+
*/
|
|
241
|
+
async getOrder(id, source, headers) {
|
|
214
242
|
const query = buildQuery({ source });
|
|
215
243
|
const url = `${this.endpoint}/api/v1/orders/${encodeURIComponent(id)}${query}`;
|
|
216
|
-
return await httpGet(url);
|
|
244
|
+
return await httpGet(url, headers ? { headers } : void 0);
|
|
217
245
|
}
|
|
218
|
-
/**
|
|
219
|
-
|
|
246
|
+
/**
|
|
247
|
+
* Maps to `DELETE /api/v1/orders/:id?source=...`.
|
|
248
|
+
*
|
|
249
|
+
* @param id - Order ID.
|
|
250
|
+
* @param source - Provider source.
|
|
251
|
+
* @param headers - Optional extra headers (e.g. Polymarket POLY_* HMAC headers).
|
|
252
|
+
*/
|
|
253
|
+
async cancelOrder(id, source, headers) {
|
|
220
254
|
const query = buildQuery({ source });
|
|
221
255
|
const url = `${this.endpoint}/api/v1/orders/${encodeURIComponent(id)}${query}`;
|
|
222
|
-
return await httpDelete(
|
|
256
|
+
return await httpDelete(
|
|
257
|
+
url,
|
|
258
|
+
headers ? { headers } : void 0
|
|
259
|
+
);
|
|
223
260
|
}
|
|
224
261
|
// -------------------------------------------------------------------------
|
|
225
262
|
// Polymarket trading
|
|
@@ -269,6 +306,30 @@ var PredictClient = class {
|
|
|
269
306
|
return await httpGet(url);
|
|
270
307
|
}
|
|
271
308
|
// -------------------------------------------------------------------------
|
|
309
|
+
// Polymarket wallet setup
|
|
310
|
+
// -------------------------------------------------------------------------
|
|
311
|
+
/**
|
|
312
|
+
* Check Polymarket wallet setup status (Safe deployed + USDC.e approved).
|
|
313
|
+
*
|
|
314
|
+
* Maps to `GET /api/v1/setup/polymarket?wallet_address=...`.
|
|
315
|
+
*/
|
|
316
|
+
async checkPolymarketSetup(walletAddress) {
|
|
317
|
+
const query = buildQuery({ wallet_address: walletAddress });
|
|
318
|
+
const url = `${this.endpoint}/api/v1/setup/polymarket${query}`;
|
|
319
|
+
return await httpGet(url);
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Run Polymarket wallet setup (deploy Safe + approve USDC.e).
|
|
323
|
+
*
|
|
324
|
+
* Maps to `POST /api/v1/setup/polymarket`.
|
|
325
|
+
*/
|
|
326
|
+
async runPolymarketSetup(walletAddress) {
|
|
327
|
+
const url = `${this.endpoint}/api/v1/setup/polymarket`;
|
|
328
|
+
return await httpPost(url, {
|
|
329
|
+
wallet_address: walletAddress
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
// -------------------------------------------------------------------------
|
|
272
333
|
// Cross-platform matches
|
|
273
334
|
// -------------------------------------------------------------------------
|
|
274
335
|
/**
|
|
@@ -290,6 +351,16 @@ var PredictClient = class {
|
|
|
290
351
|
const url = `${this.endpoint}/api/v1/matches/${id}`;
|
|
291
352
|
return await httpGet(url);
|
|
292
353
|
}
|
|
354
|
+
/**
|
|
355
|
+
* List flattened market pairs across match groups (market-level granularity).
|
|
356
|
+
*
|
|
357
|
+
* Maps to `GET /api/v1/matches/markets`.
|
|
358
|
+
*/
|
|
359
|
+
async listMatchMarkets(params) {
|
|
360
|
+
const query = buildQuery(params ?? {});
|
|
361
|
+
const url = `${this.endpoint}/api/v1/matches/markets${query}`;
|
|
362
|
+
return await httpGet(url);
|
|
363
|
+
}
|
|
293
364
|
// -------------------------------------------------------------------------
|
|
294
365
|
// Trades by wallet
|
|
295
366
|
// -------------------------------------------------------------------------
|
|
@@ -299,6 +370,73 @@ var PredictClient = class {
|
|
|
299
370
|
const url = `${this.endpoint}/api/v1/trades${query}`;
|
|
300
371
|
return await httpGet(url);
|
|
301
372
|
}
|
|
373
|
+
// -------------------------------------------------------------------------
|
|
374
|
+
// Withdraw
|
|
375
|
+
// -------------------------------------------------------------------------
|
|
376
|
+
/** Maps to `POST /api/v1/withdraw/build`. */
|
|
377
|
+
async withdrawBuild(body) {
|
|
378
|
+
const url = `${this.endpoint}/api/v1/withdraw/build`;
|
|
379
|
+
return await httpPost(url, body);
|
|
380
|
+
}
|
|
381
|
+
/** Maps to `POST /api/v1/withdraw/submit`. */
|
|
382
|
+
async withdrawSubmit(body) {
|
|
383
|
+
const url = `${this.endpoint}/api/v1/withdraw/submit`;
|
|
384
|
+
return await httpPost(url, body);
|
|
385
|
+
}
|
|
386
|
+
/** Maps to `GET /api/v1/withdraw/status?tx_hash=...&source=...`. */
|
|
387
|
+
async withdrawStatus(txHash, source) {
|
|
388
|
+
const query = buildQuery({ tx_hash: txHash, source });
|
|
389
|
+
const url = `${this.endpoint}/api/v1/withdraw/status${query}`;
|
|
390
|
+
return await httpGet(url);
|
|
391
|
+
}
|
|
392
|
+
// -------------------------------------------------------------------------
|
|
393
|
+
// Deposit (native USDC → USDC.e for Polymarket)
|
|
394
|
+
// -------------------------------------------------------------------------
|
|
395
|
+
/**
|
|
396
|
+
* Build unsigned deposit transactions (approve + Uniswap V3 swap).
|
|
397
|
+
*
|
|
398
|
+
* The server checks the Safe's USDC.e balance and returns only
|
|
399
|
+
* the transactions needed to cover the shortfall.
|
|
400
|
+
*
|
|
401
|
+
* Maps to `POST /api/v1/deposit/polymarket/build`.
|
|
402
|
+
*/
|
|
403
|
+
async depositBuild(body) {
|
|
404
|
+
const url = `${this.endpoint}/api/v1/deposit/polymarket/build`;
|
|
405
|
+
return await httpPost(url, body);
|
|
406
|
+
}
|
|
407
|
+
/** Maps to `POST /api/v1/deposit/polymarket/submit`. */
|
|
408
|
+
async depositSubmit(body) {
|
|
409
|
+
const url = `${this.endpoint}/api/v1/deposit/polymarket/submit`;
|
|
410
|
+
return await httpPost(url, body);
|
|
411
|
+
}
|
|
412
|
+
/** Maps to `GET /api/v1/deposit/polymarket/status?tx_hash=...&source=...`. */
|
|
413
|
+
async depositStatus(txHash, source) {
|
|
414
|
+
const query = buildQuery({ tx_hash: txHash, source });
|
|
415
|
+
const url = `${this.endpoint}/api/v1/deposit/polymarket/status${query}`;
|
|
416
|
+
return await httpGet(url);
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Get multi-chain deposit addresses for a Polymarket Safe wallet via Bridge API.
|
|
420
|
+
*
|
|
421
|
+
* Maps to `GET /api/v1/deposit/polymarket/addresses?safe_address=...`.
|
|
422
|
+
*/
|
|
423
|
+
async getPolymarketDepositAddresses(safeAddress) {
|
|
424
|
+
const query = buildQuery({ safe_address: safeAddress });
|
|
425
|
+
const url = `${this.endpoint}/api/v1/deposit/polymarket/addresses${query}`;
|
|
426
|
+
return await httpGet(url);
|
|
427
|
+
}
|
|
428
|
+
// -------------------------------------------------------------------------
|
|
429
|
+
// Polymarket Relayer Withdraw
|
|
430
|
+
// -------------------------------------------------------------------------
|
|
431
|
+
/**
|
|
432
|
+
* Execute a gasless USDC.e withdrawal from a Polymarket Safe wallet via Relayer.
|
|
433
|
+
*
|
|
434
|
+
* Maps to `POST /api/v1/withdraw/polymarket/execute`.
|
|
435
|
+
*/
|
|
436
|
+
async executePolymarketWithdraw(body) {
|
|
437
|
+
const url = `${this.endpoint}/api/v1/withdraw/polymarket/execute`;
|
|
438
|
+
return await httpPost(url, body);
|
|
439
|
+
}
|
|
302
440
|
};
|
|
303
441
|
function createPredictClient(endpoint) {
|
|
304
442
|
return new PredictClient(endpoint);
|
|
@@ -643,7 +781,9 @@ function encode(str) {
|
|
|
643
781
|
return new TextEncoder().encode(str);
|
|
644
782
|
}
|
|
645
783
|
function base64ToBytes(b64) {
|
|
646
|
-
|
|
784
|
+
let std = b64.replace(/-/g, "+").replace(/_/g, "/");
|
|
785
|
+
while (std.length % 4 !== 0) std += "=";
|
|
786
|
+
const binary = atob(std);
|
|
647
787
|
const bytes = new Uint8Array(binary.length);
|
|
648
788
|
for (let i = 0; i < binary.length; i++) {
|
|
649
789
|
bytes[i] = binary.charCodeAt(i);
|
|
@@ -672,7 +812,8 @@ async function hmacSha256Base64(secretBase64, message) {
|
|
|
672
812
|
cryptoKey,
|
|
673
813
|
msgBytes.buffer
|
|
674
814
|
);
|
|
675
|
-
|
|
815
|
+
const b64 = bytesToBase64(new Uint8Array(signature));
|
|
816
|
+
return b64.replace(/\+/g, "-").replace(/\//g, "_");
|
|
676
817
|
}
|
|
677
818
|
async function buildPolymarketL2Headers(address, input) {
|
|
678
819
|
const timestamp = Math.floor(Date.now() / 1e3).toString();
|
|
@@ -708,27 +849,34 @@ function buildClobAuthMessage(input) {
|
|
|
708
849
|
message: "This message attests that I control the given wallet"
|
|
709
850
|
};
|
|
710
851
|
}
|
|
852
|
+
function buildL1Headers(address, signature, timestamp, nonce) {
|
|
853
|
+
return {
|
|
854
|
+
POLY_ADDRESS: address,
|
|
855
|
+
POLY_SIGNATURE: signature,
|
|
856
|
+
POLY_TIMESTAMP: timestamp,
|
|
857
|
+
POLY_NONCE: String(nonce)
|
|
858
|
+
};
|
|
859
|
+
}
|
|
711
860
|
async function derivePolymarketApiKey(address, signature, timestamp, nonce) {
|
|
712
|
-
const
|
|
861
|
+
const headers = buildL1Headers(address, signature, timestamp, nonce);
|
|
862
|
+
const deriveRes = await fetch(
|
|
713
863
|
`https://clob.polymarket.com/auth/derive-api-key?nonce=${nonce}`,
|
|
714
|
-
{
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
864
|
+
{ method: "GET", headers }
|
|
865
|
+
);
|
|
866
|
+
if (deriveRes.ok) {
|
|
867
|
+
return deriveRes.json();
|
|
868
|
+
}
|
|
869
|
+
const createRes = await fetch("https://clob.polymarket.com/auth/api-key", {
|
|
870
|
+
method: "POST",
|
|
871
|
+
headers
|
|
872
|
+
});
|
|
873
|
+
if (createRes.ok) {
|
|
874
|
+
return createRes.json();
|
|
875
|
+
}
|
|
876
|
+
const text = await createRes.text().catch(() => createRes.statusText);
|
|
877
|
+
throw new Error(
|
|
878
|
+
`Polymarket credential exchange failed (${createRes.status}): ${text}`
|
|
724
879
|
);
|
|
725
|
-
if (!res.ok) {
|
|
726
|
-
const text = await res.text().catch(() => res.statusText);
|
|
727
|
-
throw new Error(
|
|
728
|
-
`Polymarket credential exchange failed (${res.status}): ${text}`
|
|
729
|
-
);
|
|
730
|
-
}
|
|
731
|
-
return res.json();
|
|
732
880
|
}
|
|
733
881
|
|
|
734
882
|
// src/utils/polymarket-order.ts
|
|
@@ -738,7 +886,7 @@ var USDC_ADDRESS = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174";
|
|
|
738
886
|
var POLYGON_CHAIN_ID = 137;
|
|
739
887
|
function buildCtfExchangeDomain(negRisk = false) {
|
|
740
888
|
return {
|
|
741
|
-
name: "
|
|
889
|
+
name: "Polymarket CTF Exchange",
|
|
742
890
|
version: "1",
|
|
743
891
|
chainId: POLYGON_CHAIN_ID,
|
|
744
892
|
verifyingContract: negRisk ? NEG_RISK_CTF_EXCHANGE_ADDRESS : CTF_EXCHANGE_ADDRESS
|
|
@@ -784,6 +932,12 @@ function roundToTick(price, tickSize) {
|
|
|
784
932
|
function toMicroUsdc(amount) {
|
|
785
933
|
return BigInt(Math.round(amount * 1e6));
|
|
786
934
|
}
|
|
935
|
+
function normalizeTokenId(tokenId) {
|
|
936
|
+
if (tokenId.startsWith("0x") || tokenId.startsWith("0X")) {
|
|
937
|
+
return BigInt(tokenId).toString(10);
|
|
938
|
+
}
|
|
939
|
+
return tokenId;
|
|
940
|
+
}
|
|
787
941
|
function buildOrderMessage(input) {
|
|
788
942
|
const side = input.side === "BUY" ? SIDE.BUY : SIDE.SELL;
|
|
789
943
|
const priceStr = roundToTick(input.price, input.tickSize);
|
|
@@ -798,7 +952,7 @@ function buildOrderMessage(input) {
|
|
|
798
952
|
maker,
|
|
799
953
|
signer: input.signerAddress,
|
|
800
954
|
taker: "0x0000000000000000000000000000000000000000",
|
|
801
|
-
tokenId: input.tokenId,
|
|
955
|
+
tokenId: normalizeTokenId(input.tokenId),
|
|
802
956
|
makerAmount,
|
|
803
957
|
takerAmount,
|
|
804
958
|
expiration: String(input.expiration ?? 0),
|
|
@@ -808,15 +962,27 @@ function buildOrderMessage(input) {
|
|
|
808
962
|
signatureType: input.signatureType
|
|
809
963
|
};
|
|
810
964
|
}
|
|
811
|
-
function buildSignedOrder(
|
|
812
|
-
|
|
965
|
+
function buildSignedOrder(orderMessage, signature, orderType) {
|
|
966
|
+
return {
|
|
967
|
+
...orderMessage,
|
|
968
|
+
signature,
|
|
969
|
+
orderType: orderType ?? "GTC"
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
function buildClobPayload(signedOrder, owner) {
|
|
973
|
+
const { orderType: ot, ...order } = signedOrder;
|
|
813
974
|
return {
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
975
|
+
order: {
|
|
976
|
+
...order,
|
|
977
|
+
salt: parseInt(order.salt, 10),
|
|
978
|
+
side: order.side === SIDE.BUY ? "BUY" : "SELL"
|
|
979
|
+
},
|
|
980
|
+
owner,
|
|
981
|
+
orderType: ot ?? "GTC",
|
|
982
|
+
deferExec: false
|
|
817
983
|
};
|
|
818
984
|
}
|
|
819
985
|
|
|
820
|
-
export { CLOB_AUTH_DOMAIN, CLOB_AUTH_TYPES, CTF_EXCHANGE_ADDRESS, CTF_ORDER_TYPES, DEFAULT_PAGE_SIZE, NEG_RISK_CTF_EXCHANGE_ADDRESS, ORDER_TYPE, POLYGON_CHAIN_ID, PredictClient, PredictWsClient, SIDE, USDC_ADDRESS, buildClobAuthMessage, buildCtfExchangeDomain, buildOrderMessage, buildPolymarketL2Headers, buildSignedOrder, createPredictClient, createPredictWsClient, derivePolymarketApiKey, eventQueryKey, fetchEvent, fetchEventsPage, fetchMarket, fetchMatchesPage, hmacSha256Base64, infiniteEventsQueryKey, marketQueryKey, matchQueryKey, matchesQueryKey, resolveEventsParams, resolveTagSlug };
|
|
986
|
+
export { CLOB_AUTH_DOMAIN, CLOB_AUTH_TYPES, CTF_EXCHANGE_ADDRESS, CTF_ORDER_TYPES, DEFAULT_PAGE_SIZE, NEG_RISK_CTF_EXCHANGE_ADDRESS, ORDER_TYPE, POLYGON_CHAIN_ID, PredictClient, PredictWsClient, SIDE, USDC_ADDRESS, buildClobAuthMessage, buildClobPayload, buildCtfExchangeDomain, buildOrderMessage, buildPolymarketL2Headers, buildSignedOrder, createPredictClient, createPredictWsClient, derivePolymarketApiKey, eventQueryKey, fetchEvent, fetchEventsPage, fetchMarket, fetchMatchMarketsPage, fetchMatchesPage, hmacSha256Base64, infiniteEventsQueryKey, marketQueryKey, matchMarketsQueryKey, matchQueryKey, matchesQueryKey, resolveEventsParams, resolveTagSlug };
|
|
821
987
|
//# sourceMappingURL=server.mjs.map
|
|
822
988
|
//# sourceMappingURL=server.mjs.map
|