Commit 19b4ee46 authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: memoize tokens in TokenDetails (#4777)

parent 2aea96c3
import { Currency, OnReviewSwapClick, SwapWidget } from '@uniswap/widgets' import { Currency, OnReviewSwapClick, SwapWidget } from '@uniswap/widgets'
import { useWeb3React } from '@web3-react/core' import { useWeb3React } from '@web3-react/core'
import { RPC_PROVIDERS } from 'constants/providers'
import { useActiveLocale } from 'hooks/useActiveLocale' import { useActiveLocale } from 'hooks/useActiveLocale'
import { useMemo } from 'react' import { useMemo } from 'react'
import { useIsDarkMode } from 'state/user/hooks' import { useIsDarkMode } from 'state/user/hooks'
...@@ -34,7 +33,7 @@ export default function Widget({ defaultToken, onReviewSwapClick }: WidgetProps) ...@@ -34,7 +33,7 @@ export default function Widget({ defaultToken, onReviewSwapClick }: WidgetProps)
<SwapWidget <SwapWidget
disableBranding disableBranding
hideConnectionUI hideConnectionUI
jsonRpcUrlMap={RPC_PROVIDERS} // jsonRpcUrlMap is excluded - network providers are always passed directly
routerUrl={WIDGET_ROUTER_URL} routerUrl={WIDGET_ROUTER_URL}
width={WIDGET_WIDTH} width={WIDGET_WIDTH}
locale={locale} locale={locale}
......
import { Token } from '@uniswap/sdk-core' import { NativeCurrency, Token } from '@uniswap/sdk-core'
import { useWeb3React } from '@web3-react/core' import { useWeb3React } from '@web3-react/core'
import { formatToDecimal } from 'analytics/utils' import { formatToDecimal } from 'analytics/utils'
import { import {
...@@ -21,7 +21,7 @@ import TokenSafetyModal from 'components/TokenSafety/TokenSafetyModal' ...@@ -21,7 +21,7 @@ import TokenSafetyModal from 'components/TokenSafety/TokenSafetyModal'
import Widget, { WIDGET_WIDTH } from 'components/Widget' import Widget, { WIDGET_WIDTH } from 'components/Widget'
import { getChainInfo } from 'constants/chainInfo' import { getChainInfo } from 'constants/chainInfo'
import { L1_CHAIN_IDS, L2_CHAIN_IDS, SupportedChainId, TESTNET_CHAIN_IDS } from 'constants/chains' import { L1_CHAIN_IDS, L2_CHAIN_IDS, SupportedChainId, TESTNET_CHAIN_IDS } from 'constants/chains'
import { isCelo, nativeOnChain, WRAPPED_NATIVE_CURRENCY } from 'constants/tokens' import { isCelo, nativeOnChain } from 'constants/tokens'
import { checkWarning } from 'constants/tokenSafety' import { checkWarning } from 'constants/tokenSafety'
import { Chain } from 'graphql/data/__generated__/TokenQuery.graphql' import { Chain } from 'graphql/data/__generated__/TokenQuery.graphql'
import { useTokenQuery } from 'graphql/data/Token' import { useTokenQuery } from 'graphql/data/Token'
...@@ -99,10 +99,10 @@ export default function TokenDetails() { ...@@ -99,10 +99,10 @@ export default function TokenDetails() {
const { tokenAddress: tokenAddressParam, chainName } = useParams<{ tokenAddress?: string; chainName?: string }>() const { tokenAddress: tokenAddressParam, chainName } = useParams<{ tokenAddress?: string; chainName?: string }>()
const chainId = CHAIN_NAME_TO_CHAIN_ID[validateUrlChainParam(chainName)] const chainId = CHAIN_NAME_TO_CHAIN_ID[validateUrlChainParam(chainName)]
let tokenAddress = tokenAddressParam let tokenAddress = tokenAddressParam
let nativeCurrency let nativeCurrency: NativeCurrency | Token | undefined
if (tokenAddressParam === 'NATIVE') { if (tokenAddressParam === 'NATIVE') {
nativeCurrency = nativeOnChain(chainId) nativeCurrency = nativeOnChain(chainId)
tokenAddress = WRAPPED_NATIVE_CURRENCY[chainId]?.address?.toLowerCase() tokenAddress = nativeCurrency.wrapped.address
} }
const tokenWarning = tokenAddress ? checkWarning(tokenAddress) : null const tokenWarning = tokenAddress ? checkWarning(tokenAddress) : null
...@@ -152,7 +152,10 @@ export default function TokenDetails() { ...@@ -152,7 +152,10 @@ export default function TokenDetails() {
const { chainId: connectedChainId, account } = useWeb3React() const { chainId: connectedChainId, account } = useWeb3React()
// TODO: consider updating useTokenBalance to work with just address/chain to avoid using Token data structure here // TODO: consider updating useTokenBalance to work with just address/chain to avoid using Token data structure here
const balanceValue = useTokenBalance(account, new Token(chainId, tokenAddress ?? '', 18)) const balanceValue = useTokenBalance(
account,
useMemo(() => new Token(chainId, tokenAddress ?? '', 18), [chainId, tokenAddress])
)
const balance = balanceValue ? formatToDecimal(balanceValue, Math.min(balanceValue.currency.decimals, 6)) : undefined const balance = balanceValue ? formatToDecimal(balanceValue, Math.min(balanceValue.currency.decimals, 6)) : undefined
const balanceUsdValue = useStablecoinValue(balanceValue)?.toFixed(2) const balanceUsdValue = useStablecoinValue(balanceValue)?.toFixed(2)
const balanceUsd = balanceUsdValue ? parseFloat(balanceUsdValue) : undefined const balanceUsd = balanceUsdValue ? parseFloat(balanceUsdValue) : undefined
...@@ -188,11 +191,18 @@ export default function TokenDetails() { ...@@ -188,11 +191,18 @@ export default function TokenDetails() {
}) })
: null : null
const defaultWidgetToken = const widgetToken = useMemo(() => {
nativeCurrency ?? const currentChainId = CHAIN_NAME_TO_CHAIN_ID[currentChainName]
(token?.address && token.symbol && token.name // The widget is not yet configured to use Celo.
? new Token(CHAIN_NAME_TO_CHAIN_ID[currentChainName], token.address, 18, token.symbol, token.name) if (isCelo(chainId) || isCelo(currentChainId)) return undefined
: undefined)
return (
nativeCurrency ??
(token?.address && token.symbol && token.name
? new Token(currentChainId, token.address, 18, token.symbol, token.name)
: undefined)
)
}, [chainId, currentChainName, nativeCurrency, token?.address, token?.name, token?.symbol])
return ( return (
<TokenDetailsLayout> <TokenDetailsLayout>
...@@ -219,7 +229,7 @@ export default function TokenDetails() { ...@@ -219,7 +229,7 @@ export default function TokenDetails() {
<AddressSection address={token.address ?? ''} /> <AddressSection address={token.address ?? ''} />
</LeftPanel> </LeftPanel>
<RightPanel> <RightPanel>
<Widget defaultToken={!isCelo(chainId) ? defaultWidgetToken : undefined} onReviewSwapClick={onReviewSwap} /> <Widget defaultToken={widgetToken} onReviewSwapClick={onReviewSwap} />
{tokenWarning && <TokenSafetyMessage tokenAddress={token.address ?? ''} warning={tokenWarning} />} {tokenWarning && <TokenSafetyMessage tokenAddress={token.address ?? ''} warning={tokenWarning} />}
<BalanceSummary address={token.address ?? ''} balance={balance} balanceUsd={balanceUsd} /> <BalanceSummary address={token.address ?? ''} balance={balance} balanceUsd={balanceUsd} />
</RightPanel> </RightPanel>
......
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