Skip to content

Instantly share code, notes, and snippets.

@Josh4324
Last active May 10, 2022 14:04
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 Josh4324/03d5f9dec912db4ad8459d2087bc3b57 to your computer and use it in GitHub Desktop.
Save Josh4324/03d5f9dec912db4ad8459d2087bc3b57 to your computer and use it in GitHub Desktop.
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