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

fix: update inpage provider chainChanged, networkChanged, connect, and disconnect events #29936

Draft
wants to merge 4 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.

NOTE: this PR still needs the @metamask/providers changes linked below before it can be merged. To test manually you will need to manually build and copy the package dist into your local extension build node_modules

Open in GitHub Codespaces

Related issues

Needs: MetaMask/providers#404
See: 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

Manual testing steps

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 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

Screenshots/Recordings

Before

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

After

Screen.Recording.2025-01-27.at.3.53.39.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%)

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.

2 participants