fix: Remove block number check for quotes (#6148)

* remove block number check

* add refetchOnMountOrArgChange
This commit is contained in:
Tina 2023-03-14 16:30:31 -04:00 committed by GitHub
parent b0aea5f62a
commit bc251230da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 38 deletions

@ -1,31 +0,0 @@
import { useWeb3React } from '@web3-react/core'
import { atomWithImmer } from 'jotai/immer'
import { useAtomValue } from 'jotai/utils'
import { useCallback } from 'react'
import useBlockNumber from './useBlockNumber'
// The oldest block (per chain) to be considered valid.
const oldestBlockMapAtom = atomWithImmer<{ [chainId: number]: number }>({})
const DEFAULT_MAX_BLOCK_AGE = 10
function useGetIsValidBlock(maxBlockAge = DEFAULT_MAX_BLOCK_AGE): (block: number) => boolean {
const { chainId } = useWeb3React()
const currentBlock = useBlockNumber()
const oldestBlockMap = useAtomValue(oldestBlockMapAtom)
const oldestBlock = chainId ? oldestBlockMap[chainId] : 0
return useCallback(
(block: number) => {
if (!currentBlock) return false
if (currentBlock - block > maxBlockAge) return false
if (currentBlock < oldestBlock) return false
return true
},
[currentBlock, maxBlockAge, oldestBlock]
)
}
export default function useIsValidBlock(block: number): boolean {
return useGetIsValidBlock()(block)
}

@ -5,12 +5,11 @@ import { sendTiming } from 'components/analytics'
import { AVERAGE_L1_BLOCK_TIME } from 'constants/chainInfo'
import { useStablecoinAmountFromFiatValue } from 'hooks/useStablecoinPrice'
import { useRoutingAPIArguments } from 'lib/hooks/routing/useRoutingAPIArguments'
import useIsValidBlock from 'lib/hooks/useIsValidBlock'
import ms from 'ms.macro'
import { useMemo } from 'react'
import { RouterPreference, useGetQuoteQuery } from 'state/routing/slice'
import { GetQuoteResult, InterfaceTrade, TradeState } from './types'
import { InterfaceTrade, TradeState } from './types'
import { computeRoutes, transformRoutesToTrade } from './utils'
/**
@ -44,13 +43,18 @@ export function useRoutingAPITrade<TTradeType extends TradeType>(
routerPreference,
})
const { isLoading, isError, data, currentData } = useGetQuoteQuery(queryArgs ?? skipToken, {
const {
isLoading,
isError,
data: quoteResult,
currentData,
} = useGetQuoteQuery(queryArgs ?? skipToken, {
// Price-fetching is informational and costly, so it's done less frequently.
pollingInterval: routerPreference === RouterPreference.PRICE ? ms`2m` : AVERAGE_L1_BLOCK_TIME,
pollingInterval: routerPreference === RouterPreference.PRICE ? ms`1m` : AVERAGE_L1_BLOCK_TIME,
// If latest quote from cache was fetched > 2m ago, instantly repoll for another instead of waiting for next poll period
refetchOnMountOrArgChange: 2 * 60,
})
const quoteResult: GetQuoteResult | undefined = useIsValidBlock(Number(data?.blockNumber) || 0) ? data : undefined
const route = useMemo(
() => computeRoutes(currencyIn, currencyOut, tradeType, quoteResult),
[currencyIn, currencyOut, quoteResult, tradeType]
@ -59,7 +63,7 @@ export function useRoutingAPITrade<TTradeType extends TradeType>(
// get USD gas cost of trade in active chains stablecoin amount
const gasUseEstimateUSD = useStablecoinAmountFromFiatValue(quoteResult?.gasUseEstimateUSD) ?? null
const isSyncing = currentData !== data
const isSyncing = currentData !== quoteResult
return useMemo(() => {
if (!currencyIn || !currencyOut || currencyIn.equals(currencyOut)) {