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] Inconsistent switchChain Behavior with Metamask Application #3490

Open
SerhiiKirik opened this issue Dec 17, 2024 · 3 comments
Open

[bug] Inconsistent switchChain Behavior with Metamask Application #3490

SerhiiKirik opened this issue Dec 17, 2024 · 3 comments
Labels
bug Something isn't working needs review

Comments

@SerhiiKirik
Copy link

Link to minimal reproducible example

https://appkit-lab.reown.com/library/wagmi/

Summary

When attempting to use switchChain to align the user’s Metamask wallet chain with the required Sepolia test network (chain ID: 11155111), various issues occur. These issues arise during initial wallet connection, subsequent chain changes initiated on the user’s phone, and while performing contract interactions. Occasionally, the native Switch chain modal from reown appears or fails unexpectedly, leading to user confusion and disrupted workflows. Below are the detailed scenarios:

Safari PC: 18.1.1 (20619.2.8.11.12)
Metamask 7.37.0

Details & Scenarios

Context:

  • Required (correct) chain: Sepolia (ID: 11155111)
  • Incorrect chain: Ethereum Mainnet (ID: 1)
  • The Switch chain modal is the native UI modal from reown.
    Scenario 1: Connecting Wallet on an Incorrect Chain
  • Steps:
  1. The user opens the application while their Metamask is on an incorrect chain (e.g., Ethereum Mainnet).
  2. Upon connecting, the app prompts the user to switch to the correct chain (Sepolia).
  3. Occasionally, after confirming the switch, the chain does successfully switch in Metamask.
  • Issue:
    Despite the chain successfully switching in Metamask, the native Switch chain modal displays an error:
Switch declined, switch can be declined if chain is not supported by a wallet previous request still active

It then offers a "try again" option. Selecting "try again" closes the modal, but this step is unnecessary and confusing since the chain is already switched.

2024-12-17.19.50.30.mov

Expected Behavior:
No error should appear if the chain switch is successful. The modal should close automatically without prompting to "try again."

Scenario 2: Changing to an Incorrect Chain After Initial Correct Connection

Steps:

  1. The user initially connects their wallet on the correct chain (Sepolia).
  2. On their phone, they manually switch the wallet’s chain to an incorrect one (e.g., Ethereum Mainnet).
  3. The Switch chain modal appears, prompting the user to switch back to Sepolia.
  • Issue:
    When the user selects Sepolia in the modal, the modal closes but does not actually trigger the chain switch in Metamask. The chain remains on the incorrect network, leaving the user stuck on the wrong chain.
2024-12-17.20.04.05.mov

Expected Behavior:
Selecting the correct chain (Sepolia) in the modal should actively switch the chain in Metamask rather than just closing the modal.

Scenario 3: Performing Contract Actions on an Incorrect Chain

  • Steps:
  1. The user is connected to their wallet but is currently on an incorrect chain.
  2. The user attempts a contract interaction (e.g., readContract or writeContract).
  3. The application prompts the user to switch to the correct chain.
  4. The user agrees to switch chains.
  • Issue:
    After agreeing to switch, an error occurs:
{ ..., code: 4001, message: "User rejected the request." }

This can happen if the user previously switched the chain manually outside the app (similar to Scenario 2), causing the chain switching logic to fail or become confused.

Expected Behavior:
If the user agrees to switch, the chain should switch correctly, and the contract call should proceed without a "User rejected" error message.

Scenario 4: Spontaneous Appearance of the Switch chain Modal

  • Behavior: Without any user action (such as navigating, switching chains manually, or performing contract actions), the Switch chain modal sometimes appears on its own, even when the wallet is already on the correct chain.
  • Issue: This unexpected prompt is both confusing and disruptive. It’s not clear what triggers it, and since the chain is already correct, it should not appear at all.

Expected Behavior:
The Switch chain modal should only appear when the wallet’s current chain does not match the required chain and should not randomly pop up otherwise.

I’ve recorded videos demonstrating these issues (though they are local), but you can reproduce the behavior here:
AppKit Lab (Wagmi)

I am happy to provide more details about my setup and even prepare a small demo if that would be helpful.

List of related npm package versions

"@reown/appkit": "1.5.3",
"@reown/appkit-adapter-wagmi": "1.5.3",
"viem": "2.21.54",
"wagmi": "2.13.5"
"@wagmi/core": "^2.15.2"

Node.js Version

v20.17.0

Package Manager

10.8.2

@SerhiiKirik SerhiiKirik added bug Something isn't working needs review labels Dec 17, 2024
Copy link

linear bot commented Dec 17, 2024

Copy link

thanks for this we are looking into it!

@fablerq
Copy link

fablerq commented Jan 5, 2025

Same error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs review
Projects
None yet
Development

No branches or pull requests

3 participants