@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.cjs CHANGED
@@ -1986,11 +1986,144 @@ function useEkuboPriceHistory({
1986
1986
  [data, isLoading, error, refetch]
1987
1987
  );
1988
1988
  }
1989
+ var defaultClientInstance6 = null;
1990
+ function getDefaultClient6(config) {
1991
+ if (!defaultClientInstance6) {
1992
+ defaultClientInstance6 = createEkuboClient(config);
1993
+ }
1994
+ return defaultClientInstance6;
1995
+ }
1996
+ function useEkuboQuotes({
1997
+ sellTokens,
1998
+ buyToken,
1999
+ amount,
2000
+ enabled = true,
2001
+ pollingInterval = 3e4,
2002
+ config
2003
+ }) {
2004
+ const contextClient = useOptionalEkuboClient();
2005
+ const client = contextClient ?? getDefaultClient6(config);
2006
+ const [quotes, setQuotes] = react.useState({});
2007
+ const pollerRef = react.useRef(null);
2008
+ const abortControllersRef = react.useRef(/* @__PURE__ */ new Map());
2009
+ const sellTokensKey = react.useMemo(
2010
+ () => JSON.stringify([...sellTokens].sort()),
2011
+ [sellTokens]
2012
+ );
2013
+ const canFetch = react.useMemo(() => {
2014
+ return buyToken !== null && amount !== 0n && sellTokens.length > 0;
2015
+ }, [buyToken, amount, sellTokens.length]);
2016
+ const shouldPoll = enabled && canFetch;
2017
+ const fetchAllQuotes = react.useCallback(async () => {
2018
+ if (!canFetch || !buyToken) return;
2019
+ abortControllersRef.current.forEach((controller) => controller.abort());
2020
+ abortControllersRef.current.clear();
2021
+ setQuotes((prev) => {
2022
+ const updated = { ...prev };
2023
+ for (const sellToken of sellTokens) {
2024
+ if (sellToken.toLowerCase() === buyToken.toLowerCase()) continue;
2025
+ updated[sellToken] = {
2026
+ quote: prev[sellToken]?.quote ?? null,
2027
+ loading: true,
2028
+ error: null,
2029
+ insufficientLiquidity: false
2030
+ };
2031
+ }
2032
+ return updated;
2033
+ });
2034
+ const fetchPromises = sellTokens.filter((sellToken) => sellToken.toLowerCase() !== buyToken.toLowerCase()).map(async (sellToken) => {
2035
+ const abortController = new AbortController();
2036
+ abortControllersRef.current.set(sellToken, abortController);
2037
+ try {
2038
+ const quote = await client.getQuote({
2039
+ amount,
2040
+ tokenFrom: sellToken,
2041
+ tokenTo: buyToken,
2042
+ signal: abortController.signal
2043
+ });
2044
+ return {
2045
+ sellToken,
2046
+ result: {
2047
+ quote,
2048
+ loading: false,
2049
+ error: null,
2050
+ insufficientLiquidity: false
2051
+ }
2052
+ };
2053
+ } catch (err) {
2054
+ if (err instanceof Error && err.name === "AbortError") {
2055
+ return null;
2056
+ }
2057
+ const isInsufficient = err instanceof InsufficientLiquidityError;
2058
+ return {
2059
+ sellToken,
2060
+ result: {
2061
+ quote: null,
2062
+ loading: false,
2063
+ error: err instanceof Error ? err : new Error("Unknown error"),
2064
+ insufficientLiquidity: isInsufficient
2065
+ }
2066
+ };
2067
+ }
2068
+ });
2069
+ const results = await Promise.all(fetchPromises);
2070
+ setQuotes((prev) => {
2071
+ const updated = { ...prev };
2072
+ for (const result of results) {
2073
+ if (result) {
2074
+ updated[result.sellToken] = result.result;
2075
+ }
2076
+ }
2077
+ return updated;
2078
+ });
2079
+ }, [canFetch, buyToken, sellTokens, amount, client]);
2080
+ react.useEffect(() => {
2081
+ if (pollerRef.current) {
2082
+ clearInterval(pollerRef.current);
2083
+ pollerRef.current = null;
2084
+ }
2085
+ if (!shouldPoll) {
2086
+ return;
2087
+ }
2088
+ fetchAllQuotes();
2089
+ pollerRef.current = setInterval(fetchAllQuotes, pollingInterval);
2090
+ return () => {
2091
+ if (pollerRef.current) {
2092
+ clearInterval(pollerRef.current);
2093
+ pollerRef.current = null;
2094
+ }
2095
+ abortControllersRef.current.forEach((controller) => controller.abort());
2096
+ abortControllersRef.current.clear();
2097
+ };
2098
+ }, [shouldPoll, fetchAllQuotes, pollingInterval, sellTokensKey]);
2099
+ const isLoading = react.useMemo(() => {
2100
+ return Object.values(quotes).some((q) => q.loading);
2101
+ }, [quotes]);
2102
+ const getQuote = react.useCallback(
2103
+ (sellToken) => {
2104
+ return quotes[sellToken];
2105
+ },
2106
+ [quotes]
2107
+ );
2108
+ const refetch = react.useCallback(() => {
2109
+ fetchAllQuotes();
2110
+ }, [fetchAllQuotes]);
2111
+ return react.useMemo(
2112
+ () => ({
2113
+ quotes,
2114
+ isLoading,
2115
+ getQuote,
2116
+ refetch
2117
+ }),
2118
+ [quotes, isLoading, getQuote, refetch]
2119
+ );
2120
+ }
1989
2121
 
1990
2122
  exports.EkuboProvider = EkuboProvider;
1991
2123
  exports.useEkuboClient = useEkuboClient;
1992
2124
  exports.useEkuboPriceHistory = useEkuboPriceHistory;
1993
2125
  exports.useEkuboPrices = useEkuboPrices;
2126
+ exports.useEkuboQuotes = useEkuboQuotes;
1994
2127
  exports.useEkuboStats = useEkuboStats;
1995
2128
  exports.useEkuboSwap = useEkuboSwap;
1996
2129
  exports.useEkuboTokens = useEkuboTokens;