Skip to content

[Bug] WebGLMetaMask.Instance.GetActiveChainId() prevents correct network switch after user changes network outside dApp #237

@starker-xp

Description

@starker-xp

Hello thirdweb team,

We are using thirdweb Unity v5 in a WebGL game with MetaMask wallet integration.
We’ve encountered a critical issue with network switching in a multi-chain context:

If a user changes their network in MetaMask outside of our dApp (e.g., from Ethereum to Base), then visits our site, WebGLMetaMask.Instance.GetActiveChainId() returns the previous chainId (e.g., 8453), even though MetaMask is now on a different network (e.g., 1).
The library checks if (WebGLMetaMask.Instance.GetActiveChainId() != chainId) before attempting wallet.SwitchNetwork(chainId).
As a result, the switch is not attempted, and any write transaction fails because the wallet is not actually on the correct network.
Workaround:
Removing this check in the library resolves the issue, as the switch is always attempted and the wallet is correctly synchronized.

Expected behavior:
The SDK should always attempt to switch the network, or at least ensure that the chainId is up-to-date with the actual wallet state before skipping the switch.

Steps to reproduce:

User connects MetaMask to network A (e.g., Base 8453).
User disconnects and changes MetaMask to network B (e.g., Ethereum 1) outside the dApp.
User visits the dApp and attempts a transaction on network B.
The dApp/SDK believes the wallet is still on network A, does not attempt a switch, and the transaction fails.
Environment:

Unity version: 2022.3.60f1
thirdweb Unity SDK version: v5
Wallet: MetaMask (WebGL)
Chain(s): 8453, 84532, 1, 137
Suggested solution:

Always attempt the network switch, or
Force a refresh of the chainId from MetaMask before the check.
Thank you for your support!

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions