Skip to content

Commit

Permalink
Move state update call after networkClient is instantiated and added …
Browse files Browse the repository at this point in the history
…to registry in `upsertNetworkConfiguration` (#3679)

## @metamask/network-controller
### Changed:
- Moves state update call after networkClient was instantiated so that
`NetworkController:stateChange` event can be safely used to identify
when new networkClients have been added and are available for use

## Checklist

- [x] 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
  • Loading branch information
adonesky1 authored Dec 19, 2023
1 parent 3d673e7 commit 9ff9e45
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
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

0 comments on commit 9ff9e45

Please sign in to comment.