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 SelectedNetworkController default metamask networkClientId #1757

Merged
merged 11 commits into from
Oct 4, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,18 @@ export class SelectedNetworkController extends BaseControllerV2<
this.setNetworkClientIdForDomain.bind(this),
);

// subscribe to networkController statechange:: selectedNetworkClientId changed
// update the value for the domain 'metamask'
// subscribe to NetworkController state change events
// set the value for the domain 'metamask' if not already initialized
// or update it if the selectedNetworkClientId has changed
this.messagingSystem.subscribe(
'NetworkController:stateChange',
(state: NetworkState, patch: Patch[]) => {
const isChangingNetwork = patch.some(
(p) => p.path[0] === 'selectedNetworkClientId',
);
if (!isChangingNetwork) {
const hasMetamaskNetworkClientId =
this.getNetworkClientIdForDomain(METAMASK_DOMAIN);
if (!isChangingNetwork && hasMetamaskNetworkClientId) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,30 @@ describe('SelectedNetworkController', () => {
});
});

it('updates the networkClientId for the metamask domain if not already set when the networkControllers state changes', () => {
const messenger = buildMessenger();
const options: SelectedNetworkControllerOptions = {
messenger: buildSelectedNetworkControllerMessenger(messenger),
};
const controller = new SelectedNetworkController(options);
expect(controller.state.domains.metamask).toBeUndefined();

const patch = [
{
path: ['anythingelse'],
op: 'replace' as const,
value: 'abc',
},
];

const state = {
...networkControllerDefaultState,
selectedNetworkClientId: 'currentNetwork',
};
messenger.publish('NetworkController:stateChange', state, patch);
expect(controller.state.domains.metamask).toBe('currentNetwork');
});

it('updates the networkClientId for the metamask domain when the networkControllers selectedNetworkClientId changes', () => {
const messenger = buildMessenger();
const options: SelectedNetworkControllerOptions = {
Expand Down
Loading