Commit 7d20dd97 authored by Moody Salem's avatar Moody Salem

Fix a crash related to WETH being loaded from add liquidity

parent 2b4f511b
import { ChainId, Token } from '@uniswap/sdk' import { ChainId, Token, WETH } from '@uniswap/sdk'
import { useEffect, useMemo } from 'react' import { useEffect, useMemo } from 'react'
import { ALL_TOKENS } from '../constants/tokens' import { ALL_TOKENS } from '../constants/tokens'
import { useAddUserToken, useFetchTokenByAddress, useUserAddedTokens } from '../state/user/hooks' import { useAddUserToken, useFetchTokenByAddress, useUserAddedTokens } from '../state/user/hooks'
import { isAddress } from '../utils'
import { useActiveWeb3React } from './index' import { useActiveWeb3React } from './index'
...@@ -11,19 +12,26 @@ export function useAllTokens(): { [address: string]: Token } { ...@@ -11,19 +12,26 @@ export function useAllTokens(): { [address: string]: Token } {
return useMemo(() => { return useMemo(() => {
if (!chainId) return {} if (!chainId) return {}
return ( const tokens = userAddedTokens
userAddedTokens // reduce into all ALL_TOKENS filtered by the current chain
// reduce into all ALL_TOKENS filtered by the current chain .reduce<{ [address: string]: Token }>(
.reduce<{ [address: string]: Token }>( (tokenMap, token) => {
(tokenMap, token) => { tokenMap[token.address] = token
tokenMap[token.address] = token return tokenMap
return tokenMap },
}, // must make a copy because reduce modifies the map, and we do not
// must make a copy because reduce modifies the map, and we do not // want to make a copy in every iteration
// want to make a copy in every iteration { ...ALL_TOKENS[chainId as ChainId] }
{ ...ALL_TOKENS[chainId as ChainId] } )
)
) const weth = WETH[chainId as ChainId]
if (weth) {
// we have to replace it as a workaround because if it is automatically
// fetched by address it will cause an invariant when used in constructing
// pairs since we replace the name and symbol with 'ETH' and 'Ether'
tokens[weth.address] = WETH[chainId as ChainId]
}
return tokens
}, [userAddedTokens, chainId]) }, [userAddedTokens, chainId])
} }
...@@ -39,8 +47,13 @@ export function useTokenByAddressAndAutomaticallyAdd(tokenAddress?: string): Tok ...@@ -39,8 +47,13 @@ export function useTokenByAddressAndAutomaticallyAdd(tokenAddress?: string): Tok
const fetchTokenByAddress = useFetchTokenByAddress() const fetchTokenByAddress = useFetchTokenByAddress()
const addToken = useAddUserToken() const addToken = useAddUserToken()
const allTokens = useAllTokens() const allTokens = useAllTokens()
const { chainId } = useActiveWeb3React()
useEffect(() => { useEffect(() => {
if (!chainId) return
const weth = WETH[chainId as ChainId]
if (weth && weth.address === isAddress(tokenAddress)) return
if (tokenAddress && !allTokens?.[tokenAddress]) { if (tokenAddress && !allTokens?.[tokenAddress]) {
fetchTokenByAddress(tokenAddress).then(token => { fetchTokenByAddress(tokenAddress).then(token => {
if (token !== null) { if (token !== null) {
...@@ -48,7 +61,7 @@ export function useTokenByAddressAndAutomaticallyAdd(tokenAddress?: string): Tok ...@@ -48,7 +61,7 @@ export function useTokenByAddressAndAutomaticallyAdd(tokenAddress?: string): Tok
} }
}) })
} }
}, [tokenAddress, allTokens, fetchTokenByAddress, addToken]) }, [tokenAddress, allTokens, fetchTokenByAddress, addToken, chainId])
return tokenAddress ? allTokens?.[tokenAddress] : undefined return tokenAddress ? allTokens?.[tokenAddress] : undefined
} }
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