Skip to content

Instantly share code, notes, and snippets.

@bitpshr
Last active November 8, 2023 16:13
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save bitpshr/076b164843f0414077164fe7fe3278d9 to your computer and use it in GitHub Desktop.
Save bitpshr/076b164843f0414077164fe7fe3278d9 to your computer and use it in GitHub Desktop.
Example provider enablement code for EIP-1102
window.addEventListener('load', async () => {
// Modern dapp browsers...
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
// Request account access if needed
await ethereum.enable();
// Acccounts now exposed
web3.eth.sendTransaction({/* ... */});
} catch (error) {
// User denied account access...
}
}
// Legacy dapp browsers...
else if (window.web3) {
window.web3 = new Web3(web3.currentProvider);
// Acccounts always exposed
web3.eth.sendTransaction({/* ... */});
}
// Non-dapp browsers...
else {
console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
}
});
@Hoangthienbao
Copy link

Hi

@levino
Copy link

levino commented Dec 6, 2018

Broken code. How could line 4 work? ethereum is undefined.

@Ramarti
Copy link

Ramarti commented Dec 12, 2018

Is there a way to check if the user already enabled the dapp and skip calling await ethereum.enable(); again?

@wbt
Copy link

wbt commented Jan 15, 2019

@Ramarti That would be possible with implementation of the feature request in Issue #6022.

@samajammin
Copy link

@levino the code works because ethereum is on the global window object. window.ethereum is the same as a global ethereum variable because in a browser, window is the global context. Same reason window.document is accessible by only referencing document. Open your browser console & give it a try.

@mesqueeb
Copy link

Hi guys!
I was wondering.

What the difference / recommended way is between this:

// Modern dapp browsers...
if (window.ethereum) {
  window.web3 = new Web3(ethereum);
  try {
    // Request account access if needed
    await ethereum.enable();
    // Acccounts now exposed
  }

and this:

// from eip1102
if (window.ethereum) {
  try {
    // Request account access if needed
    const accounts = await ethereum.send('eth_requestAccounts');
    // Accounts now exposed, use them   
  }

@batcoder1
Copy link

batcoder1 commented May 7, 2019

undefined etherum fixed

   if (window.ethereum) {
      window.web3 = new Web3(window.ethereum);
      try {
        // Request account access if needed
        await window.ethereum.enable();
        // Acccounts now exposed
        this.web3.eth.sendTransaction({/* ... */ });
      } catch (error) {
        // User denied account access...
      }
    }
    // Legacy dapp browsers...
    else if (window.web3) {
      window.web3 = new Web3(this.web3.currentProvider);
      // Acccounts always exposed
      this.web3.eth.sendTransaction({/* ... */ });
    }
    // Non-dapp browsers...
    else {
      console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
    }

@JesseHerring33
Copy link

Did it work your work I did what I was supposed to?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment