Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NetworkController] Give consumers a way to get the selected chain ID purely from state #4892

Open
mcmire opened this issue Nov 1, 2024 · 0 comments

Comments

@mcmire
Copy link
Contributor

mcmire commented Nov 1, 2024

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:

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:

networkControllerState.networkConfigurationsByChainId[networkControllerState.selectedChainId]

which is not currently possible just using selectedNetworkClientId.

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

No branches or pull requests

1 participant