(Update) Add support of a new version of MetaMask
Solves https://github.com/poanetwork/poa-dapps-voting/issues/179
This commit is contained in:
parent
24bc88d77a
commit
bc00a5af18
112
src/getWeb3.js
112
src/getWeb3.js
|
@ -4,63 +4,75 @@ import { constants } from './constants'
|
|||
let getWeb3 = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Wait for loading completion to avoid race conditions with web3 injection timing.
|
||||
window.addEventListener('load', () => {
|
||||
var results
|
||||
var web3 = window.web3
|
||||
window.addEventListener('load', async () => {
|
||||
let results
|
||||
let web3 = window.web3
|
||||
|
||||
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
|
||||
if (typeof web3 !== 'undefined') {
|
||||
// Use Mist/MetaMask's provider.
|
||||
var errorMsg = null
|
||||
if (window.ethereum) {
|
||||
web3 = new window.Web3(window.ethereum)
|
||||
console.log('Injected web3 detected.')
|
||||
try {
|
||||
await window.ethereum.enable()
|
||||
} catch (e) {
|
||||
console.error('User denied account access')
|
||||
reject({ message: messages.USER_DENIED_ACCOUNT_ACCESS })
|
||||
return
|
||||
}
|
||||
} else if (typeof web3 !== 'undefined') {
|
||||
web3 = new window.Web3(web3.currentProvider)
|
||||
web3.version.getNetwork((err, netId) => {
|
||||
let netIdName
|
||||
console.log('netId', netId)
|
||||
switch (netId) {
|
||||
case constants.NETID_DAI:
|
||||
netIdName = 'Dai'
|
||||
console.log('This is Dai', netId)
|
||||
break
|
||||
case constants.NETID_CORE:
|
||||
netIdName = 'Core'
|
||||
console.log('This is Core', netId)
|
||||
break
|
||||
case constants.NETID_DAI_TEST:
|
||||
netIdName = 'Dai-Test'
|
||||
console.log('This is Dai-Test', netId)
|
||||
break
|
||||
case constants.NETID_SOKOL:
|
||||
netIdName = 'Sokol'
|
||||
console.log('This is Sokol', netId)
|
||||
break
|
||||
default:
|
||||
netIdName = 'ERROR'
|
||||
errorMsg = messages.WRONG_NETWORK_MSG
|
||||
console.log('This is an unknown network.', netId)
|
||||
}
|
||||
document.title = `${netIdName} - POA Network Governance DApp`
|
||||
var defaultAccount = web3.eth.defaultAccount || null
|
||||
if (defaultAccount === null) {
|
||||
reject({ message: messages.NO_METAMASK_MSG })
|
||||
}
|
||||
if (errorMsg !== null) {
|
||||
reject({ message: errorMsg })
|
||||
}
|
||||
results = {
|
||||
web3Instance: web3,
|
||||
netIdName,
|
||||
netId,
|
||||
injectedWeb3: true,
|
||||
defaultAccount
|
||||
}
|
||||
resolve(results)
|
||||
})
|
||||
|
||||
console.log('Injected web3 detected.')
|
||||
} else {
|
||||
reject({ message: messages.NO_METAMASK_MSG })
|
||||
console.error('Metamask not found')
|
||||
reject({ message: messages.NO_METAMASK_MSG })
|
||||
return
|
||||
}
|
||||
|
||||
let errorMsg = null
|
||||
web3.version.getNetwork((err, netId) => {
|
||||
let netIdName
|
||||
console.log('netId', netId)
|
||||
switch (netId) {
|
||||
case constants.NETID_DAI:
|
||||
netIdName = 'Dai'
|
||||
console.log('This is Dai', netId)
|
||||
break
|
||||
case constants.NETID_CORE:
|
||||
netIdName = 'Core'
|
||||
console.log('This is Core', netId)
|
||||
break
|
||||
case constants.NETID_DAI_TEST:
|
||||
netIdName = 'Dai-Test'
|
||||
console.log('This is Dai-Test', netId)
|
||||
break
|
||||
case constants.NETID_SOKOL:
|
||||
netIdName = 'Sokol'
|
||||
console.log('This is Sokol', netId)
|
||||
break
|
||||
default:
|
||||
netIdName = 'ERROR'
|
||||
errorMsg = messages.WRONG_NETWORK_MSG
|
||||
console.log('This is an unknown network.', netId)
|
||||
}
|
||||
document.title = `${netIdName} - POA Network Governance DApp`
|
||||
if (errorMsg !== null) {
|
||||
reject({ message: errorMsg })
|
||||
return
|
||||
}
|
||||
var defaultAccount = web3.eth.defaultAccount || null
|
||||
if (defaultAccount === null) {
|
||||
reject({ message: messages.NO_METAMASK_MSG })
|
||||
return
|
||||
}
|
||||
results = {
|
||||
web3Instance: web3,
|
||||
netIdName,
|
||||
netId,
|
||||
injectedWeb3: true,
|
||||
defaultAccount
|
||||
}
|
||||
resolve(results)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ messages.MINING_KEY_IS_NOT_ADDRESS_MSG = "Ballot miningKey isn't address"
|
|||
messages.PROPOSED_ADDRESS_IS_NOT_ADDRESS_MSG = "Proposed address isn't address"
|
||||
messages.END_TIME_SHOULD_BE_GREATER_THAN_NOW_MSG = 'Ballot end time should be greater than now'
|
||||
messages.BALLOT_TYPE_IS_EMPTY_MSG = 'Ballot type is empty'
|
||||
messages.USER_DENIED_ACCOUNT_ACCESS = 'You have denied access to your accounts'
|
||||
messages.NO_METAMASK_MSG = `You haven't chosen any account in MetaMask.
|
||||
Please, choose your voting key in MetaMask and reload the page.
|
||||
Check POA Network <a href='https://github.com/poanetwork/wiki' target='blank'>wiki</a> for more info.`
|
||||
|
|
Loading…
Reference in New Issue