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 + } +} 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) + } +} 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 + } +}