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