From 49ae6ef6bd9005af3705cb6069fab2e55e7ecb51 Mon Sep 17 00:00:00 2001 From: Jordan Frankfurt Date: Tue, 22 Aug 2023 14:52:59 -0500 Subject: [PATCH] fix: ensure lists are present before running update (#7206) * fix: ensure lists are present before running update * pr input from zzmp --- src/constants/tokenSafetyLookup.ts | 6 +++--- src/hooks/useEagerlyConnect.ts | 4 +++- src/hooks/useStateRehydrated.ts | 5 +++++ src/state/lists/updater.ts | 6 ++++-- 4 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 src/hooks/useStateRehydrated.ts diff --git a/src/constants/tokenSafetyLookup.ts b/src/constants/tokenSafetyLookup.ts index fd36ab9d12..18fc115dde 100644 --- a/src/constants/tokenSafetyLookup.ts +++ b/src/constants/tokenSafetyLookup.ts @@ -24,12 +24,12 @@ class TokenSafetyLookupTable { this.initialized = true // Initialize extended tokens first - lists.byUrl[UNI_EXTENDED_LIST].current?.tokens.forEach((token) => { + lists.byUrl[UNI_EXTENDED_LIST]?.current?.tokens.forEach((token) => { this.dict[token.address.toLowerCase()] = TOKEN_LIST_TYPES.UNI_EXTENDED }) // Initialize default tokens second, so that any tokens on both default and extended will display as default (no warning) - lists.byUrl[UNI_LIST].current?.tokens.forEach((token) => { + lists.byUrl[UNI_LIST]?.current?.tokens.forEach((token) => { this.dict[token.address.toLowerCase()] = TOKEN_LIST_TYPES.UNI_DEFAULT }) @@ -39,7 +39,7 @@ class TokenSafetyLookupTable { }) // Initialize blocked tokens from all urls included - UNSUPPORTED_LIST_URLS.map((url) => lists.byUrl[url].current?.tokens) + UNSUPPORTED_LIST_URLS.map((url) => lists.byUrl[url]?.current?.tokens) .filter((x): x is TokenInfo[] => !!x) .flat(1) .forEach((token) => { diff --git a/src/hooks/useEagerlyConnect.ts b/src/hooks/useEagerlyConnect.ts index b7c969b5f1..1a3e94e624 100644 --- a/src/hooks/useEagerlyConnect.ts +++ b/src/hooks/useEagerlyConnect.ts @@ -5,6 +5,8 @@ import { useEffect } from 'react' import { useAppDispatch, useAppSelector } from 'state/hooks' import { updateSelectedWallet } from 'state/user/reducer' +import { useStateRehydrated } from './useStateRehydrated' + async function connect(connector: Connector) { try { if (connector.connectEagerly) { @@ -21,7 +23,7 @@ export default function useEagerlyConnect() { const dispatch = useAppDispatch() const selectedWallet = useAppSelector((state) => state.user.selectedWallet) - const rehydrated = useAppSelector((state) => state._persist.rehydrated) + const rehydrated = useStateRehydrated() useEffect(() => { try { diff --git a/src/hooks/useStateRehydrated.ts b/src/hooks/useStateRehydrated.ts new file mode 100644 index 0000000000..0a60e2affa --- /dev/null +++ b/src/hooks/useStateRehydrated.ts @@ -0,0 +1,5 @@ +import { useAppSelector } from 'state/hooks' + +export function useStateRehydrated() { + return useAppSelector((state) => state._persist.rehydrated) +} diff --git a/src/state/lists/updater.ts b/src/state/lists/updater.ts index b61f93ead7..655aed67d8 100644 --- a/src/state/lists/updater.ts +++ b/src/state/lists/updater.ts @@ -2,6 +2,7 @@ import { getVersionUpgrade, VersionUpgrade } from '@uniswap/token-lists' import { useWeb3React } from '@web3-react/core' import { DEFAULT_LIST_OF_LISTS, UNSUPPORTED_LIST_URLS } from 'constants/lists' import TokenSafetyLookupTable from 'constants/tokenSafetyLookup' +import { useStateRehydrated } from 'hooks/useStateRehydrated' import useInterval from 'lib/hooks/useInterval' import ms from 'ms' import { useCallback, useEffect } from 'react' @@ -21,10 +22,11 @@ export default function Updater(): null { // get all loaded lists, and the active urls const lists = useAllLists() const listsState = useAppSelector((state) => state.lists) + const rehydrated = useStateRehydrated() useEffect(() => { - TokenSafetyLookupTable.update(listsState) - }, [listsState]) + if (rehydrated) TokenSafetyLookupTable.update(listsState) + }, [listsState, rehydrated]) const fetchList = useFetchListCallback() const fetchAllListsCallback = useCallback(() => {