Skip to content

Commit 85f4650

Browse files
fix: migration script of update MegaETH testnet v2 (#38573)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** This PR fix the migration script 184, to not replace the network configuration for a user if he already has megaETH testnet v2 exist instead we only merge the information <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/38573?quickstart=1) ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: Fixed the migration script for adding MegaETH testnet v2 ## **Related issues** Fixes: ## **Manual testing steps** 1. Install a extension without MegaETH testnet v2 2. Manually add MegaETH testnet v2 config ``` MEGAETH_TESTNET_V2_CONFIG = { chainId: '0x18c7', // 6343 name: 'MegaETH Testnet', nativeCurrency: 'MegaETH', blockExplorerUrls: ['https://megaeth-testnet-v2.blockscout.com'], defaultRpcEndpointIndex: 0, defaultBlockExplorerUrlIndex: 0, rpcEndpoints: [ { type: RpcEndpointType.Custom, url: 'https://timothy.megaeth.com/rpc', }, ], }; ``` 3. select the MegaETH testnet v2 as current network 4. update the extension to have the migration run 5. the network should remain the same ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <img width="300" alt="image" src="https://github.com/user-attachments/assets/b5eba40d-f044-45a9-8c17-6349e56c886a" /> <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> <img width="300" alt="image" src="https://github.com/user-attachments/assets/f8de66ef-37f7-4762-a5f7-af129519b265" /> ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Update migration 184 to merge an existing MegaETH Testnet v2 config instead of overwriting, add it to the enabled map only if missing, and retain v1-to-mainnet switching; add a test for the merge path. > > - **Migration 184**: > - Merge existing `MEGAETH_TESTNET_V2_CONFIG` instead of overwriting: > - Update `name` and `nativeCurrency`. > - Append missing RPC endpoint and block explorer URL; set default indices accordingly. > - Add v2 to `enabledNetworkMap` only if absent. > - Keep logic to switch selected client from v1 to `mainnet` and remove v1 config. > - **Tests**: > - Add test to verify merging behavior when v2 already exists. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 9b4283d. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent d058e75 commit 85f4650

File tree

2 files changed

+117
-8
lines changed

2 files changed

+117
-8
lines changed

app/scripts/migrations/184.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,69 @@ describe(`migration #${VERSION}`, () => {
220220
expect(newStorage).toStrictEqual(expectedStorage);
221221
});
222222

223+
it('merges the megaeth testnet v2 network configuration if user already has it', async () => {
224+
const oldStorage = {
225+
meta: { version: oldVersion },
226+
data: {
227+
NetworkController: {
228+
networkConfigurationsByChainId: {
229+
[MEGAETH_TESTNET_V2_CONFIG.chainId]: {
230+
...MEGAETH_TESTNET_V2_CONFIG,
231+
name: 'MegaETH Testnet custom',
232+
rpcEndpoints: [
233+
{
234+
networkClientId: 'some-network-client-id',
235+
type: RpcEndpointType.Custom,
236+
url: 'https://timothy.megaeth.com/rpc',
237+
failoverUrls: [],
238+
},
239+
],
240+
},
241+
},
242+
},
243+
NetworkEnablementController: {
244+
enabledNetworkMap: {
245+
[KnownCaipNamespace.Eip155]: {
246+
[MEGAETH_TESTNET_V2_CONFIG.chainId]: true,
247+
},
248+
},
249+
},
250+
},
251+
};
252+
253+
const expectedStorage = {
254+
meta: { version: VERSION },
255+
data: {
256+
NetworkController: {
257+
networkConfigurationsByChainId: {
258+
[MEGAETH_TESTNET_V2_CONFIG.chainId]: {
259+
...MEGAETH_TESTNET_V2_CONFIG,
260+
rpcEndpoints: [
261+
{
262+
networkClientId: 'some-network-client-id',
263+
type: RpcEndpointType.Custom,
264+
url: 'https://timothy.megaeth.com/rpc',
265+
failoverUrls: [],
266+
},
267+
],
268+
},
269+
},
270+
},
271+
NetworkEnablementController: {
272+
enabledNetworkMap: {
273+
[KnownCaipNamespace.Eip155]: {
274+
[MEGAETH_TESTNET_V2_CONFIG.chainId]: true,
275+
},
276+
},
277+
},
278+
},
279+
};
280+
281+
const newStorage = await migrate(oldStorage);
282+
283+
expect(newStorage).toStrictEqual(expectedStorage);
284+
});
285+
223286
// @ts-expect-error 'each' function is not recognized by TypeScript types
224287
it.each(['megaeth-testnet', 'random-network-client-id'])(
225288
'switchs to mainnet when the selected network client id is in MegaETH Testnet v1 - %s',

app/scripts/migrations/184.ts

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,63 @@ function transformState(state: Record<string, unknown>) {
172172
enabledNetworkMap: { [KnownCaipNamespace.Eip155]: eip155NetworkMap },
173173
} = networkEnablementState;
174174

175-
const megaethTestnetV1Configuration = networkConfigurationsByChainId[
176-
MEGAETH_TESTNET_V1_CHAIN_ID
177-
] as unknown as NetworkConfiguration;
175+
// Merge the MegaETH Testnet v2 network configuration if user already has it.
176+
if (
177+
hasProperty(
178+
networkConfigurationsByChainId,
179+
MEGAETH_TESTNET_V2_CONFIG.chainId,
180+
)
181+
) {
182+
const megaethTestnetV2Configuration = networkConfigurationsByChainId[
183+
MEGAETH_TESTNET_V2_CONFIG.chainId
184+
] as unknown as NetworkConfiguration;
185+
megaethTestnetV2Configuration.name = MEGAETH_TESTNET_V2_CONFIG.name;
186+
megaethTestnetV2Configuration.nativeCurrency =
187+
MEGAETH_TESTNET_V2_CONFIG.nativeCurrency;
188+
189+
const isEndpointExist = megaethTestnetV2Configuration.rpcEndpoints.find(
190+
(rpcEndpoint) =>
191+
rpcEndpoint.url === MEGAETH_TESTNET_V2_CONFIG.rpcEndpoints[0].url,
192+
);
193+
if (!isEndpointExist) {
194+
megaethTestnetV2Configuration.rpcEndpoints.push({
195+
failoverUrls: [],
196+
networkClientId:
197+
MEGAETH_TESTNET_V2_CONFIG.rpcEndpoints[0].networkClientId,
198+
type: RpcEndpointType.Custom,
199+
url: MEGAETH_TESTNET_V2_CONFIG.rpcEndpoints[0].url,
200+
});
201+
megaethTestnetV2Configuration.defaultRpcEndpointIndex =
202+
megaethTestnetV2Configuration.rpcEndpoints.length - 1;
203+
}
178204

179-
// Add the MegaETH Testnet v2 network configuration.
180-
networkConfigurationsByChainId[MEGAETH_TESTNET_V2_CONFIG.chainId] =
181-
MEGAETH_TESTNET_V2_CONFIG;
205+
const isBlockExplorerUrlExist =
206+
megaethTestnetV2Configuration.blockExplorerUrls.find(
207+
(url) => url === MEGAETH_TESTNET_V2_CONFIG.blockExplorerUrls[0],
208+
);
209+
if (!isBlockExplorerUrlExist) {
210+
megaethTestnetV2Configuration.blockExplorerUrls.push(
211+
MEGAETH_TESTNET_V2_CONFIG.blockExplorerUrls[0],
212+
);
213+
megaethTestnetV2Configuration.defaultBlockExplorerUrlIndex =
214+
megaethTestnetV2Configuration.blockExplorerUrls.length - 1;
215+
}
216+
} else {
217+
// Add the MegaETH Testnet v2 network configuration if user doesn't have it.
218+
(networkConfigurationsByChainId as Record<string, NetworkConfiguration>)[
219+
MEGAETH_TESTNET_V2_CONFIG.chainId
220+
] = MEGAETH_TESTNET_V2_CONFIG as NetworkConfiguration;
221+
}
182222

183-
// Add the MegaETH Testnet v2 network configuration to the enabled network map.
184-
eip155NetworkMap[MEGAETH_TESTNET_V2_CONFIG.chainId] = false;
223+
// Add the MegaETH Testnet v2 network configuration to the enabled network map if it doesn't exist.
224+
if (!hasProperty(eip155NetworkMap, MEGAETH_TESTNET_V2_CONFIG.chainId)) {
225+
(eip155NetworkMap as Record<string, boolean>)[
226+
MEGAETH_TESTNET_V2_CONFIG.chainId
227+
] = false;
228+
}
185229

230+
const megaethTestnetV1Configuration =
231+
networkConfigurationsByChainId[MEGAETH_TESTNET_V1_CHAIN_ID];
186232
// If the selected network client id is the old MegaETH Testnet v1,
187233
// then update it to the mainnet
188234
if (

0 commit comments

Comments
 (0)