Replies: 1 comment
|
Hello @Jenny1610, this is a known behavior when interacting with MetaMask Mobile via WalletConnect (especially on non-Ethereum mainnet chains like BSC Testnet). Root CauseThe
Recommended Solutions1. Explicitly Await
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi Viem team 👋,
I'm encountering a confusing issue using viem@2.21.31 and wagmi@2.x when calling writeContract on mobile devices (MetaMask via WalletConnect).
✅ Context:
I'm working on a dApp that supports multiple chains (BSC, Polygon, Base, etc.).
The user connects via MetaMask mobile (WalletConnect).
Chain bscTestnet (chainId = 97) is already added and active in MetaMask.
I'm using getWalletClient(...) from wagmi/actions to confirm chain is correct.
The full flow works fine on desktop MetaMask, but fails on mobile.
📋 Code Summary:
const client = await getWalletClient(wagmiConfig, { chainId });
toastInfo(
wallet client id: ${client?.chain?.id}); // Logs: 97await writeContract({
abi: [...],
address: tokenAddress,
functionName: 'transfer',
args: [receiver, amount],
chainId: 97, // Also tried without this param, still fails
});
🔥 Error on mobile:

Details: Invalid chainId
Version: viem@2.21.31
Even though:
chainId is 97
wagmiConfig.chains includes bscTestnet
getWalletClient() returns chainId 97
MetaMask is visibly on BSC Testnet
🧪 Tried:
Ensuring chainId is number
Switching chain via switchChainAsync(...)
Confirming bscTestnet is part of both Viem + Wagmi config
Logging chainId at every step (it's always 97)
Forcing chainId into writeContract(...)
Still get the same Invalid chainId error only on mobile MetaMask (WalletConnect).
❓Questions:
P/S: i tried the newest viem version (2.31.7) but it didn't work and has the same issue
All reactions