Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Bump @metamask/providers to ^20.0.0 #29936

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

jiexi
Copy link
Contributor

@jiexi jiexi commented Jan 28, 2025

Description

Previously the inpage provider would withhold events for chainChanged events (and property value updates, i.e. window.ethereum.chainId and .networkVersion) when the dapp's network was changed to an rpc endpoint that was unresponsive or did not support net_version. The dapp would instead receive a disconnect event.

Now the inpage provider always emits chainChanged and networkChanged events (and exposes the correct values on window.ethereum.chainId and .networkVersion) when the selected network for the dapp has changed regardless of if the network being changed to is responsive or if it supports net_version requests. It does this by having the wallet send a loading for networkVersion when it cannot be resolved (same behavior as before) AND a new isConnected property in the metamask_getProviderState request and metamask_chainChanged events (these are different from the events emittted by window.ethereum). isConnected is derived from whether the NetworkController.state.networkMetadata[].status value is the Available constant.

Additionally, this PR also no longer fires metamask_unlockStateChanged events to the inpage provider when the wallet is locked or unlocked. This has the downstream effect of making window.ethereum no longer emit an empty array value for the accountsChanged event when the logic becomes locked.

Open in GitHub Codespaces

Related issues

Fixes: https://github.com/orgs/MetaMask/projects/146/views/1?filterQuery=label%3A%22team-wallet-api-platform%22+-status%3ABacklog&pane=issue&itemId=95374156&issue=MetaMask%7CMetaMask-planning%7C4039
Fixes: https://github.com/MetaMask/MetaMask-planning/issues/3853

Manual testing steps

window.ethereum.on('accountsChanged', (data) => console.log('accountsChanged', data))
window.ethereum.on('chainChanged', (data) => console.log('chainChanged', data))
window.ethereum.on('networkChanged', (data) => console.log('networkChanged', data))
window.ethereum.on('connect', (data) => console.log('connect', data))
window.ethereum.on('disconnect', (data) => console.log('disconnect', data))

For chainChanged/networkChanged/connect/disconnect:

  1. Go to a dapp and enter the above in console
  2. Using the wallet ui, switch the network for the dapp
  3. When switching to a network without net_version support, networkChanged should be emitted by the inpage provider with null
  4. When switching to a network that is not responsive, a disconnect event should be fired AND chainChanged and networkChanged should fire as well if those values have changed
  5. When switching from an unresponsive network to a responsive network, a connect event should be fired AND chainChanged and networkChanged should fire as well if those values have changed
  6. When switching to a network that was previously responsive that also supported net_version but is no longer responsive, a disconnect event should be fired and networkChanged should return the previously cached value

For accountsChanged:

  1. Go to a permissioned dapp and enter the above in console
  2. Using the wallet ui, lock the wallet
  3. There should be no accountsChanged event emitted on the dapp console
  4. Using the wallet ui, unlock the wallet,
  5. There should be no accountsChanged event emitted on the dapp console

Screenshots/Recordings

Before

For chainChanged/networkChanged/connect/disconnect:

Screen.Recording.2025-01-27.at.4.13.46.PM.mov

For accountsChanged:

Screen.Recording.2025-02-03.at.3.32.32.PM.mov

After

For chainChanged/networkChanged/connect/disconnect:

Screen.Recording.2025-01-27.at.3.53.39.PM.mov

For accountsChanged:

