Skip to content

Instantly share code, notes, and snippets.

@wissalHaji
Created December 27, 2020 19:19
Show Gist options
  • Save wissalHaji/865d6b586d40651cc5ad54981d66b7f2 to your computer and use it in GitHub Desktop.
Save wissalHaji/865d6b586d40651cc5ad54981d66b7f2 to your computer and use it in GitHub Desktop.
const getWeb3 = () => {
return new Promise((resolve, reject) => {
window.addEventListener("load", async () => {
if (window.ethereum) {
const web3 = new Web3(window.ethereum);
try {
// ask user permission to access his accounts
await window.ethereum.request({ method: "eth_requestAccounts" });
resolve(web3);
} catch (error) {
reject(error);
}
} else {
reject("Must install MetaMask");
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment