Skip to content

Instantly share code, notes, and snippets.

@Josh4324
Last active May 10, 2022 13:46
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/6fe82ab8dd92d24cefb8c1843b904ad4 to your computer and use it in GitHub Desktop.
Save Josh4324/6fe82ab8dd92d24cefb8c1843b904ad4 to your computer and use it in GitHub Desktop.
const web3ModalRef = useRef();
let provider;
let web3Modal;
const providerOptions = {
walletconnect: {
package: WalletConnectProvider, // required
options: {
infuraId: "", // required
},
},
binancechainwallet: {
package: true,
},
};
const getProviderOrSigner = async (needSigner = false) => {
web3Modal = new Web3Modal({
network: "bsc", // optional
cacheProvider: true, // optional
providerOptions, // required
});
provider = await web3Modal.connect();
const web3Provider = new providers.Web3Provider(provider);
const { chainId } = await web3Provider.getNetwork();
// rinkbey - 4
// bsc - 97
// polygon - 80001
if (chainId !== 4) {
window.alert("Change the network to Rinkeby");
throw new Error("Change network to Rinkeby");
}
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