Add network with existing chain ID via API

I would like to use API method AddEthereumChainParameter but MetaMask will reject request because I want to add a network whose chain ID is Ethereum Mainnet. I want to add a such network because it would have a FlashBot RPC for MEV protection. Maybe there is some workaround what can be done programmatically?

Hello @PPrieditis ! Welcome to MetaMask community !
Please show the piece of code in which you call this method.

1 Like

It is method from MetaMask docs. I didn’t yet actually tried it in code but docs say that request will be rejected because of chain id:

https://docs.metamask.io/guide/rpc-api.html#wallet-addethereumchain

interface AddEthereumChainParameter {
  chainId: string; // A 0x-prefixed hexadecimal string
  chainName: string;
  nativeCurrency: {
    name: string;
    symbol: string; // 2-6 characters long
    decimals: 18;
  };
  rpcUrls: string[];
  blockExplorerUrls?: string[];
  iconUrls?: string[]; // Currently ignored.
}

MetaMask will automatically reject the request under the following circumstances:

  • If the RPC endpoint doesn’t respond to RPC calls. NOTE: Calls are made from the extension’s background page, not the foreground page. If you use an origin allowlist, they will be blocked.
  • If the RPC endpoint returns a different chain ID when eth_chainId is called.
  • If the chain ID corresponds to any default MetaMask chains.
2 Likes

As stated in the documentation, you will not be able to do this on the Ethereum Mainnet.
But you can use Flashbots RPC on your own networks or test Ethereum networks.

2 Likes

Yes, MetaMask docs and ethereum.org EIP-3085 doesn’t want to allow to add a second network with the same chain id but that doesn’t make any sense if user wants to use MEV protected RPC like Flashbots.

The only workaround I see is to add a network with the wrong RPC, chain id and ask a user to change it to the correct ones manually. That is a terrible UX…

do you think it will work correctly?

2 Likes

It is possible to have multiple networks with the same chain id. The only limitation is that it isn’t possible to create a network with an existing chain id via API. It will work corectlly but there is a huge difference in user experience if a network is added automatically to the MetaMask or user has to go in settings and manually change parameters.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.