Last active
January 7, 2022 19:18
-
-
Save LukeSamkharadze/9b8a3345c62f91f8cfc80e68bd5e242e to your computer and use it in GitHub Desktop.
Redirect network on Metamask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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