metamask-extension icon indicating copy to clipboard operation
metamask-extension copied to clipboard

Switching to the added network is unexpected behavior of wallet_addEthereumChain for some dapp developers

Open sbauch opened this issue 5 years ago • 3 comments

NB: sorry if this isn't considered a "bug" by the team. the code is working as expected, but based on the documentation and the general expectation of this function, the call leads to unexpected behavior

Describe the bug

When adding a new network with wallet_addEthereumChain, asking the user to then switch to that chain is unexpected behavior.

Steps to reproduce (REQUIRED)

call wallet_addEthereumChain - whether the network exists or not, the following lines then ask the user to switch networks to the newly added chain:

https://github.com/MetaMask/metamask-extension/blob/35531d83602a6cdf775cd9816bd375ab852a694b/app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js#L270-L290

Expected behavior

The chain is added if all the checks are successful, and the developer should be responsible for deciding if we want the user to switch to that network.

Browser details (please complete the following information):

This is pretty clearly by design, I just think it's maybe not the right design choice. This will happen in every browser etc etc.

It is perhaps necessary to ensure that the network was correctly added, which would be a fine reason!

Additional context (Error Messages, etc.)

Sorry if this isn't technically a "bug" but none of the docs mention this, and suggest a null response if successful. The response there is null, but it also asks the user to switch to that network, and I'd rather they not do that immediately.

I'm adding multi-chain capabilities to my dApp. I want to ensure users have set up the Matic chain with their wallet prior to interacting with the dApp to migrate tokens from ethereum mainnet to matic. The migration requires an ethereum signer, so this is forcing me to

  1. ask user to add matic
  2. ask user to switch to matic
  3. ask user to switch back to ethereum mainnet

I could rework the UX, but if its possible to remove this requirement to switch the the newly added network, perhaps add it as an option for the developer to pass, that would support more use cases?

sbauch avatar Jun 04 '21 18:06 sbauch

try { await updateRpcTarget( await requestUserApproval({ origin, type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN, requestData: { rpcUrl: firstValidRPCUrl, chainId: _chainId, nickname: _chainName, ticker, }, }), ); } catch (error) { // For the purposes of this method, it does not matter if the user // declines to switch the selected network. However, other errors indicate // that something is wrong. if (error.code !== errorCodes.provider.userRejectedRequest) { return end(error); } }

saifalmea avatar Jun 07 '21 06:06 saifalmea

try { await updateRpcTarget( await requestUserApproval({ origin, type: MESSAGE_TYPE.SWITCH_ETHEREUM_CHAIN, requestData: { rpcUrl: firstValidRPCUrl, chainId: _chainId, nickname: _chainName, ticker, }, }), ); } catch (error) { // For the purposes of this method, it does not matter if the user // declines to switch the selected network. However, other errors indicate // that something is wrong. if (error.code !== errorCodes.provider.userRejectedRequest) { return end(error); } }

saifalmea avatar Jun 07 '21 06:06 saifalmea

https://github.com/MetaMask/metamask-extension/issues/11238

ngalee8311 avatar Sep 25 '25 18:09 ngalee8311