Commit 2fc3f3c0 authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: network token memoization (#4877)

* build: upgrade redux-multicall

* fix: memoize network token

* docs: invalid token
parent 21e0faeb
...@@ -31,50 +31,45 @@ function parseStringOrBytes32(str: string | undefined, bytes32: string | undefin ...@@ -31,50 +31,45 @@ function parseStringOrBytes32(str: string | undefined, bytes32: string | undefin
*/ */
export function useTokenFromNetwork( export function useTokenFromNetwork(
tokenAddress: string | null | undefined, tokenAddress: string | null | undefined,
chainId?: number tokenChainId?: number
): Token | null | undefined { ): Token | null | undefined {
const web3ReactChainId = useWeb3React().chainId const { chainId } = useWeb3React()
if (!chainId) chainId = web3ReactChainId
const supportedChain = isSupportedChain(chainId)
const formattedAddress = isAddress(tokenAddress) const formattedAddress = isAddress(tokenAddress)
const tokenContract = useTokenContract(formattedAddress ? formattedAddress : undefined, false) const tokenContract = useTokenContract(formattedAddress ? formattedAddress : undefined, false)
const tokenContractBytes32 = useBytes32TokenContract(formattedAddress ? formattedAddress : undefined, false) const tokenContractBytes32 = useBytes32TokenContract(formattedAddress ? formattedAddress : undefined, false)
// TODO: Fix redux-multicall so that these values do not reload.
const tokenName = useSingleCallResult(tokenContract, 'name', undefined, NEVER_RELOAD) const tokenName = useSingleCallResult(tokenContract, 'name', undefined, NEVER_RELOAD)
const tokenNameBytes32 = useSingleCallResult(tokenContractBytes32, 'name', undefined, NEVER_RELOAD) const tokenNameBytes32 = useSingleCallResult(tokenContractBytes32, 'name', undefined, NEVER_RELOAD)
const symbol = useSingleCallResult(tokenContract, 'symbol', undefined, NEVER_RELOAD) const symbol = useSingleCallResult(tokenContract, 'symbol', undefined, NEVER_RELOAD)
const symbolBytes32 = useSingleCallResult(tokenContractBytes32, 'symbol', undefined, NEVER_RELOAD) const symbolBytes32 = useSingleCallResult(tokenContractBytes32, 'symbol', undefined, NEVER_RELOAD)
const decimals = useSingleCallResult(tokenContract, 'decimals', undefined, NEVER_RELOAD) const decimals = useSingleCallResult(tokenContract, 'decimals', undefined, NEVER_RELOAD)
return useMemo(() => { const isLoading = useMemo(
if (typeof tokenAddress !== 'string' || !supportedChain || !formattedAddress) return undefined () => decimals.loading || symbol.loading || tokenName.loading,
if (decimals.loading || symbol.loading || tokenName.loading || !chainId) return null [decimals.loading, symbol.loading, tokenName.loading]
if (decimals.result) { )
return new Token( const parsedDecimals = useMemo(() => decimals.result?.[0], [decimals.result])
chainId, const parsedSymbol = useMemo(
formattedAddress, () => parseStringOrBytes32(symbol.result?.[0], symbolBytes32.result?.[0], 'UNKNOWN'),
decimals.result[0], [symbol.result, symbolBytes32.result]
parseStringOrBytes32(symbol.result?.[0], symbolBytes32.result?.[0], 'UNKNOWN'), )
parseStringOrBytes32(tokenName.result?.[0], tokenNameBytes32.result?.[0], 'Unknown Token') const parsedName = useMemo(
() => parseStringOrBytes32(tokenName.result?.[0], tokenNameBytes32.result?.[0], 'Unknown Token'),
[tokenName.result, tokenNameBytes32.result]
) )
}
return undefined return useMemo(() => {
}, [ // If the token is on another chain, we cannot fetch it on-chain, and it is invalid.
formattedAddress, if (tokenChainId !== undefined && tokenChainId !== chainId) return undefined
chainId, if (typeof tokenAddress !== 'string' || !isSupportedChain(chainId) || !formattedAddress) return undefined
supportedChain,
decimals.loading, if (isLoading || !chainId) return null
decimals.result, if (!parsedDecimals) return undefined
symbol.loading,
symbol.result, return new Token(chainId, formattedAddress, parsedDecimals, parsedSymbol, parsedName)
symbolBytes32.result, }, [tokenChainId, chainId, tokenAddress, formattedAddress, isLoading, parsedDecimals, parsedSymbol, parsedName])
tokenAddress,
tokenName.loading,
tokenName.result,
tokenNameBytes32.result,
])
} }
type TokenMap = { [address: string]: Token } type TokenMap = { [address: string]: Token }
...@@ -109,8 +104,7 @@ export function useCurrencyFromMap(tokens: TokenMap, currencyId?: string | null) ...@@ -109,8 +104,7 @@ export function useCurrencyFromMap(tokens: TokenMap, currencyId?: string | null)
const token = useTokenFromMapOrNetwork(tokens, isNative ? undefined : shorthandMatchAddress ?? currencyId) const token = useTokenFromMapOrNetwork(tokens, isNative ? undefined : shorthandMatchAddress ?? currencyId)
const supportedChain = isSupportedChain(chainId) if (currencyId === null || currencyId === undefined || !isSupportedChain(chainId)) return null
if (currencyId === null || currencyId === undefined || !supportedChain) return null
// this case so we use our builtin wrapped token instead of wrapped tokens on token lists // this case so we use our builtin wrapped token instead of wrapped tokens on token lists
const wrappedNative = nativeCurrency?.wrapped const wrappedNative = nativeCurrency?.wrapped
......
...@@ -4144,10 +4144,10 @@ ...@@ -4144,10 +4144,10 @@
resolved "https://registry.npmjs.org/@uniswap/merkle-distributor/-/merkle-distributor-1.0.1.tgz" resolved "https://registry.npmjs.org/@uniswap/merkle-distributor/-/merkle-distributor-1.0.1.tgz"
integrity sha512-5gDiTI5hrXIh5UWTrxKYjw30QQDnpl8ckDSpefldNenDlYO1RKkdUYMYpvrqGi2r7YzLYTlO6+TDlNs6O7hDRw== integrity sha512-5gDiTI5hrXIh5UWTrxKYjw30QQDnpl8ckDSpefldNenDlYO1RKkdUYMYpvrqGi2r7YzLYTlO6+TDlNs6O7hDRw==
"@uniswap/redux-multicall@^1.1.1", "@uniswap/redux-multicall@^1.1.5": "@uniswap/redux-multicall@^1.1.1", "@uniswap/redux-multicall@^1.1.6":
version "1.1.5" version "1.1.6"
resolved "https://registry.yarnpkg.com/@uniswap/redux-multicall/-/redux-multicall-1.1.5.tgz#7c097047d489c1624038c0fbbd3d76dc705bf153" resolved "https://registry.yarnpkg.com/@uniswap/redux-multicall/-/redux-multicall-1.1.6.tgz#27e52d97e359b6de06410fbc6d0699b9e6422707"
integrity sha512-RSMhfuAX2rPimnevvAAiwoyV2bCGTIKhVHEBOLTMF+oVxYcKKe9hCwx/cffY12t/usXWHlEJ//V7JoxTKI1Lyg== integrity sha512-B7kR63dtIJzr9iGITsNYAh8ktDfKEi9Ilsfq0oKxSzAChZmkq9Ss/EZ6/VKSvq1m4rPe+b9CPxZIBdRzpmf2/w==
"@uniswap/router-sdk@^1.3.0": "@uniswap/router-sdk@^1.3.0":
version "1.3.0" version "1.3.0"
......
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