[NetworkController] Give consumers a way to get the selected chain ID purely from state
- Dominant language
- TypeScript
- Stars
- 413
- Forks
- 308
- Avg merge
- 1d 4h
- Merged PRs (30d)
- 253
Description
## Problem
Both the extension and mobile apps operate directly on controller state:
- The extension picks properties from state via selectors and uses them within Redux actions
- The mobile app reads state directly from controller state
In some cases, the client needs to get the chain ID of the currently selected global network, and all they have is state (they do not have access to a controller instance or a messenger). While the network client ID is easy to get purely from state — it's just stored as `selectedNetworkClientId` on NetworkController state` — the chain ID is not. The client must do the following:
``` typescript
function getCurrentChainId(networkControllerState: NetworkControllerState) {
const selectedNetworkConfiguration = Object.values(networkControllerState)
.networkConfigurationsByChainId
.find((networkConfiguration) => {
return networkConfiguration.rpcEndpoints.some((rpcEndpoint) => {
return rpcEndpoint.networkClientId === networkControllerState.selectedNetworkClientId;
});
});
if (selectedNetworkConfiguration === undefined) {
throw new Error("Could not find network configuration for selected network client ID '${networkControllerState.selectedNetworkClientId}'");
}
return networkConfiguration.chainId;
}
```
This is cumbersome, and I imagine engineers do not want to do this (or will figure out a shortcut which may compromise type safety).
## Acceptance Criteria
- Consumers are able to access the chain ID of the currently selected network in a type-safe way without needing to iterate over `networkConfigurationsByChainId`.
## Considerations
We could add a new state property, `selectedChainId`, which would be set whenever `selectedNetworkClientId` is set.
This would not only satisfy the requirements but would also allow the consumer to easily look up the network configuration for the currently selected network purely from state if they wanted:
``` typescript
networkControllerState.networkConfigurationsByChainId[networkControllerState.selectedChainId]
```
which is not currently possible just using `selectedNetworkClientId`.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by locating the NetworkController state definition and every path that sets or reads selectedNetworkClientId. Trace how networkConfigurationsByChainId and rpcEndpoints identify the selected chain, then evaluate a type-safe state access path against the acceptance criteria. Done means consumers can obtain the selected chain ID without iterating over networkConfigurationsByChainId or using a controller instance.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100