Hi,
So. I build a small dapp. A user is able to connect through Metamask or Wallet-Connect.
If a user connects with MetaMask (Desktop) I use this:
if (!isClaimed[stateId]){
providerInstance.currentProvider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: chainId}]
}).catch((err) => {
if (err.code == 4902){
addFuseNetwork(chainId);
} else {
setError({status: null, code: err.code});
}
});
}
For desktop, this works.
The same request done while a user is connected through WalletConnect with their mobile Metamask, doesn’t trigger anything?
What do I need to do or change in order for sending the switch network request to the mobile wallet through WalletConnect?
Is this possible at all? (The docs don’t state it as unavailable?)
Thanks in advance!
5 Likes
Hey @DeMewlingOak , welcome to the MetaMask community!
Try using this source code:
https://docs.metamask.io/guide/rpc-api.html#:~:text=We%20recommend%20using%20this%20method%20with%20wallet_addEthereumChain%3A
If you are also interested, you can join the developer community on ConsenSys Discord (MetaMask is a part of ConsenSys):
5 Likes
thanks first of all for the response.
I have used that approach.
And for desktop this works.
It’s just mobile metamask doesn’t get the request, or sends out the ‘chainChanged’ event when a user does it manually.
What can cause this?
Should I look into metamask, or is rather a problem with using walletConnect as the provider?
2 Likes
Tried searching for the issue for you, but there doesn’t seem to be any answers for it
I would suggest try posting it in https://ethereum.stackexchange.com/ also.
1 Like
YYee
April 18, 2022, 11:26am
6
Hi, I’m facing the same issue for mobile too.
I called the wallet_switchEthereumChain as well and all it did is redirects me to Metamask and back without doing anything.
Is there any fix or workaround for this already? Can’t seem to search this anywhere in ethereum stackexchange also.
4 Likes
vivekm
August 22, 2022, 9:07am
7
Hello @YYee
have you found any solution to this?
2 Likes
I am facing the same problem. I don´t know how to switch the network via MetaMask mobile.
Hello @robsonsf ! Welcome to MetaMask community!
Can you provide your code with more verbose error output?
here or create a new topic on the forum
2 Likes
I don´t have an error. I just want to know how to switch the network in MetaMask mobile using code.
1 Like
I got it. I already wrote about it in the next topic.
Let me know if something didn’t work out for you
@rc_plt hello again )
When initializing the application, you need to determine the current web3 provider and a web3 instance:
useEffect(() => {
const loadProvider = async () => {
const provider = await detectEthereumProvider()
if (provider) {
const web3 = new Web3(provider);
setListener(provider);
setApi({
provider,
web3
});
} else {
console.error("gotta be install metamask");
}
};
loadProvider()…
As for displaying the interface based on the current network, for convenience, you can create a hook:
const NETWORKS = {
1: "Ethereum Main Network",
56: "Binance Smart Chain",
};
export const handler = web3 => {
const targetChain = NETWORKS[process.env.CHAIN_YOU_WANNA];
const { data, error } = useSWR(
() => (web3 ? "web3/network" : null),
async () => {
const chainId = await web3.eth.getChainId();
if (!chainId) {
throw new Error("Gotta refresh page.");
…
4 Likes