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

Move state update call after networkClient is instantiated and added to registry in upsertNetworkConfiguration #3679

Merged
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
14 changes: 7 additions & 7 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1237,13 +1237,6 @@ export class NetworkController extends BaseController<
upsertedNetworkConfigurationId,
);

this.update((state) => {
state.networkConfigurations[upsertedNetworkConfigurationId] = {
id: upsertedNetworkConfigurationId,
...sanitizedNetworkConfiguration,
};
});

const customNetworkClientRegistry =
autoManagedNetworkClientRegistry[NetworkClientType.Custom];
const existingAutoManagedNetworkClient =
Expand All @@ -1267,6 +1260,13 @@ export class NetworkController extends BaseController<
});
}

this.update((state) => {
state.networkConfigurations[upsertedNetworkConfigurationId] = {
id: upsertedNetworkConfigurationId,
...sanitizedNetworkConfiguration,
};
});

if (!existingNetworkConfiguration) {
this.#trackMetaMetricsEvent({
event: 'Custom Network Added',
Expand Down
44 changes: 44 additions & 0 deletions packages/network-controller/tests/NetworkController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3921,6 +3921,50 @@ describe('NetworkController', () => {
);
});

it('updates state only after creating the new network client', async () => {
await withController(
{ infuraProjectId: 'some-infura-project-id' },
async ({ controller, messenger }) => {
uuidV4Mock.mockReturnValue('AAAA-AAAA-AAAA-AAAA');
const newCustomNetworkClient = buildFakeClient();
mockCreateNetworkClientWithDefaultsForBuiltInNetworkClients({
infuraProjectId: 'some-infura-project-id',
})
.calledWith({
chainId: toHex(111),
rpcUrl: 'https://test.network',
type: NetworkClientType.Custom,
ticker: 'TICKER',
})
.mockReturnValue(newCustomNetworkClient);

await waitForStateChanges({
messenger,
count: 1,
operation: async () => {
await controller.upsertNetworkConfiguration(
{
rpcUrl: 'https://test.network',
chainId: toHex(111),
ticker: 'TICKER',
},
{
referrer: 'https://test-dapp.com',
source: 'dapp',
},
);
},
beforeResolving: () => {
const newNetworkClient = controller.getNetworkClientById(
'AAAA-AAAA-AAAA-AAAA',
);
expect(newNetworkClient).toBeDefined();
},
});
},
);
});

describe('if the setActive option is not given', () => {
it('does not update the provider config to the new network configuration by default', async () => {
const originalProvider = {
Expand Down
Loading