diff --git a/centrifuge-app/src/config.ts b/centrifuge-app/src/config.ts index 6615d37017..f673c4a055 100644 --- a/centrifuge-app/src/config.ts +++ b/centrifuge-app/src/config.ts @@ -163,6 +163,7 @@ export const evmChains: EvmChains = { `https://eth-mainnet.g.alchemy.com/v2/${alchemyKey}`, `https://eth.api.onfinality.io/rpc?apikey=${onfinalityKey}`, ], + network: 'mainnet', iconUrl: ethereumLogo, isTestnet: false, }, @@ -174,6 +175,7 @@ export const evmChains: EvmChains = { `https://eth-sepolia.g.alchemy.com/v2/${alchemyKey}`, `https://eth-sepolia.api.onfinality.io/rpc?apikey=${onfinalityKey}`, ], + network: 'sepolia', iconUrl: sepoliaLogo, isTestnet: true, }, @@ -183,6 +185,7 @@ export const evmChains: EvmChains = { blockExplorerUrl: 'https://basescan.org/', urls: ['https://mainnet.base.org'], iconUrl: baseLogo, + network: 'base-mainnet', isTestnet: false, }, 84532: { @@ -191,6 +194,7 @@ export const evmChains: EvmChains = { blockExplorerUrl: 'https://sepolia.basescan.org/', urls: [`https://sepolia.base.org`], iconUrl: baseLogo, + network: 'base-sepolia', isTestnet: true, }, 42161: { @@ -203,6 +207,7 @@ export const evmChains: EvmChains = { blockExplorerUrl: 'https://arbiscan.io/', urls: ['https://arb1.arbitrum.io/rpc'], iconUrl: arbitrumLogo, + network: 'arbitrum-mainnet', isTestnet: false, }, 42220: { @@ -216,6 +221,7 @@ export const evmChains: EvmChains = { urls: ['https://forno.celo.org'], iconUrl: celoLogo, isTestnet: false, + network: 'celo-mainnet', }, 44787: { name: 'Celo Alfajores', @@ -228,6 +234,7 @@ export const evmChains: EvmChains = { urls: ['https://alfajores-forno.celo-testnet.org'], iconUrl: celoLogo, isTestnet: true, + network: 'celo-alfajores', }, } diff --git a/centrifuge-react/src/components/WalletProvider/WalletProvider.tsx b/centrifuge-react/src/components/WalletProvider/WalletProvider.tsx index 4d3b893bc8..cb2c099dc3 100644 --- a/centrifuge-react/src/components/WalletProvider/WalletProvider.tsx +++ b/centrifuge-react/src/components/WalletProvider/WalletProvider.tsx @@ -4,7 +4,7 @@ import { getWallets } from '@subwallet/wallet-connect/dotsama/wallets' import { Wallet } from '@subwallet/wallet-connect/types' import { Web3ReactState } from '@web3-react/types' import { WalletConnect as WalletConnectV2 } from '@web3-react/walletconnect-v2' -import { JsonRpcProvider, Provider } from 'ethers' +import { AbstractProvider, getDefaultProvider, JsonRpcProvider } from 'ethers' import * as React from 'react' import { useQuery } from 'react-query' import { firstValueFrom, map, switchMap } from 'rxjs' @@ -63,10 +63,19 @@ export type WalletContextType = { selectedWallet: EvmConnectorMeta | null isSmartContractWallet: boolean selectedAddress: string | null - getProvider(chainId: number): Provider + getProvider(chainId: number): JsonRpcProvider | AbstractProvider } } +const unsupportedNetworksByDefaultProvider = [ + 'base-sepolia', + 'base-mainnet', + 'celo-alfajores', + 'celo-mainnet', + 'arbitrum-sepolia', + 'arbitrum-mainnet', +] + const WalletContext = React.createContext(null as any) export const wallets = getWallets() @@ -129,10 +138,13 @@ type WalletProviderProps = { showAdvancedAccounts?: boolean showTestNets?: boolean showFinoa?: boolean + infuraApiKey?: string + alchemyApiKey?: string + tenderlyApiKey?: string } let cachedEvmConnectors: EvmConnectorMeta[] | undefined = undefined -const cachedProviders = new Map() +const cachedProviders = new Map() export function WalletProvider({ children, @@ -148,6 +160,9 @@ export function WalletProvider({ showAdvancedAccounts, showTestNets, showFinoa, + infuraApiKey, + alchemyApiKey, + tenderlyApiKey, }: WalletProviderProps) { if (!evmChainsProp[1]?.urls[0]) throw new Error('Mainnet should be defined in EVM Chains') @@ -240,26 +255,21 @@ export function WalletProvider({ const [proxies] = useCentrifugeQuery(['allProxies'], (cent) => cent.proxies.getAllProxies()) - async function findHealthyProvider(chainId: number, urls: string[]): Promise { - for (const url of urls) { - try { - const provider = new JsonRpcProvider(url, chainId) - await provider.getBlockNumber() - cachedProviders.set(chainId, provider) - return provider - } catch (error) { - console.error(`Provider health check failed for ${url}:`, error) - } - } - throw new Error(`No healthy provider found for chain ${chainId}`) - } - - function getProvider(chainId: number): JsonRpcProvider { - const urls = (evmChains as any)[chainId].urls + function getProvider(chainId: number) { + const defaultUrl = (evmChains as any)[chainId].urls[0] + const network = (evmChains as any)[chainId].network if (!cachedProviders.has(chainId)) { - findHealthyProvider(chainId, urls).catch(console.error) + let networkish = unsupportedNetworksByDefaultProvider.includes(network) ? defaultUrl : network + const provider = getDefaultProvider(networkish, { + infura: infuraApiKey, + alchemy: alchemyApiKey, + tenderly: tenderlyApiKey, + exclusive: ['infura', 'alchemy', 'tenderly'], + }) + cachedProviders.set(chainId, provider) + return provider } - return cachedProviders.get(chainId) || new JsonRpcProvider(urls[0], chainId) + return cachedProviders.get(chainId) as JsonRpcProvider | AbstractProvider } function setFilteredAccounts(accounts: SubstrateAccount[]) { diff --git a/centrifuge-react/src/components/WalletProvider/evm/chains.ts b/centrifuge-react/src/components/WalletProvider/evm/chains.ts index ea11f502a9..db0a2106f8 100644 --- a/centrifuge-react/src/components/WalletProvider/evm/chains.ts +++ b/centrifuge-react/src/components/WalletProvider/evm/chains.ts @@ -7,6 +7,7 @@ type BasicChainInformation = { } type ExtendedChainInformation = BasicChainInformation & { + network: string name: string nativeCurrency: AddEthereumChainParameter['nativeCurrency'] blockExplorerUrl: string