From d0be3bf222fb3bcd5f8ecf92d3eacc9f2d75c3e8 Mon Sep 17 00:00:00 2001 From: Jordan Frankfurt Date: Fri, 12 Nov 2021 09:18:25 -0600 Subject: [PATCH] fix(L2): update block warning updater to check most recent block timestamp (#2777) * update block warning updater to check most recent block timestamp * stop doing dumb state manipulation --- src/state/application/updater.ts | 34 ++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/state/application/updater.ts b/src/state/application/updater.ts index 54062bdea2..d1b7dba13e 100644 --- a/src/state/application/updater.ts +++ b/src/state/application/updater.ts @@ -1,4 +1,5 @@ import { CHAIN_INFO } from 'constants/chains' +import useCurrentBlockTimestamp from 'hooks/useCurrentBlockTimestamp' import useDebounce from 'hooks/useDebounce' import useIsWindowVisible from 'hooks/useIsWindowVisible' import { useActiveWeb3React } from 'hooks/web3' @@ -9,7 +10,6 @@ import { useAppDispatch, useAppSelector } from 'state/hooks' import { supportedChainId } from 'utils/supportedChainId' import { switchToNetwork } from 'utils/switchToNetwork' -import { useBlockNumber } from './hooks' import { setChainConnectivityWarning, setImplements3085, updateBlockNumber, updateChainId } from './reducer' function useQueryCacheInvalidator() { @@ -35,16 +35,26 @@ function useBlockWarningTimer() { const timeout = useRef() const isWindowVisible = useIsWindowVisible() const [msSinceLastBlock, setMsSinceLastBlock] = useState(0) - const currentBlock = useBlockNumber() + const blockTimestamp = useCurrentBlockTimestamp() - useEffect(() => { - setMsSinceLastBlock(0) - }, [currentBlock]) + const waitMsBeforeWarning = + (chainId ? CHAIN_INFO[chainId]?.blockWaitMsBeforeWarning : DEFAULT_MS_BEFORE_WARNING) ?? DEFAULT_MS_BEFORE_WARNING useEffect(() => { - const waitMsBeforeWarning = - (chainId ? CHAIN_INFO[chainId]?.blockWaitMsBeforeWarning : DEFAULT_MS_BEFORE_WARNING) ?? DEFAULT_MS_BEFORE_WARNING + if (blockTimestamp && chainId) { + if (Math.floor(Date.now() - blockTimestamp.mul(1000).toNumber()) > waitMsBeforeWarning) { + if (!chainConnectivityWarningActive) { + dispatch(setChainConnectivityWarning({ warn: true })) + } + } else { + if (chainConnectivityWarningActive) { + dispatch(setChainConnectivityWarning({ warn: false })) + } + } + } + }, [blockTimestamp, chainId, chainConnectivityWarningActive, dispatch, waitMsBeforeWarning]) + useEffect(() => { timeout.current = setTimeout(() => { setMsSinceLastBlock(NETWORK_HEALTH_CHECK_MS + msSinceLastBlock) if (msSinceLastBlock > waitMsBeforeWarning && isWindowVisible) { @@ -59,7 +69,15 @@ function useBlockWarningTimer() { clearTimeout(timeout.current) } } - }, [chainId, chainConnectivityWarningActive, dispatch, isWindowVisible, msSinceLastBlock, setMsSinceLastBlock]) + }, [ + chainId, + chainConnectivityWarningActive, + dispatch, + isWindowVisible, + msSinceLastBlock, + setMsSinceLastBlock, + waitMsBeforeWarning, + ]) } export default function Updater(): null {