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

Incorporate Infura networks into networkConfigurations #4189

Open
mcmire opened this issue Apr 18, 2024 · 3 comments · May be fixed by #4286
Open

Incorporate Infura networks into networkConfigurations #4189

mcmire opened this issue Apr 18, 2024 · 3 comments · May be fixed by #4286
Assignees

Comments

@mcmire
Copy link
Contributor

mcmire commented Apr 18, 2024

Today we have two types of networks that are accessible via the network controller: built-in Infura networks and RPC endpoints that the user has added. The former list is hardcoded and the latter list is tracked via networkConfigurations. Because of this bifurcation, when the network controller creates clients for these networks, it needs to use one way for built-in networks and another way for custom networks. For this reason, the configuration object that is used to create an Infura network client is different from the one used to create a custom network client. This creates unnecessary complexity.

Moving built-in networks into networkConfigurations would not completely remove the complexity, but it would help simplify some of the boilerplate code in NetworkController, and is a prerequisite for #1279.

@mcmire
Copy link
Contributor Author

mcmire commented Apr 22, 2024

Here is some more context for this ticket as well as ideas for how we could implement this ticket.

Currently, NetworkConfigurations (the type of the networkConfigurations property) is defined as Record<string, NetworkConfiguration & { id: string }>.

And NetworkConfiguration is defined as:

{
  rpcUrl: string;
  chainId: Hex;
  ticker: string;
  nickname?: string;
  rpcPrefs?: {
    blockExplorerUrl: string;
  };
}

All of these properties in NetworkConfiguration correspond to the fields that you can provide when you add a new network in MetaMask, and ultimately, they are used to create a custom network client like this:

createAutoManagedNetworkClient({
  type: NetworkClientType.Custom,
  chainId: networkConfiguration.chainId,
  rpcUrl: networkConfiguration.rpcUrl,
  ticker: networkConfiguration.ticker,
})

However, an Infura network configuration is different, because at the moment it is internal and isn't connected to the UI. All we need is the name of the network we want to connect to; once we have that, we can derive chainId and rpcUrl. This is demonstrated by how the Infura network client is created:

createAutoManagedNetworkClient({
  type: NetworkClientType.Infura,
  network,
  infuraProjectId: this.#infuraProjectId,
  chainId: BUILT_IN_NETWORKS[network].chainId,
  ticker: BUILT_IN_NETWORKS[network].ticker,
})

So, we could introduce an Infura network configuration like so:

{
  network: InfuraNetworkType;
}

Putting that together with the existing NetworkConfiguration type, we could have something like:

type CustomNetworkConfiguration = {
  type: NetworkClientType.Custom;
  rpcUrl: string;
  chainId: Hex;
  ticker: string;
  nickname?: string;
  rpcPrefs?: {
    blockExplorerUrl: string;
  };
};

type InfuraNetworkConfiguration = {
  type: NetworkClientType.Infura;
  network: InfuraNetworkType;
};

type NetworkConfiguration = CustomNetworkConfiguration | InfuraNetworkConfiguration;

type NetworkConfigurations = Record<string, NetworkConfiguration & { id: string }>;

Once we have this, we should be able to refactor the existing code which creates the network clients. See #createAutoManagedNetworkClientRegistry, #buildIdentifiedInfuraNetworkClientConfigurations, and #buildIdentifiedCustomNetworkClientConfigurations for more.

@mcmire
Copy link
Contributor Author

mcmire commented Apr 29, 2024

Moving this back to Product Backlog because it's dependent on the investigatory work I'm doing for #3793.

@mcmire mcmire linked a pull request May 17, 2024 that will close this issue
3 tasks
@mcmire
Copy link
Contributor Author

mcmire commented May 22, 2024

This ticket is being addressed by #4286.

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

Successfully merging a pull request may close this issue.

1 participant