@@ -4,31 +4,24 @@ import Web3Utils from 'web3-utils'
44const getWeb3 = ( ) => {
55 return new Promise ( function ( resolve , reject ) {
66 // Wait for loading completion to avoid race conditions with web3 injection timing.
7- window . addEventListener ( 'load' , function ( ) {
7+ window . addEventListener ( 'load' , async function ( ) {
88 let web3 = window . web3
9+ const { ethereum } = window
910
10- // Checking if Web3 has been injected by the browser (Mist/MetaMask)
11- if ( typeof web3 !== 'undefined' ) {
12- // Use Mist/MetaMask's provider.
11+ if ( ethereum ) {
12+ web3 = new window . Web3 ( ethereum )
13+ try {
14+ // Request account access
15+ await ethereum . enable ( )
16+ processWeb3 ( web3 , resolve , reject )
17+ } catch ( error ) {
18+ console . log ( error )
19+ const errorMsg = `Wallet account rejected by user`
20+ reject ( { message : errorMsg } )
21+ }
22+ } else if ( typeof web3 !== 'undefined' ) {
1323 web3 = new window . Web3 ( web3 . currentProvider )
14- web3 . version . getNetwork ( ( err , netId ) => {
15- const netIdName = getNetworkName ( netId )
16- console . log ( `This is ${ netIdName } network.` , netId )
17- document . title = `${ netIdName } - Bridge UI dApp`
18- const defaultAccount = web3 . eth . defaultAccount || null ;
19- if ( defaultAccount === null ) {
20- reject ( { message : 'Please unlock your metamask and refresh the page' } )
21- }
22- const results = {
23- web3Instance : new Web3 ( web3 . currentProvider ) ,
24- netIdName,
25- netId,
26- injectedWeb3 : true ,
27- defaultAccount
28- }
29- resolve ( results )
30- } )
31-
24+ processWeb3 ( web3 , resolve , reject )
3225 } else {
3326 // Fallback to localhost if no web3 injection.
3427 const errorMsg = `Metamask is not installed. Please go to
@@ -94,3 +87,23 @@ export const estimateGas = async (web3, to, gasPrice, from, value, data) =>{
9487}
9588
9689export const getGasPrices = ( ) => fetch ( 'https://gasprice.poa.network/' ) . then ( response => response . json ( ) )
90+
91+ const processWeb3 = ( web3 , resolve , reject ) => {
92+ web3 . version . getNetwork ( ( err , netId ) => {
93+ const netIdName = getNetworkName ( netId )
94+ console . log ( `This is ${ netIdName } network.` , netId )
95+ document . title = `${ netIdName } - Bridge UI dApp`
96+ const defaultAccount = web3 . eth . defaultAccount || null ;
97+ if ( defaultAccount === null ) {
98+ reject ( { message : 'Please unlock your wallet and refresh the page' } )
99+ }
100+ const results = {
101+ web3Instance : new Web3 ( web3 . currentProvider ) ,
102+ netIdName,
103+ netId,
104+ injectedWeb3 : true ,
105+ defaultAccount
106+ }
107+ resolve ( results )
108+ } )
109+ }
0 commit comments