Skip to content

Commit

Permalink
fix: unrecognised chainId error when using MetaMask mobile browser (#…
Browse files Browse the repository at this point in the history
…774)

* check for metamask mobile app error code as well on chain switching

* code review - keep error type

* code review - description for error code 4902

* Update index.ts

* chore: inline editing can cause troubles

---------

Co-authored-by: Mike Grabowski <[email protected]>
  • Loading branch information
vercy and grabbou authored Apr 27, 2023
1 parent ebe11f5 commit 8639646
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/metamask/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ export class MetaMask extends Connector {
params: [{ chainId: desiredChainIdHex }],
})
.catch((error: ProviderRpcError) => {
if (error.code === 4902 && typeof desiredChainIdOrChainParameters !== 'number') {
// https://github.com/MetaMask/metamask-mobile/issues/3312#issuecomment-1065923294
const errorCode = (error.data as any)?.originalError?.code || error.code

// 4902 indicates that the chain has not been added to MetaMask and wallet_addEthereumChain needs to be called
// https://docs.metamask.io/guide/rpc-api.html#wallet-switchethereumchain
if (errorCode === 4902 && typeof desiredChainIdOrChainParameters !== 'number') {
if (!this.provider) throw new Error('No provider')
// if we're here, we can try to add a new network
return this.provider.request({
Expand Down

0 comments on commit 8639646

Please sign in to comment.