wevm / wagmi

Reactive primitives for Ethereum apps
https://wagmi.sh
MIT License
6.01k stars 1.07k forks source link

After reload page, the chainId is different from the one in the Metamask. #4386

Open AliceW-03 opened 2 weeks ago

AliceW-03 commented 2 weeks ago

Check existing issues

Describe the bug

When I connect metamask in my telegram webapp, switch chains, and then reload the page, the chainId is different from the one in the wallet.

I noticed that metamask store a .MMSDK_cached_chainId in the localStorage to persist the chain info. After switching chains, this field retains the old chainId. When the page is reloaded, the retrieved chainId becomes the old value, even though the wallet has already switched to the new chain.

I have raised an issue on the metamask-sdk, but after testing the metamask-sdk demo provided, I found that directly using metamask-sdk, rather than connecting through wagmi, did not have this issue. It seems that after switching chains, the original callback for updating the cache either didn’t execute or was overridden by your code.

Link to Minimal Reproducible Example

https://metamask-cache-chain-id.vercel.app/ https://github.com/AliceW-03/metamask-cacheChainId.git

Steps To Reproduce

Please read my description carefully first.

I deployed a reproduction in my test bot. You can enter the bot and type /metamask.

  1. connect wallet
  2. switch chain
  3. reload in the top right corner.
  4. check localStorage

What Wagmi package(s) are you using?

wagmi, @wagmi/connectors

Wagmi Package(s) Version(s)

2.12.25

Viem Version

2.21.37

TypeScript Version

5.5.3

Anything else?

No response

AliceW-03 commented 2 weeks ago

metamask-sdk: https://github.com/user-attachments/assets/ea278188-e8ab-4dfc-ab14-f95757e4fd1f

AliceW-03 commented 2 weeks ago

wagmi: https://github.com/user-attachments/assets/4c996245-23c7-45f3-9495-834a9a71465f

imaksp commented 2 weeks ago

is this specific to injected connector ? have you tried using wagmi's metaMask connector (which uses metamask's sdk)?

AliceW-03 commented 1 week ago

@imaksp I'm using wagmi's metaMask connector of course🙂

roshan-synquote commented 1 day ago

Any updates here?