… switch the network without prompting user approval (#4846)
## Explanation
The release of Chain Permissions in Extension have changed the behavior
of `wallet_switchEthereumChain` and `wallet_addEthereumChain` which
breaks previous assumptions that the `QueuedRequestController` operated
from. Specifically, it was previously assumed that the above methods
would ALWAYS generate an approval that the user must accept or reject.
This is important because the side effect of this behavior was that it
was not possible for those two methods to immediately switch the chain
if there happened to be existing pending approvals. The result of this
new behavior was that pending approvals were being immediately cleared
if one of the method calls above were to a chain that was already
permitted which is the case where a network switch happens immediately
without user interaction.
This PR fixes this new case by:
* explicitly waiting for any batch of processing requests to finish
before proceeding with processing a request that has the potential to
switch the globally selected network
* processing a request that has the potential to switch the globally
selected network by itself (rather than releasing it within a batch of
several requests for the same origin)
* flushing the request queue for the origin IF a request that has the
potential to switch the globally selected network actually does change
the globally selected network
## References
Related: MetaMask/metamask-extension#28090
## Changelog
<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.
(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)
Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).
If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.
Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->
### `@metamask/queued-request-controller`
- **BREAKING**: `QueuedRequestController` now requires the
`canRequestSwitchNetworkWithoutApproval` callback in its constructor
params.
- **CHANGED**: `QueuedRequestController` now ensures that any queued
requests for a origin are failed if a request that can switch the
globally selected network without approval actually does change the
globally selected network for that origin.
- **CHANGED**: `QueuedRequestController` now ensures that a request that
can switch the globally selected network without approval is queued
behind any existing pending requests.
## Checklist
- [ ] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
- [ ] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
---------
Co-authored-by: Mark Stacey <markjstacey@gmail.com>
Description
Bumps
@metamask/queued-request-controllerto fix queueing issue with Chain Permissionwallet_switchEthereumChainandwallet_addEthereumChainwhen switching to a previously permitted chain and withwallet_addEthereumChainnot being enqueued when it still should be.Related issues
Related: MetaMask/core#4846
Fixes: #28101
Fixes: #27977
Fixes: #28102
Manual testing steps
The easiest way to test this would be a combination of using the test dapp and the following request to switch chains
The behaviors you should see include:
One dapp:
Two dapps:
Screenshots/Recordings
Before
After
Screen.Recording.2024-10-24.at.1.57.19.PM.mov
Screen.Recording.2024-10-30.at.8.44.54.AM.mov
Pre-merge author checklist
Pre-merge reviewer checklist