fix: resolve some tokens not searchable by contract address in token selector cx bug (#4988)

* init

* ask zzmp question

* fixes omg

* remove comment

* comments

* respond to zzmp

* fix
This commit is contained in:
lynn 2022-10-24 23:53:42 -04:00 committed by GitHub
parent 8be9701700
commit 8fca286099
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 5 deletions

@ -6,6 +6,11 @@ import { SupportedChainId } from './chains'
export const NATIVE_CHAIN_ID = 'NATIVE' export const NATIVE_CHAIN_ID = 'NATIVE'
// When decimals are not specified for an ERC20 token
// use default ERC20 token decimals as specified here:
// https://docs.openzeppelin.com/contracts/3.x/erc20
export const DEFAULT_ERC20_DECIMALS = 18
export const USDC_MAINNET = new Token( export const USDC_MAINNET = new Token(
SupportedChainId.MAINNET, SupportedChainId.MAINNET,
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',

@ -12,6 +12,7 @@ import useNativeCurrency from 'lib/hooks/useNativeCurrency'
import { useEffect, useMemo, useState } from 'react' import { useEffect, useMemo, useState } from 'react'
import { WrappedTokenInfo } from 'state/lists/wrappedTokenInfo' import { WrappedTokenInfo } from 'state/lists/wrappedTokenInfo'
import { DEFAULT_ERC20_DECIMALS } from '../../constants/tokens'
import { TOKEN_SHORTHANDS } from '../../constants/tokens' import { TOKEN_SHORTHANDS } from '../../constants/tokens'
import { getContract, isAddress } from '../../utils' import { getContract, isAddress } from '../../utils'
import { supportedChainId } from '../../utils/supportedChainId' import { supportedChainId } from '../../utils/supportedChainId'
@ -121,7 +122,8 @@ export function useTokenFromActiveNetwork(tokenAddress: string | undefined): Tok
() => decimals.loading || symbol.loading || tokenName.loading, () => decimals.loading || symbol.loading || tokenName.loading,
[decimals.loading, symbol.loading, tokenName.loading] [decimals.loading, symbol.loading, tokenName.loading]
) )
const parsedDecimals = useMemo(() => decimals.result?.[0], [decimals.result]) const parsedDecimals = useMemo(() => decimals?.result?.[0] ?? DEFAULT_ERC20_DECIMALS, [decimals.result])
const parsedSymbol = useMemo( const parsedSymbol = useMemo(
() => parseStringOrBytes32(symbol.result?.[0], symbolBytes32.result?.[0], 'UNKNOWN'), () => parseStringOrBytes32(symbol.result?.[0], symbolBytes32.result?.[0], 'UNKNOWN'),
[symbol.result, symbolBytes32.result] [symbol.result, symbolBytes32.result]
@ -134,9 +136,7 @@ export function useTokenFromActiveNetwork(tokenAddress: string | undefined): Tok
return useMemo(() => { return useMemo(() => {
// If the token is on another chain, we cannot fetch it on-chain, and it is invalid. // If the token is on another chain, we cannot fetch it on-chain, and it is invalid.
if (typeof tokenAddress !== 'string' || !isSupportedChain(chainId) || !formattedAddress) return undefined if (typeof tokenAddress !== 'string' || !isSupportedChain(chainId) || !formattedAddress) return undefined
if (isLoading || !chainId) return null if (isLoading || !chainId) return null
if (!parsedDecimals) return undefined
return new Token(chainId, formattedAddress, parsedDecimals, parsedSymbol, parsedName) return new Token(chainId, formattedAddress, parsedDecimals, parsedSymbol, parsedName)
}, [chainId, tokenAddress, formattedAddress, isLoading, parsedDecimals, parsedSymbol, parsedName]) }, [chainId, tokenAddress, formattedAddress, isLoading, parsedDecimals, parsedSymbol, parsedName])
@ -152,7 +152,6 @@ type TokenMap = { [address: string]: Token }
export function useTokenFromMapOrNetwork(tokens: TokenMap, tokenAddress?: string | null): Token | null | undefined { export function useTokenFromMapOrNetwork(tokens: TokenMap, tokenAddress?: string | null): Token | null | undefined {
const address = isAddress(tokenAddress) const address = isAddress(tokenAddress)
const token: Token | undefined = address ? tokens[address] : undefined const token: Token | undefined = address ? tokens[address] : undefined
const tokenFromNetwork = useTokenFromActiveNetwork(token ? undefined : address ? address : undefined) const tokenFromNetwork = useTokenFromActiveNetwork(token ? undefined : address ? address : undefined)
return tokenFromNetwork ?? token return tokenFromNetwork ?? token

@ -8,7 +8,9 @@ import { ChainTokenMap } from 'lib/hooks/useTokenList/utils'
// returns the checksummed address if the address is valid, otherwise returns false // returns the checksummed address if the address is valid, otherwise returns false
export function isAddress(value: any): string | false { export function isAddress(value: any): string | false {
try { try {
return getAddress(value) // Alphabetical letters must be made lowercase for getAddress to work.
// See documentation here: https://docs.ethers.io/v5/api/utils/address/
return getAddress(value.toLowerCase())
} catch { } catch {
return false return false
} }