metamask-extension icon indicating copy to clipboard operation
metamask-extension copied to clipboard

[Bug]: Unrecognized chain ID "0x14a34". Try adding the chain using wallet_switchEthereumChain first.' even with a correct chainId only happens on brave browser

Open pegasus4me opened this issue 1 year ago • 2 comments

Describe the bug

Hello There! When I try to switch network with "wallet_switchEthereumChain" I got this error -32603 telling me that the chainId is not recognised, I have double checked and everything seems good on my code the chain id for "Base sepolia" is correct so I don't know why I got this error when I run it on Brave browser, when I try on chrome it works fine.

The code where the error come from

export function useSwitchNetwork (): {
  switchNetwork: () => Promise<void>
} {
  const { provider, chainId } = useWalletData()
  const switchNetwork = useCallback(async () => {
    if ((provider == null) || !chainId) {
      console.log('Provider or chainId is missing')
      return
    }

    console.log('Attempting to switch network to:', `0x${Number(ChainId.SEPOLIA).toString(16)}`)

    try {
      await window.ethereum?.request({
        method: 'wallet_switchEthereumChain',
        params: [{ chainId: `0x${Number(ChainId.SEPOLIA).toString(16)}` }]
      })
      console.log('Network switched successfully')
    } catch (switchError: any) {
      console.log('Error switching network:', switchError)

      // This error code indicates that the chain has not been added to MetaMask
      if (switchError.code === 4902) {
        console.log('Chain not found. Adding new chain...')
        try {
          await window.ethereum?.request({
            method: 'wallet_addEthereumChain',
            params: [{
              chainId: `0x${Number(ChainId.SEPOLIA).toString(16)}`,
              rpcUrls: ['https://base-sepolia-rpc.publicnode.com'],
              chainName: 'Base sepolia', 
              nativeCurrency: {
                name: 'ETH',
                symbol: 'ETH',
                decimals: 18
              },
              blockExplorerUrls: ['https://explorer-sepolia.etherscan.io'] 
            }]
          })
          console.log('New chain added successfully')
        } catch (addError: any) {
          console.error('Error adding new chain:', addError)
        }
      }
    }
  }, [provider, chainId])

Expected behavior

metamask popup opens and suggest me to switch chain to base sepolia

Screenshots/Recordings

No response

Steps to reproduce

Error messages or log output

No response

Version

11.16.11

Build type

None

Browser

Brave

Operating system

Windows

Hardware wallet

No response

Additional context

No response

Severity

No response

pegasus4me avatar Jun 20 '24 19:06 pegasus4me

You may be encountering issues with contention between multiple wallets using window.ethereum. Please take a look at the new standard for connecting to wallets: https://docs.metamask.io/wallet/concepts/wallet-interoperability/ https://docs.metamask.io/wallet/how-to/connect/ https://docs.metamask.io/wallet/how-to/manage-networks/add-network/

vandan avatar Jun 28 '24 18:06 vandan

We were unable to reproduce. Can you copy and share the exact error message you receive?

vandan avatar Jun 28 '24 18:06 vandan

@vandan I am experiencing this issue with Chrome and Brave browsers. I am using a custom dApp with a custom chain. For our app, we are using web3onboard and I am pretty sure that the add of a non-existing chain was working like a charm before. Now, I see the following:

image

With FireFox it works.

If you want to try it out, checkout our testing dApp - https://app.testnet.hydrachain.org/delegation/

Vitomir2 avatar Aug 29 '24 10:08 Vitomir2