Commit 9bccb7ae authored by Moody Salem's avatar Moody Salem

fix(multicall): better use the block number returned from calls, and cancel...

fix(multicall): better use the block number returned from calls, and cancel all calls when a new block comes in
parent 8a2e4123
...@@ -5,6 +5,7 @@ import { useMulticall2Contract } from '../../hooks/useContract' ...@@ -5,6 +5,7 @@ import { useMulticall2Contract } from '../../hooks/useContract'
import useDebounce from '../../hooks/useDebounce' import useDebounce from '../../hooks/useDebounce'
import chunkArray from '../../utils/chunkArray' import chunkArray from '../../utils/chunkArray'
import { retry, RetryableError } from '../../utils/retry' import { retry, RetryableError } from '../../utils/retry'
import { updateBlockNumber } from '../application/actions'
import { useBlockNumber } from '../application/hooks' import { useBlockNumber } from '../application/hooks'
import { AppState } from '../index' import { AppState } from '../index'
import { import {
...@@ -149,8 +150,8 @@ export default function Updater(): null { ...@@ -149,8 +150,8 @@ export default function Updater(): null {
const chunkedCalls = chunkArray(calls) const chunkedCalls = chunkArray(calls)
if (cancellations.current?.blockNumber !== latestBlockNumber) { if (cancellations.current && cancellations.current.blockNumber !== latestBlockNumber) {
cancellations.current?.cancellations?.forEach((c) => c()) cancellations.current.cancellations.forEach((c) => c())
} }
dispatch( dispatch(
...@@ -171,8 +172,6 @@ export default function Updater(): null { ...@@ -171,8 +172,6 @@ export default function Updater(): null {
}) })
promise promise
.then(({ results: returnData, blockNumber: fetchBlockNumber }) => { .then(({ results: returnData, blockNumber: fetchBlockNumber }) => {
cancellations.current = { cancellations: [], blockNumber: latestBlockNumber }
// accumulates the length of all previous indices // accumulates the length of all previous indices
const firstCallKeyIndex = chunkedCalls.slice(0, index).reduce<number>((memo, curr) => memo + curr.length, 0) const firstCallKeyIndex = chunkedCalls.slice(0, index).reduce<number>((memo, curr) => memo + curr.length, 0)
const lastCallKeyIndex = firstCallKeyIndex + returnData.length const lastCallKeyIndex = firstCallKeyIndex + returnData.length
...@@ -216,10 +215,14 @@ export default function Updater(): null { ...@@ -216,10 +215,14 @@ export default function Updater(): null {
}) })
) )
} }
if (fetchBlockNumber > latestBlockNumber) {
dispatch(updateBlockNumber({ chainId, blockNumber: fetchBlockNumber }))
}
}) })
.catch((error: any) => { .catch((error: any) => {
if (error.isCancelledError) { if (error.isCancelledError) {
console.debug('Cancelled fetch for blockNumber', latestBlockNumber) console.debug('Cancelled fetch for blockNumber', latestBlockNumber, chunk, chainId)
return return
} }
console.error('Failed to fetch multicall chunk', chunk, chainId, error) console.error('Failed to fetch multicall chunk', chunk, chainId, error)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment