Commit f0961127 authored by Moody Salem's avatar Moody Salem Committed by GitHub

fix: improve how we use blocks per fetch in the memoization of the listeners (#1920)

fixes https://github.com/Uniswap/uniswap-interface/issues/1877
parent 50c7d361
...@@ -39,13 +39,13 @@ export function parseCallKey(callKey: string): Call { ...@@ -39,13 +39,13 @@ export function parseCallKey(callKey: string): Call {
export interface ListenerOptions { export interface ListenerOptions {
// how often this data should be fetched, by default 1 // how often this data should be fetched, by default 1
readonly blocksPerFetch?: number readonly blocksPerFetch: number
} }
export const addMulticallListeners = createAction<{ chainId: number; calls: Call[]; options?: ListenerOptions }>( export const addMulticallListeners = createAction<{ chainId: number; calls: Call[]; options: ListenerOptions }>(
'multicall/addMulticallListeners' 'multicall/addMulticallListeners'
) )
export const removeMulticallListeners = createAction<{ chainId: number; calls: Call[]; options?: ListenerOptions }>( export const removeMulticallListeners = createAction<{ chainId: number; calls: Call[]; options: ListenerOptions }>(
'multicall/removeMulticallListeners' 'multicall/removeMulticallListeners'
) )
export const fetchingMulticallResults = createAction<{ chainId: number; calls: Call[]; fetchingBlockNumber: number }>( export const fetchingMulticallResults = createAction<{ chainId: number; calls: Call[]; fetchingBlockNumber: number }>(
......
...@@ -48,7 +48,10 @@ export const NEVER_RELOAD: ListenerOptions = { ...@@ -48,7 +48,10 @@ export const NEVER_RELOAD: ListenerOptions = {
} }
// the lowest level call for subscribing to contract data // the lowest level call for subscribing to contract data
function useCallsData(calls: (Call | undefined)[], options?: ListenerOptions): CallResult[] { function useCallsData(
calls: (Call | undefined)[],
{ blocksPerFetch }: ListenerOptions = { blocksPerFetch: 1 }
): CallResult[] {
const { chainId } = useActiveWeb3React() const { chainId } = useActiveWeb3React()
const callResults = useAppSelector((state) => state.multicall.callResults) const callResults = useAppSelector((state) => state.multicall.callResults)
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
...@@ -73,7 +76,7 @@ function useCallsData(calls: (Call | undefined)[], options?: ListenerOptions): C ...@@ -73,7 +76,7 @@ function useCallsData(calls: (Call | undefined)[], options?: ListenerOptions): C
addMulticallListeners({ addMulticallListeners({
chainId, chainId,
calls, calls,
options, options: { blocksPerFetch },
}) })
) )
...@@ -82,11 +85,11 @@ function useCallsData(calls: (Call | undefined)[], options?: ListenerOptions): C ...@@ -82,11 +85,11 @@ function useCallsData(calls: (Call | undefined)[], options?: ListenerOptions): C
removeMulticallListeners({ removeMulticallListeners({
chainId, chainId,
calls, calls,
options, options: { blocksPerFetch },
}) })
) )
} }
}, [chainId, dispatch, options, serializedCallKeys]) }, [chainId, dispatch, blocksPerFetch, serializedCallKeys])
return useMemo( return useMemo(
() => () =>
......
...@@ -32,6 +32,7 @@ describe('multicall reducer', () => { ...@@ -32,6 +32,7 @@ describe('multicall reducer', () => {
callData: '0x', callData: '0x',
}, },
], ],
options: { blocksPerFetch: 1 },
}) })
) )
expect(store.getState()).toEqual({ expect(store.getState()).toEqual({
...@@ -58,6 +59,7 @@ describe('multicall reducer', () => { ...@@ -58,6 +59,7 @@ describe('multicall reducer', () => {
}, },
], ],
chainId: 1, chainId: 1,
options: { blocksPerFetch: 1 },
}) })
) )
expect(store.getState()).toEqual({ callResults: {}, callListeners: {} }) expect(store.getState()).toEqual({ callResults: {}, callListeners: {} })
...@@ -72,6 +74,7 @@ describe('multicall reducer', () => { ...@@ -72,6 +74,7 @@ describe('multicall reducer', () => {
callData: '0x', callData: '0x',
}, },
], ],
options: { blocksPerFetch: 1 },
}) })
) )
store.dispatch( store.dispatch(
...@@ -83,6 +86,7 @@ describe('multicall reducer', () => { ...@@ -83,6 +86,7 @@ describe('multicall reducer', () => {
}, },
], ],
chainId: 1, chainId: 1,
options: { blocksPerFetch: 1 },
}) })
) )
expect(store.getState()).toEqual({ expect(store.getState()).toEqual({
......
...@@ -37,20 +37,41 @@ const initialState: MulticallState = { ...@@ -37,20 +37,41 @@ const initialState: MulticallState = {
export default createReducer(initialState, (builder) => export default createReducer(initialState, (builder) =>
builder builder
.addCase(addMulticallListeners, (state, { payload: { calls, chainId, options: { blocksPerFetch = 1 } = {} } }) => { .addCase(
const listeners: MulticallState['callListeners'] = state.callListeners addMulticallListeners,
? state.callListeners (
: (state.callListeners = {}) state,
listeners[chainId] = listeners[chainId] ?? {} {
calls.forEach((call) => { payload: {
const callKey = toCallKey(call) calls,
listeners[chainId][callKey] = listeners[chainId][callKey] ?? {} chainId,
listeners[chainId][callKey][blocksPerFetch] = (listeners[chainId][callKey][blocksPerFetch] ?? 0) + 1 options: { blocksPerFetch },
}) },
}) }
) => {
const listeners: MulticallState['callListeners'] = state.callListeners
? state.callListeners
: (state.callListeners = {})
listeners[chainId] = listeners[chainId] ?? {}
calls.forEach((call) => {
const callKey = toCallKey(call)
listeners[chainId][callKey] = listeners[chainId][callKey] ?? {}
listeners[chainId][callKey][blocksPerFetch] = (listeners[chainId][callKey][blocksPerFetch] ?? 0) + 1
})
}
)
.addCase( .addCase(
removeMulticallListeners, removeMulticallListeners,
(state, { payload: { chainId, calls, options: { blocksPerFetch = 1 } = {} } }) => { (
state,
{
payload: {
chainId,
calls,
options: { blocksPerFetch },
},
}
) => {
const listeners: MulticallState['callListeners'] = state.callListeners const listeners: MulticallState['callListeners'] = state.callListeners
? state.callListeners ? state.callListeners
: (state.callListeners = {}) : (state.callListeners = {})
......
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