Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -135,6 +135,28 @@ export class SelectedNetworkController extends BaseController<
state,
});
this.#registerMessageHandlers();

this.messagingSystem.subscribe(
'NetworkController:stateChange',
({ selectedNetworkClientId }, patches) => {
patches.forEach(({ op, path }) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Interesting idea to use patches for this. Might be more performant this way? I worry about the potential for a network configuration to be replaced by a different operation though, e.g. move or replace.

At the moment I think remove is the only possibility, but later with data syncing, I am not so sure.

Comparing each selected network client ID with each existing network might be safer.

@GudahttGudahttFeb 16, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could ensure this only runs for config changes by using a selector as well, e.g.

this.messagingSystem.subscribe(
'NetworkController:stateChange',
(networkConfigurations) => {
...
},
(state) => state.networkConfigurations,
)

// if a network is removed, update the networkClientId for all domains that were using it to the selected network
if (op === 'remove' && path[0] === 'networkConfigurations') {
const removedNetworkClientId = path[1] as NetworkClientId;
Object.entries(this.state.domains).forEach(
([domain, networkClientIdForDomain]) => {
if (networkClientIdForDomain === removedNetworkClientId) {
this.setNetworkClientIdForDomain(
domain,
selectedNetworkClientId,
);
}
},
);
}
});
},
);
}

#registerMessageHandlers(): void {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -118,7 +118,10 @@ const setup = ({
});
const messenger = buildMessenger();
const selectedNetworkControllerMessenger =
buildSelectedNetworkControllerMessenger({ messenger, hasPermissions });
buildSelectedNetworkControllerMessenger({
messenger,
hasPermissions,
});
const controller = new SelectedNetworkController({
messenger: selectedNetworkControllerMessenger,
state,
Expand DownExpand Up@@ -158,6 +161,41 @@ describe('SelectedNetworkController', () => {
});
});

describe('It updates domain state when the network controller state changes', () => {
describe('when a networkClient is deleted from the network controller state', () => {
it('updates the networkClientId for domains which were previously set to the deleted networkClientId', () => {
const { controller, messenger } = setup({
state: {
perDomainNetwork: true,
domains: {
metamask: 'goerli',
'example.com': 'test-network-client-id',
'test.com': 'test-network-client-id',
},
},
});

messenger.publish(
'NetworkController:stateChange',
{
providerConfig: { chainId: '0x5', ticker: 'ETH', type: 'goerli' },
selectedNetworkClientId: 'goerli',
networkConfigurations: {},
networksMetadata: {},
},
[
{
op: 'remove',
path: ['networkConfigurations', 'test-network-client-id'],
},
],
);
expect(controller.state.domains['example.com']).toBe('goerli');
expect(controller.state.domains['test.com']).toBe('goerli');
});
});
});

describe('setNetworkClientIdForDomain', () => {
afterEach(() => {
jest.clearAllMocks();
Expand Down