From 23667ebb964ffdaadc384baf31fe66ce3fe86a09 Mon Sep 17 00:00:00 2001 From: Rick Staa Date: Thu, 21 Nov 2024 12:50:59 +0100 Subject: [PATCH] fix: ensure one public RPC client is used This commit ensures that only one public rpc client is used on the explorer. --- lib/chains.ts | 12 ++++++------ lib/utils.tsx | 4 ---- pages/_app.tsx | 12 ++++-------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/lib/chains.ts b/lib/chains.ts index 6c9b4aaf..b71ecce4 100644 --- a/lib/chains.ts +++ b/lib/chains.ts @@ -18,14 +18,14 @@ export const WALLET_CONNECT_PROJECT_ID = export const INFURA_KEY = process.env.NEXT_PUBLIC_INFURA_KEY; const NETWORK = process.env.NEXT_PUBLIC_NETWORK; +const L1_RPC_URL = process.env.NEXT_PUBLIC_L1_RPC_URL; +const L2_RPC_URL = process.env.NEXT_PUBLIC_L2_RPC_URL; const SUBGRAPH_KEY = process.env.NEXT_PUBLIC_SUBGRAPH_API_KEY; const SUBGRAPH_ID = process.env.NEXT_PUBLIC_SUBGRAPH_ID; -if (typeof INFURA_KEY === "undefined" || typeof NETWORK === "undefined") { - throw new Error( - `NEXT_PUBLIC_INFURA_KEY and NETWORK must be defined environment variables` - ); +if (!INFURA_KEY && (!L1_RPC_URL || !L2_RPC_URL)) { + throw new Error("Missing environment variables: NEXT_PUBLIC_INFURA_KEY or both NEXT_PUBLIC_L1_RPC_URL and NEXT_PUBLIC_L2_RPC_URL must be defined"); } export const AVERAGE_L1_BLOCK_TIME = 12; // ethereum blocks come in at exactly 12s +99% of the time @@ -125,9 +125,9 @@ export const ALL_SUPPORTED_CHAIN_IDS = [ * configured in the environment variables. */ export const INFURA_NETWORK_URLS = { - [chain.mainnet.id]: process.env.NEXT_PUBLIC_L1_RPC_URL || `https://mainnet.infura.io/v3/${INFURA_KEY}`, + [chain.mainnet.id]: L1_RPC_URL || `https://mainnet.infura.io/v3/${INFURA_KEY}`, // [chain.goerli.id]: `https://rinkeby.infura.io/v3/${INFURA_KEY}`, - [chain.arbitrum.id]: process.env.NEXT_PUBLIC_L2_RPC_URL || `https://arbitrum-mainnet.infura.io/v3/${INFURA_KEY}`, + [chain.arbitrum.id]: L2_RPC_URL || `https://arbitrum-mainnet.infura.io/v3/${INFURA_KEY}`, // [chain.arbitrumGoerli // .id]: `https://arbitrum-rinkeby.infura.io/v3/${INFURA_KEY}`, }; diff --git a/lib/utils.tsx b/lib/utils.tsx index 674d194b..ce13ea69 100644 --- a/lib/utils.tsx +++ b/lib/utils.tsx @@ -5,10 +5,6 @@ import { StakingAction } from "hooks"; import { CHAIN_INFO, DEFAULT_CHAIN_ID, INFURA_NETWORK_URLS } from "lib/chains"; import Numeral from "numeral"; -export const provider = new ethers.providers.JsonRpcProvider( - INFURA_NETWORK_URLS[DEFAULT_CHAIN_ID] -); - export function avg(obj, key) { const arr = Object.values(obj); const sum = (prev, cur) => ({ [key]: prev[key] + cur[key] }); diff --git a/pages/_app.tsx b/pages/_app.tsx index 593b872c..7525f9c1 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -6,15 +6,13 @@ import "@rainbow-me/rainbowkit/styles.css"; import rainbowTheme from "constants/rainbowTheme"; import * as TooltipPrimitive from "@radix-ui/react-tooltip"; import Layout from "layouts/main"; -import { DEFAULT_CHAIN, WALLET_CONNECT_PROJECT_ID, INFURA_KEY, L1_CHAIN } from "lib/chains"; +import { DEFAULT_CHAIN, WALLET_CONNECT_PROJECT_ID, L1_CHAIN, l1PublicClient, l2PublicClient} from "lib/chains"; import Head from "next/head"; import { useRouter } from "next/router"; import { useMemo } from "react"; import { CookiesProvider } from "react-cookie"; import { SWRConfig } from "swr"; -import { configureChains, createConfig, WagmiConfig } from "wagmi"; -import { infuraProvider } from "wagmi/providers/infura"; -import { publicProvider } from "wagmi/providers/public"; +import { createConfig, WagmiConfig } from "wagmi"; import { useApollo } from "../apollo"; function App({ Component, pageProps, fallback = null }) { @@ -25,10 +23,8 @@ function App({ Component, pageProps, fallback = null }) { const isMigrateRoute = useMemo(() => route.includes("/migrate"), [route]); const { config, chains, layoutKey } = useMemo(() => { - const { chains, publicClient } = configureChains( - [isMigrateRoute ? L1_CHAIN : DEFAULT_CHAIN], - [infuraProvider({ apiKey: INFURA_KEY ?? "" }), publicProvider()] - ); + const chains = [isMigrateRoute ? L1_CHAIN : DEFAULT_CHAIN]; + const publicClient = isMigrateRoute ? l1PublicClient : l2PublicClient; const { connectors } = getDefaultWallets({ appName: "Livepeer Explorer",