-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate Base network RPC from https://mainnet.base.org to base-…
…mainnet.infura.io
- Loading branch information
Showing
5 changed files
with
208 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { cloneDeep } from 'lodash'; | ||
import { migrate, version } from './135'; | ||
import { infuraProjectId } from '../../../shared/constants/network'; | ||
import { NetworkState } from '@metamask/network-controller'; | ||
|
||
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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. | ||
* @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.rpcEndpoints; | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters