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

@ -2,11 +2,11 @@ import { arrayify } from '@ethersproject/bytes'
import { parseBytes32String } from '@ethersproject/strings'
import { Currency, Token } from '@uniswap/sdk-core'
import { useWeb3React } from '@web3-react/core'
import { isSupportedChain } from 'constants/chains'
import { useBytes32TokenContract, useTokenContract } from 'hooks/useContract'
import { NEVER_RELOAD, useSingleCallResult } from 'lib/hooks/multicall'
import useNativeCurrency from 'lib/hooks/useNativeCurrency'
import { useMemo } from 'react'
import { isChainAllowed } from 'utils/switchChain'
import { TOKEN_SHORTHANDS } from '../../constants/tokens'
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 {
const { chainId } = useWeb3React()
const chainAllowed = chainId && isChainAllowed(chainId)
const supportedChain = isSupportedChain(chainId)
const formattedAddress = isAddress(tokenAddress)
@ -45,7 +45,7 @@ export function useTokenFromNetwork(tokenAddress: string | null | undefined): To
const decimals = useSingleCallResult(tokenContract, 'decimals', undefined, NEVER_RELOAD)
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.result) {
return new Token(
@ -60,7 +60,7 @@ export function useTokenFromNetwork(tokenAddress: string | null | undefined): To
}, [
formattedAddress,
chainId,
chainAllowed,
supportedChain,
decimals.loading,
decimals.result,
symbol.loading,
@ -105,8 +105,8 @@ export function useCurrencyFromMap(tokens: TokenMap, currencyId?: string | null)
const token = useTokenFromMapOrNetwork(tokens, isNative ? undefined : shorthandMatchAddress ?? currencyId)
const chainAllowed = chainId && isChainAllowed(chainId)
if (currencyId === null || currencyId === undefined || !chainAllowed) return null
const supportedChain = isSupportedChain(chainId)
if (currencyId === null || currencyId === undefined || !supportedChain) return null
// this case so we use our builtin wrapped token instead of wrapped tokens on token lists
const wrappedNative = nativeCurrency?.wrapped

@ -1,7 +1,7 @@
import { Connector } from '@web3-react/types'
import { networkConnection, walletConnectConnection } from 'connection'
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'
function getRpcUrls(chainId: SupportedChainId): [string] {
@ -34,12 +34,8 @@ function getRpcUrls(chainId: SupportedChainId): [string] {
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) => {
if (!isChainAllowed(chainId)) {
if (!isSupportedChain(chainId)) {
throw new Error(`Chain ${chainId} not supported for connector (${typeof connector})`)
} else if (connector === walletConnectConnection.connector || connector === networkConnection.connector) {
await connector.activate(chainId)