@provable-games/ekubo-sdk 0.1.8 → 0.1.9

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/react.d.cts CHANGED
@@ -329,4 +329,75 @@ interface UseEkuboPriceHistoryProps {
329
329
  */
330
330
  declare function useEkuboPriceHistory({ token, quoteToken, interval, config, enabled, }: UseEkuboPriceHistoryProps): UseEkuboPriceHistoryResult;
331
331
 
332
- export { EkuboProvider, type EkuboProviderProps, type TokenPrices, type UseEkuboPriceHistoryProps, type UseEkuboPriceHistoryResult, type UseEkuboPricesProps, type UseEkuboPricesResult, type UseEkuboStatsProps, type UseEkuboStatsResult, type UseEkuboSwapProps, type UseEkuboSwapResult, type UseEkuboSwapState, type UseEkuboTokensProps, type UseEkuboTokensResult, useEkuboClient, useEkuboPriceHistory, useEkuboPrices, useEkuboStats, useEkuboSwap, useEkuboTokens, useOptionalEkuboClient };
332
+ interface QuoteResult {
333
+ /** The swap quote */
334
+ quote: SwapQuote | null;
335
+ /** Whether this quote is loading */
336
+ loading: boolean;
337
+ /** Error from fetching this quote */
338
+ error: Error | null;
339
+ /** Whether there's insufficient liquidity */
340
+ insufficientLiquidity: boolean;
341
+ }
342
+ interface QuotesMap {
343
+ [sellToken: string]: QuoteResult;
344
+ }
345
+ interface UseEkuboQuotesResult {
346
+ /** Map of sellToken address to quote result */
347
+ quotes: QuotesMap;
348
+ /** Whether any quotes are still loading */
349
+ isLoading: boolean;
350
+ /** Get quote for a specific sell token */
351
+ getQuote: (sellToken: string) => QuoteResult | undefined;
352
+ /** Manually refetch all quotes */
353
+ refetch: () => void;
354
+ }
355
+ interface UseEkuboQuotesProps {
356
+ /** Array of tokens that can be sold (addresses or symbols) */
357
+ sellTokens: string[];
358
+ /** Token being bought (address or symbol) */
359
+ buyToken: string | null;
360
+ /** Amount to swap (positive for exact input, negative for exact output) */
361
+ amount: bigint;
362
+ /** Whether to enable quote fetching (default: true) */
363
+ enabled?: boolean;
364
+ /** Polling interval in ms (default: 30000 - 30 seconds) */
365
+ pollingInterval?: number;
366
+ /** Client config (optional if using EkuboProvider) */
367
+ config?: EkuboClientConfig;
368
+ }
369
+ /**
370
+ * Hook for fetching swap quotes for multiple sell tokens at once.
371
+ *
372
+ * Useful when you want to show users multiple payment options with
373
+ * their costs upfront, allowing instant switching between options.
374
+ *
375
+ * @example
376
+ * ```tsx
377
+ * import { useEkuboQuotes } from '@provable-games/ekubo-sdk/react';
378
+ *
379
+ * function PaymentSelector() {
380
+ * const { quotes, isLoading, getQuote } = useEkuboQuotes({
381
+ * sellTokens: [ETH_ADDRESS, STRK_ADDRESS, USDC_ADDRESS],
382
+ * buyToken: ENTRY_TOKEN,
383
+ * amount: entryFeeAmount,
384
+ * });
385
+ *
386
+ * return (
387
+ * <div>
388
+ * {sellTokens.map(token => {
389
+ * const result = getQuote(token);
390
+ * return (
391
+ * <button key={token} onClick={() => setSelectedToken(token)}>
392
+ * Pay with {token}: {result?.quote?.total ?? 'Loading...'}
393
+ * </button>
394
+ * );
395
+ * })}
396
+ * </div>
397
+ * );
398
+ * }
399
+ * ```
400
+ */
401
+ declare function useEkuboQuotes({ sellTokens, buyToken, amount, enabled, pollingInterval, config, }: UseEkuboQuotesProps): UseEkuboQuotesResult;
402
+
403
+ export { EkuboProvider, type EkuboProviderProps, type QuoteResult, type QuotesMap, type TokenPrices, type UseEkuboPriceHistoryProps, type UseEkuboPriceHistoryResult, type UseEkuboPricesProps, type UseEkuboPricesResult, type UseEkuboQuotesProps, type UseEkuboQuotesResult, type UseEkuboStatsProps, type UseEkuboStatsResult, type UseEkuboSwapProps, type UseEkuboSwapResult, type UseEkuboSwapState, type UseEkuboTokensProps, type UseEkuboTokensResult, useEkuboClient, useEkuboPriceHistory, useEkuboPrices, useEkuboQuotes, useEkuboStats, useEkuboSwap, useEkuboTokens, useOptionalEkuboClient };
package/dist/react.d.ts CHANGED
@@ -329,4 +329,75 @@ interface UseEkuboPriceHistoryProps {
329
329
  */
330
330
  declare function useEkuboPriceHistory({ token, quoteToken, interval, config, enabled, }: UseEkuboPriceHistoryProps): UseEkuboPriceHistoryResult;
331
331
 
332
- export { EkuboProvider, type EkuboProviderProps, type TokenPrices, type UseEkuboPriceHistoryProps, type UseEkuboPriceHistoryResult, type UseEkuboPricesProps, type UseEkuboPricesResult, type UseEkuboStatsProps, type UseEkuboStatsResult, type UseEkuboSwapProps, type UseEkuboSwapResult, type UseEkuboSwapState, type UseEkuboTokensProps, type UseEkuboTokensResult, useEkuboClient, useEkuboPriceHistory, useEkuboPrices, useEkuboStats, useEkuboSwap, useEkuboTokens, useOptionalEkuboClient };
332
+ interface QuoteResult {
333
+ /** The swap quote */
334
+ quote: SwapQuote | null;
335
+ /** Whether this quote is loading */
336
+ loading: boolean;
337
+ /** Error from fetching this quote */
338
+ error: Error | null;
339
+ /** Whether there's insufficient liquidity */
340
+ insufficientLiquidity: boolean;
341
+ }
342
+ interface QuotesMap {
343
+ [sellToken: string]: QuoteResult;
344
+ }
345
+ interface UseEkuboQuotesResult {
346
+ /** Map of sellToken address to quote result */
347
+ quotes: QuotesMap;
348
+ /** Whether any quotes are still loading */
349
+ isLoading: boolean;
350
+ /** Get quote for a specific sell token */
351
+ getQuote: (sellToken: string) => QuoteResult | undefined;
352
+ /** Manually refetch all quotes */
353
+ refetch: () => void;
354
+ }
355
+ interface UseEkuboQuotesProps {
356
+ /** Array of tokens that can be sold (addresses or symbols) */
357
+ sellTokens: string[];
358
+ /** Token being bought (address or symbol) */
359
+ buyToken: string | null;
360
+ /** Amount to swap (positive for exact input, negative for exact output) */
361
+ amount: bigint;
362
+ /** Whether to enable quote fetching (default: true) */
363
+ enabled?: boolean;
364
+ /** Polling interval in ms (default: 30000 - 30 seconds) */
365
+ pollingInterval?: number;
366
+ /** Client config (optional if using EkuboProvider) */
367
+ config?: EkuboClientConfig;
368
+ }
369
+ /**
370
+ * Hook for fetching swap quotes for multiple sell tokens at once.
371
+ *
372
+ * Useful when you want to show users multiple payment options with
373
+ * their costs upfront, allowing instant switching between options.
374
+ *
375
+ * @example
376
+ * ```tsx
377
+ * import { useEkuboQuotes } from '@provable-games/ekubo-sdk/react';
378
+ *
379
+ * function PaymentSelector() {
380
+ * const { quotes, isLoading, getQuote } = useEkuboQuotes({
381
+ * sellTokens: [ETH_ADDRESS, STRK_ADDRESS, USDC_ADDRESS],
382
+ * buyToken: ENTRY_TOKEN,
383
+ * amount: entryFeeAmount,
384
+ * });
385
+ *
386
+ * return (
387
+ * <div>
388
+ * {sellTokens.map(token => {
389
+ * const result = getQuote(token);
390
+ * return (
391
+ * <button key={token} onClick={() => setSelectedToken(token)}>
392
+ * Pay with {token}: {result?.quote?.total ?? 'Loading...'}
393
+ * </button>
394
+ * );
395
+ * })}
396
+ * </div>
397
+ * );
398
+ * }
399
+ * ```
400
+ */
401
+ declare function useEkuboQuotes({ sellTokens, buyToken, amount, enabled, pollingInterval, config, }: UseEkuboQuotesProps): UseEkuboQuotesResult;
402
+
403
+ export { EkuboProvider, type EkuboProviderProps, type QuoteResult, type QuotesMap, type TokenPrices, type UseEkuboPriceHistoryProps, type UseEkuboPriceHistoryResult, type UseEkuboPricesProps, type UseEkuboPricesResult, type UseEkuboQuotesProps, type UseEkuboQuotesResult, type UseEkuboStatsProps, type UseEkuboStatsResult, type UseEkuboSwapProps, type UseEkuboSwapResult, type UseEkuboSwapState, type UseEkuboTokensProps, type UseEkuboTokensResult, useEkuboClient, useEkuboPriceHistory, useEkuboPrices, useEkuboQuotes, useEkuboStats, useEkuboSwap, useEkuboTokens, useOptionalEkuboClient };
package/dist/react.js CHANGED
@@ -1984,7 +1984,139 @@ function useEkuboPriceHistory({
1984
1984
  [data, isLoading, error, refetch]
1985
1985
  );
1986
1986
  }
1987
+ var defaultClientInstance6 = null;
1988
+ function getDefaultClient6(config) {
1989
+ if (!defaultClientInstance6) {
1990
+ defaultClientInstance6 = createEkuboClient(config);
1991
+ }
1992
+ return defaultClientInstance6;
1993
+ }
1994
+ function useEkuboQuotes({
1995
+ sellTokens,
1996
+ buyToken,
1997
+ amount,
1998
+ enabled = true,
1999
+ pollingInterval = 3e4,
2000
+ config
2001
+ }) {
2002
+ const contextClient = useOptionalEkuboClient();
2003
+ const client = contextClient ?? getDefaultClient6(config);
2004
+ const [quotes, setQuotes] = useState({});
2005
+ const pollerRef = useRef(null);
2006
+ const abortControllersRef = useRef(/* @__PURE__ */ new Map());
2007
+ const sellTokensKey = useMemo(
2008
+ () => JSON.stringify([...sellTokens].sort()),
2009
+ [sellTokens]
2010
+ );
2011
+ const canFetch = useMemo(() => {
2012
+ return buyToken !== null && amount !== 0n && sellTokens.length > 0;
2013
+ }, [buyToken, amount, sellTokens.length]);
2014
+ const shouldPoll = enabled && canFetch;
2015
+ const fetchAllQuotes = useCallback(async () => {
2016
+ if (!canFetch || !buyToken) return;
2017
+ abortControllersRef.current.forEach((controller) => controller.abort());
2018
+ abortControllersRef.current.clear();
2019
+ setQuotes((prev) => {
2020
+ const updated = { ...prev };
2021
+ for (const sellToken of sellTokens) {
2022
+ if (sellToken.toLowerCase() === buyToken.toLowerCase()) continue;
2023
+ updated[sellToken] = {
2024
+ quote: prev[sellToken]?.quote ?? null,
2025
+ loading: true,
2026
+ error: null,
2027
+ insufficientLiquidity: false
2028
+ };
2029
+ }
2030
+ return updated;
2031
+ });
2032
+ const fetchPromises = sellTokens.filter((sellToken) => sellToken.toLowerCase() !== buyToken.toLowerCase()).map(async (sellToken) => {
2033
+ const abortController = new AbortController();
2034
+ abortControllersRef.current.set(sellToken, abortController);
2035
+ try {
2036
+ const quote = await client.getQuote({
2037
+ amount,
2038
+ tokenFrom: sellToken,
2039
+ tokenTo: buyToken,
2040
+ signal: abortController.signal
2041
+ });
2042
+ return {
2043
+ sellToken,
2044
+ result: {
2045
+ quote,
2046
+ loading: false,
2047
+ error: null,
2048
+ insufficientLiquidity: false
2049
+ }
2050
+ };
2051
+ } catch (err) {
2052
+ if (err instanceof Error && err.name === "AbortError") {
2053
+ return null;
2054
+ }
2055
+ const isInsufficient = err instanceof InsufficientLiquidityError;
2056
+ return {
2057
+ sellToken,
2058
+ result: {
2059
+ quote: null,
2060
+ loading: false,
2061
+ error: err instanceof Error ? err : new Error("Unknown error"),
2062
+ insufficientLiquidity: isInsufficient
2063
+ }
2064
+ };
2065
+ }
2066
+ });
2067
+ const results = await Promise.all(fetchPromises);
2068
+ setQuotes((prev) => {
2069
+ const updated = { ...prev };
2070
+ for (const result of results) {
2071
+ if (result) {
2072
+ updated[result.sellToken] = result.result;
2073
+ }
2074
+ }
2075
+ return updated;
2076
+ });
2077
+ }, [canFetch, buyToken, sellTokens, amount, client]);
2078
+ useEffect(() => {
2079
+ if (pollerRef.current) {
2080
+ clearInterval(pollerRef.current);
2081
+ pollerRef.current = null;
2082
+ }
2083
+ if (!shouldPoll) {
2084
+ return;
2085
+ }
2086
+ fetchAllQuotes();
2087
+ pollerRef.current = setInterval(fetchAllQuotes, pollingInterval);
2088
+ return () => {
2089
+ if (pollerRef.current) {
2090
+ clearInterval(pollerRef.current);
2091
+ pollerRef.current = null;
2092
+ }
2093
+ abortControllersRef.current.forEach((controller) => controller.abort());
2094
+ abortControllersRef.current.clear();
2095
+ };
2096
+ }, [shouldPoll, fetchAllQuotes, pollingInterval, sellTokensKey]);
2097
+ const isLoading = useMemo(() => {
2098
+ return Object.values(quotes).some((q) => q.loading);
2099
+ }, [quotes]);
2100
+ const getQuote = useCallback(
2101
+ (sellToken) => {
2102
+ return quotes[sellToken];
2103
+ },
2104
+ [quotes]
2105
+ );
2106
+ const refetch = useCallback(() => {
2107
+ fetchAllQuotes();
2108
+ }, [fetchAllQuotes]);
2109
+ return useMemo(
2110
+ () => ({
2111
+ quotes,
2112
+ isLoading,
2113
+ getQuote,
2114
+ refetch
2115
+ }),
2116
+ [quotes, isLoading, getQuote, refetch]
2117
+ );
2118
+ }
1987
2119
 
1988
- export { EkuboProvider, useEkuboClient, useEkuboPriceHistory, useEkuboPrices, useEkuboStats, useEkuboSwap, useEkuboTokens, useOptionalEkuboClient };
2120
+ export { EkuboProvider, useEkuboClient, useEkuboPriceHistory, useEkuboPrices, useEkuboQuotes, useEkuboStats, useEkuboSwap, useEkuboTokens, useOptionalEkuboClient };
1989
2121
  //# sourceMappingURL=react.js.map
1990
2122
  //# sourceMappingURL=react.js.map