-
-
Save Josh4324/d9ca7b591375d7fe3399a38eea1bfd8b to your computer and use it in GitHub Desktop.
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
const getProviderOrSigner = async (needSigner = false) => { | |
// Connect to Metamask | |
// Since we store `web3Modal` as a reference, we need to access the `current` value to get access to the underlying object | |
web3Modal = new Web3Modal({ | |
network: "bsc", // optional | |
cacheProvider: true, // optional | |
providerOptions, // required | |
}); | |
provider = await web3Modal.connect(); | |
const web3Provider = new providers.Web3Provider(provider); | |
// If user is not connected to the Mainnet network, let them know and throw an error | |
const { chainId } = await web3Provider.getNetwork(); | |
// rinkbey - 4 | |
// bsc - 97 | |
// polygon - 80001 | |
if (chainId !== 97) { | |
window.alert("Change the network to BSC Testnet"); | |
throw new Error("Change network to BSC Testnet"); | |
} | |
if (needSigner) { | |
const signer = web3Provider.getSigner(); | |
return signer; | |
} | |
return web3Provider; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment