@liberfi.io/react-predict 0.3.37 → 0.3.39

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
@@ -107,9 +107,9 @@ var PredictClient = class {
107
107
  const url = `${this.endpoint}/api/v1/markets/${encodeURIComponent(slug)}${query}`;
108
108
  return await httpGet(url);
109
109
  }
110
- /** Maps to `GET /api/v1/markets/:slug/orderbook?source=...`. */
111
- async getOrderbook(slug, source) {
112
- const query = buildQuery({ source });
110
+ /** Maps to `GET /api/v1/markets/:slug/orderbook?source=...&outcome=...`. */
111
+ async getOrderbook(slug, source, outcome) {
112
+ const query = buildQuery({ source, outcome });
113
113
  const url = `${this.endpoint}/api/v1/markets/${encodeURIComponent(slug)}/orderbook${query}`;
114
114
  return await httpGet(url);
115
115
  }
@@ -1295,14 +1295,15 @@ function useMarketHistory(markets, range = ChartRange.ALL) {
1295
1295
  }, [markets, queries]);
1296
1296
  return { series, isLoading };
1297
1297
  }
1298
- function orderbookQueryKey(slug, source) {
1299
- return ["predict", "orderbook", slug, source];
1298
+ function orderbookQueryKey(slug, source, outcome = "yes") {
1299
+ return ["predict", "orderbook", slug, source, outcome];
1300
1300
  }
1301
1301
  function useOrderbook(params, queryOptions = {}) {
1302
1302
  const client = usePredictClient();
1303
+ const outcome = params.outcome ?? "yes";
1303
1304
  return useQuery({
1304
- queryKey: orderbookQueryKey(params.slug, params.source),
1305
- queryFn: () => client.getOrderbook(params.slug, params.source),
1305
+ queryKey: orderbookQueryKey(params.slug, params.source, outcome),
1306
+ queryFn: () => client.getOrderbook(params.slug, params.source, outcome),
1306
1307
  enabled: Boolean(params.slug),
1307
1308
  refetchInterval: 3e4,
1308
1309
  ...queryOptions
@@ -1961,6 +1962,7 @@ function useOrderbookSubscription({
1961
1962
  wsClient,
1962
1963
  slug,
1963
1964
  enabled = true,
1965
+ outcome,
1964
1966
  onUpdate
1965
1967
  }) {
1966
1968
  const [orderbook, setOrderbook] = useState(null);
@@ -1970,11 +1972,13 @@ function useOrderbookSubscription({
1970
1972
  const handleUpdate = useCallback(
1971
1973
  (msg) => {
1972
1974
  if (msg.data.market_slug === slug) {
1973
- setOrderbook(msg.data);
1975
+ if (!outcome || msg.data.outcome === outcome) {
1976
+ setOrderbook(msg.data);
1977
+ }
1974
1978
  onUpdateRef.current?.(msg);
1975
1979
  }
1976
1980
  },
1977
- [slug]
1981
+ [slug, outcome]
1978
1982
  );
1979
1983
  useEffect(() => {
1980
1984
  if (!wsClient || !enabled || !slug) {
@@ -2039,28 +2043,33 @@ var REST_POLL_INTERVAL = 5e3;
2039
2043
  function useRealtimeOrderbook(params, queryOptions = {}) {
2040
2044
  const { wsClient } = usePredictWsClient();
2041
2045
  const queryClient = useQueryClient();
2042
- const subParams = {
2043
- wsClient,
2044
- slug: params.slug,
2045
- enabled: Boolean(params.slug)
2046
- };
2047
- const { isSubscribed } = useOrderbookSubscription(subParams);
2048
- const qk = orderbookQueryKey(params.slug, params.source);
2049
- const qkRef = useRef(qk);
2050
- qkRef.current = qk;
2051
- useEffect(() => {
2052
- if (!wsClient || !params.slug) return;
2053
- const unsub = wsClient.subscribeOrderbook([params.slug], (msg) => {
2046
+ const outcome = params.outcome ?? "yes";
2047
+ const handleUpdate = useCallback(
2048
+ (msg) => {
2049
+ if (msg.data.market_slug !== params.slug) return;
2050
+ if (msg.data.outcome !== "yes" && msg.data.outcome !== "no") return;
2054
2051
  const ob = {
2055
2052
  market_id: msg.data.market_slug,
2053
+ outcome: msg.data.outcome,
2056
2054
  bids: msg.data.bids,
2057
2055
  asks: msg.data.asks,
2058
2056
  spread: msg.data.spread
2059
2057
  };
2060
- queryClient.setQueryData(qkRef.current, ob);
2061
- });
2062
- return unsub;
2063
- }, [wsClient, params.slug, queryClient]);
2058
+ queryClient.setQueryData(
2059
+ orderbookQueryKey(params.slug, params.source, msg.data.outcome),
2060
+ ob
2061
+ );
2062
+ },
2063
+ [params.slug, params.source, queryClient]
2064
+ );
2065
+ const subParams = {
2066
+ wsClient,
2067
+ slug: params.slug,
2068
+ enabled: Boolean(params.slug),
2069
+ outcome,
2070
+ onUpdate: handleUpdate
2071
+ };
2072
+ const { isSubscribed } = useOrderbookSubscription(subParams);
2064
2073
  return useOrderbook(params, {
2065
2074
  refetchInterval: isSubscribed ? false : REST_POLL_INTERVAL,
2066
2075
  ...queryOptions
@@ -2491,20 +2500,14 @@ async function pollTxConfirmed(fetchStatus, txHash) {
2491
2500
  }
2492
2501
 
2493
2502
  // src/utils/orderbook-pricing.ts
2494
- function getWalkLevels(orderbook, outcome, side) {
2503
+ function getWalkLevels(orderbook, _outcome, side) {
2495
2504
  if (!orderbook) return [];
2496
- const isNo = outcome === "no";
2497
- const useAsks = side === "buy" && !isNo || side === "sell" && isNo;
2498
- const raw = useAsks ? orderbook.asks : orderbook.bids;
2505
+ const raw = side === "buy" ? orderbook.asks : orderbook.bids;
2499
2506
  if (!raw || raw.length === 0) return [];
2500
- const mapped = raw.map((lvl) => ({
2501
- price: isNo ? 1 - lvl.price : lvl.price,
2502
- size: lvl.size
2503
- }));
2504
2507
  if (side === "buy") {
2505
- return [...mapped].sort((a, b) => a.price - b.price);
2508
+ return [...raw].sort((a, b) => a.price - b.price);
2506
2509
  }
2507
- return [...mapped].sort((a, b) => b.price - a.price);
2510
+ return [...raw].sort((a, b) => b.price - a.price);
2508
2511
  }
2509
2512
  function pickBestAsk(orderbook, outcome) {
2510
2513
  const levels = getWalkLevels(orderbook, outcome, "buy");