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 3, 2024
1 parent 5ab288b commit 0ee11f7
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 3 deletions.
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);
});
});
});
78 changes: 78 additions & 0 deletions app/scripts/migrations/135.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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) {
// 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

0 comments on commit 0ee11f7

Please sign in to comment.