Skip to content

Commit

Permalink
Enable getDefaultProvider for evm instead of manual implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sophialittlejohn committed Sep 5, 2024
1 parent 6ff7e7c commit bb8c0cf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
7 changes: 7 additions & 0 deletions centrifuge-app/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -216,6 +221,7 @@ export const evmChains: EvmChains = {
urls: ['https://forno.celo.org'],
iconUrl: celoLogo,
isTestnet: false,
network: 'celo-mainnet',
},
44787: {
name: 'Celo Alfajores',
Expand All @@ -228,6 +234,7 @@ export const evmChains: EvmChains = {
urls: ['https://alfajores-forno.celo-testnet.org'],
iconUrl: celoLogo,
isTestnet: true,
network: 'celo-alfajores',
},
}

Expand Down
52 changes: 31 additions & 21 deletions centrifuge-react/src/components/WalletProvider/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<WalletContextType>(null as any)

export const wallets = getWallets()
Expand Down Expand Up @@ -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<number, JsonRpcProvider>()
const cachedProviders = new Map<number, JsonRpcProvider | AbstractProvider>()

export function WalletProvider({
children,
Expand All @@ -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')

Expand Down Expand Up @@ -240,26 +255,21 @@ export function WalletProvider({

const [proxies] = useCentrifugeQuery(['allProxies'], (cent) => cent.proxies.getAllProxies())

async function findHealthyProvider(chainId: number, urls: string[]): Promise<JsonRpcProvider> {
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[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type BasicChainInformation = {
}

type ExtendedChainInformation = BasicChainInformation & {
network: string
name: string
nativeCurrency: AddEthereumChainParameter['nativeCurrency']
blockExplorerUrl: string
Expand Down

0 comments on commit bb8c0cf

Please sign in to comment.