refactor: remove isChainAllowed (#4494)

This commit is contained in:
Vignesh Mohankumar 2022-08-29 10:21:18 -04:00 committed by GitHub
parent 87d6975bd8
commit ee001f86f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 25 deletions

@ -11,7 +11,6 @@ import { useCloseModal, useModalIsOpen, useOpenModal, useToggleModal } from 'sta
import { ApplicationModal } from 'state/application/reducer' import { ApplicationModal } from 'state/application/reducer'
import styled from 'styled-components/macro' import styled from 'styled-components/macro'
import { ExternalLink, MEDIA_WIDTHS } from 'theme' import { ExternalLink, MEDIA_WIDTHS } from 'theme'
import { isChainAllowed } from 'utils/switchChain'
import { isMobile } from 'utils/userAgent' import { isMobile } from 'utils/userAgent'
const ActiveRowLinkList = styled.div` const ActiveRowLinkList = styled.div`
@ -328,18 +327,16 @@ export default function NetworkSelector() {
<FlyoutHeader> <FlyoutHeader>
<Trans>Select a {!onSupportedChain ? ' supported ' : ''}network</Trans> <Trans>Select a {!onSupportedChain ? ' supported ' : ''}network</Trans>
</FlyoutHeader> </FlyoutHeader>
{NETWORK_SELECTOR_CHAINS.map((chainId: SupportedChainId) => {NETWORK_SELECTOR_CHAINS.map((chainId: SupportedChainId) => (
isChainAllowed(chainId) ? ( <Row
<Row onSelectChain={async (targetChainId: SupportedChainId) => {
onSelectChain={async (targetChainId: SupportedChainId) => { await selectChain(targetChainId)
await selectChain(targetChainId) closeModal()
closeModal() }}
}} targetChain={chainId}
targetChain={chainId} key={chainId}
key={chainId} />
/> ))}
) : null
)}
</FlyoutMenuContents> </FlyoutMenuContents>
</FlyoutMenu> </FlyoutMenu>
)} )}

@ -2,11 +2,11 @@ import { arrayify } from '@ethersproject/bytes'
import { parseBytes32String } from '@ethersproject/strings' import { parseBytes32String } from '@ethersproject/strings'
import { Currency, Token } from '@uniswap/sdk-core' import { Currency, Token } from '@uniswap/sdk-core'
import { useWeb3React } from '@web3-react/core' import { useWeb3React } from '@web3-react/core'
import { isSupportedChain } from 'constants/chains'
import { useBytes32TokenContract, useTokenContract } from 'hooks/useContract' import { useBytes32TokenContract, useTokenContract } from 'hooks/useContract'
import { NEVER_RELOAD, useSingleCallResult } from 'lib/hooks/multicall' import { NEVER_RELOAD, useSingleCallResult } from 'lib/hooks/multicall'
import useNativeCurrency from 'lib/hooks/useNativeCurrency' import useNativeCurrency from 'lib/hooks/useNativeCurrency'
import { useMemo } from 'react' import { useMemo } from 'react'
import { isChainAllowed } from 'utils/switchChain'
import { TOKEN_SHORTHANDS } from '../../constants/tokens' import { TOKEN_SHORTHANDS } from '../../constants/tokens'
import { isAddress } from '../../utils' import { isAddress } from '../../utils'
@ -31,7 +31,7 @@ function parseStringOrBytes32(str: string | undefined, bytes32: string | undefin
*/ */
export function useTokenFromNetwork(tokenAddress: string | null | undefined): Token | null | undefined { export function useTokenFromNetwork(tokenAddress: string | null | undefined): Token | null | undefined {
const { chainId } = useWeb3React() const { chainId } = useWeb3React()
const chainAllowed = chainId && isChainAllowed(chainId) const supportedChain = isSupportedChain(chainId)
const formattedAddress = isAddress(tokenAddress) const formattedAddress = isAddress(tokenAddress)
@ -45,7 +45,7 @@ export function useTokenFromNetwork(tokenAddress: string | null | undefined): To
const decimals = useSingleCallResult(tokenContract, 'decimals', undefined, NEVER_RELOAD) const decimals = useSingleCallResult(tokenContract, 'decimals', undefined, NEVER_RELOAD)
return useMemo(() => { return useMemo(() => {
if (typeof tokenAddress !== 'string' || !chainAllowed || !formattedAddress) return undefined if (typeof tokenAddress !== 'string' || !supportedChain || !formattedAddress) return undefined
if (decimals.loading || symbol.loading || tokenName.loading) return null if (decimals.loading || symbol.loading || tokenName.loading) return null
if (decimals.result) { if (decimals.result) {
return new Token( return new Token(
@ -60,7 +60,7 @@ export function useTokenFromNetwork(tokenAddress: string | null | undefined): To
}, [ }, [
formattedAddress, formattedAddress,
chainId, chainId,
chainAllowed, supportedChain,
decimals.loading, decimals.loading,
decimals.result, decimals.result,
symbol.loading, symbol.loading,
@ -105,8 +105,8 @@ 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 chainAllowed = chainId && isChainAllowed(chainId) const supportedChain = isSupportedChain(chainId)
if (currencyId === null || currencyId === undefined || !chainAllowed) 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

@ -1,7 +1,7 @@
import { Connector } from '@web3-react/types' import { Connector } from '@web3-react/types'
import { networkConnection, walletConnectConnection } from 'connection' import { networkConnection, walletConnectConnection } from 'connection'
import { getChainInfo } from 'constants/chainInfo' import { getChainInfo } from 'constants/chainInfo'
import { ALL_SUPPORTED_CHAIN_IDS, SupportedChainId } from 'constants/chains' import { isSupportedChain, SupportedChainId } from 'constants/chains'
import { RPC_URLS } from 'constants/networks' import { RPC_URLS } from 'constants/networks'
function getRpcUrls(chainId: SupportedChainId): [string] { function getRpcUrls(chainId: SupportedChainId): [string] {
@ -34,12 +34,8 @@ function getRpcUrls(chainId: SupportedChainId): [string] {
throw new Error('RPC URLs must use public endpoints') throw new Error('RPC URLs must use public endpoints')
} }
export function isChainAllowed(chainId: number) {
return ALL_SUPPORTED_CHAIN_IDS.includes(chainId)
}
export const switchChain = async (connector: Connector, chainId: SupportedChainId) => { export const switchChain = async (connector: Connector, chainId: SupportedChainId) => {
if (!isChainAllowed(chainId)) { if (!isSupportedChain(chainId)) {
throw new Error(`Chain ${chainId} not supported for connector (${typeof connector})`) throw new Error(`Chain ${chainId} not supported for connector (${typeof connector})`)
} else if (connector === walletConnectConnection.connector || connector === networkConnection.connector) { } else if (connector === walletConnectConnection.connector || connector === networkConnection.connector) {
await connector.activate(chainId) await connector.activate(chainId)