Skip to content

Commit 29db0a5

Browse files
committed
fix(multicall): reduce spam in error case
1 parent 9566fb8 commit 29db0a5

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

src/state/multicall/reducer.test.ts

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { addMulticallListeners, removeMulticallListeners, updateMulticallResults } from './actions'
1+
import {
2+
addMulticallListeners,
3+
errorFetchingMulticallResults,
4+
fetchingMulticallResults,
5+
removeMulticallListeners,
6+
updateMulticallResults
7+
} from './actions'
28
import reducer, { MulticallState } from './reducer'
39
import { Store, createStore } from '@reduxjs/toolkit'
410

@@ -169,4 +175,89 @@ describe('multicall reducer', () => {
169175
})
170176
})
171177
})
178+
describe('fetchingMulticallResults', () => {
179+
it('updates state to fetching', () => {
180+
store.dispatch(
181+
fetchingMulticallResults({
182+
chainId: 1,
183+
fetchingBlockNumber: 2,
184+
calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
185+
})
186+
)
187+
expect(store.getState()).toEqual({
188+
callResults: {
189+
[1]: {
190+
[`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 2 }
191+
}
192+
}
193+
})
194+
})
195+
})
196+
197+
describe('errorFetchingMulticallResults', () => {
198+
it('does nothing if not fetching', () => {
199+
store.dispatch(
200+
errorFetchingMulticallResults({
201+
chainId: 1,
202+
fetchingBlockNumber: 1,
203+
calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
204+
})
205+
)
206+
expect(store.getState()).toEqual({
207+
callResults: {
208+
[1]: {}
209+
}
210+
})
211+
})
212+
it('updates block number if we were fetching', () => {
213+
store.dispatch(
214+
fetchingMulticallResults({
215+
chainId: 1,
216+
fetchingBlockNumber: 2,
217+
calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
218+
})
219+
)
220+
store.dispatch(
221+
errorFetchingMulticallResults({
222+
chainId: 1,
223+
fetchingBlockNumber: 2,
224+
calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
225+
})
226+
)
227+
expect(store.getState()).toEqual({
228+
callResults: {
229+
[1]: {
230+
[`${DAI_ADDRESS}-0x0`]: {
231+
blockNumber: 2,
232+
// null data indicates error
233+
data: null
234+
}
235+
}
236+
}
237+
})
238+
})
239+
it('does nothing if not errored on latest block', () => {
240+
store.dispatch(
241+
fetchingMulticallResults({
242+
chainId: 1,
243+
fetchingBlockNumber: 3,
244+
calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
245+
})
246+
)
247+
store.dispatch(
248+
errorFetchingMulticallResults({
249+
chainId: 1,
250+
fetchingBlockNumber: 2,
251+
calls: [{ address: DAI_ADDRESS, callData: '0x0' }]
252+
})
253+
)
254+
expect(store.getState()).toEqual({
255+
callResults: {
256+
[1]: {
257+
[`${DAI_ADDRESS}-0x0`]: { fetchingBlockNumber: 3 }
258+
}
259+
}
260+
})
261+
})
262+
})
172263
})

src/state/multicall/reducer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ export default createReducer(initialState, builder =>
8989
calls.forEach(call => {
9090
const callKey = toCallKey(call)
9191
const current = state.callResults[chainId][callKey]
92-
if (current && current.fetchingBlockNumber !== fetchingBlockNumber) return
93-
delete current.fetchingBlockNumber
92+
if (!current) return // only should be dispatched if we are already fetching
93+
if (current.fetchingBlockNumber === fetchingBlockNumber) {
94+
delete current.fetchingBlockNumber
95+
current.data = null
96+
current.blockNumber = fetchingBlockNumber
97+
}
9498
})
9599
})
96100
.addCase(updateMulticallResults, (state, { payload: { chainId, results, blockNumber } }) => {

0 commit comments

Comments
 (0)