Skip to content

Instantly share code, notes, and snippets.

@LukeSamkharadze
Last active January 7, 2022 19:18
Show Gist options
  • Save LukeSamkharadze/9b8a3345c62f91f8cfc80e68bd5e242e to your computer and use it in GitHub Desktop.
Save LukeSamkharadze/9b8a3345c62f91f8cfc80e68bd5e242e to your computer and use it in GitHub Desktop.
Redirect network on Metamask
async function setupNetwork() {
const provider = window.ethereum;
if (!provider) {
console.error(
"Can't setup the BSC network on metamask because window.ethereum is undefined"
);
return false;
}
try {
await provider.request({
method: "wallet_addEthereumChain",
params: [
{
chainId: `0x${Number(56).toString(16)}`,
chainName: "Binance Smart Chain Mainnet",
nativeCurrency: {
name: "BNB",
symbol: "BNB",
decimals: 18,
},
rpcUrls: ["https://bsc-dataseed.binance.org/"],
blockExplorerUrls: ["https://bscscan.com"],
},
],
});
return true;
} catch (error) {
console.error("Failed to setup the network on Metamask:", error);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment