Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
fix: improve how we use blocks per fetch in the memoization of the li…
Browse files Browse the repository at this point in the history
…steners (#1920)

fixes Uniswap/interface#1877
  • Loading branch information
moodysalem authored Jun 28, 2021
1 parent 50c7d36 commit f096112
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/state/multicall/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ export function parseCallKey(callKey: string): Call {

export interface ListenerOptions {
// 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'
)
export const removeMulticallListeners = createAction<{ chainId: number; calls: Call[]; options?: ListenerOptions }>(
export const removeMulticallListeners = createAction<{ chainId: number; calls: Call[]; options: ListenerOptions }>(
'multicall/removeMulticallListeners'
)
export const fetchingMulticallResults = createAction<{ chainId: number; calls: Call[]; fetchingBlockNumber: number }>(
Expand Down
11 changes: 7 additions & 4 deletions src/state/multicall/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export const NEVER_RELOAD: ListenerOptions = {
}

// 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 callResults = useAppSelector((state) => state.multicall.callResults)
const dispatch = useAppDispatch()
Expand All @@ -73,7 +76,7 @@ function useCallsData(calls: (Call | undefined)[], options?: ListenerOptions): C
addMulticallListeners({
chainId,
calls,
options,
options: { blocksPerFetch },
})
)

Expand All @@ -82,11 +85,11 @@ function useCallsData(calls: (Call | undefined)[], options?: ListenerOptions): C
removeMulticallListeners({
chainId,
calls,
options,
options: { blocksPerFetch },
})
)
}
}, [chainId, dispatch, options, serializedCallKeys])
}, [chainId, dispatch, blocksPerFetch, serializedCallKeys])

return useMemo(
() =>
Expand Down
4 changes: 4 additions & 0 deletions src/state/multicall/reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('multicall reducer', () => {
callData: '0x',
},
],
options: { blocksPerFetch: 1 },
})
)
expect(store.getState()).toEqual({
Expand All @@ -58,6 +59,7 @@ describe('multicall reducer', () => {
},
],
chainId: 1,
options: { blocksPerFetch: 1 },
})
)
expect(store.getState()).toEqual({ callResults: {}, callListeners: {} })
Expand All @@ -72,6 +74,7 @@ describe('multicall reducer', () => {
callData: '0x',
},
],
options: { blocksPerFetch: 1 },
})
)
store.dispatch(
Expand All @@ -83,6 +86,7 @@ describe('multicall reducer', () => {
},
],
chainId: 1,
options: { blocksPerFetch: 1 },
})
)
expect(store.getState()).toEqual({
Expand Down
45 changes: 33 additions & 12 deletions src/state/multicall/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,41 @@ const initialState: MulticallState = {

export default createReducer(initialState, (builder) =>
builder
.addCase(addMulticallListeners, (state, { payload: { calls, chainId, options: { blocksPerFetch = 1 } = {} } }) => {
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(
addMulticallListeners,
(
state,
{
payload: {
calls,
chainId,
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(
removeMulticallListeners,
(state, { payload: { chainId, calls, options: { blocksPerFetch = 1 } = {} } }) => {
(
state,
{
payload: {
chainId,
calls,
options: { blocksPerFetch },
},
}
) => {
const listeners: MulticallState['callListeners'] = state.callListeners
? state.callListeners
: (state.callListeners = {})
Expand Down

0 comments on commit f096112

Please sign in to comment.