Last active
May 10, 2022 14:04
-
-
Save Josh4324/03d5f9dec912db4ad8459d2087bc3b57 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: "polygon", // 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 !== 80001) { | |
window.alert("Change the network to Polygon Mumbai Testnet"); | |
throw new Error("Change network to Polygon Mumbai 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