diff --git a/src/lib/hooks/routing/clientSideSmartOrderRouter.ts b/src/lib/hooks/routing/clientSideSmartOrderRouter.ts index e35ed4fe75..98015eb73f 100644 --- a/src/lib/hooks/routing/clientSideSmartOrderRouter.ts +++ b/src/lib/hooks/routing/clientSideSmartOrderRouter.ts @@ -100,7 +100,11 @@ export async function getClientSideQuote( ) } -export function useFreshQuote(quoteResult: GetQuoteResult | undefined, maxBlockAge = 10): GetQuoteResult | undefined { +/** Used to keep quotes up to date given a certain block age. Returns undefined if past limit. */ +export function useFilterFreshQuote( + quoteResult: GetQuoteResult | undefined, + maxBlockAge = 10 +): GetQuoteResult | undefined { const block = useBlockNumber() if (!block || !quoteResult) return undefined if (block - (Number(quoteResult.blockNumber) || 0) > maxBlockAge) return undefined diff --git a/src/lib/hooks/routing/useClientSideSmartOrderRouterTrade.ts b/src/lib/hooks/routing/useClientSideSmartOrderRouterTrade.ts index b2dda4a1a1..294047f9a8 100644 --- a/src/lib/hooks/routing/useClientSideSmartOrderRouterTrade.ts +++ b/src/lib/hooks/routing/useClientSideSmartOrderRouterTrade.ts @@ -2,6 +2,7 @@ import { Protocol } from '@uniswap/router-sdk' import { Currency, CurrencyAmount, TradeType } from '@uniswap/sdk-core' import { ChainId } from '@uniswap/smart-order-router' import useDebounce from 'hooks/useDebounce' +import useLast from 'hooks/useLast' import { useStablecoinAmountFromFiatValue } from 'hooks/useUSDCPrice' import { useCallback, useMemo } from 'react' import { GetQuoteResult, InterfaceTrade, TradeState } from 'state/routing/types' @@ -10,7 +11,7 @@ import { computeRoutes, transformRoutesToTrade } from 'state/routing/utils' import useWrapCallback, { WrapType } from '../swap/useWrapCallback' import useActiveWeb3React from '../useActiveWeb3React' import usePoll from '../usePoll' -import { getClientSideQuote, useFreshQuote } from './clientSideSmartOrderRouter' +import { getClientSideQuote, useFilterFreshQuote } from './clientSideSmartOrderRouter' import { useRoutingAPIArguments } from './useRoutingAPIArguments' /** @@ -78,26 +79,38 @@ export default function useClientSideSmartOrderRouterTrade computeRoutes(currencyIn, currencyOut, tradeType, quoteResult), [currencyIn, currencyOut, quoteResult, tradeType] ) const gasUseEstimateUSD = useStablecoinAmountFromFiatValue(quoteResult?.gasUseEstimateUSD) ?? null - const trade = useMemo(() => { - if (route) { - try { - return route && transformRoutesToTrade(route, tradeType, gasUseEstimateUSD) - } catch (e: unknown) { - console.debug('transformRoutesToTrade failed: ', e) - } - } - return - }, [gasUseEstimateUSD, route, tradeType]) + const trade = + useLast( + useMemo(() => { + if (route) { + try { + return route && transformRoutesToTrade(route, tradeType, gasUseEstimateUSD) + } catch (e: unknown) { + console.debug('transformRoutesToTrade failed: ', e) + } + } + return + }, [gasUseEstimateUSD, route, tradeType]), + Boolean + ) ?? undefined + + // Dont return old trade if currencies dont match. + const isStale = + (currencyIn && !trade?.inputAmount?.currency.equals(currencyIn)) || + (currencyOut && !trade?.outputAmount?.currency.equals(currencyOut)) return useMemo(() => { if (!currencyIn || !currencyOut) { @@ -106,9 +119,11 @@ export default function useClientSideSmartOrderRouterTrade( refetchOnFocus: true, }) - const quoteResult: GetQuoteResult | undefined = useFreshQuote(data) + const quoteResult: GetQuoteResult | undefined = useFilterFreshQuote(data) const route = useMemo( () => computeRoutes(currencyIn, currencyOut, tradeType, quoteResult),