From 8e52ed3fac704a3f031a57c9c84980b51379ebd2 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Wed, 19 Apr 2023 20:24:50 -0230 Subject: [PATCH] Make `upsertNetworkConfiguration` async (#18606) The network controller method `upsertNetworkConfiguration` has been made async. It makes one async call internally, which is the optional step of setting the given network as the active network. If the `setActive` option is set to `true`, this function will not resolve until after the network switch has completed. If the `setActive` option is `false`, this change will not have any functional impact. Relates to https://github.com/MetaMask/metamask-extension/issues/18587 --- .../network/network-controller.test.ts | 46 +++++++++---------- .../controllers/network/network-controller.ts | 6 +-- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/app/scripts/controllers/network/network-controller.test.ts b/app/scripts/controllers/network/network-controller.test.ts index 8368decd0..2e2c1b0b9 100644 --- a/app/scripts/controllers/network/network-controller.test.ts +++ b/app/scripts/controllers/network/network-controller.test.ts @@ -6740,7 +6740,7 @@ describe('NetworkController', () => { it('throws if the given chain ID is not a 0x-prefixed hex number', async () => { const invalidChainId = '1'; await withController(async ({ controller }) => { - expect(() => + await expect(() => controller.upsertNetworkConfiguration( { /* @ts-expect-error We are intentionally passing bad input. */ @@ -6755,7 +6755,7 @@ describe('NetworkController', () => { source: MetaMetricsNetworkEventSource.Dapp, }, ), - ).toThrow( + ).rejects.toThrow( new Error( `Invalid chain ID "${invalidChainId}": invalid hex string.`, ), @@ -6765,7 +6765,7 @@ describe('NetworkController', () => { it('throws if the given chain ID is greater than the maximum allowed ID', async () => { await withController(async ({ controller }) => { - expect(() => + await expect(() => controller.upsertNetworkConfiguration( { chainId: '0xFFFFFFFFFFFFFFFF', @@ -6779,7 +6779,7 @@ describe('NetworkController', () => { source: MetaMetricsNetworkEventSource.Dapp, }, ), - ).toThrow( + ).rejects.toThrow( new Error( 'Invalid chain ID "0xFFFFFFFFFFFFFFFF": numerical value greater than max safe value.', ), @@ -6789,7 +6789,7 @@ describe('NetworkController', () => { it('throws if the no (or a falsy) rpcUrl is passed', async () => { await withController(async ({ controller }) => { - expect(() => + await expect(() => controller.upsertNetworkConfiguration( /* @ts-expect-error We are intentionally passing bad input. */ { @@ -6803,7 +6803,7 @@ describe('NetworkController', () => { source: MetaMetricsNetworkEventSource.Dapp, }, ), - ).toThrow( + ).rejects.toThrow( new Error( 'An rpcUrl is required to add or update network configuration', ), @@ -6813,7 +6813,7 @@ describe('NetworkController', () => { it('throws if rpcUrl passed is not a valid Url', async () => { await withController(async ({ controller }) => { - expect(() => + await expect(() => controller.upsertNetworkConfiguration( { chainId: '0x9999', @@ -6827,13 +6827,13 @@ describe('NetworkController', () => { source: MetaMetricsNetworkEventSource.Dapp, }, ), - ).toThrow(new Error('rpcUrl must be a valid URL')); + ).rejects.toThrow(new Error('rpcUrl must be a valid URL')); }); }); it('throws if the no (or a falsy) ticker is passed', async () => { await withController(async ({ controller }) => { - expect(() => + await expect(() => controller.upsertNetworkConfiguration( /* @ts-expect-error We are intentionally passing bad input. */ { @@ -6847,7 +6847,7 @@ describe('NetworkController', () => { source: MetaMetricsNetworkEventSource.Dapp, }, ), - ).toThrow( + ).rejects.toThrow( new Error( 'A ticker is required to add or update networkConfiguration', ), @@ -6857,7 +6857,7 @@ describe('NetworkController', () => { it('throws if an options object is not passed as a second argument', async () => { await withController(async ({ controller }) => { - expect(() => + await expect(() => /* @ts-expect-error We are intentionally passing bad input. */ controller.upsertNetworkConfiguration({ chainId: '0x5', @@ -6865,7 +6865,7 @@ describe('NetworkController', () => { rpcPrefs: { blockExplorerUrl: 'test-block-explorer.com' }, rpcUrl: 'https://mock-rpc-url', }), - ).toThrow( + ).rejects.toThrow( new Error( "Cannot read properties of undefined (reading 'setActive')", ), @@ -6888,7 +6888,7 @@ describe('NetworkController', () => { ticker: 'test_ticker', }; - controller.upsertNetworkConfiguration(rpcUrlNetwork, { + await controller.upsertNetworkConfiguration(rpcUrlNetwork, { referrer: 'https://test-dapp.com', source: MetaMetricsNetworkEventSource.Dapp, }); @@ -6926,7 +6926,7 @@ describe('NetworkController', () => { invalidKey2: {}, }; - controller.upsertNetworkConfiguration(rpcUrlNetwork, { + await controller.upsertNetworkConfiguration(rpcUrlNetwork, { referrer: 'https://test-dapp.com', source: MetaMetricsNetworkEventSource.Dapp, }); @@ -6975,7 +6975,7 @@ describe('NetworkController', () => { ticker: 'RPC', }; - controller.upsertNetworkConfiguration(rpcUrlNetwork, { + await controller.upsertNetworkConfiguration(rpcUrlNetwork, { referrer: 'https://test-dapp.com', source: MetaMetricsNetworkEventSource.Dapp, }); @@ -7024,7 +7024,7 @@ describe('NetworkController', () => { rpcPrefs: { blockExplorerUrl: 'alternativetestchainscan.io' }, chainId: '0x1' as const, }; - controller.upsertNetworkConfiguration(updatedConfiguration, { + await controller.upsertNetworkConfiguration(updatedConfiguration, { referrer: 'https://test-dapp.com', source: MetaMetricsNetworkEventSource.Dapp, }); @@ -7069,7 +7069,7 @@ describe('NetworkController', () => { }, }, async ({ controller }) => { - controller.upsertNetworkConfiguration( + await controller.upsertNetworkConfiguration( { rpcUrl: 'https://test-rpc-url', ticker: 'new-ticker', @@ -7136,7 +7136,7 @@ describe('NetworkController', () => { ticker: 'test_ticker', }; - controller.upsertNetworkConfiguration(rpcUrlNetwork, { + await controller.upsertNetworkConfiguration(rpcUrlNetwork, { referrer: 'https://test-dapp.com', source: MetaMetricsNetworkEventSource.Dapp, }); @@ -7180,7 +7180,7 @@ describe('NetworkController', () => { ticker: 'test_ticker', }; - controller.upsertNetworkConfiguration(rpcUrlNetwork, { + await controller.upsertNetworkConfiguration(rpcUrlNetwork, { setActive: true, referrer: 'https://test-dapp.com', source: MetaMetricsNetworkEventSource.Dapp, @@ -7229,7 +7229,7 @@ describe('NetworkController', () => { rpcPrefs: { blockExplorerUrl: 'https://block-explorer' }, }; - controller.upsertNetworkConfiguration(newNetworkConfiguration, { + await controller.upsertNetworkConfiguration(newNetworkConfiguration, { referrer: 'https://test-dapp.com', source: MetaMetricsNetworkEventSource.Dapp, }); @@ -7296,10 +7296,10 @@ describe('NetworkController', () => { rpcPrefs: { blockExplorerUrl: 'https://block-explorer' }, }; - expect(() => { + await expect(() => /* @ts-expect-error We are intentionally passing bad input. */ - controller.upsertNetworkConfiguration(newNetworkConfiguration, {}); - }).toThrow( + controller.upsertNetworkConfiguration(newNetworkConfiguration, {}), + ).rejects.toThrow( 'referrer and source are required arguments for adding or updating a network configuration', ); }, diff --git a/app/scripts/controllers/network/network-controller.ts b/app/scripts/controllers/network/network-controller.ts index 9b95a8c7c..eaf3f6931 100644 --- a/app/scripts/controllers/network/network-controller.ts +++ b/app/scripts/controllers/network/network-controller.ts @@ -1034,7 +1034,7 @@ export class NetworkController extends EventEmitter { * @throws if `rpcUrl` is not a valid URL. * @returns The ID for the added or updated network configuration. */ - upsertNetworkConfiguration( + async upsertNetworkConfiguration( { rpcUrl, chainId, @@ -1051,7 +1051,7 @@ export class NetworkController extends EventEmitter { referrer: string; source: string; }, - ): NetworkConfigurationId { + ): Promise { assert.ok( isPrefixedFormattedHexString(chainId), `Invalid chain ID "${chainId}": invalid hex string.`, @@ -1129,7 +1129,7 @@ export class NetworkController extends EventEmitter { } if (setActive) { - this.setActiveNetwork(newNetworkConfigurationId); + await this.setActiveNetwork(newNetworkConfigurationId); } return newNetworkConfigurationId;