@liberfi.io/react-predict 0.3.47 → 0.3.49

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.mjs CHANGED
@@ -519,8 +519,9 @@ var PredictClient = class {
519
519
  // Redeem (resolved market token redemption)
520
520
  // -------------------------------------------------------------------------
521
521
  /**
522
- * Prepare EIP-712 SafeTx typed data for a Polymarket redeem transaction.
523
- * The frontend must sign this data and pass the signature to `redeemPolymarket`.
522
+ * Prepare the signing payload for a Polymarket redeem transaction.
523
+ * The frontend must sign the returned Safe hash or deposit-wallet typed data
524
+ * and pass the signature to `redeemPolymarket`.
524
525
  *
525
526
  * Maps to `POST /api/v1/redeem/polymarket/prepare`.
526
527
  */
@@ -530,7 +531,7 @@ var PredictClient = class {
530
531
  }
531
532
  /**
532
533
  * Redeem tokens from a resolved Polymarket market via gasless Relayer.
533
- * Requires a valid EIP-712 signature from the prepare step.
534
+ * Requires a valid signature for the payload returned by the prepare step.
534
535
  *
535
536
  * Maps to `POST /api/v1/redeem/polymarket`.
536
537
  */
@@ -548,6 +549,13 @@ var DEFAULT_RECONNECT_BASE = 1e3;
548
549
  var DEFAULT_RECONNECT_MAX = 3e4;
549
550
  var DEFAULT_PING_INTERVAL = 2e4;
550
551
  var DEFAULT_PONG_TIMEOUT = 1e4;
552
+ var MAX_SUBSCRIPTION_FRAME_BYTES = 3500;
553
+ function byteLength(str) {
554
+ if (typeof TextEncoder !== "undefined") {
555
+ return new TextEncoder().encode(str).length;
556
+ }
557
+ return str.length;
558
+ }
551
559
  var PredictWsClient = class {
552
560
  ws = null;
553
561
  wsUrl;
@@ -665,7 +673,7 @@ var PredictWsClient = class {
665
673
  const set = this.subs.channels.get(ch);
666
674
  for (const slug of marketSlugs) set.add(slug);
667
675
  }
668
- this.send({ type: "subscribe", channels, market_slugs: marketSlugs });
676
+ this.sendChunkedSubscription("subscribe", channels, marketSlugs);
669
677
  }
670
678
  /**
671
679
  * Unsubscribe from one or more channels for the given market slugs.
@@ -675,7 +683,7 @@ var PredictWsClient = class {
675
683
  const set = this.subs.channels.get(ch);
676
684
  for (const slug of marketSlugs) set.delete(slug);
677
685
  }
678
- this.send({ type: "unsubscribe", channels, market_slugs: marketSlugs });
686
+ this.sendChunkedSubscription("unsubscribe", channels, marketSlugs);
679
687
  }
680
688
  // -------------------------------------------------------------------------
681
689
  // Subscription — convenience (single channel)
@@ -831,13 +839,39 @@ var PredictWsClient = class {
831
839
  restoreSubscriptions() {
832
840
  for (const [channel, slugs] of this.subs.channels.entries()) {
833
841
  if (slugs.size > 0) {
834
- this.send({
835
- type: "subscribe",
836
- channels: [channel],
837
- market_slugs: Array.from(slugs)
838
- });
842
+ this.sendChunkedSubscription("subscribe", [channel], Array.from(slugs));
843
+ }
844
+ }
845
+ }
846
+ /**
847
+ * Send a subscribe/unsubscribe message, splitting `marketSlugs` across
848
+ * multiple frames so no single frame exceeds the server read limit.
849
+ */
850
+ sendChunkedSubscription(type, channels, marketSlugs) {
851
+ if (marketSlugs.length === 0) {
852
+ this.send({ type, channels, market_slugs: [] });
853
+ return;
854
+ }
855
+ const envelopeBytes = byteLength(
856
+ JSON.stringify({ type, channels, market_slugs: [] })
857
+ );
858
+ const budget = MAX_SUBSCRIPTION_FRAME_BYTES - envelopeBytes;
859
+ let chunk = [];
860
+ let chunkBytes = 0;
861
+ const flush = () => {
862
+ if (chunk.length > 0) {
863
+ this.send({ type, channels, market_slugs: chunk });
864
+ chunk = [];
865
+ chunkBytes = 0;
839
866
  }
867
+ };
868
+ for (const slug of marketSlugs) {
869
+ const slugBytes = byteLength(JSON.stringify(slug)) + 1;
870
+ if (chunk.length > 0 && chunkBytes + slugBytes > budget) flush();
871
+ chunk.push(slug);
872
+ chunkBytes += slugBytes;
840
873
  }
874
+ flush();
841
875
  }
842
876
  startPing() {
843
877
  this.stopPing();
@@ -1908,21 +1942,51 @@ function useRedeemPosition() {
1908
1942
  wallet_address,
1909
1943
  condition_id,
1910
1944
  neg_risk,
1911
- signMessage
1945
+ signMessage,
1946
+ signTypedData
1912
1947
  }) => {
1913
1948
  const prepared = await client.prepareRedeemPolymarket({
1914
1949
  wallet_address,
1915
1950
  condition_id,
1916
1951
  neg_risk
1917
1952
  });
1918
- const signature = await signMessage(prepared.message_hash);
1919
- return client.redeemPolymarket({
1953
+ const walletKind = prepared.wallet_kind ?? "safe";
1954
+ let signature;
1955
+ if (walletKind === "deposit") {
1956
+ if (!prepared.typed_data) {
1957
+ throw new Error("Redeem prepare response missing typed_data");
1958
+ }
1959
+ if (!prepared.deadline) {
1960
+ throw new Error("Redeem prepare response missing deadline");
1961
+ }
1962
+ if (!signTypedData) {
1963
+ throw new Error("Deposit wallet redeem requires signTypedData");
1964
+ }
1965
+ signature = await signTypedData(prepared.typed_data);
1966
+ } else {
1967
+ if (!prepared.message_hash) {
1968
+ throw new Error("Redeem prepare response missing message_hash");
1969
+ }
1970
+ signature = await signMessage(prepared.message_hash);
1971
+ }
1972
+ const result = await client.redeemPolymarket({
1920
1973
  wallet_address,
1921
1974
  condition_id,
1922
1975
  neg_risk,
1923
1976
  signature,
1924
- nonce: prepared.nonce
1977
+ nonce: prepared.nonce,
1978
+ wallet_kind: walletKind,
1979
+ deadline: prepared.deadline
1925
1980
  });
1981
+ if (result.status === "STATE_FAILED" || result.status === "STATE_INVALID") {
1982
+ throw new Error(`Redeem transaction ${result.status}`);
1983
+ }
1984
+ if (result.status !== "STATE_MINED" && result.status !== "STATE_CONFIRMED") {
1985
+ throw new Error(
1986
+ `Redeem transaction not settled yet (${result.status})`
1987
+ );
1988
+ }
1989
+ return result;
1926
1990
  },
1927
1991
  onSuccess: () => {
1928
1992
  queryClient.invalidateQueries({ queryKey: ["predict", "positions"] });