Last active
May 10, 2022 13:46
-
-
Save Josh4324/6fe82ab8dd92d24cefb8c1843b904ad4 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 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