From 0600cb9daf7ee032be77f45fc66a970cbeb7757b Mon Sep 17 00:00:00 2001 From: Nur Fikri Date: Tue, 16 Apr 2024 11:58:17 +0700 Subject: [PATCH 1/3] feat: don't refetch if it's a known error --- src/solve/queries.ts | 78 +++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/src/solve/queries.ts b/src/solve/queries.ts index 5b953547..6f9f7468 100644 --- a/src/solve/queries.ts +++ b/src/solve/queries.ts @@ -73,7 +73,7 @@ export function useRoute({ const skipClient = useSkipClient(); const [refetchCount, setRefetchCount] = useState(0); - + const [isError, setIsError] = useState(false); const { data: experimentalFeatures } = useExperimentalFeatures(); const queryKey = useMemo( @@ -119,42 +119,53 @@ export function useRoute({ if (!sourceAsset || !sourceAssetChainID || !destinationAsset || !destinationAssetChainID) { return; } + try { + const route = await skipClient.route( + direction === "swap-in" + ? { + amountIn: amount, + sourceAssetDenom: sourceAsset, + sourceAssetChainID: sourceAssetChainID, + destAssetDenom: destinationAsset, + destAssetChainID: destinationAssetChainID, + swapVenue, + allowMultiTx: true, + allowUnsafe: true, + experimentalFeatures, + smartRelay: true, + } + : { + amountOut: amount, + sourceAssetDenom: sourceAsset, + sourceAssetChainID: sourceAssetChainID, + destAssetDenom: destinationAsset, + destAssetChainID: destinationAssetChainID, + swapVenue, + allowMultiTx: true, + allowUnsafe: true, + experimentalFeatures, + smartRelay: true, + }, + ); - const route = await skipClient.route( - direction === "swap-in" - ? { - amountIn: amount, - sourceAssetDenom: sourceAsset, - sourceAssetChainID: sourceAssetChainID, - destAssetDenom: destinationAsset, - destAssetChainID: destinationAssetChainID, - swapVenue, - allowMultiTx: true, - allowUnsafe: true, - experimentalFeatures, - smartRelay: true, - } - : { - amountOut: amount, - sourceAssetDenom: sourceAsset, - sourceAssetChainID: sourceAssetChainID, - destAssetDenom: destinationAsset, - destAssetChainID: destinationAssetChainID, - swapVenue, - allowMultiTx: true, - allowUnsafe: true, - experimentalFeatures, - smartRelay: true, - }, - ); + if (!route.operations) { + throw new Error("No route found"); + } - if (!route.operations) { - throw new Error("No route found"); + return route; + } catch (error) { + if ( + // @ts-expect-error - error + String(error?.message).toLowerCase().includes("no routes found") || + // @ts-expect-error - error + String(error?.message).toLowerCase().includes("smart relay") + ) { + setIsError(true); + } + throw error; } - - return route; }, - refetchInterval: refetchCount < 10 ? 1000 * 10 : false, + refetchInterval: isError ? false : refetchCount < 10 ? 1000 * 1 : false, retry: false, enabled: enabled && @@ -172,6 +183,7 @@ export function useRoute({ }, [query.isRefetching]); useEffect(() => { + setIsError(false); setRefetchCount(0); }, [queryKey]); From 0bfb7281af0a89d1cdc753760367b62bc21c1dd3 Mon Sep 17 00:00:00 2001 From: Nur Fikri Date: Tue, 16 Apr 2024 11:59:23 +0700 Subject: [PATCH 2/3] update error message --- src/solve/queries.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/solve/queries.ts b/src/solve/queries.ts index 6f9f7468..f5691133 100644 --- a/src/solve/queries.ts +++ b/src/solve/queries.ts @@ -149,7 +149,7 @@ export function useRoute({ ); if (!route.operations) { - throw new Error("No route found"); + throw new Error("no routes found"); } return route; From 0727cba65936ef1d33bf9338dd54667eca296276 Mon Sep 17 00:00:00 2001 From: Nur Fikri Date: Tue, 16 Apr 2024 11:59:52 +0700 Subject: [PATCH 3/3] Update src/solve/queries.ts Co-authored-by: Not Jeremy Liu <31809888+NotJeremyLiu@users.noreply.github.com> --- src/solve/queries.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/solve/queries.ts b/src/solve/queries.ts index f5691133..ef9a28b3 100644 --- a/src/solve/queries.ts +++ b/src/solve/queries.ts @@ -158,7 +158,7 @@ export function useRoute({ // @ts-expect-error - error String(error?.message).toLowerCase().includes("no routes found") || // @ts-expect-error - error - String(error?.message).toLowerCase().includes("smart relay") + String(error?.message).toLowerCase().includes("relay") ) { setIsError(true); }