-
Notifications
You must be signed in to change notification settings - Fork 0
/
web3.js
29 lines (25 loc) · 786 Bytes
/
web3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import Web3 from 'web3';
const getWeb3 = () => new Promise((resolve) => {
window.addEventListener('load', () => {
let currentWeb3;
if (window.ethereum) {
currentWeb3 = new Web3(window.ethereum);
try {
// Request account access if needed
window.ethereum.enable();
// Acccounts now exposed
resolve(currentWeb3);
} catch (error) {
// User denied account access...
alert('Please allow access for the app to work');
}
} else if (window.web3) {
window.web3 = new Web3(web3.currentProvider);
// Acccounts always exposed
resolve(currentWeb3);
} else {
console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
}
});
});
export default getWeb3;