walletconnect-monorepo
walletconnect-monorepo copied to clipboard
Sending an RPC call, that the provider doesn't support, never resolves
Background: I'm writing an application that uses wallet_switchEthereumChain
method, suggested by EIP-3326. While it seems like only MetaMask supports it at the moment, I'd like to call it anyway and fallback to asking a user to switch a network manually if the method isn't supported.
When I try to do the following call, WelletConnect's provider creates a promise that never resolves:
provider.send('wallet_switchEthereumChain', [{chainId}]);
I'd expect the promise to get rejected though. The current behaviour, unfortunately, is impossible to handle and can make any application freeze if it expects promises to always resolve/reject eventually.
Any update on this issue ?
I struggle one hour as well on this. How did you know that it was not supported by walletconnect? It should normally work on metamask mobile or no? The same the promise never resolve or reject. Thanks in advance, if you found a solution.
I too experienced this issue. I am using Web3Modal as my umbrella library for wallets. To this day it is unclear which wallets support or freeze with the following:
web3.currentProvider.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: "0x3" }],
})
But i have had problems with WalletConnect and Metamask Mobile. I'm considering doing a check for WalletConnect and simply bypassing the above request. For now I've addd a setTimeout to timeout the promise and reject. The following will make the request but will timeout after 10 secs issuing a reject(). The following is written in Javascript:
try {
return new Promise((resolve, reject) => {
web3.currentProvider.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: "0x3" }],
})
.then(a => {
console.log("Resolve = ", a);
resolve(null)
})
.catch(e => {
console.log("Error = ", e);
reject(e)
});
// bc Metamask Mobile and Walletconnect appear to have issues with wallet_switchEthereumChain (the above will never finish)
// the following will time out rejecting the promise
setTimeout(() => {
console.log("SwitchChain Timed out");
reject(false);
}, 10000);
});
} catch (error) {}
I also recall reading javascript having a way to abort async calls or Promises via AbortController(). It might be worth checking out.
For anyone else that lands here with this issue, for me, it turned out to be that MM mobile did not have the network I was switching to installed. Once I added the missing network, I was able to get prompt to change networks from my dApp. So maybe a good solution combines the timeout suggestion by @quantass with a call to first add the network and then switch if that call goes through
@mvlabat is this still an issue?