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

Unrecognized chain ID "0x14a34" Try adding the chain using wallet_switchEthereumChain first. #25449

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

Comments

@pegasus4me
Copy link

What is this about?

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. Thanks!
The code where the error come from :

  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]) ```


### Scenario

_No response_

### Design

_No response_

### Technical Details

_No response_

### Threat Modeling Framework

_No response_

### Acceptance Criteria

_No response_

### Stakeholder review needed before the work gets merged

- [ ] Engineering (needed in most cases)
- [ ] Design
- [ ] Product
- [ ] QA (automation tests are required to pass before merging PRs but not all changes are covered by automation tests - please review if QA is needed beyond automation tests)
- [ ] Security
- [ ] Legal
- [ ] Marketing
- [ ] Management (please specify)
- [ ] Other (please specify)

### References

_No response_
@pegasus4me
Copy link
Author

open on Bug

@Wynnfan
Copy link

Wynnfan commented Dec 26, 2024

How to fix it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants