MetaMask / MetaMask/metamask-extension
Switching to the added network is unexpected behavior of wallet_addEthereumChain for some dapp developers
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 13.2k
- Forks
- 5.6k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 451
Description
_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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with app/scripts/lib/rpc-method-middleware/handlers/add-ethereum-chain.js at lines 270-290, where the issue says the switch prompt follows wallet_addEthereumChain. Review the surrounding handler behavior and the documentation expectation described here. Done means the intended post-add switching behavior is decided and reflected in the implementation and its user-facing documentation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100