|
| 1 | + |
| 2 | +import { captureException } from '@sentry/react-native'; |
| 3 | +import { cloneDeep } from 'lodash'; |
| 4 | +import { NetworkConfiguration, RpcEndpointType } from '@metamask/network-controller'; |
| 5 | +import { Hex } from '@metamask/utils'; |
| 6 | + |
| 7 | +import { ensureValidState } from './util'; |
| 8 | +import migrate from './077'; |
| 9 | + |
| 10 | +jest.mock('@sentry/react-native', () => ({ |
| 11 | + captureException: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +jest.mock('./util', () => ({ |
| 15 | + ensureValidState: jest.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +const mockedCaptureException = jest.mocked(captureException); |
| 19 | +const mockedEnsureValidState = jest.mocked(ensureValidState); |
| 20 | + |
| 21 | +const createTestState = () => ({ |
| 22 | + engine: { |
| 23 | + backgroundState: { |
| 24 | + NetworkController: { |
| 25 | + selectedNetworkClientId: 'mainnet', |
| 26 | + networksMetadata: {}, |
| 27 | + networkConfigurationsByChainId: { |
| 28 | + '0x1': { |
| 29 | + chainId: '0x1', |
| 30 | + rpcEndpoints: [ |
| 31 | + { |
| 32 | + networkClientId: 'mainnet', |
| 33 | + url: 'https://mainnet.infura.io/v3/{infuraProjectId}', |
| 34 | + type: 'infura', |
| 35 | + }, |
| 36 | + ], |
| 37 | + defaultRpcEndpointIndex: 0, |
| 38 | + blockExplorerUrls: ['https://etherscan.io'], |
| 39 | + defaultBlockExplorerUrlIndex: 0, |
| 40 | + name: 'Ethereum Mainnet', |
| 41 | + nativeCurrency: 'ETH', |
| 42 | + }, |
| 43 | + '0xaa36a7': { |
| 44 | + chainId: '0xaa36a7', |
| 45 | + rpcEndpoints: [ |
| 46 | + { |
| 47 | + networkClientId: 'sepolia', |
| 48 | + url: 'https://sepolia.infura.io/v3/{infuraProjectId}', |
| 49 | + type: 'infura', |
| 50 | + }, |
| 51 | + ], |
| 52 | + defaultRpcEndpointIndex: 0, |
| 53 | + blockExplorerUrls: ['https://sepolia.etherscan.io'], |
| 54 | + defaultBlockExplorerUrlIndex: 0, |
| 55 | + name: 'Sepolia', |
| 56 | + nativeCurrency: 'SepoliaETH', |
| 57 | + }, |
| 58 | + '0xe705': { |
| 59 | + chainId: '0xe705', |
| 60 | + rpcEndpoints: [ |
| 61 | + { |
| 62 | + networkClientId: 'linea-sepolia', |
| 63 | + url: 'https://linea-sepolia.infura.io/v3/{infuraProjectId}', |
| 64 | + type: 'infura', |
| 65 | + }, |
| 66 | + ], |
| 67 | + defaultRpcEndpointIndex: 0, |
| 68 | + blockExplorerUrls: ['https://sepolia.lineascan.build'], |
| 69 | + defaultBlockExplorerUrlIndex: 0, |
| 70 | + name: 'Linea Sepolia', |
| 71 | + nativeCurrency: 'LineaETH', |
| 72 | + }, |
| 73 | + '0xe708': { |
| 74 | + chainId: '0xe708', |
| 75 | + rpcEndpoints: [ |
| 76 | + { |
| 77 | + networkClientId: 'linea-mainnet', |
| 78 | + url: 'https://linea-mainnet.infura.io/v3/{infuraProjectId}', |
| 79 | + type: 'infura', |
| 80 | + }, |
| 81 | + ], |
| 82 | + defaultRpcEndpointIndex: 0, |
| 83 | + blockExplorerUrls: ['https://lineascan.build'], |
| 84 | + defaultBlockExplorerUrlIndex: 0, |
| 85 | + name: 'Linea Mainnet', |
| 86 | + nativeCurrency: 'ETH', |
| 87 | + }, |
| 88 | + '0x18c6': { |
| 89 | + chainId: '0x18c6', |
| 90 | + rpcEndpoints: [ |
| 91 | + { |
| 92 | + networkClientId: 'megaeth-testnet', |
| 93 | + url: 'https://carrot.megaeth.com/rpc', |
| 94 | + type: RpcEndpointType.Custom, |
| 95 | + failoverUrls: [], |
| 96 | + }, |
| 97 | + ], |
| 98 | + defaultRpcEndpointIndex: 0, |
| 99 | + blockExplorerUrls: ['https://megaexplorer.xyz'], |
| 100 | + defaultBlockExplorerUrlIndex: 0, |
| 101 | + name: 'Mega Testnet', |
| 102 | + nativeCurrency: 'MegaETH', |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | + } |
| 108 | +}); |
| 109 | + |
| 110 | +const createMonadTestnetConfiguration = (): NetworkConfiguration => ({ |
| 111 | + chainId: '0x279f', |
| 112 | + rpcEndpoints: [ |
| 113 | + { |
| 114 | + networkClientId: 'monad-testnet', |
| 115 | + url: 'https://testnet-rpc.monad.xyz', |
| 116 | + type: RpcEndpointType.Custom, |
| 117 | + failoverUrls: [], |
| 118 | + }, |
| 119 | + ], |
| 120 | + defaultRpcEndpointIndex: 0, |
| 121 | + blockExplorerUrls: ['https://testnet.monadexplorer.com'], |
| 122 | + defaultBlockExplorerUrlIndex: 0, |
| 123 | + name: 'Monad Testnet', |
| 124 | + nativeCurrency: 'MON', |
| 125 | +}); |
| 126 | + |
| 127 | +describe('Migration 77: Add `Monad Testnet`', () => { |
| 128 | + beforeEach(() => { |
| 129 | + jest.resetAllMocks(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('returns state unchanged if ensureValidState fails', () => { |
| 133 | + const state = { some: 'state' }; |
| 134 | + mockedEnsureValidState.mockReturnValue(false); |
| 135 | + |
| 136 | + const migratedState = migrate(state); |
| 137 | + |
| 138 | + expect(migratedState).toStrictEqual({ some: 'state' }); |
| 139 | + expect(mockedCaptureException).not.toHaveBeenCalled(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('adds `Monad Testnet` as default network to state', () => { |
| 143 | + const monadTestnetConfiguration = createMonadTestnetConfiguration(); |
| 144 | + const oldState = createTestState(); |
| 145 | + mockedEnsureValidState.mockReturnValue(true); |
| 146 | + |
| 147 | + const expectedData = { |
| 148 | + engine: { |
| 149 | + backgroundState: { |
| 150 | + NetworkController: { |
| 151 | + ...oldState.engine.backgroundState.NetworkController, |
| 152 | + networkConfigurationsByChainId: { |
| 153 | + ...oldState.engine.backgroundState.NetworkController.networkConfigurationsByChainId, |
| 154 | + [monadTestnetConfiguration.chainId]: monadTestnetConfiguration |
| 155 | + }, |
| 156 | + }, |
| 157 | + } |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + const migratedState = migrate(oldState); |
| 162 | + |
| 163 | + expect(migratedState).toStrictEqual(expectedData); |
| 164 | + expect(mockedCaptureException).not.toHaveBeenCalled(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('replaces `Monad Testnet` NetworkConfiguration if there is one', () => { |
| 168 | + const monadTestnetConfiguration = createMonadTestnetConfiguration(); |
| 169 | + const oldState = createTestState(); |
| 170 | + const networkConfigurationsByChainId = oldState.engine.backgroundState.NetworkController.networkConfigurationsByChainId as Record<Hex, NetworkConfiguration>; |
| 171 | + networkConfigurationsByChainId[monadTestnetConfiguration.chainId] = { |
| 172 | + ...monadTestnetConfiguration, |
| 173 | + rpcEndpoints: [ |
| 174 | + { |
| 175 | + networkClientId: 'some-client-id', |
| 176 | + url: 'https://some-url.com/rpc', |
| 177 | + type: RpcEndpointType.Custom, |
| 178 | + }, |
| 179 | + ], |
| 180 | + }; |
| 181 | + mockedEnsureValidState.mockReturnValue(true); |
| 182 | + |
| 183 | + const expectedData = { |
| 184 | + engine: { |
| 185 | + backgroundState: { |
| 186 | + NetworkController: { |
| 187 | + ...oldState.engine.backgroundState.NetworkController, |
| 188 | + networkConfigurationsByChainId: { |
| 189 | + ...oldState.engine.backgroundState.NetworkController.networkConfigurationsByChainId, |
| 190 | + [monadTestnetConfiguration.chainId]: monadTestnetConfiguration |
| 191 | + }, |
| 192 | + }, |
| 193 | + } |
| 194 | + } |
| 195 | + }; |
| 196 | + |
| 197 | + const migratedState = migrate(oldState); |
| 198 | + |
| 199 | + expect(migratedState).toStrictEqual(expectedData); |
| 200 | + expect(mockedCaptureException).not.toHaveBeenCalled(); |
| 201 | + }); |
| 202 | + |
| 203 | + it.each([{ |
| 204 | + state: { |
| 205 | + engine: {} |
| 206 | + }, |
| 207 | + test: 'empty engine state', |
| 208 | + }, { |
| 209 | + state: { |
| 210 | + engine: { |
| 211 | + backgroundState: {} |
| 212 | + } |
| 213 | + }, |
| 214 | + test: 'empty backgroundState', |
| 215 | + }, { |
| 216 | + state: { |
| 217 | + engine: { |
| 218 | + backgroundState: { |
| 219 | + NetworkController: 'invalid' |
| 220 | + } |
| 221 | + }, |
| 222 | + }, |
| 223 | + test: 'invalid NetworkController state' |
| 224 | + }, { |
| 225 | + state: { |
| 226 | + engine: { |
| 227 | + backgroundState: { |
| 228 | + NetworkController: { |
| 229 | + networkConfigurationsByChainId: 'invalid' |
| 230 | + } |
| 231 | + } |
| 232 | + }, |
| 233 | + }, |
| 234 | + test: 'invalid networkConfigurationsByChainId state' |
| 235 | + } |
| 236 | + ])('does not modify state if the state is invalid - $test', ({ state }) => { |
| 237 | + const orgState = cloneDeep(state); |
| 238 | + mockedEnsureValidState.mockReturnValue(true); |
| 239 | + |
| 240 | + const migratedState = migrate(state); |
| 241 | + |
| 242 | + // State should be unchanged |
| 243 | + expect(migratedState).toStrictEqual(orgState); |
| 244 | + expect(mockedCaptureException).toHaveBeenCalledWith( |
| 245 | + new Error('Migration 77: NetworkController or networkConfigurationsByChainId not found in state'), |
| 246 | + ); |
| 247 | + }); |
| 248 | +}); |
0 commit comments