diff --git a/src/lib/network.ts b/src/lib/network.ts index 91fd606fef..84ebc1a7ee 100644 --- a/src/lib/network.ts +++ b/src/lib/network.ts @@ -19,3 +19,25 @@ export const getChainName = (chainId?: number, chainType?: ChainType) => { ]?.name || unknown ); }; + +// Network mapping for DRPC +export const getDrpcNetwork = (chainId: number): string | null => { + const networkMap: { [key: number]: string } = { + 1: 'ethereum', + 137: 'polygon', + 10: 'optimism', + 42161: 'arbitrum', + 8453: 'base', + 42220: 'celo', + 100: 'gnosis', + 1101: 'polygon-zkevm', + 11155111: 'sepolia', + 11155420: 'optimism-sepolia', + 421614: 'arbitrum-sepolia', + 84532: 'base-sepolia', + 44787: 'celo-alfajores', + 1442: 'polygon-zkevm-cardona', + }; + + return networkMap[chainId] || null; +}; diff --git a/src/wagmiConfigs.ts b/src/wagmiConfigs.ts index e33ec15218..f87be44fbb 100644 --- a/src/wagmiConfigs.ts +++ b/src/wagmiConfigs.ts @@ -2,7 +2,7 @@ import { cookieStorage, createConfig, createStorage } from 'wagmi'; import { walletConnect, coinbaseWallet, safe } from '@wagmi/connectors'; import { createClient, http } from 'viem'; -import { mainnet } from 'viem/chains'; +import { getDrpcNetwork } from './lib/network'; import configuration from './configuration'; // Get projectId at https://cloud.walletconnect.com @@ -20,6 +20,14 @@ const metadata = { const chains = configuration.EVM_CHAINS; +const createDrpcTransport = (chainId: number) => { + const network = getDrpcNetwork(chainId); + const drpcKey = process.env.NEXT_PUBLIC_DRPC_KEY; + return network && drpcKey + ? http(`https://lb.drpc.org/ogrpc?network=${network}&dkey=${drpcKey}`) + : http(); +}; + // Create wagmiConfig export const wagmiConfig = createConfig({ chains: chains, // required @@ -38,16 +46,9 @@ export const wagmiConfig = createConfig({ storage: cookieStorage, }), client({ chain }) { - // TODO: we must manage this for all chains in a better way - // basically to stop using viem's public transport - // leaving this as a hotfix to keep it quick - const infuraKey = process.env.NEXT_PUBLIC_INFURA_API_KEY; - - const customHttp = - chain.id === mainnet.id && infuraKey - ? http(`https://mainnet.infura.io/v3/${infuraKey}`) - : http(); - - return createClient({ chain, transport: customHttp }); + return createClient({ + chain, + transport: createDrpcTransport(chain.id), + }); }, });