-
-
Notifications
You must be signed in to change notification settings - Fork 187
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
selected network controller: redirect domains to default endpoint #4679
selected network controller: redirect domains to default endpoint #4679
Conversation
Object.entries(this.state.domains).forEach( | ||
([domain, networkClientIdForDomain]) => { | ||
const chainIdForDomain = | ||
networkClientIdToChainId[networkClientIdForDomain]; | ||
|
||
if (patch.op === 'remove' && !chainIdForDomain) { | ||
// If the network was removed, fall back to the globally selected network | ||
this.setNetworkClientIdForDomain( | ||
domain, | ||
selectedNetworkClientId, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this set the chainId for the domain to the globally selected network if we just removed the rpcUrl that it was connected to? I would assume in this case we'd want to replace it with the new "default" rpcUrl for that chainId? And, if that network was removed entirely (not just the rpcUrl) we'd set it to the globally selected network?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh does the remove
op only apply if the entire chainId is removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the case you described of removing an RPC url falls into the replace operation.
Deleting the whole network falls into the remove operation.
I also updated the migration in MetaMask/metamask-extension#26433 to point domains to their default endpoint so its correct initially. |
const patch = patches.find( | ||
({ op, path }) => | ||
(op === 'replace' || op === 'remove') && | ||
path[0] === 'networkConfigurationsByChainId', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on using filter vs find? Not that it does currently, but patches
could include multiple state changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole technique won't work if there are multiple changes. It relies on the fact that the network controller only exposes an APIs to update or remove a single network at a time.
If multiple networks were updated in 1 update, you wouldn't be able to correlate the missing network client ids to a particular chain id. Since there's only 1, we can do it with path[1]
.
This is relevant for the case where you simultaneously delete the default endpoint and replace it with a new one in 1 update
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unless I missed some overload of subscribe
that gives you enough information to do so
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Explanation
Changes how the
SelectedNetworkController
responds to state updates from the network controller. Now when the default RPC endpoint changes for a network, it will redirect domains that were referencing a network client id on that network to the new default endpoint for that network. Instead of redirecting them to the globally selected network, as happened before.Accompanies MetaMask/metamask-extension#26433
References
Changelog
@metamask/selected-network-controller
Checklist