@provable-games/ekubo-sdk 0.1.8 → 0.1.10

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,145 @@ 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 exactOutputAmount = amount > 0n ? -amount : amount;
2035
+ const fetchPromises = sellTokens.filter((sellToken) => sellToken.toLowerCase() !== buyToken.toLowerCase()).map(async (sellToken) => {
2036
+ const abortController = new AbortController();
2037
+ abortControllersRef.current.set(sellToken, abortController);
2038
+ try {
2039
+ const quote = await client.getQuote({
2040
+ amount: exactOutputAmount,
2041
+ tokenFrom: sellToken,
2042
+ tokenTo: buyToken,
2043
+ signal: abortController.signal
2044
+ });
2045
+ return {
2046
+ sellToken,
2047
+ result: {
2048
+ quote,
2049
+ loading: false,
2050
+ error: null,
2051
+ insufficientLiquidity: false
2052
+ }
2053
+ };
2054
+ } catch (err) {
2055
+ if (err instanceof Error && err.name === "AbortError") {
2056
+ return null;
2057
+ }
2058
+ const isInsufficient = err instanceof InsufficientLiquidityError;
2059
+ return {
2060
+ sellToken,
2061
+ result: {
2062
+ quote: null,
2063
+ loading: false,
2064
+ error: err instanceof Error ? err : new Error("Unknown error"),
2065
+ insufficientLiquidity: isInsufficient
2066
+ }
2067
+ };
2068
+ }
2069
+ });
2070
+ const results = await Promise.all(fetchPromises);
2071
+ setQuotes((prev) => {
2072
+ const updated = { ...prev };
2073
+ for (const result of results) {
2074
+ if (result) {
2075
+ updated[result.sellToken] = result.result;
2076
+ }
2077
+ }
2078
+ return updated;
2079
+ });
2080
+ }, [canFetch, buyToken, sellTokens, amount, client]);
2081
+ react.useEffect(() => {
2082
+ if (pollerRef.current) {
2083
+ clearInterval(pollerRef.current);
2084
+ pollerRef.current = null;
2085
+ }
2086
+ if (!shouldPoll) {
2087
+ return;
2088
+ }
2089
+ fetchAllQuotes();
2090
+ pollerRef.current = setInterval(fetchAllQuotes, pollingInterval);
2091
+ return () => {
2092
+ if (pollerRef.current) {
2093
+ clearInterval(pollerRef.current);
2094
+ pollerRef.current = null;
2095
+ }
2096
+ abortControllersRef.current.forEach((controller) => controller.abort());
2097
+ abortControllersRef.current.clear();
2098
+ };
2099
+ }, [shouldPoll, fetchAllQuotes, pollingInterval, sellTokensKey]);
2100
+ const isLoading = react.useMemo(() => {
2101
+ return Object.values(quotes).some((q) => q.loading);
2102
+ }, [quotes]);
2103
+ const getQuote = react.useCallback(
2104
+ (sellToken) => {
2105
+ return quotes[sellToken];
2106
+ },
2107
+ [quotes]
2108
+ );
2109
+ const refetch = react.useCallback(() => {
2110
+ fetchAllQuotes();
2111
+ }, [fetchAllQuotes]);
2112
+ return react.useMemo(
2113
+ () => ({
2114
+ quotes,
2115
+ isLoading,
2116
+ getQuote,
2117
+ refetch
2118
+ }),
2119
+ [quotes, isLoading, getQuote, refetch]
2120
+ );
2121
+ }
1989
2122
 
1990
2123
  exports.EkuboProvider = EkuboProvider;
1991
2124
  exports.useEkuboClient = useEkuboClient;
1992
2125
  exports.useEkuboPriceHistory = useEkuboPriceHistory;
1993
2126
  exports.useEkuboPrices = useEkuboPrices;
2127
+ exports.useEkuboQuotes = useEkuboQuotes;
1994
2128
  exports.useEkuboStats = useEkuboStats;
1995
2129
  exports.useEkuboSwap = useEkuboSwap;
1996
2130
  exports.useEkuboTokens = useEkuboTokens;