From 27dcf659640b8a88e17e64f67789149429904995 Mon Sep 17 00:00:00 2001 From: Trung-Tin Pham <60747384+AtelyPham@users.noreply.github.com> Date: Tue, 1 Aug 2023 14:27:51 +0700 Subject: [PATCH] Add More Rpc Urls to Support Multiple Providers (#1471) Co-authored-by: drewstone --- .../src/WebbProvider.tsx | 18 +- libs/dapp-config/src/chains/evm/index.tsx | 189 ++++++++++++------ .../src/utils/getViemClient.ts | 6 +- 3 files changed, 146 insertions(+), 67 deletions(-) diff --git a/libs/api-provider-environment/src/WebbProvider.tsx b/libs/api-provider-environment/src/WebbProvider.tsx index abbaf3a646..cf56a82904 100644 --- a/libs/api-provider-environment/src/WebbProvider.tsx +++ b/libs/api-provider-environment/src/WebbProvider.tsx @@ -620,17 +620,17 @@ const WebbProviderInner: FC = ({ children, appEvent }) => { if (currentChainId !== chain.id) { await webbWeb3Provider.switchOrAddChain(chain.id); - - // Emit events - appEvent.send('networkSwitched', [ - { - chainType: chain.chainType, - chainId: chain.id, - }, - wallet.id, - ]); } + // Emit events + appEvent.send('networkSwitched', [ + { + chainType: chain.chainType, + chainId: chain.id, + }, + wallet.id, + ]); + await setActiveApiWithAccounts( webbWeb3Provider, chain, diff --git a/libs/dapp-config/src/chains/evm/index.tsx b/libs/dapp-config/src/chains/evm/index.tsx index ed4e146f69..b72465cec3 100644 --- a/libs/dapp-config/src/chains/evm/index.tsx +++ b/libs/dapp-config/src/chains/evm/index.tsx @@ -7,7 +7,7 @@ import { goerli, moonbaseAlpha, optimismGoerli, - polygonMumbai, + polygonMumbai as polygonMumbai_, scrollTestnet, sepolia, type Chain, @@ -15,6 +15,8 @@ import { import { EVMChainId, PresetTypedChainId } from '@webb-tools/dapp-types'; import { ChainType } from '@webb-tools/sdk-core/typed-chain-id'; import merge from 'lodash/merge'; +import mergeWith from 'lodash/mergeWith'; +import cloneDeep from 'lodash/cloneDeep'; import { DEFAULT_EVM_CURRENCY } from '../../currencies'; import { ChainConfig, WebbExtendedChain } from '../chain-config.interface'; @@ -54,72 +56,147 @@ const localDemeterMulticall3DeploymentBlock = process.env ? parseInt(process.env.BRIDGE_DAPP_LOCAL_DEMETER_MULTICALL3_DEPLOYMENT_BLOCK) : 0; +// Default rpc url of mumbai is not working so we override it +// endpoint here: https://chainid.network/chains.json +const polygonMumbai = merge(cloneDeep(polygonMumbai_), { + rpcUrls: { + default: { + http: ['https://endpoints.omniatech.io/v1/matic/mumbai/public'], + }, + public: { + http: ['https://endpoints.omniatech.io/v1/matic/mumbai/public'], + }, + }, +}); + +const mergeChain = ( + chain: Chain, + extended: WebbExtendedChain & { rpcUrls: Chain['rpcUrls'] } +): ChainConfig => { + return mergeWith(cloneDeep(chain), extended, (objValue, srcValue) => { + if (Array.isArray(objValue)) { + return objValue.concat(srcValue); + } + + return undefined; + }); +}; + +const additionalRpcUrls = { + [PresetTypedChainId.Goerli]: [ + 'https://ethereum-goerli.publicnode.com', + 'https://rpc.ankr.com/eth_goerli', + ], + [PresetTypedChainId.OptimismTestnet]: [ + 'https://endpoints.omniatech.io/v1/op/goerli/public', + 'https://optimism-goerli.public.blastapi.io', + ], + [PresetTypedChainId.ArbitrumTestnet]: [ + 'https://endpoints.omniatech.io/v1/arbitrum/goerli/public', + 'https://arbitrum-goerli.publicnode.com', + ], + [PresetTypedChainId.PolygonTestnet]: [ + 'https://polygon-mumbai.blockpi.network/v1/rpc/public', + 'https://polygon-mumbai-bor.publicnode.com/', + ], + [PresetTypedChainId.MoonbaseAlpha]: [ + 'https://moonbase-alpha.public.blastapi.io', + 'https://moonbeam-alpha.api.onfinality.io/public', + ], + [PresetTypedChainId.Sepolia]: [ + 'https://endpoints.omniatech.io/v1/eth/sepolia/public', + 'https://eth-sepolia.public.blastapi.io', + ], + [PresetTypedChainId.AvalancheFuji]: [ + 'https://avalanche-fuji-c-chain.publicnode.com', + 'https://api.avax-test.network/ext/bc/C/rpc', + ], + [PresetTypedChainId.ScrollAlpha]: [ + 'https://scroll-testnet.blockpi.network/v1/rpc/public', + 'https://scroll-alphanet.public.blastapi.io', + ], +}; + export const chainsConfig: Record = { // Testnet - [PresetTypedChainId.Goerli]: merge(goerli, { + [PresetTypedChainId.Goerli]: mergeChain(goerli, { chainType: ChainType.EVM, group: 'ethereum', tag: 'test', + rpcUrls: { + public: { http: additionalRpcUrls[PresetTypedChainId.Goerli] }, + default: { http: additionalRpcUrls[PresetTypedChainId.Goerli] }, + }, }), - [PresetTypedChainId.OptimismTestnet]: merge( - optimismGoerli, - { - chainType: ChainType.EVM, - group: 'optimism', - tag: 'test', - } - ), - [PresetTypedChainId.ArbitrumTestnet]: merge( - arbitrumGoerli, - { - chainType: ChainType.EVM, - group: 'arbitrum', - tag: 'test', - } - ), - [PresetTypedChainId.PolygonTestnet]: merge( - polygonMumbai, - { - chainType: ChainType.EVM, - group: 'polygon', - tag: 'test', - } - ), - [PresetTypedChainId.MoonbaseAlpha]: merge( - moonbaseAlpha, - { - chainType: ChainType.EVM, - group: 'moonbeam', - tag: 'test', - } - ), - [PresetTypedChainId.Sepolia]: merge(sepolia, { + [PresetTypedChainId.OptimismTestnet]: mergeChain(optimismGoerli, { + chainType: ChainType.EVM, + group: 'optimism', + tag: 'test', + rpcUrls: { + public: { http: additionalRpcUrls[PresetTypedChainId.OptimismTestnet] }, + default: { http: additionalRpcUrls[PresetTypedChainId.OptimismTestnet] }, + }, + }), + [PresetTypedChainId.ArbitrumTestnet]: mergeChain(arbitrumGoerli, { + chainType: ChainType.EVM, + group: 'arbitrum', + tag: 'test', + rpcUrls: { + public: { http: additionalRpcUrls[PresetTypedChainId.ArbitrumTestnet] }, + default: { http: additionalRpcUrls[PresetTypedChainId.ArbitrumTestnet] }, + }, + }), + [PresetTypedChainId.PolygonTestnet]: mergeChain(polygonMumbai, { + chainType: ChainType.EVM, + group: 'polygon', + tag: 'test', + rpcUrls: { + public: { http: additionalRpcUrls[PresetTypedChainId.PolygonTestnet] }, + default: { http: additionalRpcUrls[PresetTypedChainId.PolygonTestnet] }, + }, + }), + [PresetTypedChainId.MoonbaseAlpha]: mergeChain(moonbaseAlpha, { + chainType: ChainType.EVM, + group: 'moonbeam', + tag: 'test', + rpcUrls: { + public: { http: additionalRpcUrls[PresetTypedChainId.MoonbaseAlpha] }, + default: { http: additionalRpcUrls[PresetTypedChainId.MoonbaseAlpha] }, + }, + }), + [PresetTypedChainId.Sepolia]: mergeChain(sepolia, { chainType: ChainType.EVM, group: 'ethereum', tag: 'test', + rpcUrls: { + public: { http: additionalRpcUrls[PresetTypedChainId.Sepolia] }, + default: { http: additionalRpcUrls[PresetTypedChainId.Sepolia] }, + }, }), - [PresetTypedChainId.AvalancheFuji]: merge( - avalancheFuji, - { - chainType: ChainType.EVM, - group: 'avalanche', - tag: 'test', - } - ), - [PresetTypedChainId.ScrollAlpha]: merge( - scrollTestnet, - { - chainType: ChainType.EVM, - group: 'scroll', - tag: 'test', - contracts: { - multicall3: { - address: '0xcA11bde05977b3631167028862bE2a173976CA11', - blockCreated: 2745641, - }, + [PresetTypedChainId.AvalancheFuji]: mergeChain(avalancheFuji, { + chainType: ChainType.EVM, + group: 'avalanche', + tag: 'test', + rpcUrls: { + public: { http: additionalRpcUrls[PresetTypedChainId.AvalancheFuji] }, + default: { http: additionalRpcUrls[PresetTypedChainId.AvalancheFuji] }, + }, + }), + [PresetTypedChainId.ScrollAlpha]: mergeChain(scrollTestnet, { + chainType: ChainType.EVM, + group: 'scroll', + tag: 'test', + contracts: { + multicall3: { + address: '0xcA11bde05977b3631167028862bE2a173976CA11', + blockCreated: 2745641, }, - } - ), + }, + rpcUrls: { + public: { http: additionalRpcUrls[PresetTypedChainId.ScrollAlpha] }, + default: { http: additionalRpcUrls[PresetTypedChainId.ScrollAlpha] }, + }, + }), // Self hosted chains [PresetTypedChainId.HermesOrbit]: { diff --git a/libs/web3-api-provider/src/utils/getViemClient.ts b/libs/web3-api-provider/src/utils/getViemClient.ts index 23b4222d29..2c624e6516 100644 --- a/libs/web3-api-provider/src/utils/getViemClient.ts +++ b/libs/web3-api-provider/src/utils/getViemClient.ts @@ -1,5 +1,5 @@ import { parseTypedChainId } from '@webb-tools/sdk-core'; -import { PublicClient, createPublicClient, http } from 'viem'; +import { PublicClient, createPublicClient, fallback, http } from 'viem'; import { VIEM_NOT_SUPPORTED_MULTICALL_CHAINS, defineViemChain, @@ -19,7 +19,9 @@ function getViemClient(typedChainId: number): PublicClient { batch: { multicall: !!chain.contracts?.multicall3, }, - transport: http(undefined, { timeout: 60_000 }), + transport: fallback( + chain.rpcUrls.public.http.map((url) => http(url, { timeout: 60_000 })) + ), }); }