Skip to content

Commit

Permalink
feat: migrate Base network RPC from https://mainnet.base.org to base-…
Browse files Browse the repository at this point in the history
…mainnet.infura.io
  • Loading branch information
salimtb committed Dec 5, 2024
1 parent 5ab288b commit 92a09ff
Show file tree
Hide file tree
Showing 21 changed files with 319 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/scripts/controllers/preferences-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ describe('preferences controller', () => {
isRedesignedConfirmationsDeveloperEnabled: false,
showConfirmationAdvancedDetails: false,
showMultiRpcModal: false,
showBaseNetworkToast: false,
showNativeTokenAsMainBalance: false,
tokenSortConfig: {
key: 'tokenFiatAmount',
Expand Down Expand Up @@ -774,6 +775,7 @@ describe('preferences controller', () => {
isRedesignedConfirmationsDeveloperEnabled: false,
showConfirmationAdvancedDetails: true,
showMultiRpcModal: false,
showBaseNetworkToast: false,
showNativeTokenAsMainBalance: false,
tokenSortConfig: {
key: 'tokenFiatAmount',
Expand Down
2 changes: 2 additions & 0 deletions app/scripts/controllers/preferences-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export type Preferences = {
redesignedTransactionsEnabled: boolean;
featureNotificationsEnabled: boolean;
showMultiRpcModal: boolean;
showBaseNetworkToast: boolean;
privacyMode: boolean;
isRedesignedConfirmationsDeveloperEnabled: boolean;
showConfirmationAdvancedDetails: boolean;
Expand Down Expand Up @@ -227,6 +228,7 @@ export const getDefaultPreferencesControllerState =
isRedesignedConfirmationsDeveloperEnabled: false,
showConfirmationAdvancedDetails: false,
showMultiRpcModal: false,
showBaseNetworkToast: false,
privacyMode: false,
shouldShowAggregatedBalancePopover: true, // by default user should see popover;
tokenSortConfig: {
Expand Down
1 change: 1 addition & 0 deletions app/scripts/lib/backup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ const jsonData = JSON.stringify({
smartTransactionsOptInStatus: true,
useNativeCurrencyAsPrimaryCurrency: true,
showMultiRpcModal: false,
showBaseNetworkToast: false,
},
ipfsGateway: 'dweb.link',
ledgerTransportType: 'webhid',
Expand Down
131 changes: 131 additions & 0 deletions app/scripts/migrations/135.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { cloneDeep } from 'lodash';
import { NetworkState } from '@metamask/network-controller';
import { infuraProjectId } from '../../../shared/constants/network';
import { migrate, version } from './135';

const oldVersion = 134;
const BASE_CHAIN_ID = '0x2105';

describe('migration #135', () => {
it('updates the version metadata', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {},
};

const newStorage = await migrate(cloneDeep(oldStorage));

expect(newStorage.meta).toStrictEqual({ version });
});

describe('Base Network Migration', () => {
it('does nothing if networkConfigurationsByChainId is not in state', async () => {
const oldState = {
OtherController: {},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: cloneDeep(oldState),
});

expect(transformedState.data).toEqual(oldState);
});

it('replaces "https://mainnet.base.org" with "base-mainnet.infura.io" when it exists', async () => {
const oldState = {
NetworkController: {
networkConfigurationsByChainId: {
[BASE_CHAIN_ID]: {
rpcEndpoints: [
{
url: 'https://mainnet.base.org',
type: 'custom',
networkClientId: 'base-mainnet',
},
],
defaultRpcEndpointIndex: 0,
},
},
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: cloneDeep(oldState),
});

const updatedNetworkController = transformedState.data
.NetworkController as NetworkState;

expect(
updatedNetworkController.networkConfigurationsByChainId[BASE_CHAIN_ID]
.rpcEndpoints[0].url,
).toEqual(`https://base-mainnet.infura.io/v3/${infuraProjectId}`);
});

it('does not modify RPC endpoints other than "https://mainnet.base.org"', async () => {
const oldState = {
NetworkController: {
networkConfigurationsByChainId: {
[BASE_CHAIN_ID]: {
rpcEndpoints: [
{
url: 'https://other.rpc',
type: 'custom',
networkClientId: 'other-mainnet',
},
],
defaultRpcEndpointIndex: 0,
},
},
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: cloneDeep(oldState),
});

const updatedNetworkController = transformedState.data
.NetworkController as NetworkState;

expect(
updatedNetworkController.networkConfigurationsByChainId[BASE_CHAIN_ID]
.rpcEndpoints[0].url,
).toEqual('https://other.rpc');
});

it('keeps defaultRpcEndpointIndex unchanged when replacing "https://mainnet.base.org"', async () => {
const oldState = {
NetworkController: {
networkConfigurationsByChainId: {
[BASE_CHAIN_ID]: {
rpcEndpoints: [
{
url: 'https://mainnet.base.org',
type: 'custom',
networkClientId: 'base-mainnet',
},
],
defaultRpcEndpointIndex: 0,
},
},
},
};

const transformedState = await migrate({
meta: { version: oldVersion },
data: cloneDeep(oldState),
});

const updatedNetworkController = transformedState.data
.NetworkController as NetworkState;

expect(
updatedNetworkController.networkConfigurationsByChainId[BASE_CHAIN_ID]
.defaultRpcEndpointIndex,
).toEqual(0);
});
});
});
88 changes: 88 additions & 0 deletions app/scripts/migrations/135.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { hasProperty, isObject } from '@metamask/utils';
import { cloneDeep } from 'lodash';
import { infuraProjectId } from '../../../shared/constants/network';

export const version = 135;
const BASE_CHAIN_ID = '0x2105';

/**
* Replace all occurrences of "https://mainnet.base.org" with
* "https://base-mainnet.infura.io/v3/${infuraProjectId}" in the Base network configuration.
*
* @param originalVersionedData - Versioned MetaMask extension state, exactly
* what we persist to dist.
* @param originalVersionedData.meta - State metadata.
* @param originalVersionedData.meta.version - The current state version.
* @param originalVersionedData.data - The persisted MetaMask state, keyed by
* controller.
* @returns Updated versioned MetaMask extension state.
*/
export async function migrate(originalVersionedData: {
meta: { version: number };
data: Record<string, unknown>;
}) {
const versionedData = cloneDeep(originalVersionedData);
versionedData.meta.version = version;
versionedData.data = transformState(versionedData.data);
return versionedData;
}

function transformState(state: Record<string, unknown>) {
if (
hasProperty(state, 'NetworkController') &&
isObject(state.NetworkController) &&
hasProperty(state.NetworkController, 'networkConfigurationsByChainId') &&
isObject(state.NetworkController.networkConfigurationsByChainId)
) {
const { networkConfigurationsByChainId } = state.NetworkController;

// Check for Base network configuration (chainId 8453 / 0x2105)
const baseNetworkConfig = networkConfigurationsByChainId[BASE_CHAIN_ID];
if (isObject(baseNetworkConfig)) {
const { rpcEndpoints } = baseNetworkConfig;

if (Array.isArray(rpcEndpoints)) {
// Find the first occurrence of "https://mainnet.base.org"
// rpc URL are Unique so we can use findIndex
const index = rpcEndpoints.findIndex(
(endpoint) =>
isObject(endpoint) && endpoint.url === 'https://mainnet.base.org',
);

if (index !== -1) {
// Set `showBaseNetworkToast` based on if the Base network configuration has been updated
if (
hasProperty(state, 'PreferencesController') &&
isObject(state.PreferencesController) &&
hasProperty(state.PreferencesController, 'preferences') &&
isObject(state.PreferencesController.preferences)
) {
state.PreferencesController.preferences.showBaseNetworkToast = true;
}

// Replace the URL with the new Infura URL
rpcEndpoints[index] = {
...rpcEndpoints[index],
url: `https://base-mainnet.infura.io/v3/${infuraProjectId}`,
};

// Update the configuration
networkConfigurationsByChainId[BASE_CHAIN_ID] = {
...baseNetworkConfig,
rpcEndpoints,
};

return {
...state,
NetworkController: {
...state.NetworkController,
networkConfigurationsByChainId,
},
};
}
}
}
}

return state;
}
1 change: 1 addition & 0 deletions app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ const migrations = [
require('./132'),
require('./133'),
require('./134'),
require('./135'),
];

export default migrations;
4 changes: 2 additions & 2 deletions shared/constants/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ describe('NetworkConstants', () => {
expect(zksyncEraRpc.rpcEndpoints[0].url).not.toContain('infura.io');
});

it('base entry should not use Infura', () => {
it('base entry should use Infura', () => {
const [baseRpc] = FEATURED_RPCS.filter(
(rpc) => rpc.chainId === CHAIN_IDS.BASE,
);
expect(baseRpc.rpcEndpoints[0].url).not.toContain('infura.io');
expect(baseRpc.rpcEndpoints[0].url).toContain('infura.io');
});
});
});
2 changes: 1 addition & 1 deletion shared/constants/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ export const FEATURED_RPCS: AddNetworkFields[] = [
nativeCurrency: CURRENCY_SYMBOLS.ETH,
rpcEndpoints: [
{
url: `https://mainnet.base.org`,
url: `https://base-mainnet.infura.io/v3/${infuraProjectId}`,
type: RpcEndpointType.Custom,
},
],
Expand Down
1 change: 1 addition & 0 deletions test/e2e/default-fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ function defaultFixture(inputChainId = CHAIN_IDS.LOCALHOST) {
showNativeTokenAsMainBalance: true,
petnamesEnabled: true,
showMultiRpcModal: false,
showBaseNetworkToast: false,
isRedesignedConfirmationsDeveloperEnabled: false,
showConfirmationAdvancedDetails: false,
tokenSortConfig: {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/fixture-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function onboardingFixture() {
showNativeTokenAsMainBalance: true,
petnamesEnabled: true,
showMultiRpcModal: false,
showBaseNetworkToast: false,
isRedesignedConfirmationsDeveloperEnabled: false,
showConfirmationAdvancedDetails: false,
tokenSortConfig: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
"showNativeTokenAsMainBalance": true,
"petnamesEnabled": true,
"showMultiRpcModal": "boolean",
"showBaseNetworkToast": "boolean",
"isRedesignedConfirmationsDeveloperEnabled": "boolean",
"redesignedConfirmationsEnabled": true,
"redesignedTransactionsEnabled": "boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"showNativeTokenAsMainBalance": true,
"petnamesEnabled": true,
"showMultiRpcModal": "boolean",
"showBaseNetworkToast": "boolean",
"isRedesignedConfirmationsDeveloperEnabled": "boolean",
"tokenSortConfig": "object",
"tokenNetworkFilter": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"tokenSortConfig": "object",
"tokenNetworkFilter": {},
"showMultiRpcModal": "boolean",
"showBaseNetworkToast": "boolean",
"shouldShowAggregatedBalancePopover": "boolean"
},
"selectedAddress": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"tokenSortConfig": "object",
"tokenNetworkFilter": {},
"showMultiRpcModal": "boolean",
"showBaseNetworkToast": "boolean",
"shouldShowAggregatedBalancePopover": "boolean"
},
"selectedAddress": "string",
Expand Down
1 change: 1 addition & 0 deletions test/integration/data/integration-init-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@
"petnamesEnabled": false,
"showConfirmationAdvancedDetails": false,
"showMultiRpcModal": false,
"showBaseNetworkToast": false,
"tokenNetworkFilter": {}
},
"preventPollingOnNetworkRestart": true,
Expand Down
1 change: 1 addition & 0 deletions test/jest/mock-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export const createSwapsMockStore = () => {
smartTransactionsOptInStatus: true,
tokenNetworkFilter: {},
showMultiRpcModal: false,
showBaseNetworkToast: false,
},
transactions: [
{
Expand Down
Loading

0 comments on commit 92a09ff

Please sign in to comment.