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

switchChainAsync throw an error when user approved #4223

Closed
1 task done
xianchenxc opened this issue Aug 25, 2024 · 2 comments · Fixed by #4227
Closed
1 task done

switchChainAsync throw an error when user approved #4223

xianchenxc opened this issue Aug 25, 2024 · 2 comments · Fixed by #4227

Comments

@xianchenxc
Copy link
Contributor

Check existing issues

Describe the bug

What I am doing:

  1. Connect MetaMask wallet using the "metaMask" connector.
  2. Refresh page
  3. Switch to a network (e.g., BSC) that is not yet added to your MetaMask wallet.
  4. A popup to switch to BSC appears, click “Approve” and then “Switch Network”.

What I expected:

The network is added and switched without any errors.

What is actually happening:

switchChainAsync throws an error: User rejected the request.
Details: The user rejected the switch after the network was added.
Version: 2.20.0

Link to Minimal Reproducible Example

https://codesandbox.io/p/sandbox/wagmi-switchchain-6m86q5

Steps To Reproduce

Here is the link: https://codesandbox.io/p/sandbox/wagmi-switchchain-6m86q5

Steps to reproduce the issue:

  1. Remove BNB Smart Chain from your MetaMask wallet.
  2. Click the Connect button.
  3. Refresh the page.
  4. Click the BNB Smart Chain button.
  5. A popup to switch to BSC will appear. Click Approve, then Switch Network.
  6. An error message will be displayed below the button.

What Wagmi package(s) are you using?

wagmi

Wagmi Package(s) Version(s)

2.12.7

Viem Version

2.20.0

TypeScript Version

5.5.4

Anything else?

No response

@xianchenxc
Copy link
Contributor Author

xianchenxc commented Aug 25, 2024

I checked the code in "packages/connectors/src/metaMask.ts", and I believe it needs to wait for the "change" event emitter:

// packages/connectors/src/metaMask.ts
await provider.request({
  method: 'wallet_addEthereumChain',
  params: [addEthereumChain],
})

const currentChainId = await this.getChainId()

It might look something like this:

await Promise.all([
  provider.request({
    method: 'wallet_addEthereumChain',
    params: [addEthereumChain],
  }),
  new Promise<void>((resolve) =>
    config.emitter.once('change', ({ chainId: currentChainId }) => {
      if (currentChainId === chainId) resolve()
    }),
  ),
])

const currentChainId = await this.getChainId()

Copy link
Contributor

github-actions bot commented Oct 1, 2024

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Wagmi version. If you have any questions or comments you can create a new discussion thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant