Skip to content

Instantly share code, notes, and snippets.

@heri16
Created June 29, 2021 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heri16/70f0824fdbe9a105439ccf8534b51864 to your computer and use it in GitHub Desktop.
Save heri16/70f0824fdbe9a105439ccf8534b51864 to your computer and use it in GitHub Desktop.
Add network or switch network button for Metamask
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.
}
const provider = useProvider() as JsonRpcProvider | undefined;
const chainId = useChainId(provider);
const goerliChainParam: AddEthereumChainParameter = {
chainId: '0x5',
chainName: 'Goerli Test Network',
nativeCurrency: { name: 'Ethereum', symbol: 'ETH', decimals: 18 },
rpcUrls: ['https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161'],
blockExplorerUrls: ['https://goerli.etherscan.io']
}
const mumbaiChainParam: AddEthereumChainParameter = {
// See: https://docs.matic.network/docs/develop/network-details/network
}
// onClick handler
await provider.send('wallet_addEthereumChain', [goerliChainParam]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment