@liberfi.io/react-predict 0.1.36 → 0.1.38

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
@@ -203,6 +203,11 @@ var PredictClient = class {
203
203
  const url = `${this.endpoint}/api/v1/orders/polymarket`;
204
204
  return await httpPost(url, input, { headers });
205
205
  }
206
+ /** Maps to `GET /api/v1/polymarket/tick-size?token_id=xxx`. */
207
+ async getPolymarketTickSize(tokenId) {
208
+ const url = `${this.endpoint}/api/v1/polymarket/tick-size?token_id=${encodeURIComponent(tokenId)}`;
209
+ return await httpGet(url);
210
+ }
206
211
  // -------------------------------------------------------------------------
207
212
  // DFlow trading
208
213
  // -------------------------------------------------------------------------
@@ -1548,6 +1553,19 @@ function usePolymarketWithdraw() {
1548
1553
  mutationFn: (req) => client.executePolymarketWithdraw(req)
1549
1554
  });
1550
1555
  }
1556
+ function tickSizeQueryKey(tokenId) {
1557
+ return ["predict", "polymarket", "tick-size", tokenId];
1558
+ }
1559
+ function useTickSize(tokenId, queryOptions = {}) {
1560
+ const client = usePredictClient();
1561
+ return useQuery({
1562
+ queryKey: tickSizeQueryKey(tokenId ?? ""),
1563
+ queryFn: () => client.getPolymarketTickSize(tokenId),
1564
+ enabled: !!tokenId,
1565
+ staleTime: 5 * 6e4,
1566
+ ...queryOptions
1567
+ });
1568
+ }
1551
1569
  function usePredictWsClient() {
1552
1570
  const context = useContext(PredictContext);
1553
1571
  if (!context) {
@@ -1829,15 +1847,15 @@ var ORDER_TYPE = {
1829
1847
  // Fill-And-Kill
1830
1848
  };
1831
1849
  var SIDE = { BUY: 0, SELL: 1 };
1832
- var TICK_DECIMALS = {
1833
- "0.1": 1,
1834
- "0.01": 2,
1835
- "0.001": 3,
1836
- "0.0001": 4
1850
+ var ROUNDING_CONFIG = {
1851
+ "0.1": { size: 2, price: 1, amount: 3 },
1852
+ "0.01": { size: 2, price: 2, amount: 4 },
1853
+ "0.001": { size: 3, price: 3, amount: 6 },
1854
+ "0.0001": { size: 4, price: 4, amount: 8 }
1837
1855
  };
1838
- function roundToTick(price, tickSize) {
1839
- const decimals = TICK_DECIMALS[tickSize] ?? 2;
1840
- return price.toFixed(decimals);
1856
+ var DEFAULT_ROUNDING = { size: 2, price: 2, amount: 4 };
1857
+ function decimalPlaces(n, d) {
1858
+ return parseFloat(n.toFixed(d));
1841
1859
  }
1842
1860
  function toMicroUsdc(amount) {
1843
1861
  return BigInt(Math.round(amount * 1e6));
@@ -1850,12 +1868,14 @@ function normalizeTokenId(tokenId) {
1850
1868
  }
1851
1869
  function buildOrderMessage(input) {
1852
1870
  const side = input.side === "BUY" ? SIDE.BUY : SIDE.SELL;
1853
- const priceStr = roundToTick(input.price, input.tickSize);
1854
- const priceNum = parseFloat(priceStr);
1855
- const sizeShares = toMicroUsdc(input.size);
1856
- const sizeUsdc = toMicroUsdc(input.size * priceNum);
1857
- const makerAmount = side === SIDE.BUY ? sizeUsdc.toString() : sizeShares.toString();
1858
- const takerAmount = side === SIDE.BUY ? sizeShares.toString() : sizeUsdc.toString();
1871
+ const rc = ROUNDING_CONFIG[input.tickSize] ?? DEFAULT_ROUNDING;
1872
+ const rawPrice = decimalPlaces(input.price, rc.price);
1873
+ const rawSize = decimalPlaces(input.size, rc.size);
1874
+ const rawAmount = decimalPlaces(rawSize * rawPrice, rc.amount);
1875
+ const sizeInMicro = toMicroUsdc(rawSize);
1876
+ const amountInMicro = toMicroUsdc(rawAmount);
1877
+ const makerAmount = side === SIDE.BUY ? amountInMicro.toString() : sizeInMicro.toString();
1878
+ const takerAmount = side === SIDE.BUY ? sizeInMicro.toString() : amountInMicro.toString();
1859
1879
  const maker = input.funderAddress ?? input.signerAddress;
1860
1880
  return {
1861
1881
  salt: Math.floor(Math.random() * 1e15).toString(),
@@ -1986,6 +2006,6 @@ async function pollTxConfirmed(fetchStatus, txHash) {
1986
2006
  );
1987
2007
  }
1988
2008
 
1989
- export { CLOB_AUTH_DOMAIN, CLOB_AUTH_TYPES, CTF_EXCHANGE_ADDRESS, CTF_ORDER_TYPES, ChartRange, NEG_RISK_CTF_EXCHANGE_ADDRESS, ORDER_TYPE, POLYGON_CHAIN_ID, PolymarketContext, PolymarketProvider, PredictClient, PredictContext, PredictProvider, PredictWsClient, SIDE, USDC_ADDRESS, balanceQueryKey, buildClobAuthMessage, buildClobPayload, buildCtfExchangeDomain, buildOrderMessage, buildPolymarketL2Headers, buildSignedOrder, candlesticksQueryKey, createPredictClient, createPredictWsClient, derivePolymarketApiKey, dflowKYCQueryKey, dflowQuoteQueryKey, eventQueryKey, eventsQueryKey, fetchEvent, fetchEvents, fetchEventsPage, fetchMarket, fetchMatchMarketsPage, fetchMatchesPage, hmacSha256Base64, infiniteEventsQueryKey, infiniteOrdersQueryKey, infiniteTradesQueryKey, marketQueryKey, marketTradesQueryKey, matchMarketsQueryKey, matchQueryKey, matchesQueryKey, orderQueryKey, orderbookQueryKey, ordersQueryKey, polymarketDepositAddressesQueryKey, polymarketSetupQueryKey, positionsMultiQueryKey, positionsQueryKey, priceHistoryQueryKey, resolveEventsParams, resolveTagSlug, similarEventsQueryKey, tradesQueryKey, useBalance, useCancelOrder, useCandlesticks, useCreatePolymarketOrder, useDFlowKYC, useDFlowQuote, useDFlowSubmit, useEvent, useEvents, useInfiniteEvents, useInfiniteMatchMarkets, useInfiniteMatches, useInfiniteOrders, useInfiniteTrades, useMarket, useMarketHistory, useMarketTrades, useMatch, useOrder, useOrderbook, useOrderbookSubscription, useOrders, usePolymarket, usePolymarketDeposit, usePolymarketDepositAddresses, usePolymarketSetup, usePolymarketWithdraw, usePositions, usePositionsMulti, usePredictClient, usePredictWsClient, usePriceHistory, usePricesSubscription, useRealtimeOrderbook, useRealtimePrices, useRealtimeTrades, useRunPolymarketSetup, useSearchEvents, useSimilarEvents, useTrades, useTradesSubscription, useWithdrawBuildMutation, useWithdrawStatusQuery, useWithdrawSubmitMutation, withdrawStatusQueryKey };
2009
+ export { CLOB_AUTH_DOMAIN, CLOB_AUTH_TYPES, CTF_EXCHANGE_ADDRESS, CTF_ORDER_TYPES, ChartRange, NEG_RISK_CTF_EXCHANGE_ADDRESS, ORDER_TYPE, POLYGON_CHAIN_ID, PolymarketContext, PolymarketProvider, PredictClient, PredictContext, PredictProvider, PredictWsClient, SIDE, USDC_ADDRESS, balanceQueryKey, buildClobAuthMessage, buildClobPayload, buildCtfExchangeDomain, buildOrderMessage, buildPolymarketL2Headers, buildSignedOrder, candlesticksQueryKey, createPredictClient, createPredictWsClient, derivePolymarketApiKey, dflowKYCQueryKey, dflowQuoteQueryKey, eventQueryKey, eventsQueryKey, fetchEvent, fetchEvents, fetchEventsPage, fetchMarket, fetchMatchMarketsPage, fetchMatchesPage, hmacSha256Base64, infiniteEventsQueryKey, infiniteOrdersQueryKey, infiniteTradesQueryKey, marketQueryKey, marketTradesQueryKey, matchMarketsQueryKey, matchQueryKey, matchesQueryKey, orderQueryKey, orderbookQueryKey, ordersQueryKey, polymarketDepositAddressesQueryKey, polymarketSetupQueryKey, positionsMultiQueryKey, positionsQueryKey, priceHistoryQueryKey, resolveEventsParams, resolveTagSlug, similarEventsQueryKey, tickSizeQueryKey, tradesQueryKey, useBalance, useCancelOrder, useCandlesticks, useCreatePolymarketOrder, useDFlowKYC, useDFlowQuote, useDFlowSubmit, useEvent, useEvents, useInfiniteEvents, useInfiniteMatchMarkets, useInfiniteMatches, useInfiniteOrders, useInfiniteTrades, useMarket, useMarketHistory, useMarketTrades, useMatch, useOrder, useOrderbook, useOrderbookSubscription, useOrders, usePolymarket, usePolymarketDeposit, usePolymarketDepositAddresses, usePolymarketSetup, usePolymarketWithdraw, usePositions, usePositionsMulti, usePredictClient, usePredictWsClient, usePriceHistory, usePricesSubscription, useRealtimeOrderbook, useRealtimePrices, useRealtimeTrades, useRunPolymarketSetup, useSearchEvents, useSimilarEvents, useTickSize, useTrades, useTradesSubscription, useWithdrawBuildMutation, useWithdrawStatusQuery, useWithdrawSubmitMutation, withdrawStatusQueryKey };
1990
2010
  //# sourceMappingURL=index.mjs.map
1991
2011
  //# sourceMappingURL=index.mjs.map