-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
50 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,25 @@ | ||
import { useMemo } from 'react'; | ||
import { type TokenListMap } from '@metamask/assets-controllers'; | ||
import contractMap from '@metamask/contract-metadata'; | ||
|
||
import { TokenListToken } from '@metamask/assets-controllers'; | ||
import { useSelector } from 'react-redux'; | ||
import { selectChainId } from '../../../selectors/networkController'; | ||
import { selectUseTokenDetection } from '../../../selectors/preferencesController'; | ||
import { selectTokenList } from '../../../selectors/tokenListController'; | ||
import { selectTokenListArray } from '../../../selectors/tokenListController'; | ||
import { isMainnetByChainId } from '../../../util/networks'; | ||
|
||
function normalizeTokenAddresses(tokenMap: TokenListMap) { | ||
return Object.keys(tokenMap).reduce((acc, address) => { | ||
const tokenMetadata = tokenMap[address]; | ||
return { | ||
...acc, | ||
[address.toLowerCase()]: { | ||
...tokenMetadata, | ||
}, | ||
}; | ||
}, {}); | ||
} | ||
|
||
const NORMALIZED_MAINNET_TOKEN_LIST = normalizeTokenAddresses(contractMap); | ||
const NORMALIZED_MAINNET_TOKEN_ARRAY = Object.values(contractMap); | ||
|
||
export default function useTokenList(): TokenListMap { | ||
export default function useTokenList(): TokenListToken[] { | ||
const chainId = useSelector(selectChainId); | ||
const isMainnet = isMainnetByChainId(chainId); | ||
const isTokenDetectionEnabled = useSelector(selectUseTokenDetection); | ||
const tokenList = useSelector(selectTokenList); | ||
const tokenListArray = useSelector(selectTokenListArray); | ||
const shouldUseStaticList = !isTokenDetectionEnabled && isMainnet; | ||
|
||
return useMemo(() => { | ||
if (shouldUseStaticList) { | ||
return NORMALIZED_MAINNET_TOKEN_LIST; | ||
return NORMALIZED_MAINNET_TOKEN_ARRAY; | ||
} | ||
return normalizeTokenAddresses(tokenList); | ||
}, [shouldUseStaticList, tokenList]); | ||
return tokenListArray; | ||
}, [shouldUseStaticList, tokenListArray]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters