Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable fallback rpc provider #2107

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/arb-token-bridge-ui/src/util/rpc/alchemy.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
22 changes: 22 additions & 0 deletions packages/arb-token-bridge-ui/src/util/rpc/getRpcUrl.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
57 changes: 57 additions & 0 deletions packages/arb-token-bridge-ui/src/util/rpc/infura.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading