Files: 787b951e7dc923dce3d3c88e2d38cb36c75a765b / src / util / getweb3.js
937 bytesRaw
1 | import Web3 from 'web3' |
2 | |
3 | let getWeb3 = new Promise(function(resolve, reject) { |
4 | // Wait for loading completion to avoid race conditions with web3 injection timing. |
5 | window.addEventListener('load', function() { |
6 | var results |
7 | var web3 = window.web3 |
8 | |
9 | // Checking if Web3 has been injected by the browser (Mist/MetaMask) |
10 | // debugger |
11 | if (typeof web3 !== 'undefined') { |
12 | // Use Mist/MetaMask's provider. |
13 | web3 = new Web3(web3.currentProvider) |
14 | |
15 | results = { |
16 | web3: web3 |
17 | } |
18 | |
19 | console.log('Injected web3 detected.'); |
20 | |
21 | resolve(results) |
22 | } else { |
23 | // Fallback to localhost if no web3 injection. |
24 | var provider = new Web3.providers.HttpProvider('http://localhost:8545') |
25 | |
26 | web3 = new Web3(provider) |
27 | |
28 | results = { |
29 | web3: web3 |
30 | } |
31 | |
32 | console.log('No web3 instance injected, using Local web3.'); |
33 | |
34 | resolve(results) |
35 | } |
36 | }) |
37 | }) |
38 | |
39 | export default getWeb3 |
40 |
Built with git-ssb-web