Skip to content

Conversation

@seaona
Copy link
Member

@seaona seaona commented Aug 19, 2025

Description

This PR adds a network picker to be able to switch networks from the dapp.

  • If the network is already in the wallet, and we gave permissions to that network, it switches networks.
  • If the network is already in the wallet, but we didn't give permissions to that network, it triggers the MM permissions dialog and then the network is switched
  • If the network is not present in the wallet, we throw an error in the UI.

Future work:

  • support addEtehreumChain for non-added networks, so we can add that network and then switch to it

Screenshots

Main networks

Screenshot from 2025-08-19 11-38-32

Test networks

Screenshot from 2025-08-19 11-38-35

Happy Path

net-picker.mp4

Error Handling

network-picker-test-dapp.mp4

Manual Testing Steps

Flow 1: Connected + Permissions

  1. Connect to the test dapp
  2. Give permissions to a cople of networks
  3. Click on Network picker
  4. See modal with network list appears
  5. Select another network that you currently have on the Wallet and you gave permissions to
  6. See network is switch and chain id is switch
  7. Trigger a tx/signature, see your updated network

Flow 2: Connected without Permissions

  1. Connect to the test dapp
  2. Give permissions to just 1 network
  3. Click on Network picker
  4. See modal with network list appears
  5. Select another network that you currently have on the Wallet and you didn't give permissions to
  6. See MM dialog appears with the Permissions screen
  7. Accept
  8. See network is switch and chain id is switch
  9. Trigger a tx/signature, see your updated network

Flow 3: Connected without Network in the Wallet

  1. Click on the Network picker
  2. Select a network that you don't currently have on the wallet
  3. See error on the UI and nothing happens

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@seaona seaona self-assigned this Aug 19, 2025
cursor[bot]

This comment was marked as outdated.

const openButton = document.getElementById('openNetworkPicker');
const closeButton = modal.querySelector('.network-modal-close');

// Populate network lists
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method name is pretty self descriptive 👍🏾
so I'd prefer we remove this comment to avoid redundancy

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great point, I've removed all of them 🙏

}
});

// Initial update
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this one, and even the above comment for the event listeners I would remove, it's all pretty straight forward to follow

Comment on lines 193 to 212
if (
globalContext.chainIdInt !== undefined &&
globalContext.chainIdInt !== null
) {
const network = NETWORKS.find((n) => {
const networkChainId = parseInt(n.chainId, 16);
return networkChainId === globalContext.chainIdInt;
});

if (network) {
currentNetworkName.textContent = `Current Network: ${network.name}`;
} else {
// Fallback to chain ID if network not found
currentNetworkName.textContent = `Current Network: Chain ID 0x${globalContext.chainIdInt.toString(
16,
)}`;
}
} else {
currentNetworkName.textContent = 'Current Network: Not Connected';
}
Copy link
Member

@ffmcgee725 ffmcgee725 Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (
globalContext.chainIdInt !== undefined &&
globalContext.chainIdInt !== null
) {
const network = NETWORKS.find((n) => {
const networkChainId = parseInt(n.chainId, 16);
return networkChainId === globalContext.chainIdInt;
});
if (network) {
currentNetworkName.textContent = `Current Network: ${network.name}`;
} else {
// Fallback to chain ID if network not found
currentNetworkName.textContent = `Current Network: Chain ID 0x${globalContext.chainIdInt.toString(
16,
)}`;
}
} else {
currentNetworkName.textContent = 'Current Network: Not Connected';
}
if (!globalContext.chainIdInt) {
currentNetworkName.textContent = 'Current Network: Not Connected';
return;
}
const network = NETWORKS.find((n) => {
const networkChainId = parseInt(n.chainId, 16);
return networkChainId === globalContext.chainIdInt;
});
// Fallback to chain ID if network not found
currentNetworkName.textContent = network
? `Current Network: ${network.name}`
: `Current Network: Chain ID 0x${globalContext.chainIdInt.toString(16)}`;

I believe the cognitive complexity of this function is reduced if we use an early return, instead of having nested if..else clauses, what do you think ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoa much better 😍 Added the changes 🙏 thank you vmuch!

Copy link
Member

@ffmcgee725 ffmcgee725 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀

@seaona seaona merged commit 96b4d07 into main Aug 19, 2025
9 checks passed
@seaona seaona deleted the network-picker branch August 19, 2025 16:20
@seaona seaona mentioned this pull request Aug 19, 2025
Copy link

@Thatguysdino Thatguysdino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


@Thatguysdino
Copy link

We the people, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants