Commit 29db0a50 authored by Moody Salem's avatar Moody Salem

fix(multicall): reduce spam in error case

parent 9566fb88
import { addMulticallListeners, removeMulticallListeners, updateMulticallResults } from './actions' import {
addMulticallListeners,
errorFetchingMulticallResults,
fetchingMulticallResults,
removeMulticallListeners,
updateMulticallResults
} from './actions'
import reducer, { MulticallState } from './reducer' import reducer, { MulticallState } from './reducer'
import { Store, createStore } from '@reduxjs/toolkit' import { Store, createStore } from '@reduxjs/toolkit'
...@@ -169,4 +175,89 @@ describe('multicall reducer', () => { ...@@ -169,4 +175,89 @@ describe('multicall reducer', () => {
}) })
}) })
}) })
describe('fetchingMulticallResults', () => {
it('updates state to fetching', () => {
store.dispatch(
fetchingMulticallResults({
chainId: 1,
fetchingBlockNumber: 2,
calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
})
)
expect(store.getState()).toEqual({
callResults: {
[1]: {
[`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 2 }
}
}
})
})
})
describe('errorFetchingMulticallResults', () => {
it('does nothing if not fetching', () => {
store.dispatch(
errorFetchingMulticallResults({
chainId: 1,
fetchingBlockNumber: 1,
calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
})
)
expect(store.getState()).toEqual({
callResults: {
[1]: {}
}
})
})
it('updates block number if we were fetching', () => {
store.dispatch(
fetchingMulticallResults({
chainId: 1,
fetchingBlockNumber: 2,
calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
})
)
store.dispatch(
errorFetchingMulticallResults({
chainId: 1,
fetchingBlockNumber: 2,
calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
})
)
expect(store.getState()).toEqual({
callResults: {
[1]: {
[`${DAI_ADDRESS}-0x0`]: {
blockNumber: 2,
// null data indicates error
data: null
}
}
}
})
})
it('does nothing if not errored on latest block', () => {
store.dispatch(
fetchingMulticallResults({
chainId: 1,
fetchingBlockNumber: 3,
calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
})
)
store.dispatch(
errorFetchingMulticallResults({
chainId: 1,
fetchingBlockNumber: 2,
calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
})
)
expect(store.getState()).toEqual({
callResults: {
[1]: {
[`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 3 }
}
}
})
})
})
}) })
...@@ -89,8 +89,12 @@ export default createReducer(initialState, builder => ...@@ -89,8 +89,12 @@ export default createReducer(initialState, builder =>
calls.forEach(call => { calls.forEach(call => {
const callKey = toCallKey(call) const callKey = toCallKey(call)
const current = state.callResults[chainId][callKey] const current = state.callResults[chainId][callKey]
if (current && current.fetchingBlockNumber !== fetchingBlockNumber) return if (!current) return // only should be dispatched if we are already fetching
delete current.fetchingBlockNumber if (current.fetchingBlockNumber === fetchingBlockNumber) {
delete current.fetchingBlockNumber
current.data = null
current.blockNumber = fetchingBlockNumber
}
}) })
}) })
.addCase(updateMulticallResults, (state, { payload: { chainId, results, blockNumber } }) => { .addCase(updateMulticallResults, (state, { payload: { chainId, results, blockNumber } }) => {
......
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