From c4ffac8472086f0bbc3a9c104e8ae17aa77f3452 Mon Sep 17 00:00:00 2001 From: spsjvc Date: Fri, 29 Nov 2024 15:42:02 +0100 Subject: [PATCH 1/3] add alchemy stuff --- .../src/util/rpc/alchemy.ts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 packages/arb-token-bridge-ui/src/util/rpc/alchemy.ts diff --git a/packages/arb-token-bridge-ui/src/util/rpc/alchemy.ts b/packages/arb-token-bridge-ui/src/util/rpc/alchemy.ts new file mode 100644 index 0000000000..d1913a112b --- /dev/null +++ b/packages/arb-token-bridge-ui/src/util/rpc/alchemy.ts @@ -0,0 +1,37 @@ +import { ChainId } from '../networks' + +export function getAlchemyRpcUrl(chainId: ChainId): string | null { + const alchemyKey = process.env.NEXT_PUBLIC_ALCHEMY_KEY! + + switch (chainId) { + // L1 Mainnet + case ChainId.Ethereum: + return `https://eth-mainnet.g.alchemy.com/v2/${alchemyKey}` + + // L1 Testnet + case ChainId.Sepolia: + return `https://eth-sepolia.g.alchemy.com/v2/${alchemyKey}` + case ChainId.Holesky: + return `https://eth-holesky.g.alchemy.com/v2/${alchemyKey}` + + // L2 Mainnet + case ChainId.ArbitrumOne: + return `https://arb-mainnet.g.alchemy.com/v2/${alchemyKey}` + case ChainId.ArbitrumNova: + return null + case ChainId.Base: + return `https://base-mainnet.g.alchemy.com/v2/${alchemyKey}` + + // L2 Testnet + case ChainId.ArbitrumSepolia: + return `https://arb-sepolia.g.alchemy.com/v2/${alchemyKey}` + case ChainId.BaseSepolia: + return `https://base-sepolia.g.alchemy.com/v2/${alchemyKey}` + + // Local + case ChainId.Local: + case ChainId.ArbitrumLocal: + case ChainId.L3Local: + return null + } +} From ac090b5757b132f6aae6462c1f16714917c0f16c Mon Sep 17 00:00:00 2001 From: spsjvc Date: Fri, 29 Nov 2024 15:45:14 +0100 Subject: [PATCH 2/3] copy infura stuff with edits --- .../src/util/rpc/infura.ts | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 packages/arb-token-bridge-ui/src/util/rpc/infura.ts diff --git a/packages/arb-token-bridge-ui/src/util/rpc/infura.ts b/packages/arb-token-bridge-ui/src/util/rpc/infura.ts new file mode 100644 index 0000000000..c2ea3bd907 --- /dev/null +++ b/packages/arb-token-bridge-ui/src/util/rpc/infura.ts @@ -0,0 +1,57 @@ +import { ChainId } from '../networks' + +export function getInfuraKey(chainId: ChainId) { + const defaultInfuraKey = process.env.NEXT_PUBLIC_INFURA_KEY + + switch (chainId) { + case ChainId.Ethereum: + return process.env.NEXT_PUBLIC_INFURA_KEY_ETHEREUM || defaultInfuraKey + case ChainId.Sepolia: + return process.env.NEXT_PUBLIC_INFURA_KEY_SEPOLIA || defaultInfuraKey + case ChainId.ArbitrumOne: + return process.env.NEXT_PUBLIC_INFURA_KEY_ARBITRUM_ONE || defaultInfuraKey + case ChainId.Base: + return process.env.NEXT_PUBLIC_INFURA_KEY_BASE || defaultInfuraKey + case ChainId.ArbitrumSepolia: + return ( + process.env.NEXT_PUBLIC_INFURA_KEY_ARBITRUM_SEPOLIA || defaultInfuraKey + ) + case ChainId.BaseSepolia: + return process.env.NEXT_PUBLIC_INFURA_KEY_BASE_SEPOLIA || defaultInfuraKey + + default: + return defaultInfuraKey + } +} + +export function getInfuraRpcUrl(chainId: ChainId): string | null { + const infuraKey = getInfuraKey(chainId) + + switch (chainId) { + // L1 Mainnet + case ChainId.Ethereum: + return `https://mainnet.infura.io/v3/${infuraKey}` + // L1 Testnet + case ChainId.Sepolia: + return `https://sepolia.infura.io/v3/${infuraKey}` + case ChainId.Holesky: + return null + // L2 Mainnet + case ChainId.ArbitrumNova: + return null + case ChainId.ArbitrumOne: + return `https://arbitrum-mainnet.infura.io/v3/${infuraKey}` + case ChainId.Base: + // L2 Testnet + return `https://base-mainnet.infura.io/v3/${infuraKey}` + case ChainId.ArbitrumSepolia: + return `https://arbitrum-sepolia.infura.io/v3/${infuraKey}` + case ChainId.BaseSepolia: + return `https://base-sepolia.infura.io/v3/${infuraKey}` + // Local + case ChainId.Local: + case ChainId.ArbitrumLocal: + case ChainId.L3Local: + return null + } +} From 41119f5d00abbc2c5669c8ec39a7656497136d35 Mon Sep 17 00:00:00 2001 From: spsjvc Date: Fri, 29 Nov 2024 15:45:31 +0100 Subject: [PATCH 3/3] add fn to switch between rpc provider --- .../src/util/rpc/getRpcUrl.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 packages/arb-token-bridge-ui/src/util/rpc/getRpcUrl.ts diff --git a/packages/arb-token-bridge-ui/src/util/rpc/getRpcUrl.ts b/packages/arb-token-bridge-ui/src/util/rpc/getRpcUrl.ts new file mode 100644 index 0000000000..18c4b0e60f --- /dev/null +++ b/packages/arb-token-bridge-ui/src/util/rpc/getRpcUrl.ts @@ -0,0 +1,22 @@ +import { ChainId } from '../networks' + +import { getInfuraRpcUrl } from './infura' +import { getAlchemyRpcUrl } from './alchemy' + +type RpcProvider = 'infura' | 'alchemy' + +function getRpcProvider(): RpcProvider { + const rpcProviderFromEnv = process.env.NEXT_PUBLIC_RPC_PROVIDER + return (rpcProviderFromEnv?.toLowerCase() as RpcProvider) || 'infura' // the || is intentional to handle empty strings +} + +export function getRpcUrl(chainId: ChainId): string | null { + const rpcProvider = getRpcProvider() + + switch (rpcProvider) { + case 'infura': + return getInfuraRpcUrl(chainId) + case 'alchemy': + return getAlchemyRpcUrl(chainId) + } +}