js icon indicating copy to clipboard operation
js copied to clipboard

Coinbase wallet chain doesn't switch correctly

Open codingfarhan opened this issue 1 year ago • 4 comments
trafficstars

Hi guys,

I'm having trouble with executing transactions with coinbase wallet extension, as every single time I switch to a different chain (in this case, Sepolia) and try to execute a transaction, it gives me the following error:

underlying network changed (event="changed", network= {"name":"sepolia", " chainId": 11155111,"ens Address": null}, detectedNetwork= ["name". "homestead" "chainld":1"ensAddress"."0×00000000000C2 E074eC69A0dFb2997BA6C7d2e1e"}, code=NETWORK_ERROR, version=providers/5.7.2)

My packagees:

"@thirdweb-dev/react": "^4.6.2",
"@thirdweb-dev/sdk": "^4.0.63",
"@thirdweb-dev/wallets": "2.5.3",
"ethers": "^5",

It works sometimes, other times it shows me the error above. Would be a great help 🙏 .

Thanks!

codingfarhan avatar Jul 08 '24 20:07 codingfarhan

can you share your code? One thing that i would say from experience is that it's safer to do do the switch chain first as a user click, then the transaction another user click. If done in quick succession the wallet often doesnt handle it well

joaquim-verges avatar Jul 09 '24 07:07 joaquim-verges

Hi, Im doing exactly as you said. There's a button to switch the chain (using useSwitchChain()) and once user is on the correct chain they get asked to execute the transaction. The transaction happens as follows:


if (txChainId !== chainId) {
      await switchChain(txChainId);
    } else {
      let transferTxResponse;

      try {
        transferTxResponse = await sdk?.wallet.transfer(message.to, message.value);
      } catch (e: any) {
        errorToast(e?.data?.message || e?.message);
        setIsClicked(false); // If process fails, we should allow user to try again
      }

.... and so on

codingfarhan avatar Jul 09 '24 12:07 codingfarhan

yes this has been a problem with the v4 sdk for a while, likely not something we can fix in v4.

in order to make this work in v4:

  • you have to make sure the sdk.wallet.transfer gets called at least on the next JS tick (meaning you could try wrapping the transfer call in a setTimeout(() => {/* your code to transfer here */}, 0) and that should make it work

  • if that is still flakey then the guaranteed way to make it work is to split it into 2 user interactions:

    • first interacting switches wallet
    • then click again to send the transaction

    that said I believe this behavior is fixed in v5 of the SDK, so if upgrading is an option that should do it, too

jnsdls avatar Jul 11 '24 22:07 jnsdls

I see now that it seems like you already do it in 2 steps... that is... very odd that it does not work in this case and something we'll likely have to look at. I would still assume that this will work in v5 (I believe we have tests for this even? if not we will add some)

jnsdls avatar Jul 11 '24 22:07 jnsdls

I see now that it seems like you already do it in 2 steps... that is... very odd that it does not work in this case and something we'll likely have to look at. I would still assume that this will work in v5 (I believe we have tests for this even? if not we will add some)

sure! please let me know if it works in v5! It'll be very helpful. Thanks.

codingfarhan avatar Jul 25 '24 07:07 codingfarhan