fix: add default enable networks state and null guards#40402
fix: add default enable networks state and null guards#40402Prithpal-Sooriya merged 8 commits intomainfrom
Conversation
The getEnabledNetworks selector returns state.metamask.enabledNetworkMap
which can be undefined if the state hasn't been properly initialized.
Multiple selectors were accessing properties on this potentially undefined
object, causing 'TypeError: Cannot read properties of undefined (reading eip155)'.
This fix adds optional chaining (?.) and nullish coalescing (?? {}) to all
affected selectors to gracefully handle the undefined case:
- getEnabledNetworksByNamespace
- getAllEnabledNetworksForAllNamespaces
- selectEnabledNetworksAsCaipChainIds
- selectNonEvmChainIds
- getEnabledChainIds
- getEnabledNetworkClientIds
- selectAnyEnabledNetworksAreAvailable
- selectFirstUnavailableEvmNetwork
- selectAllEnabledNetworkClientIds
Fixes: METAMASK-XPHD
Co-authored-by: Prithpal Sooriya <prithpal.sooriya@gmail.com>
|
Cursor Agent can help with this pull request. Just |
Builds ready [c364841]
⚡ Performance Benchmarks (1448 ± 114 ms)
🌐 Dapp Page Load BenchmarksCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs
|
| (allEnabledNetworks, defaultNetworkClientIdsByChainId) => { | ||
| const chainIds = Object.entries( | ||
| allEnabledNetworks[KnownCaipNamespace.Eip155], | ||
| allEnabledNetworks?.[KnownCaipNamespace.Eip155] ?? {}, |
There was a problem hiding this comment.
Nice this is valid.
Per review feedback, moved the null guard to the source selector
(getEnabledNetworks) instead of adding individual guards to each
downstream selector. This is cleaner and ensures all current and
future consumers are automatically protected.
The change: state.metamask.enabledNetworkMap ?? {}
This prevents TypeError when enabledNetworkMap is undefined during
state initialization.
Co-authored-by: Prithpal Sooriya <prithpal.sooriya@gmail.com>
Co-authored-by: Prithpal Sooriya <prithpal.sooriya@gmail.com>
Co-authored-by: Prithpal Sooriya <prithpal.sooriya@gmail.com>
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
… in getEnabledNetworks selector This change centralizes the default value for enabled networks, preventing potential TypeErrors when accessing the state. The selector now returns an empty object if the enabledNetworkMap is undefined, enhancing stability during state initialization.
Builds ready [d741d0e]
⚡ Performance Benchmarks
🌐 Dapp Page Load BenchmarksCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs
|
Builds ready [cde6708]
⚡ Performance Benchmarks
🌐 Dapp Page Load BenchmarksCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs
|
|
Builds ready [499a629]
⚡ Performance Benchmarks
🌐 Dapp Page Load BenchmarksCurrent Commit: 📄 Localhost MetaMask Test DappSamples: 100 Summary
📈 Detailed Results
Bundle size diffs
|



Description
The redux state we are picking might not exist if accessed before state gets rehydrated.
Added a default state to enabled networks, and also a null guard for where we initially select from this state
Changelog
CHANGELOG entry: fix: add enable networks null guards
Related issues
Fixes: #35232 https://consensyssoftware.atlassian.net/browse/ASSETS-2021
Manual testing steps
Screenshots/Recordings
Before
After
Pre-merge author checklist
Pre-merge reviewer checklist
Note
Low Risk
Low risk: small selector hardening to avoid crashes when
enabledNetworkMapor itseip155entry is undefined; behavior only changes in uninitialized/rehydration edge cases.Overview
Prevents selector crashes when Redux state hasn’t rehydrated by defaulting
getEnabledNetworksto an empty map and guardingselectAllEnabledNetworkClientIdsagainst a missingeip155namespace entry.Written by Cursor Bugbot for commit 499a629. This will update automatically on new commits. Configure here.