Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pegasus4me opened this issue Jun 20, 2024 · 2 comments

Comments

@pegasus4me
Copy link

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

@vandan
Copy link

vandan commented Jun 28, 2024

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
Copy link

vandan commented Jun 28, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: To be fixed
Status: To be fixed
Development

No branches or pull requests

4 participants