Created
March 3, 2019 08:53
-
-
Save mr-fool/65d2528e2c2f333d358b2ac00330f701 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Web3 from 'web3'; | |
const web3 = new Web3(window.web3.currentProvider); | |
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!'); | |
} | |
}); | |
//https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8 | |
export default web3; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment