From bdd638400cfbec9a76ab28d69e05c9a90fef04fd Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Wed, 21 Jun 2023 10:31:08 -0700 Subject: [PATCH] Shorten network type references in NC tests (#19430) Update NetworkController tests to replace `NETWORK_TYPES.` with `NetworkType.`. This makes it easier to visually compare differences in the tests between core and this repo. --- .../network/network-controller.test.ts | 2007 +++++++++-------- 1 file changed, 1009 insertions(+), 998 deletions(-) diff --git a/app/scripts/controllers/network/network-controller.test.ts b/app/scripts/controllers/network/network-controller.test.ts index ce07d114c..b011b095e 100644 --- a/app/scripts/controllers/network/network-controller.test.ts +++ b/app/scripts/controllers/network/network-controller.test.ts @@ -55,6 +55,13 @@ type Block = { const createNetworkClientMock = jest.mocked(createNetworkClient); const uuidV4Mock = jest.mocked(v4); +const NetworkType = { + mainnet: NETWORK_TYPES.MAINNET, + goerli: NETWORK_TYPES.GOERLI, + sepolia: NETWORK_TYPES.SEPOLIA, + rpc: NETWORK_TYPES.RPC, +} as const; + /** * A dummy block that matches the pre-EIP-1559 format (i.e. it doesn't have the * `baseFeePerGas` property). @@ -84,19 +91,19 @@ const BLOCK: Block = POST_1559_BLOCK; */ const INFURA_NETWORKS = [ { - networkType: NETWORK_TYPES.MAINNET, + networkType: NetworkType.mainnet, chainId: toHex(1), ticker: 'ETH', blockExplorerUrl: 'https://etherscan.io', }, { - networkType: NETWORK_TYPES.GOERLI, + networkType: NetworkType.goerli, chainId: toHex(5), ticker: 'GoerliETH', blockExplorerUrl: 'https://goerli.etherscan.io', }, { - networkType: NETWORK_TYPES.SEPOLIA, + networkType: NetworkType.sepolia, chainId: toHex(11155111), ticker: 'SepoliaETH', blockExplorerUrl: 'https://sepolia.etherscan.io', @@ -346,7 +353,7 @@ describe('NetworkController', () => { { state: { providerConfig: { - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, chainId: toHex(1337), rpcUrl: 'http://example.com', }, @@ -392,11 +399,11 @@ describe('NetworkController', () => { lookupNetworkTests({ expectedProviderConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, }), initialState: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, }), }, operation: async (controller: NetworkController) => { @@ -411,7 +418,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, chainId: undefined, }), }, @@ -435,7 +442,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, chainId: undefined, }), }, @@ -467,7 +474,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: undefined, }), }, @@ -491,7 +498,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: undefined, }), }, @@ -680,7 +687,7 @@ describe('NetworkController', () => { ]; mockCreateNetworkClient() .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -737,921 +744,927 @@ describe('NetworkController', () => { }); }); - [ - NETWORK_TYPES.MAINNET, - NETWORK_TYPES.GOERLI, - NETWORK_TYPES.SEPOLIA, - ].forEach((networkType) => { - describe(`when the provider config in state contains a network type of "${networkType}"`, () => { - describe('if the network was switched after the net_version request started but before it completed', () => { - it('stores the network status of the second network, not the first', async () => { - await withController( - { - state: { - providerConfig: buildProviderConfig({ type: networkType }), - networkConfigurations: { - testNetworkConfigurationId: { - id: 'testNetworkConfigurationId', - rpcUrl: 'https://mock-rpc-url', - chainId: toHex(1337), - ticker: 'ABC', + [NetworkType.mainnet, NetworkType.goerli, NetworkType.sepolia].forEach( + (networkType) => { + describe(`when the provider config in state contains a network type of "${networkType}"`, () => { + describe('if the network was switched after the net_version request started but before it completed', () => { + it('stores the network status of the second network, not the first', async () => { + await withController( + { + state: { + providerConfig: buildProviderConfig({ type: networkType }), + networkConfigurations: { + testNetworkConfigurationId: { + id: 'testNetworkConfigurationId', + rpcUrl: 'https://mock-rpc-url', + chainId: toHex(1337), + ticker: 'ABC', + }, }, }, + infuraProjectId: 'some-infura-project-id', }, - infuraProjectId: 'some-infura-project-id', - }, - async ({ controller }) => { - const fakeProviders = [ - buildFakeProvider([ - // Called during provider initialization - { - request: { - method: 'net_version', + async ({ controller }) => { + const fakeProviders = [ + buildFakeProvider([ + // Called during provider initialization + { + request: { + method: 'net_version', + }, + response: SUCCESSFUL_NET_VERSION_RESPONSE, }, - response: SUCCESSFUL_NET_VERSION_RESPONSE, - }, - { - request: { - method: 'eth_getBlockByNumber', + { + request: { + method: 'eth_getBlockByNumber', + }, + response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, }, - response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, - }, - // Called via `lookupNetwork` directly - { - request: { - method: 'net_version', + // Called via `lookupNetwork` directly + { + request: { + method: 'net_version', + }, + response: SUCCESSFUL_NET_VERSION_RESPONSE, + beforeCompleting: () => { + // Intentionally not awaited because don't want this to + // block the `net_version` request + controller.setActiveNetwork( + 'testNetworkConfigurationId', + ); + }, }, - response: SUCCESSFUL_NET_VERSION_RESPONSE, - beforeCompleting: () => { - // Intentionally not awaited because don't want this to - // block the `net_version` request - controller.setActiveNetwork( - 'testNetworkConfigurationId', - ); + { + request: { + method: 'eth_getBlockByNumber', + }, + response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, }, - }, - { - request: { - method: 'eth_getBlockByNumber', + ]), + buildFakeProvider([ + // Called when switching networks + { + request: { + method: 'net_version', + }, + error: GENERIC_JSON_RPC_ERROR, }, - response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, - }, - ]), - buildFakeProvider([ - // Called when switching networks - { - request: { - method: 'net_version', - }, - error: GENERIC_JSON_RPC_ERROR, - }, - ]), - ]; - const fakeNetworkClients = [ - buildFakeClient(fakeProviders[0]), - buildFakeClient(fakeProviders[1]), - ]; - mockCreateNetworkClient() - .calledWith({ - network: networkType, - infuraProjectId: 'some-infura-project-id', - type: NetworkClientType.Infura, - }) - .mockReturnValue(fakeNetworkClients[0]) - .calledWith({ - chainId: toHex(1337), - rpcUrl: 'https://mock-rpc-url', - type: NetworkClientType.Custom, - }) - .mockReturnValue(fakeNetworkClients[1]); - await controller.initializeProvider(); - expect(controller.store.getState().networkStatus).toBe( - 'available', - ); - - await waitForStateChanges({ - controller, - propertyPath: ['networkStatus'], - operation: async () => { - await controller.lookupNetwork(); - }, - }); - - expect(controller.store.getState().networkStatus).toBe( - 'unknown', - ); - }, - ); - }); - - it('stores the ID of the second network, not the first', async () => { - await withController( - { - state: { - providerConfig: buildProviderConfig({ type: networkType }), - networkConfigurations: { - testNetworkConfigurationId: { - id: 'testNetworkConfigurationId', - rpcUrl: 'https://mock-rpc-url', + ]), + ]; + const fakeNetworkClients = [ + buildFakeClient(fakeProviders[0]), + buildFakeClient(fakeProviders[1]), + ]; + mockCreateNetworkClient() + .calledWith({ + network: networkType, + infuraProjectId: 'some-infura-project-id', + type: NetworkClientType.Infura, + }) + .mockReturnValue(fakeNetworkClients[0]) + .calledWith({ chainId: toHex(1337), - ticker: 'ABC', - }, - }, - }, - infuraProjectId: 'some-infura-project-id', - }, - async ({ controller }) => { - const fakeProviders = [ - buildFakeProvider([ - // Called during provider initialization - { - request: { - method: 'net_version', - }, - response: { - result: '1', - }, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, - }, - // Called via `lookupNetwork` directly - { - request: { - method: 'net_version', - }, - response: { - result: '1', - }, - beforeCompleting: async () => { - // Intentionally not awaited because don't want this to - // block the `net_version` request - controller.setActiveNetwork( - 'testNetworkConfigurationId', - ); - }, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, - }, - ]), - buildFakeProvider([ - // Called when switching networks - { - request: { - method: 'net_version', - }, - response: { - result: '2', - }, - }, - ]), - ]; - const fakeNetworkClients = [ - buildFakeClient(fakeProviders[0]), - buildFakeClient(fakeProviders[1]), - ]; - mockCreateNetworkClient() - .calledWith({ - network: networkType, - infuraProjectId: 'some-infura-project-id', - type: NetworkClientType.Infura, - }) - .mockReturnValue(fakeNetworkClients[0]) - .calledWith({ - chainId: toHex(1337), - rpcUrl: 'https://mock-rpc-url', - type: NetworkClientType.Custom, - }) - .mockReturnValue(fakeNetworkClients[1]); - await waitForStateChanges({ - controller, - propertyPath: ['networkId'], - operation: async () => { - await controller.initializeProvider(); - }, - }); - expect(controller.store.getState().networkId).toBe('1'); - - await waitForStateChanges({ - controller, - propertyPath: ['networkId'], - operation: async () => { - await controller.lookupNetwork(); - }, - }); - - expect(controller.store.getState().networkId).toBe('2'); - }, - ); - }); - - it('stores the EIP-1559 support of the second network, not the first', async () => { - await withController( - { - state: { - providerConfig: buildProviderConfig({ type: networkType }), - networkConfigurations: { - testNetworkConfigurationId: { - id: 'testNetworkConfigurationId', rpcUrl: 'https://mock-rpc-url', - chainId: toHex(1337), - ticker: 'ABC', - }, - }, - }, - infuraProjectId: 'some-infura-project-id', - }, - async ({ controller }) => { - const fakeProviders = [ - buildFakeProvider([ - // Called during provider initialization - { - request: { - method: 'net_version', - }, - response: { - result: '1', - }, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: { - result: POST_1559_BLOCK, - }, - }, - // Called via `lookupNetwork` directly - { - request: { - method: 'net_version', - }, - response: { - result: '1', - }, - beforeCompleting: () => { - // Intentionally not awaited because don't want this to - // block the `net_version` request - controller.setActiveNetwork( - 'testNetworkConfigurationId', - ); - }, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: { - result: POST_1559_BLOCK, - }, - }, - ]), - buildFakeProvider([ - // Called when switching networks - { - request: { - method: 'net_version', - }, - response: { - result: '2', - }, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: { - result: PRE_1559_BLOCK, - }, - }, - ]), - ]; - const fakeNetworkClients = [ - buildFakeClient(fakeProviders[0]), - buildFakeClient(fakeProviders[1]), - ]; - mockCreateNetworkClient() - .calledWith({ - network: networkType, - infuraProjectId: 'some-infura-project-id', - type: NetworkClientType.Infura, - }) - .mockReturnValue(fakeNetworkClients[0]) - .calledWith({ - chainId: toHex(1337), - rpcUrl: 'https://mock-rpc-url', - type: NetworkClientType.Custom, - }) - .mockReturnValue(fakeNetworkClients[1]); - await controller.initializeProvider(); - expect( - controller.store.getState().networkDetails, - ).toStrictEqual({ - EIPS: { - 1559: true, - }, - }); + type: NetworkClientType.Custom, + }) + .mockReturnValue(fakeNetworkClients[1]); + await controller.initializeProvider(); + expect(controller.store.getState().networkStatus).toBe( + 'available', + ); - await waitForStateChanges({ - controller, - propertyPath: ['networkDetails'], - operation: async () => { - await controller.lookupNetwork(); - }, - }); - - expect( - controller.store.getState().networkDetails, - ).toStrictEqual({ - EIPS: { - 1559: false, - }, - }); - }, - ); - }); - - it('emits infuraIsUnblocked, not infuraIsBlocked, assuming that the first network was blocked', async () => { - await withController( - { - state: { - providerConfig: buildProviderConfig({ type: networkType }), - networkConfigurations: { - testNetworkConfigurationId: { - id: 'testNetworkConfigurationId', - rpcUrl: 'https://mock-rpc-url', - chainId: toHex(1337), - ticker: 'ABC', + await waitForStateChanges({ + controller, + propertyPath: ['networkStatus'], + operation: async () => { + await controller.lookupNetwork(); }, - }, - }, - infuraProjectId: 'some-infura-project-id', - }, - async ({ controller, messenger }) => { - const fakeProviders = [ - buildFakeProvider([ - // Called during provider initialization - { - request: { - method: 'net_version', - }, - response: SUCCESSFUL_NET_VERSION_RESPONSE, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, - }, - // Called via `lookupNetwork` directly - { - request: { - method: 'net_version', - }, - response: SUCCESSFUL_NET_VERSION_RESPONSE, - beforeCompleting: () => { - // Intentionally not awaited because don't want this to - // block the `net_version` request - controller.setActiveNetwork( - 'testNetworkConfigurationId', - ); - }, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - error: BLOCKED_INFURA_JSON_RPC_ERROR, - }, - ]), - buildFakeProvider([ - // Called when switching networks - { - request: { - method: 'net_version', - }, - response: SUCCESSFUL_NET_VERSION_RESPONSE, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, - }, - ]), - ]; - const fakeNetworkClients = [ - buildFakeClient(fakeProviders[0]), - buildFakeClient(fakeProviders[1]), - ]; - mockCreateNetworkClient() - .calledWith({ - network: networkType, - infuraProjectId: 'some-infura-project-id', - type: NetworkClientType.Infura, - }) - .mockReturnValue(fakeNetworkClients[0]) - .calledWith({ - chainId: toHex(1337), - rpcUrl: 'https://mock-rpc-url', - type: NetworkClientType.Custom, - }) - .mockReturnValue(fakeNetworkClients[1]); - await controller.initializeProvider(); - const promiseForInfuraIsUnblockedEvents = - waitForPublishedEvents({ - messenger, - eventType: 'NetworkController:infuraIsUnblocked', - }); - const promiseForNoInfuraIsBlockedEvents = - waitForPublishedEvents({ - messenger, - eventType: 'NetworkController:infuraIsBlocked', - count: 0, }); - await waitForStateChanges({ - controller, - propertyPath: ['networkStatus'], - operation: async () => { - await controller.lookupNetwork(); - }, - }); + expect(controller.store.getState().networkStatus).toBe( + 'unknown', + ); + }, + ); + }); - await expect(promiseForInfuraIsUnblockedEvents).toBeFulfilled(); - await expect(promiseForNoInfuraIsBlockedEvents).toBeFulfilled(); - }, - ); + it('stores the ID of the second network, not the first', async () => { + await withController( + { + state: { + providerConfig: buildProviderConfig({ type: networkType }), + networkConfigurations: { + testNetworkConfigurationId: { + id: 'testNetworkConfigurationId', + rpcUrl: 'https://mock-rpc-url', + chainId: toHex(1337), + ticker: 'ABC', + }, + }, + }, + infuraProjectId: 'some-infura-project-id', + }, + async ({ controller }) => { + const fakeProviders = [ + buildFakeProvider([ + // Called during provider initialization + { + request: { + method: 'net_version', + }, + response: { + result: '1', + }, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, + }, + // Called via `lookupNetwork` directly + { + request: { + method: 'net_version', + }, + response: { + result: '1', + }, + beforeCompleting: async () => { + // Intentionally not awaited because don't want this to + // block the `net_version` request + controller.setActiveNetwork( + 'testNetworkConfigurationId', + ); + }, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, + }, + ]), + buildFakeProvider([ + // Called when switching networks + { + request: { + method: 'net_version', + }, + response: { + result: '2', + }, + }, + ]), + ]; + const fakeNetworkClients = [ + buildFakeClient(fakeProviders[0]), + buildFakeClient(fakeProviders[1]), + ]; + mockCreateNetworkClient() + .calledWith({ + network: networkType, + infuraProjectId: 'some-infura-project-id', + type: NetworkClientType.Infura, + }) + .mockReturnValue(fakeNetworkClients[0]) + .calledWith({ + chainId: toHex(1337), + rpcUrl: 'https://mock-rpc-url', + type: NetworkClientType.Custom, + }) + .mockReturnValue(fakeNetworkClients[1]); + await waitForStateChanges({ + controller, + propertyPath: ['networkId'], + operation: async () => { + await controller.initializeProvider(); + }, + }); + expect(controller.store.getState().networkId).toBe('1'); + + await waitForStateChanges({ + controller, + propertyPath: ['networkId'], + operation: async () => { + await controller.lookupNetwork(); + }, + }); + + expect(controller.store.getState().networkId).toBe('2'); + }, + ); + }); + + it('stores the EIP-1559 support of the second network, not the first', async () => { + await withController( + { + state: { + providerConfig: buildProviderConfig({ type: networkType }), + networkConfigurations: { + testNetworkConfigurationId: { + id: 'testNetworkConfigurationId', + rpcUrl: 'https://mock-rpc-url', + chainId: toHex(1337), + ticker: 'ABC', + }, + }, + }, + infuraProjectId: 'some-infura-project-id', + }, + async ({ controller }) => { + const fakeProviders = [ + buildFakeProvider([ + // Called during provider initialization + { + request: { + method: 'net_version', + }, + response: { + result: '1', + }, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: { + result: POST_1559_BLOCK, + }, + }, + // Called via `lookupNetwork` directly + { + request: { + method: 'net_version', + }, + response: { + result: '1', + }, + beforeCompleting: () => { + // Intentionally not awaited because don't want this to + // block the `net_version` request + controller.setActiveNetwork( + 'testNetworkConfigurationId', + ); + }, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: { + result: POST_1559_BLOCK, + }, + }, + ]), + buildFakeProvider([ + // Called when switching networks + { + request: { + method: 'net_version', + }, + response: { + result: '2', + }, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: { + result: PRE_1559_BLOCK, + }, + }, + ]), + ]; + const fakeNetworkClients = [ + buildFakeClient(fakeProviders[0]), + buildFakeClient(fakeProviders[1]), + ]; + mockCreateNetworkClient() + .calledWith({ + network: networkType, + infuraProjectId: 'some-infura-project-id', + type: NetworkClientType.Infura, + }) + .mockReturnValue(fakeNetworkClients[0]) + .calledWith({ + chainId: toHex(1337), + rpcUrl: 'https://mock-rpc-url', + type: NetworkClientType.Custom, + }) + .mockReturnValue(fakeNetworkClients[1]); + await controller.initializeProvider(); + expect( + controller.store.getState().networkDetails, + ).toStrictEqual({ + EIPS: { + 1559: true, + }, + }); + + await waitForStateChanges({ + controller, + propertyPath: ['networkDetails'], + operation: async () => { + await controller.lookupNetwork(); + }, + }); + + expect( + controller.store.getState().networkDetails, + ).toStrictEqual({ + EIPS: { + 1559: false, + }, + }); + }, + ); + }); + + it('emits infuraIsUnblocked, not infuraIsBlocked, assuming that the first network was blocked', async () => { + await withController( + { + state: { + providerConfig: buildProviderConfig({ type: networkType }), + networkConfigurations: { + testNetworkConfigurationId: { + id: 'testNetworkConfigurationId', + rpcUrl: 'https://mock-rpc-url', + chainId: toHex(1337), + ticker: 'ABC', + }, + }, + }, + infuraProjectId: 'some-infura-project-id', + }, + async ({ controller, messenger }) => { + const fakeProviders = [ + buildFakeProvider([ + // Called during provider initialization + { + request: { + method: 'net_version', + }, + response: SUCCESSFUL_NET_VERSION_RESPONSE, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, + }, + // Called via `lookupNetwork` directly + { + request: { + method: 'net_version', + }, + response: SUCCESSFUL_NET_VERSION_RESPONSE, + beforeCompleting: () => { + // Intentionally not awaited because don't want this to + // block the `net_version` request + controller.setActiveNetwork( + 'testNetworkConfigurationId', + ); + }, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + error: BLOCKED_INFURA_JSON_RPC_ERROR, + }, + ]), + buildFakeProvider([ + // Called when switching networks + { + request: { + method: 'net_version', + }, + response: SUCCESSFUL_NET_VERSION_RESPONSE, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, + }, + ]), + ]; + const fakeNetworkClients = [ + buildFakeClient(fakeProviders[0]), + buildFakeClient(fakeProviders[1]), + ]; + mockCreateNetworkClient() + .calledWith({ + network: networkType, + infuraProjectId: 'some-infura-project-id', + type: NetworkClientType.Infura, + }) + .mockReturnValue(fakeNetworkClients[0]) + .calledWith({ + chainId: toHex(1337), + rpcUrl: 'https://mock-rpc-url', + type: NetworkClientType.Custom, + }) + .mockReturnValue(fakeNetworkClients[1]); + await controller.initializeProvider(); + const promiseForInfuraIsUnblockedEvents = + waitForPublishedEvents({ + messenger, + eventType: 'NetworkController:infuraIsUnblocked', + }); + const promiseForNoInfuraIsBlockedEvents = + waitForPublishedEvents({ + messenger, + eventType: 'NetworkController:infuraIsBlocked', + count: 0, + }); + + await waitForStateChanges({ + controller, + propertyPath: ['networkStatus'], + operation: async () => { + await controller.lookupNetwork(); + }, + }); + + await expect( + promiseForInfuraIsUnblockedEvents, + ).toBeFulfilled(); + await expect( + promiseForNoInfuraIsBlockedEvents, + ).toBeFulfilled(); + }, + ); + }); + }); + + describe('if the network was switched after the eth_getBlockByNumber request started but before it completed', () => { + it('stores the network status of the second network, not the first', async () => { + await withController( + { + state: { + providerConfig: buildProviderConfig({ type: networkType }), + networkConfigurations: { + testNetworkConfigurationId: { + id: 'testNetworkConfigurationId', + rpcUrl: 'https://mock-rpc-url', + chainId: toHex(1337), + ticker: 'ABC', + }, + }, + }, + infuraProjectId: 'some-infura-project-id', + }, + async ({ controller }) => { + const fakeProviders = [ + buildFakeProvider([ + // Called during provider initialization + { + request: { + method: 'net_version', + }, + response: SUCCESSFUL_NET_VERSION_RESPONSE, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, + }, + // Called via `lookupNetwork` directly + { + request: { + method: 'net_version', + }, + response: SUCCESSFUL_NET_VERSION_RESPONSE, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, + beforeCompleting: () => { + // Intentionally not awaited because don't want this to + // block the `net_version` request + controller.setActiveNetwork( + 'testNetworkConfigurationId', + ); + }, + }, + ]), + buildFakeProvider([ + // Called when switching networks + { + request: { + method: 'net_version', + }, + error: GENERIC_JSON_RPC_ERROR, + }, + ]), + ]; + const fakeNetworkClients = [ + buildFakeClient(fakeProviders[0]), + buildFakeClient(fakeProviders[1]), + ]; + mockCreateNetworkClient() + .calledWith({ + network: networkType, + infuraProjectId: 'some-infura-project-id', + type: NetworkClientType.Infura, + }) + .mockReturnValue(fakeNetworkClients[0]) + .calledWith({ + chainId: toHex(1337), + rpcUrl: 'https://mock-rpc-url', + type: NetworkClientType.Custom, + }) + .mockReturnValue(fakeNetworkClients[1]); + await controller.initializeProvider(); + expect(controller.store.getState().networkStatus).toBe( + 'available', + ); + + await waitForStateChanges({ + controller, + propertyPath: ['networkStatus'], + operation: async () => { + await controller.lookupNetwork(); + }, + }); + + expect(controller.store.getState().networkStatus).toBe( + 'unknown', + ); + }, + ); + }); + + it('stores the ID of the second network, not the first', async () => { + await withController( + { + state: { + providerConfig: buildProviderConfig({ type: networkType }), + networkConfigurations: { + testNetworkConfigurationId: { + id: 'testNetworkConfigurationId', + rpcUrl: 'https://mock-rpc-url', + chainId: toHex(1337), + ticker: 'ABC', + }, + }, + }, + infuraProjectId: 'some-infura-project-id', + }, + async ({ controller }) => { + const fakeProviders = [ + buildFakeProvider([ + // Called during provider initialization + { + request: { + method: 'net_version', + }, + response: { + result: '1', + }, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, + }, + // Called via `lookupNetwork` directly + { + request: { + method: 'net_version', + }, + response: { + result: '1', + }, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, + beforeCompleting: async () => { + // Intentionally not awaited because don't want this to + // block the `net_version` request + controller.setActiveNetwork( + 'testNetworkConfigurationId', + ); + }, + }, + ]), + buildFakeProvider([ + // Called when switching networks + { + request: { + method: 'net_version', + }, + response: { + result: '2', + }, + }, + ]), + ]; + const fakeNetworkClients = [ + buildFakeClient(fakeProviders[0]), + buildFakeClient(fakeProviders[1]), + ]; + mockCreateNetworkClient() + .calledWith({ + network: networkType, + infuraProjectId: 'some-infura-project-id', + type: NetworkClientType.Infura, + }) + .mockReturnValue(fakeNetworkClients[0]) + .calledWith({ + chainId: toHex(1337), + rpcUrl: 'https://mock-rpc-url', + type: NetworkClientType.Custom, + }) + .mockReturnValue(fakeNetworkClients[1]); + await waitForStateChanges({ + controller, + propertyPath: ['networkId'], + operation: async () => { + await controller.initializeProvider(); + }, + }); + expect(controller.store.getState().networkId).toBe('1'); + + await waitForStateChanges({ + controller, + propertyPath: ['networkId'], + operation: async () => { + await controller.lookupNetwork(); + }, + }); + + expect(controller.store.getState().networkId).toBe('2'); + }, + ); + }); + + it('stores the EIP-1559 support of the second network, not the first', async () => { + await withController( + { + state: { + providerConfig: buildProviderConfig({ type: networkType }), + networkConfigurations: { + testNetworkConfigurationId: { + id: 'testNetworkConfigurationId', + rpcUrl: 'https://mock-rpc-url', + chainId: toHex(1337), + ticker: 'ABC', + }, + }, + }, + infuraProjectId: 'some-infura-project-id', + }, + async ({ controller }) => { + const fakeProviders = [ + buildFakeProvider([ + // Called during provider initialization + { + request: { + method: 'net_version', + }, + response: { + result: '1', + }, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: { + result: POST_1559_BLOCK, + }, + }, + // Called via `lookupNetwork` directly + { + request: { + method: 'net_version', + }, + response: { + result: '1', + }, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: { + result: POST_1559_BLOCK, + }, + beforeCompleting: () => { + // Intentionally not awaited because don't want this to + // block the `net_version` request + controller.setActiveNetwork( + 'testNetworkConfigurationId', + ); + }, + }, + ]), + buildFakeProvider([ + // Called when switching networks + { + request: { + method: 'net_version', + }, + response: { + result: '2', + }, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: { + result: PRE_1559_BLOCK, + }, + }, + ]), + ]; + const fakeNetworkClients = [ + buildFakeClient(fakeProviders[0]), + buildFakeClient(fakeProviders[1]), + ]; + mockCreateNetworkClient() + .calledWith({ + network: networkType, + infuraProjectId: 'some-infura-project-id', + type: NetworkClientType.Infura, + }) + .mockReturnValue(fakeNetworkClients[0]) + .calledWith({ + chainId: toHex(1337), + rpcUrl: 'https://mock-rpc-url', + type: NetworkClientType.Custom, + }) + .mockReturnValue(fakeNetworkClients[1]); + await controller.initializeProvider(); + expect( + controller.store.getState().networkDetails, + ).toStrictEqual({ + EIPS: { + 1559: true, + }, + }); + + await waitForStateChanges({ + controller, + propertyPath: ['networkDetails'], + operation: async () => { + await controller.lookupNetwork(); + }, + }); + + expect( + controller.store.getState().networkDetails, + ).toStrictEqual({ + EIPS: { + 1559: false, + }, + }); + }, + ); + }); + + it('emits infuraIsUnblocked, not infuraIsBlocked, assuming that the first network was blocked', async () => { + await withController( + { + state: { + providerConfig: buildProviderConfig({ type: networkType }), + networkConfigurations: { + testNetworkConfigurationId: { + id: 'testNetworkConfigurationId', + rpcUrl: 'https://mock-rpc-url', + chainId: toHex(1337), + ticker: 'ABC', + }, + }, + }, + infuraProjectId: 'some-infura-project-id', + }, + async ({ controller, messenger }) => { + const fakeProviders = [ + buildFakeProvider([ + // Called during provider initialization + { + request: { + method: 'net_version', + }, + response: SUCCESSFUL_NET_VERSION_RESPONSE, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, + }, + // Called via `lookupNetwork` directly + { + request: { + method: 'net_version', + }, + response: SUCCESSFUL_NET_VERSION_RESPONSE, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + error: BLOCKED_INFURA_JSON_RPC_ERROR, + beforeCompleting: () => { + // Intentionally not awaited because don't want this to + // block the `net_version` request + controller.setActiveNetwork( + 'testNetworkConfigurationId', + ); + }, + }, + ]), + buildFakeProvider([ + // Called when switching networks + { + request: { + method: 'net_version', + }, + response: SUCCESSFUL_NET_VERSION_RESPONSE, + }, + { + request: { + method: 'eth_getBlockByNumber', + }, + response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, + }, + ]), + ]; + const fakeNetworkClients = [ + buildFakeClient(fakeProviders[0]), + buildFakeClient(fakeProviders[1]), + ]; + mockCreateNetworkClient() + .calledWith({ + network: networkType, + infuraProjectId: 'some-infura-project-id', + type: NetworkClientType.Infura, + }) + .mockReturnValue(fakeNetworkClients[0]) + .calledWith({ + chainId: toHex(1337), + rpcUrl: 'https://mock-rpc-url', + type: NetworkClientType.Custom, + }) + .mockReturnValue(fakeNetworkClients[1]); + await controller.initializeProvider(); + const promiseForInfuraIsUnblockedEvents = + waitForPublishedEvents({ + messenger, + eventType: 'NetworkController:infuraIsUnblocked', + }); + const promiseForNoInfuraIsBlockedEvents = + waitForPublishedEvents({ + messenger, + eventType: 'NetworkController:infuraIsBlocked', + count: 0, + }); + + await waitForStateChanges({ + controller, + propertyPath: ['networkStatus'], + operation: async () => { + await controller.lookupNetwork(); + }, + }); + + await expect( + promiseForInfuraIsUnblockedEvents, + ).toBeFulfilled(); + await expect( + promiseForNoInfuraIsBlockedEvents, + ).toBeFulfilled(); + }, + ); + }); + }); + + lookupNetworkTests({ + expectedProviderConfig: buildProviderConfig({ type: networkType }), + initialState: { + providerConfig: buildProviderConfig({ type: networkType }), + }, + operation: async (controller) => { + await controller.lookupNetwork(); + }, }); }); - - describe('if the network was switched after the eth_getBlockByNumber request started but before it completed', () => { - it('stores the network status of the second network, not the first', async () => { - await withController( - { - state: { - providerConfig: buildProviderConfig({ type: networkType }), - networkConfigurations: { - testNetworkConfigurationId: { - id: 'testNetworkConfigurationId', - rpcUrl: 'https://mock-rpc-url', - chainId: toHex(1337), - ticker: 'ABC', - }, - }, - }, - infuraProjectId: 'some-infura-project-id', - }, - async ({ controller }) => { - const fakeProviders = [ - buildFakeProvider([ - // Called during provider initialization - { - request: { - method: 'net_version', - }, - response: SUCCESSFUL_NET_VERSION_RESPONSE, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, - }, - // Called via `lookupNetwork` directly - { - request: { - method: 'net_version', - }, - response: SUCCESSFUL_NET_VERSION_RESPONSE, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, - beforeCompleting: () => { - // Intentionally not awaited because don't want this to - // block the `net_version` request - controller.setActiveNetwork( - 'testNetworkConfigurationId', - ); - }, - }, - ]), - buildFakeProvider([ - // Called when switching networks - { - request: { - method: 'net_version', - }, - error: GENERIC_JSON_RPC_ERROR, - }, - ]), - ]; - const fakeNetworkClients = [ - buildFakeClient(fakeProviders[0]), - buildFakeClient(fakeProviders[1]), - ]; - mockCreateNetworkClient() - .calledWith({ - network: networkType, - infuraProjectId: 'some-infura-project-id', - type: NetworkClientType.Infura, - }) - .mockReturnValue(fakeNetworkClients[0]) - .calledWith({ - chainId: toHex(1337), - rpcUrl: 'https://mock-rpc-url', - type: NetworkClientType.Custom, - }) - .mockReturnValue(fakeNetworkClients[1]); - await controller.initializeProvider(); - expect(controller.store.getState().networkStatus).toBe( - 'available', - ); - - await waitForStateChanges({ - controller, - propertyPath: ['networkStatus'], - operation: async () => { - await controller.lookupNetwork(); - }, - }); - - expect(controller.store.getState().networkStatus).toBe( - 'unknown', - ); - }, - ); - }); - - it('stores the ID of the second network, not the first', async () => { - await withController( - { - state: { - providerConfig: buildProviderConfig({ type: networkType }), - networkConfigurations: { - testNetworkConfigurationId: { - id: 'testNetworkConfigurationId', - rpcUrl: 'https://mock-rpc-url', - chainId: toHex(1337), - ticker: 'ABC', - }, - }, - }, - infuraProjectId: 'some-infura-project-id', - }, - async ({ controller }) => { - const fakeProviders = [ - buildFakeProvider([ - // Called during provider initialization - { - request: { - method: 'net_version', - }, - response: { - result: '1', - }, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, - }, - // Called via `lookupNetwork` directly - { - request: { - method: 'net_version', - }, - response: { - result: '1', - }, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, - beforeCompleting: async () => { - // Intentionally not awaited because don't want this to - // block the `net_version` request - controller.setActiveNetwork( - 'testNetworkConfigurationId', - ); - }, - }, - ]), - buildFakeProvider([ - // Called when switching networks - { - request: { - method: 'net_version', - }, - response: { - result: '2', - }, - }, - ]), - ]; - const fakeNetworkClients = [ - buildFakeClient(fakeProviders[0]), - buildFakeClient(fakeProviders[1]), - ]; - mockCreateNetworkClient() - .calledWith({ - network: networkType, - infuraProjectId: 'some-infura-project-id', - type: NetworkClientType.Infura, - }) - .mockReturnValue(fakeNetworkClients[0]) - .calledWith({ - chainId: toHex(1337), - rpcUrl: 'https://mock-rpc-url', - type: NetworkClientType.Custom, - }) - .mockReturnValue(fakeNetworkClients[1]); - await waitForStateChanges({ - controller, - propertyPath: ['networkId'], - operation: async () => { - await controller.initializeProvider(); - }, - }); - expect(controller.store.getState().networkId).toBe('1'); - - await waitForStateChanges({ - controller, - propertyPath: ['networkId'], - operation: async () => { - await controller.lookupNetwork(); - }, - }); - - expect(controller.store.getState().networkId).toBe('2'); - }, - ); - }); - - it('stores the EIP-1559 support of the second network, not the first', async () => { - await withController( - { - state: { - providerConfig: buildProviderConfig({ type: networkType }), - networkConfigurations: { - testNetworkConfigurationId: { - id: 'testNetworkConfigurationId', - rpcUrl: 'https://mock-rpc-url', - chainId: toHex(1337), - ticker: 'ABC', - }, - }, - }, - infuraProjectId: 'some-infura-project-id', - }, - async ({ controller }) => { - const fakeProviders = [ - buildFakeProvider([ - // Called during provider initialization - { - request: { - method: 'net_version', - }, - response: { - result: '1', - }, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: { - result: POST_1559_BLOCK, - }, - }, - // Called via `lookupNetwork` directly - { - request: { - method: 'net_version', - }, - response: { - result: '1', - }, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: { - result: POST_1559_BLOCK, - }, - beforeCompleting: () => { - // Intentionally not awaited because don't want this to - // block the `net_version` request - controller.setActiveNetwork( - 'testNetworkConfigurationId', - ); - }, - }, - ]), - buildFakeProvider([ - // Called when switching networks - { - request: { - method: 'net_version', - }, - response: { - result: '2', - }, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: { - result: PRE_1559_BLOCK, - }, - }, - ]), - ]; - const fakeNetworkClients = [ - buildFakeClient(fakeProviders[0]), - buildFakeClient(fakeProviders[1]), - ]; - mockCreateNetworkClient() - .calledWith({ - network: networkType, - infuraProjectId: 'some-infura-project-id', - type: NetworkClientType.Infura, - }) - .mockReturnValue(fakeNetworkClients[0]) - .calledWith({ - chainId: toHex(1337), - rpcUrl: 'https://mock-rpc-url', - type: NetworkClientType.Custom, - }) - .mockReturnValue(fakeNetworkClients[1]); - await controller.initializeProvider(); - expect( - controller.store.getState().networkDetails, - ).toStrictEqual({ - EIPS: { - 1559: true, - }, - }); - - await waitForStateChanges({ - controller, - propertyPath: ['networkDetails'], - operation: async () => { - await controller.lookupNetwork(); - }, - }); - - expect( - controller.store.getState().networkDetails, - ).toStrictEqual({ - EIPS: { - 1559: false, - }, - }); - }, - ); - }); - - it('emits infuraIsUnblocked, not infuraIsBlocked, assuming that the first network was blocked', async () => { - await withController( - { - state: { - providerConfig: buildProviderConfig({ type: networkType }), - networkConfigurations: { - testNetworkConfigurationId: { - id: 'testNetworkConfigurationId', - rpcUrl: 'https://mock-rpc-url', - chainId: toHex(1337), - ticker: 'ABC', - }, - }, - }, - infuraProjectId: 'some-infura-project-id', - }, - async ({ controller, messenger }) => { - const fakeProviders = [ - buildFakeProvider([ - // Called during provider initialization - { - request: { - method: 'net_version', - }, - response: SUCCESSFUL_NET_VERSION_RESPONSE, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, - }, - // Called via `lookupNetwork` directly - { - request: { - method: 'net_version', - }, - response: SUCCESSFUL_NET_VERSION_RESPONSE, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - error: BLOCKED_INFURA_JSON_RPC_ERROR, - beforeCompleting: () => { - // Intentionally not awaited because don't want this to - // block the `net_version` request - controller.setActiveNetwork( - 'testNetworkConfigurationId', - ); - }, - }, - ]), - buildFakeProvider([ - // Called when switching networks - { - request: { - method: 'net_version', - }, - response: SUCCESSFUL_NET_VERSION_RESPONSE, - }, - { - request: { - method: 'eth_getBlockByNumber', - }, - response: SUCCESSFUL_ETH_GET_BLOCK_BY_NUMBER_RESPONSE, - }, - ]), - ]; - const fakeNetworkClients = [ - buildFakeClient(fakeProviders[0]), - buildFakeClient(fakeProviders[1]), - ]; - mockCreateNetworkClient() - .calledWith({ - network: networkType, - infuraProjectId: 'some-infura-project-id', - type: NetworkClientType.Infura, - }) - .mockReturnValue(fakeNetworkClients[0]) - .calledWith({ - chainId: toHex(1337), - rpcUrl: 'https://mock-rpc-url', - type: NetworkClientType.Custom, - }) - .mockReturnValue(fakeNetworkClients[1]); - await controller.initializeProvider(); - const promiseForInfuraIsUnblockedEvents = - waitForPublishedEvents({ - messenger, - eventType: 'NetworkController:infuraIsUnblocked', - }); - const promiseForNoInfuraIsBlockedEvents = - waitForPublishedEvents({ - messenger, - eventType: 'NetworkController:infuraIsBlocked', - count: 0, - }); - - await waitForStateChanges({ - controller, - propertyPath: ['networkStatus'], - operation: async () => { - await controller.lookupNetwork(); - }, - }); - - await expect(promiseForInfuraIsUnblockedEvents).toBeFulfilled(); - await expect(promiseForNoInfuraIsBlockedEvents).toBeFulfilled(); - }, - ); - }); - }); - - lookupNetworkTests({ - expectedProviderConfig: buildProviderConfig({ type: networkType }), - initialState: { - providerConfig: buildProviderConfig({ type: networkType }), - }, - operation: async (controller) => { - await controller.lookupNetwork(); - }, - }); - }); - }); + }, + ); describe(`when the provider config in state contains a network type of "rpc"`, () => { describe('if the network was switched after the net_version request started but before it completed', () => { @@ -1660,7 +1673,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, chainId: toHex(1337), rpcUrl: 'https://mock-rpc-url', }), @@ -1692,7 +1705,7 @@ describe('NetworkController', () => { beforeCompleting: () => { // Intentionally not awaited because don't want this to // block the `net_version` request - controller.setProviderType(NETWORK_TYPES.GOERLI); + controller.setProviderType(NetworkType.goerli); }, }, { @@ -1724,7 +1737,7 @@ describe('NetworkController', () => { }) .mockReturnValue(fakeNetworkClients[0]) .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -1752,7 +1765,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, chainId: toHex(1337), rpcUrl: 'https://mock-rpc-url', }), @@ -1788,7 +1801,7 @@ describe('NetworkController', () => { beforeCompleting: async () => { // Intentionally not awaited because don't want this to // block the `net_version` request - controller.setProviderType(NETWORK_TYPES.GOERLI); + controller.setProviderType(NetworkType.goerli); }, }, { @@ -1822,7 +1835,7 @@ describe('NetworkController', () => { }) .mockReturnValue(fakeNetworkClients[0]) .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -1854,7 +1867,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, chainId: toHex(1337), rpcUrl: 'https://mock-rpc-url', }), @@ -1892,7 +1905,7 @@ describe('NetworkController', () => { beforeCompleting: () => { // Intentionally not awaited because don't want this to // block the `net_version` request - controller.setProviderType(NETWORK_TYPES.GOERLI); + controller.setProviderType(NetworkType.goerli); }, }, { @@ -1936,7 +1949,7 @@ describe('NetworkController', () => { }) .mockReturnValue(fakeNetworkClients[0]) .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -1970,7 +1983,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, chainId: toHex(1337), rpcUrl: 'https://mock-rpc-url', }), @@ -2002,7 +2015,7 @@ describe('NetworkController', () => { beforeCompleting: () => { // Intentionally not awaited because don't want this to // block the `net_version` request - controller.setProviderType(NETWORK_TYPES.GOERLI); + controller.setProviderType(NetworkType.goerli); }, }, { @@ -2040,7 +2053,7 @@ describe('NetworkController', () => { }) .mockReturnValue(fakeNetworkClients[0]) .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -2078,7 +2091,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, chainId: toHex(1337), rpcUrl: 'https://mock-rpc-url', }), @@ -2116,7 +2129,7 @@ describe('NetworkController', () => { beforeCompleting: () => { // Intentionally not awaited because don't want this to // block the `net_version` request - controller.setProviderType(NETWORK_TYPES.GOERLI); + controller.setProviderType(NetworkType.goerli); }, }, ]), @@ -2142,7 +2155,7 @@ describe('NetworkController', () => { }) .mockReturnValue(fakeNetworkClients[0]) .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -2170,7 +2183,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, chainId: toHex(1337), rpcUrl: 'https://mock-rpc-url', }), @@ -2212,7 +2225,7 @@ describe('NetworkController', () => { beforeCompleting: async () => { // Intentionally not awaited because don't want this to // block the `net_version` request - controller.setProviderType(NETWORK_TYPES.GOERLI); + controller.setProviderType(NetworkType.goerli); }, }, ]), @@ -2240,7 +2253,7 @@ describe('NetworkController', () => { }) .mockReturnValue(fakeNetworkClients[0]) .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -2272,7 +2285,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, chainId: toHex(1337), rpcUrl: 'https://mock-rpc-url', }), @@ -2318,7 +2331,7 @@ describe('NetworkController', () => { beforeCompleting: () => { // Intentionally not awaited because don't want this to // block the `net_version` request - controller.setProviderType(NETWORK_TYPES.GOERLI); + controller.setProviderType(NetworkType.goerli); }, }, ]), @@ -2354,7 +2367,7 @@ describe('NetworkController', () => { }) .mockReturnValue(fakeNetworkClients[0]) .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -2388,7 +2401,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, chainId: toHex(1337), rpcUrl: 'https://mock-rpc-url', }), @@ -2426,7 +2439,7 @@ describe('NetworkController', () => { beforeCompleting: () => { // Intentionally not awaited because don't want this to // block the `net_version` request - controller.setProviderType(NETWORK_TYPES.GOERLI); + controller.setProviderType(NetworkType.goerli); }, }, ]), @@ -2458,7 +2471,7 @@ describe('NetworkController', () => { }) .mockReturnValue(fakeNetworkClients[0]) .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -2492,10 +2505,10 @@ describe('NetworkController', () => { lookupNetworkTests({ expectedProviderConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, }), initialState: { - providerConfig: buildProviderConfig({ type: NETWORK_TYPES.RPC }), + providerConfig: buildProviderConfig({ type: NetworkType.rpc }), }, operation: async (controller) => { await controller.lookupNetwork(); @@ -2565,7 +2578,7 @@ describe('NetworkController', () => { { state: { providerConfig: { - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'http://somethingexisting.com', chainId: toHex(99999), ticker: 'something existing', @@ -2575,7 +2588,7 @@ describe('NetworkController', () => { }, async ({ controller }) => { await expect(() => - controller.setProviderType(NETWORK_TYPES.RPC), + controller.setProviderType(NetworkType.rpc), ).rejects.toThrow( 'NetworkController - cannot call "setProviderType" with type "rpc". Use "setActiveNetwork"', ); @@ -2590,7 +2603,7 @@ describe('NetworkController', () => { createNetworkClientMock.mockReturnValue(fakeNetworkClient); try { - await controller.setProviderType(NETWORK_TYPES.RPC); + await controller.setProviderType(NetworkType.rpc); } catch { // catch the rejection (it is tested above) } @@ -2619,7 +2632,7 @@ describe('NetworkController', () => { createNetworkClientMock.mockReturnValue(fakeNetworkClient); try { - await controller.setProviderType(NETWORK_TYPES.RPC); + await controller.setProviderType(NetworkType.rpc); } catch { // catch the rejection (it is tested above) } @@ -2653,7 +2666,7 @@ describe('NetworkController', () => { nickname: 'something existing', id: 'testNetworkConfigurationId', rpcPrefs: undefined, - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, }, initialState: { networkConfigurations: { @@ -2711,7 +2724,7 @@ describe('NetworkController', () => { { state: { providerConfig: { - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(111), ticker: 'TEST', @@ -2766,7 +2779,7 @@ describe('NetworkController', () => { { state: { providerConfig: { - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(111), ticker: 'TEST', @@ -3132,31 +3145,29 @@ describe('NetworkController', () => { }); describe('resetConnection', () => { - [ - NETWORK_TYPES.MAINNET, - NETWORK_TYPES.GOERLI, - NETWORK_TYPES.SEPOLIA, - ].forEach((networkType) => { - describe(`when the type in the provider configuration is "${networkType}"`, () => { - refreshNetworkTests({ - expectedProviderConfig: buildProviderConfig({ type: networkType }), - initialState: { - providerConfig: buildProviderConfig({ type: networkType }), - }, - operation: async (controller) => { - await controller.resetConnection(); - }, + [NetworkType.mainnet, NetworkType.goerli, NetworkType.sepolia].forEach( + (networkType) => { + describe(`when the type in the provider configuration is "${networkType}"`, () => { + refreshNetworkTests({ + expectedProviderConfig: buildProviderConfig({ type: networkType }), + initialState: { + providerConfig: buildProviderConfig({ type: networkType }), + }, + operation: async (controller) => { + await controller.resetConnection(); + }, + }); }); - }); - }); + }, + ); describe(`when the type in the provider configuration is "rpc"`, () => { refreshNetworkTests({ expectedProviderConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, }), initialState: { - providerConfig: buildProviderConfig({ type: NETWORK_TYPES.RPC }), + providerConfig: buildProviderConfig({ type: NetworkType.rpc }), }, operation: async (controller) => { await controller.resetConnection(); @@ -3171,7 +3182,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.MAINNET, + type: NetworkType.mainnet, }), }, }, @@ -3182,7 +3193,7 @@ describe('NetworkController', () => { expect(providerConfig).toStrictEqual( buildProviderConfig({ - type: NETWORK_TYPES.MAINNET, + type: NetworkType.mainnet, }), ); }, @@ -3451,7 +3462,7 @@ describe('NetworkController', () => { { state: { providerConfig: { - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(111), ticker: 'TEST', @@ -3726,7 +3737,7 @@ describe('NetworkController', () => { it('should add the given network and not set it to active if the setActive option is not passed (or a falsy value is passed)', async () => { uuidV4Mock.mockImplementationOnce(() => 'networkConfigurationId'); const originalProvider = { - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(111), ticker: 'TEST', @@ -3773,7 +3784,7 @@ describe('NetworkController', () => { { state: { providerConfig: { - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(111), ticker: 'TEST', @@ -3829,7 +3840,7 @@ describe('NetworkController', () => { { state: { providerConfig: { - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(111), ticker: 'TEST', @@ -4615,7 +4626,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, }), }, }, @@ -4623,7 +4634,7 @@ describe('NetworkController', () => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); - await controller.setProviderType(NETWORK_TYPES.GOERLI); + await controller.setProviderType(NetworkType.goerli); const networkWillChange = waitForPublishedEvents({ messenger, @@ -4645,7 +4656,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, }), }, }, @@ -4653,7 +4664,7 @@ describe('NetworkController', () => { const fakeProvider = buildFakeProvider(); const fakeNetworkClient = buildFakeClient(fakeProvider); mockCreateNetworkClient().mockReturnValue(fakeNetworkClient); - await controller.setProviderType(NETWORK_TYPES.GOERLI); + await controller.setProviderType(NetworkType.goerli); const networkDidChange = waitForPublishedEvents({ messenger, @@ -4675,7 +4686,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(1337), nickname: 'network', @@ -4695,7 +4706,7 @@ describe('NetworkController', () => { ]; mockCreateNetworkClient() .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -4741,7 +4752,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(1337), }), @@ -4772,7 +4783,7 @@ describe('NetworkController', () => { ]; mockCreateNetworkClient() .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -4812,7 +4823,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(1337), }), @@ -4839,7 +4850,7 @@ describe('NetworkController', () => { ]; mockCreateNetworkClient() .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -4885,7 +4896,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(1337), }), @@ -4912,7 +4923,7 @@ describe('NetworkController', () => { ]; mockCreateNetworkClient() .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -4947,7 +4958,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(1337), }), @@ -4962,7 +4973,7 @@ describe('NetworkController', () => { ]; mockCreateNetworkClient() .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -4991,7 +5002,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(1337), }), @@ -5006,7 +5017,7 @@ describe('NetworkController', () => { ]; mockCreateNetworkClient() .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -5037,7 +5048,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(1337), }), @@ -5075,7 +5086,7 @@ describe('NetworkController', () => { ]; mockCreateNetworkClient() .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -5102,7 +5113,7 @@ describe('NetworkController', () => { { state: { providerConfig: buildProviderConfig({ - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, rpcUrl: 'https://mock-rpc-url', chainId: toHex(1337), }), @@ -5138,7 +5149,7 @@ describe('NetworkController', () => { ]; mockCreateNetworkClient() .calledWith({ - network: NETWORK_TYPES.GOERLI, + network: NetworkType.goerli, infuraProjectId: 'some-infura-project-id', type: NetworkClientType.Infura, }) @@ -5439,7 +5450,7 @@ function refreshNetworkTests({ ); }); - if (expectedProviderConfig.type === NETWORK_TYPES.RPC) { + if (expectedProviderConfig.type === NetworkType.rpc) { it('sets the provider to a custom RPC provider initialized with the RPC target and chain ID', async () => { await withController( { @@ -5543,7 +5554,7 @@ function refreshNetworkTests({ const initializationNetworkClientOptions: Parameters< typeof createNetworkClient >[0] = - providerType === NETWORK_TYPES.RPC + providerType === NetworkType.rpc ? { chainId: controller.store.getState().providerConfig.chainId, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -5558,7 +5569,7 @@ function refreshNetworkTests({ const operationNetworkClientOptions: Parameters< typeof createNetworkClient >[0] = - expectedProviderConfig.type === NETWORK_TYPES.RPC + expectedProviderConfig.type === NetworkType.rpc ? { chainId: toHex(expectedProviderConfig.chainId), // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -5827,7 +5838,7 @@ function lookupNetworkTests({ ); }); - if (expectedProviderConfig.type === NETWORK_TYPES.RPC) { + if (expectedProviderConfig.type === NetworkType.rpc) { it('emits infuraIsUnblocked', async () => { await withController( { @@ -5920,7 +5931,7 @@ function lookupNetworkTests({ }); describe('if a country blocked error is encountered while retrieving the version of the current network', () => { - if (expectedProviderConfig.type === NETWORK_TYPES.RPC) { + if (expectedProviderConfig.type === NetworkType.rpc) { it('updates the network in state to "unknown"', async () => { await withController( { @@ -6111,7 +6122,7 @@ function lookupNetworkTests({ ); }); - if (expectedProviderConfig.type === NETWORK_TYPES.RPC) { + if (expectedProviderConfig.type === NetworkType.rpc) { it('emits infuraIsUnblocked', async () => { await withController( { @@ -6227,7 +6238,7 @@ function lookupNetworkTests({ ); }); - if (expectedProviderConfig.type === NETWORK_TYPES.RPC) { + if (expectedProviderConfig.type === NetworkType.rpc) { it('emits infuraIsUnblocked', async () => { await withController( { @@ -6346,7 +6357,7 @@ function lookupNetworkTests({ ); }); - if (expectedProviderConfig.type === NETWORK_TYPES.RPC) { + if (expectedProviderConfig.type === NetworkType.rpc) { it('emits infuraIsUnblocked', async () => { await withController( { @@ -6448,7 +6459,7 @@ function lookupNetworkTests({ }); describe('if a country blocked error is encountered while retrieving the network details of the current network', () => { - if (expectedProviderConfig.type === NETWORK_TYPES.RPC) { + if (expectedProviderConfig.type === NetworkType.rpc) { it('updates the network in state to "unknown"', async () => { await withController( { @@ -6659,7 +6670,7 @@ function lookupNetworkTests({ ); }); - if (expectedProviderConfig.type === NETWORK_TYPES.RPC) { + if (expectedProviderConfig.type === NetworkType.rpc) { it('emits infuraIsUnblocked', async () => { await withController( { @@ -6862,7 +6873,7 @@ async function withController( function buildProviderConfig( config: Partial = {}, ): ProviderConfiguration { - if (config.type && config.type !== NETWORK_TYPES.RPC) { + if (config.type && config.type !== NetworkType.rpc) { const networkConfig = INFURA_NETWORKS.find( ({ networkType }) => networkType === config.type, ); @@ -6879,7 +6890,7 @@ function buildProviderConfig( }; } return { - type: NETWORK_TYPES.RPC, + type: NetworkType.rpc, chainId: '0x42', nickname: undefined, rpcUrl: 'http://doesntmatter.com',