Skip to content

Commit

Permalink
fix(L2): update block warning updater to check most recent block time…
Browse files Browse the repository at this point in the history
…stamp (#2777)

* update block warning updater to check most recent block timestamp

* stop doing dumb state manipulation
  • Loading branch information
JFrankfurt authored Nov 12, 2021
1 parent b79fe4b commit d0be3bf
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/state/application/updater.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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() {
Expand All @@ -35,16 +35,26 @@ function useBlockWarningTimer() {
const timeout = useRef<NodeJS.Timeout>()
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) {
Expand All @@ -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 {
Expand Down

0 comments on commit d0be3bf

Please sign in to comment.