diff --git a/src/connection/index.ts b/src/connection/index.ts index c34ee7d3fa..ad2ee1874f 100644 --- a/src/connection/index.ts +++ b/src/connection/index.ts @@ -8,7 +8,7 @@ import { WalletConnect } from '@web3-react/walletconnect' import { SupportedChainId } from 'constants/chains' import UNISWAP_LOGO_URL from '../assets/svg/logo.svg' -import { RPC_URLS } from '../constants/networks' +import { RPC_PROVIDERS, RPC_URLS } from '../constants/networks' export enum ConnectionType { INJECTED = 'INJECTED', @@ -29,7 +29,7 @@ function onError(error: Error) { } const [web3Network, web3NetworkHooks] = initializeConnector( - (actions) => new Network({ actions, urlMap: RPC_URLS, defaultChainId: 1 }) + (actions) => new Network({ actions, urlMap: RPC_PROVIDERS, defaultChainId: 1 }) ) export const networkConnection: Connection = { connector: web3Network, diff --git a/src/constants/networks.ts b/src/constants/networks.ts index ed98a02e1e..0f97a75cea 100644 --- a/src/constants/networks.ts +++ b/src/constants/networks.ts @@ -1,4 +1,4 @@ -import { JsonRpcProvider } from '@ethersproject/providers' +import { StaticJsonRpcProvider } from '@ethersproject/providers' import { SupportedChainId } from './chains' @@ -7,8 +7,6 @@ if (typeof INFURA_KEY === 'undefined') { throw new Error(`REACT_APP_INFURA_KEY must be a defined environment variable`) } -export const MAINNET_PROVIDER = new JsonRpcProvider(`https://mainnet.infura.io/v3/${INFURA_KEY}`) - /** * These are the network URLs used by the interface when there is not another available source of chain data */ @@ -27,3 +25,19 @@ export const RPC_URLS: { [key in SupportedChainId]: string } = { [SupportedChainId.CELO]: `https://forno.celo.org`, [SupportedChainId.CELO_ALFAJORES]: `https://alfajores-forno.celo-testnet.org`, } + +export const RPC_PROVIDERS: { [key in SupportedChainId]: StaticJsonRpcProvider } = { + [SupportedChainId.MAINNET]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.MAINNET]), + [SupportedChainId.RINKEBY]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.RINKEBY]), + [SupportedChainId.ROPSTEN]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.ROPSTEN]), + [SupportedChainId.GOERLI]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.GOERLI]), + [SupportedChainId.KOVAN]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.KOVAN]), + [SupportedChainId.OPTIMISM]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.OPTIMISM]), + [SupportedChainId.OPTIMISTIC_KOVAN]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.OPTIMISTIC_KOVAN]), + [SupportedChainId.ARBITRUM_ONE]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.ARBITRUM_ONE]), + [SupportedChainId.ARBITRUM_RINKEBY]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.ARBITRUM_RINKEBY]), + [SupportedChainId.POLYGON]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.POLYGON]), + [SupportedChainId.POLYGON_MUMBAI]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.POLYGON_MUMBAI]), + [SupportedChainId.CELO]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.CELO]), + [SupportedChainId.CELO_ALFAJORES]: new StaticJsonRpcProvider(RPC_URLS[SupportedChainId.CELO_ALFAJORES]), +} diff --git a/src/hooks/useFetchListCallback.ts b/src/hooks/useFetchListCallback.ts index cf772f839a..d1425cf090 100644 --- a/src/hooks/useFetchListCallback.ts +++ b/src/hooks/useFetchListCallback.ts @@ -1,6 +1,7 @@ import { nanoid } from '@reduxjs/toolkit' import { TokenList } from '@uniswap/token-lists' -import { MAINNET_PROVIDER } from 'constants/networks' +import { SupportedChainId } from 'constants/chains' +import { RPC_PROVIDERS } from 'constants/networks' import getTokenList from 'lib/hooks/useTokenList/fetchTokenList' import resolveENSContentHash from 'lib/utils/resolveENSContentHash' import { useCallback } from 'react' @@ -16,7 +17,9 @@ export function useFetchListCallback(): (listUrl: string, sendDispatch?: boolean async (listUrl: string, sendDispatch = true) => { const requestId = nanoid() sendDispatch && dispatch(fetchTokenList.pending({ requestId, url: listUrl })) - return getTokenList(listUrl, (ensName: string) => resolveENSContentHash(ensName, MAINNET_PROVIDER)) + return getTokenList(listUrl, (ensName: string) => + resolveENSContentHash(ensName, RPC_PROVIDERS[SupportedChainId.MAINNET]) + ) .then((tokenList) => { sendDispatch && dispatch(fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId })) return tokenList diff --git a/src/lib/hooks/useBlockNumber.tsx b/src/lib/hooks/useBlockNumber.tsx index bdf652e8e4..fda980a559 100644 --- a/src/lib/hooks/useBlockNumber.tsx +++ b/src/lib/hooks/useBlockNumber.tsx @@ -59,7 +59,6 @@ export function BlockNumberProvider({ children }: { children: ReactNode }) { }) provider.on('block', onBlock) - return () => { stale = true provider.removeListener('block', onBlock) @@ -69,11 +68,7 @@ export function BlockNumberProvider({ children }: { children: ReactNode }) { return void 0 }, [activeChainId, provider, onBlock, setChainBlock, windowVisible]) - const value = useMemo( - () => ({ - value: chainId === activeChainId ? block : undefined, - }), - [activeChainId, block, chainId] - ) + const blockValue = useMemo(() => (chainId === activeChainId ? block : undefined), [activeChainId, block, chainId]) + const value = useMemo(() => ({ value: blockValue }), [blockValue]) return {children} } diff --git a/src/state/routing/slice.ts b/src/state/routing/slice.ts index c7abfb3122..fa728c1d4a 100644 --- a/src/state/routing/slice.ts +++ b/src/state/routing/slice.ts @@ -1,8 +1,7 @@ -import { JsonRpcProvider } from '@ethersproject/providers' import { createApi, fetchBaseQuery, FetchBaseQueryError } from '@reduxjs/toolkit/query/react' import { Protocol } from '@uniswap/router-sdk' import { AlphaRouter, ChainId } from '@uniswap/smart-order-router' -import { RPC_URLS } from 'constants/networks' +import { RPC_PROVIDERS } from 'constants/networks' import { getClientSideQuote, toSupportedChainId } from 'lib/hooks/routing/clientSideSmartOrderRouter' import ms from 'ms.macro' import qs from 'qs' @@ -22,7 +21,7 @@ function getRouter(chainId: ChainId): AlphaRouter { const supportedChainId = toSupportedChainId(chainId) if (supportedChainId) { - const provider = new JsonRpcProvider(RPC_URLS[supportedChainId]) + const provider = RPC_PROVIDERS[supportedChainId] const router = new AlphaRouter({ chainId, provider }) routers.set(chainId, router) return router