diff --git a/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.js b/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.js index 6d71ba602..e2137da87 100644 --- a/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.js +++ b/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.js @@ -116,7 +116,7 @@ async function switchEthereumChainHandler( if ( Object.values(BUILT_IN_INFURA_NETWORKS) .map(({ chainId: id }) => id) - .includes(chainId) + .includes(_chainId) ) { await setProviderType(approvedRequestData.type); } else { diff --git a/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.test.js b/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.test.js new file mode 100644 index 000000000..410697214 --- /dev/null +++ b/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.test.js @@ -0,0 +1,128 @@ +import { + CHAIN_IDS, + NETWORK_TYPES, +} from '../../../../../shared/constants/network'; +import switchEthereumChain from './switch-ethereum-chain'; + +const NON_INFURA_CHAIN_ID = '0x123456789'; + +const mockRequestUserApproval = ({ requestData }) => { + return Promise.resolve(requestData); +}; + +const MOCK_MAINNET_CONFIGURATION = { + id: 123, + chainId: CHAIN_IDS.MAINNET, + type: NETWORK_TYPES.MAINNET, +}; +const MOCK_LINEA_MAINNET_CONFIGURATION = { + id: 123, + chainId: CHAIN_IDS.LINEA_MAINNET, + type: NETWORK_TYPES.LINEA_MAINNET, +}; + +describe('switchEthereumChainHandler', () => { + it('should call setProviderType when switching to a built in infura network', async () => { + const mockSetProviderType = jest.fn(); + const mockSetActiveNetwork = jest.fn(); + const switchEthereumChainHandler = switchEthereumChain.implementation; + await switchEthereumChainHandler( + { + origin: 'example.com', + params: [{ chainId: CHAIN_IDS.MAINNET }], + }, + {}, + jest.fn(), + jest.fn(), + { + getCurrentChainId: () => NON_INFURA_CHAIN_ID, + findNetworkConfigurationBy: () => MOCK_MAINNET_CONFIGURATION, + setProviderType: mockSetProviderType, + setActiveNetwork: mockSetActiveNetwork, + requestUserApproval: mockRequestUserApproval, + }, + ); + expect(mockSetProviderType).toHaveBeenCalledTimes(1); + expect(mockSetProviderType).toHaveBeenCalledWith( + MOCK_MAINNET_CONFIGURATION.type, + ); + }); + + it('should call setProviderType when switching to a built in infura network, when chainId from request is lower case', async () => { + const mockSetProviderType = jest.fn(); + const mockSetActiveNetwork = jest.fn(); + const switchEthereumChainHandler = switchEthereumChain.implementation; + await switchEthereumChainHandler( + { + origin: 'example.com', + params: [{ chainId: CHAIN_IDS.LINEA_MAINNET.toLowerCase() }], + }, + {}, + jest.fn(), + jest.fn(), + { + getCurrentChainId: () => NON_INFURA_CHAIN_ID, + findNetworkConfigurationBy: () => MOCK_LINEA_MAINNET_CONFIGURATION, + setProviderType: mockSetProviderType, + setActiveNetwork: mockSetActiveNetwork, + requestUserApproval: mockRequestUserApproval, + }, + ); + expect(mockSetProviderType).toHaveBeenCalledTimes(1); + expect(mockSetProviderType).toHaveBeenCalledWith( + MOCK_LINEA_MAINNET_CONFIGURATION.type, + ); + }); + + it('should call setProviderType when switching to a built in infura network, when chainId from request is upper case', async () => { + const mockSetProviderType = jest.fn(); + const mockSetActiveNetwork = jest.fn(); + const switchEthereumChainHandler = switchEthereumChain.implementation; + await switchEthereumChainHandler( + { + origin: 'example.com', + params: [{ chainId: CHAIN_IDS.LINEA_MAINNET.toUpperCase() }], + }, + {}, + jest.fn(), + jest.fn(), + { + getCurrentChainId: () => NON_INFURA_CHAIN_ID, + findNetworkConfigurationBy: () => MOCK_LINEA_MAINNET_CONFIGURATION, + setProviderType: mockSetProviderType, + setActiveNetwork: mockSetActiveNetwork, + requestUserApproval: mockRequestUserApproval, + }, + ); + expect(mockSetProviderType).toHaveBeenCalledTimes(1); + expect(mockSetProviderType).toHaveBeenCalledWith( + MOCK_LINEA_MAINNET_CONFIGURATION.type, + ); + }); + + it('should call setActiveNetwork when switching to a custom network', async () => { + const mockSetProviderType = jest.fn(); + const mockSetActiveNetwork = jest.fn(); + const switchEthereumChainHandler = switchEthereumChain.implementation; + await switchEthereumChainHandler( + { + origin: 'example.com', + params: [{ chainId: NON_INFURA_CHAIN_ID }], + }, + {}, + jest.fn(), + jest.fn(), + { + getCurrentChainId: () => CHAIN_IDS.MAINNET, + findNetworkConfigurationBy: () => MOCK_MAINNET_CONFIGURATION, + setProviderType: mockSetProviderType, + setActiveNetwork: mockSetActiveNetwork, + requestUserApproval: mockRequestUserApproval, + }, + ); + expect(mockSetActiveNetwork).toHaveBeenCalledTimes(1); + expect(mockSetActiveNetwork).toHaveBeenCalledWith( + MOCK_MAINNET_CONFIGURATION.id, + ); + }); +});