Skip to content

Add NetworkState payload to NetworkController:networkWillChange and NetworkController:networkDidChange#3598

Merged
jiexi merged 2 commits intomainfrom
jl/mmp-1713/add-networkState-payload-networkWillChange-networkDidChange
Dec 1, 2023
Merged

Add NetworkState payload to NetworkController:networkWillChange and NetworkController:networkDidChange#3598
jiexi merged 2 commits intomainfrom
jl/mmp-1713/add-networkState-payload-networkWillChange-networkDidChange

Conversation

@jiexi
Copy link
Copy Markdown
Member

@jiexi jiexi commented Nov 30, 2023

Explanation

While debugging an issue with SwapsController's provider instance being updated too soon due to the remove networkId refactor, I noticed many of our controllers listen to NetworkController:stateChange which fires numerous times each time the network switches. Some of these controllers keep track of the chainId it's last seen between these events to ensure it only fires off any necessary side effects once. Some controllers don't keep track of if it's seen the chainId at all and will fire off the side effects for each and every stateChange event. Using chainId as a guard isn't even necessarily correct in some cases as the provider may have changed despite the chainId remaining the same.

It seems more appropriate/accurate for these controllers to be listening to NetworkController:networkDidChange instead which only fires once for each network switch, and after the provider proxy has been updated to point at the right RPC endpoint.

Currently the NetworkController:networkDidChange event is a little bit inconvenient to consume because generally any listeners to this event would want to know what the network changed to / what the current state of things are to determine next steps. This PR adds NetworkState to the NetworkController:networkWillChange and NetworkController:networkDidChange event payloads

References

Changelog

@metamask/network-controller

  • CHANGED: NetworkController:networkWillChange includes NetworkState as the first and only item in the payload
  • CHANGED: NetworkController:networkDidChange includes NetworkState as the first and only item in the payload

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

@jiexi jiexi marked this pull request as ready for review November 30, 2023 22:40
@jiexi jiexi requested a review from a team as a code owner November 30, 2023 22:40
Copy link
Copy Markdown
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

LGTM!

@jiexi jiexi requested a review from Gudahtt December 1, 2023 17:46
@jiexi jiexi merged commit f0549aa into main Dec 1, 2023
@jiexi jiexi deleted the jl/mmp-1713/add-networkState-payload-networkWillChange-networkDidChange branch December 1, 2023 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants