Skip to content

Commit

Permalink
feat: add new default networks (#11926)
Browse files Browse the repository at this point in the history
<!--
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 ensures that a predefined list of popular networks (Arbitrum,
BSC, Base, Optimism, and Polygon) is automatically added for new users
only during the app startup process. The networks are added using
NetworkController.addNetwork within the useEffect hook inside the app's
initialization flow.
<!--
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?
-->

## **Related issues**

Fixes:

## **Manual testing steps**

1. Do a new fresh install app
2. Import/create your wallet
3. check the list of added networks

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**
![Screenshot 2024-10-21 at 20 02
38](https://github.com/user-attachments/assets/c24f3a63-a315-4cb7-99f3-43a49ba0756f)

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->
![Screenshot 2024-10-21 at 19 56
59](https://github.com/user-attachments/assets/e5e785a2-374c-4f94-85d2-249cabede8a2)


## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/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.
  • Loading branch information
salimtb authored Oct 28, 2024
1 parent 9b8599e commit e7595b2
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions app/components/Nav/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ import OptionsSheet from '../../UI/SelectOptionSheet/OptionsSheet';
import FoxLoader from '../../../components/UI/FoxLoader';
import { AppStateEventProcessor } from '../../../core/AppStateEventListener';
import MultiRpcModal from '../../../components/Views/MultiRpcModal/MultiRpcModal';
import Engine from '../../../core/Engine';
import { CHAIN_IDS } from '@metamask/transaction-controller';
import { PopularList } from '../../../util/networks/customNetworks';
import { RpcEndpointType } from '@metamask/network-controller';
import { trace, TraceName, TraceOperation } from '../../../util/trace';

const clearStackNavigatorOptions = {
Expand Down Expand Up @@ -762,6 +766,46 @@ const App = (props) => {
useEffect(() => {
async function startApp() {
const existingUser = await StorageWrapper.getItem(EXISTING_USER);
if (!existingUser) {
// List of chainIds to add (as hex strings)
const chainIdsToAdd = [
CHAIN_IDS.ARBITRUM,
CHAIN_IDS.BASE,
CHAIN_IDS.BSC,
CHAIN_IDS.OPTIMISM,
CHAIN_IDS.POLYGON,
];

// Filter the PopularList to get only the specified networks based on chainId
const selectedNetworks = PopularList.filter((network) =>
chainIdsToAdd.includes(network.chainId),
);
const { NetworkController } = Engine.context;

// Loop through each selected network and call NetworkController.addNetwork
for (const network of selectedNetworks) {
try {
await NetworkController.addNetwork({
chainId: network.chainId,
blockExplorerUrls: [network.rpcPrefs.blockExplorerUrl],
defaultRpcEndpointIndex: 0,
defaultBlockExplorerUrlIndex: 0,
name: network.nickname,
nativeCurrency: network.ticker,
rpcEndpoints: [
{
url: network.rpcUrl,
name: network.nickname,
type: RpcEndpointType.Custom,
},
],
});
} catch (error) {
Logger.error(error);
}
}
}

try {
const currentVersion = getVersion();
const savedVersion = await StorageWrapper.getItem(CURRENT_APP_VERSION);
Expand Down

0 comments on commit e7595b2

Please sign in to comment.