diff --git a/src/js/app.js b/src/js/app.js index bab4753..88752a6 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -6,17 +6,31 @@ App = { return App.initWeb3(); }, - initWeb3: function() { - // Initialize web3 and set the provider to the testRPC. - if (typeof web3 !== 'undefined') { - App.web3Provider = web3.currentProvider; - web3 = new Web3(web3.currentProvider); - } else { - // set the provider you want from Web3.providers - App.web3Provider = new Web3.providers.HttpProvider('http://127.0.0.1:9545'); - web3 = new Web3(App.web3Provider); + initWeb3: async function() { + // Modern dapp browsers... + if (window.ethereum) { + App.web3Provider = window.ethereum; + try { + // Request account access + await window.ethereum.enable(); + } catch (error) { + // User denied account access... + console.error("User denied account access") + } + } + + // Legacy dapp browsers... + else if (window.web3) { + App.web3Provider = window.web3.currentProvider; } + // If no injected web3 instance is detected, fall back to Ganache + else { + App.web3Provider = new Web3.providers.HttpProvider('http://localhost:7545'); + } + + web3 = new Web3(App.web3Provider); + return App.initContract(); },