Screen.Recording.2025-02-03.at.3.27.55.PM.mov

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@jiexi jiexi changed the title fix: update inpage provider chainChanged, networkChanged`, 'connect', and 'disconnect', events fix: update inpage provider chainChanged, networkChanged, 'connect', and 'disconnect', events Jan 28, 2025
@jiexi jiexi changed the title fix: update inpage provider chainChanged, networkChanged, 'connect', and 'disconnect', events fix: update inpage provider chainChanged, networkChanged, connect, and disconnect events Jan 28, 2025
@metamaskbot
Copy link
Collaborator

Builds ready [5eea5ea]
Page Load Metrics (1800 ± 92 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint39823041719356171
domContentLoaded14572266176718890
load14952277180019292
domInteractive26100422110
backgroundConnect10133342914
firstReactRender15101452914
getState492212412
initialActions00000
loadScripts10331714129216579
setupStore86420199
uiStartup171828582091303146
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 139 Bytes (0.00%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@jiexi jiexi changed the title fix: update inpage provider chainChanged, networkChanged, connect, and disconnect events feat: Bump @metamask/providers to ^20.0.0 Feb 3, 2025
@jiexi jiexi marked this pull request as ready for review February 3, 2025 23:39
@jiexi
Copy link
Contributor Author

jiexi commented Feb 3, 2025

Reviewable, functional, but should probably be covered by two new E2E tests. I'll work on that tomorrow / this week

@metamaskbot
Copy link
Collaborator

Builds ready [0435868]
Page Load Metrics (1813 ± 66 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint24620811732367176
domContentLoaded15232057178813364
load15332081181313666
domInteractive2696392211
backgroundConnect1188322612
firstReactRender1792442512
getState588222211
initialActions01000
loadScripts10691493129310450
setupStore863232210
uiStartup17692525211220297
Bundle size diffs [🚀 Bundle size reduced!]
  • background: -121 Bytes (-0.00%)
  • ui: -367 Bytes (-0.00%)
  • common: -51 Bytes (-0.00%)

Copy link
Contributor

@itsyoboieltr itsyoboieltr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -3059,11 +3059,11 @@ export default class MetamaskController extends EventEmitter {
if (chains.length > 0 && !chains.includes(currentChainIdForOrigin)) {
const networkClientId =
this.networkController.findNetworkClientIdByChainId(chains[0]);
this.networkController.setActiveNetwork(networkClientId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] can we add a comment explaining why the setActiveNetwork call needs to come before the setNetworkClientIdForDomain call

* We default `isUnlocked` to `true` because even though we no longer emit events depending on this,
* embedded dapp providers might listen directly to our streams, and therefore depend on it, so we leave it here.
*/
isUnlocked: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bit unfortunate in retrospect that we batched these into the same release since we are probably looking to release this as a hotfix...

github-merge-queue bot pushed a commit that referenced this pull request Feb 4, 2025
…provider (#30111)

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

Previously the inpage provider would withhold events for chainChanged
events (and property value updates, i.e. window.ethereum.chainId and
.networkVersion) when the dapp's network was changed to an rpc endpoint
that was unresponsive or did not support net_version. The dapp would
instead receive a disconnect event.

Now the inpage provider always emits chainChanged and networkChanged
events (and exposes the correct values on window.ethereum.chainId and
.networkVersion) when the selected network for the dapp has changed
regardless of if the network being changed to is responsive or if it
supports net_version requests. It does this by having the wallet send a
loading for networkVersion when it cannot be resolved (same behavior as
before) AND a new isConnected property in the metamask_getProviderState
request and metamask_chainChanged events (these are different from the
events emittted by window.ethereum). isConnected is derived from whether
the NetworkController.state.networkMetadata[].status value is the
Available constant.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/30109?quickstart=1)

## **Related issues**

See: #29936
Providers patch from commit (d919ab6b):
MetaMask/providers#404

## **Manual testing steps**
```
window.ethereum.on('accountsChanged', (data) => console.log('accountsChanged', data))
window.ethereum.on('chainChanged', (data) => console.log('chainChanged', data))
window.ethereum.on('networkChanged', (data) => console.log('networkChanged', data))
window.ethereum.on('connect', (data) => console.log('connect', data))
window.ethereum.on('disconnect', (data) => console.log('disconnect', data))
```
1. Go to a webpage. Enter the following in console
2. Change to Linea, see that the correct values are emitted for the
`chainChanged` and `networkChanged` events
3. Change to Sepolia, see that the correct values are emitted for the
`chainChanged` and `networkChanged` events
4. Change to a network that is non-responsive, see that `chainChanged`
emits with the correct chainId, but `networkChanged` emits with a null
value, AND there is a `disconnect` event emitted
5. Change back to a working network, see that the correct values are
emitted for the `chainChanged` and `networkChanged` events, AND there is
a `connect` event emitted with the new chainId
6. Do the same above with a responsive network that does not have
`net_version` implemented. Se that the correct values are emitted for
the `chainChanged`, that `networkChanged` emits null, and that there is
no `disconnect` event emitted


## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

![Screenshot 2025-02-04 at 8 34
13 AM](https://github.com/user-attachments/assets/f6558363-f848-445a-811e-d1a052a4e6c0)

![Screenshot 2025-02-04 at 8 39
33 AM](https://github.com/user-attachments/assets/12bff524-bb3c-4bf8-b4d2-c6a01ce63b33)



## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
adonesky1 pushed a commit that referenced this pull request Feb 4, 2025
…provider (#30111)

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

Previously the inpage provider would withhold events for chainChanged
events (and property value updates, i.e. window.ethereum.chainId and
.networkVersion) when the dapp's network was changed to an rpc endpoint
that was unresponsive or did not support net_version. The dapp would
instead receive a disconnect event.

Now the inpage provider always emits chainChanged and networkChanged
events (and exposes the correct values on window.ethereum.chainId and
.networkVersion) when the selected network for the dapp has changed
regardless of if the network being changed to is responsive or if it
supports net_version requests. It does this by having the wallet send a
loading for networkVersion when it cannot be resolved (same behavior as
before) AND a new isConnected property in the metamask_getProviderState
request and metamask_chainChanged events (these are different from the
events emittted by window.ethereum). isConnected is derived from whether
the NetworkController.state.networkMetadata[].status value is the
Available constant.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/30109?quickstart=1)

See: #29936
Providers patch from commit (d919ab6b):
MetaMask/providers#404
```
window.ethereum.on('accountsChanged', (data) => console.log('accountsChanged', data))
window.ethereum.on('chainChanged', (data) => console.log('chainChanged', data))
window.ethereum.on('networkChanged', (data) => console.log('networkChanged', data))
window.ethereum.on('connect', (data) => console.log('connect', data))
window.ethereum.on('disconnect', (data) => console.log('disconnect', data))
```
1. Go to a webpage. Enter the following in console
2. Change to Linea, see that the correct values are emitted for the
`chainChanged` and `networkChanged` events
3. Change to Sepolia, see that the correct values are emitted for the
`chainChanged` and `networkChanged` events
4. Change to a network that is non-responsive, see that `chainChanged`
emits with the correct chainId, but `networkChanged` emits with a null
value, AND there is a `disconnect` event emitted
5. Change back to a working network, see that the correct values are
emitted for the `chainChanged` and `networkChanged` events, AND there is
a `connect` event emitted with the new chainId
6. Do the same above with a responsive network that does not have
`net_version` implemented. Se that the correct values are emitted for
the `chainChanged`, that `networkChanged` emits null, and that there is
no `disconnect` event emitted

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

<!-- [screenshots/recordings] -->

![Screenshot 2025-02-04 at 8 34
13 AM](https://github.com/user-attachments/assets/f6558363-f848-445a-811e-d1a052a4e6c0)

![Screenshot 2025-02-04 at 8 39
33 AM](https://github.com/user-attachments/assets/12bff524-bb3c-4bf8-b4d2-c6a01ce63b33)

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
danjm pushed a commit that referenced this pull request Feb 4, 2025
…provider (#30111)

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

Previously the inpage provider would withhold events for chainChanged
events (and property value updates, i.e. window.ethereum.chainId and
.networkVersion) when the dapp's network was changed to an rpc endpoint
that was unresponsive or did not support net_version. The dapp would
instead receive a disconnect event.

Now the inpage provider always emits chainChanged and networkChanged
events (and exposes the correct values on window.ethereum.chainId and
.networkVersion) when the selected network for the dapp has changed
regardless of if the network being changed to is responsive or if it
supports net_version requests. It does this by having the wallet send a
loading for networkVersion when it cannot be resolved (same behavior as
before) AND a new isConnected property in the metamask_getProviderState
request and metamask_chainChanged events (these are different from the
events emittted by window.ethereum). isConnected is derived from whether
the NetworkController.state.networkMetadata[].status value is the
Available constant.

[![Open in GitHub

Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/30109?quickstart=1)

See: #29936 Providers
patch from commit (d919ab6b):
MetaMask/providers#404
```
window.ethereum.on('accountsChanged', (data) => console.log('accountsChanged', data))
window.ethereum.on('chainChanged', (data) => console.log('chainChanged', data))
window.ethereum.on('networkChanged', (data) => console.log('networkChanged', data))
window.ethereum.on('connect', (data) => console.log('connect', data))
window.ethereum.on('disconnect', (data) => console.log('disconnect', data))
```
1. Go to a webpage. Enter the following in console
2. Change to Linea, see that the correct values are emitted for the
`chainChanged` and `networkChanged` events
3. Change to Sepolia, see that the correct values are emitted for the
`chainChanged` and `networkChanged` events
4. Change to a network that is non-responsive, see that `chainChanged`
emits with the correct chainId, but `networkChanged` emits with a null
value, AND there is a `disconnect` event emitted
5. Change back to a working network, see that the correct values are
emitted for the `chainChanged` and `networkChanged` events, AND there is
a `connect` event emitted with the new chainId
6. Do the same above with a responsive network that does not have
`net_version` implemented. Se that the correct values are emitted for
the `chainChanged`, that `networkChanged` emits null, and that there is
no `disconnect` event emitted

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

<!-- [screenshots/recordings] -->

![Screenshot 2025-02-04 at 8 34

13 AM](https://github.com/user-attachments/assets/f6558363-f848-445a-811e-d1a052a4e6c0)

![Screenshot 2025-02-04 at 8 39

33 AM](https://github.com/user-attachments/assets/12bff524-bb3c-4bf8-b4d2-c6a01ce63b33)

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding

Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.


<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/30114?quickstart=1)

## **Related issues**

Fixes:

## **Manual testing steps**

1. Go to this page...
2.
3.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.

---------

Co-authored-by: jiexi <[email protected]>
Co-authored-by: MetaMask Bot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants