Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

How to add a new network that is not already listed? #307

Open
vivek-consensys opened this issue Apr 17, 2023 · 4 comments
Open

How to add a new network that is not already listed? #307

vivek-consensys opened this issue Apr 17, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@vivek-consensys
Copy link

Is your feature request related to a problem? Please describe.
I would like to add a new network when setting up Metamask

Describe the solution you'd like
I would like to add an additional step in adding a network in the metamask extension, where you will have to include:-
Network name, RPC URL, Chain ID, Currency Symbol and block explorer URL

Describe alternatives you've considered
There is not much alternatives you can try, metaMask.acceptAddNetwork only works when there is a prompt within metamask.

@vivek-consensys vivek-consensys added the enhancement New feature or request label Apr 17, 2023
@ChainSafe ChainSafe deleted a comment Apr 18, 2023
@Vxatz
Copy link

Vxatz commented Apr 19, 2023

Seems like there had previously been a .addNetwork() method, but for some reason, it was deprecated.

Is there an alternative?

@vivek-consensys
Copy link
Author

@Vxatz there doesn't seem to be an alternative from the API documentation

@BeroBurny
Copy link
Contributor

Seems like there had previously been a .addNetwork() method, but for some reason, it was deprecated.

Historically we have a lot of issues with adding a network over a MetaMask UI, and this feature is actually not so important for dapp dev as he needs to use EIP-3085 for better UX

Is there an alternative?

For an alternative, you can reference this test

it("should add network and switch", async function (this: TestContext) {
const addNetworkPromise = testPage.evaluate(addNetwork);
await metaMask.acceptAddNetwork();
const res = await addNetworkPromise;
expect(res).to.equal(true);
});

that simple usage evaluation with EIP-3085

export const addNetwork = async (): Promise<boolean> => {
const addNetworkRequest = window.ethereum.request<boolean>({
method: "wallet_addEthereumChain",
params: [
{
chainId: "0xa",
chainName: "Optimism",
nativeCurrency: {
name: "ETH",
symbol: "ETH", // 2-6 characters long
decimals: 18,
},
rpcUrls: ["https://mainnet.optimism.io"],
},
],
});
return new Promise((resolve) => {
addNetworkRequest
.then(() => {
resolve(true);
})
.catch(() => {
resolve(false);
});
});
};

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants
@BeroBurny @Vxatz @vivek-consensys and others