Skip to content

Commit

Permalink
Merge pull request #122 from poanetwork/core-metamask-provider-updated
Browse files Browse the repository at this point in the history
(Fix) Update getWeb3 script according to MetaMask breaking changes
  • Loading branch information
varasev authored Nov 17, 2020
2 parents 1e74b7f + cb9fc16 commit 74ad1ff
Showing 1 changed file with 48 additions and 15 deletions.
63 changes: 48 additions & 15 deletions src/utils/getWeb3.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,38 @@ import messages from './messages'

const defaultNetId = helpers.netIdByBranch(constants.branches.CORE)

async function getAccounts(web3) {
let accounts
if (window.ethereum) {
accounts = await window.ethereum.request({ method: 'eth_accounts' })
} else {
accounts = await web3.eth.getAccounts()
}
return accounts
}

async function getNetId(web3) {
let netId
if (window.ethereum) {
const { chainId } = window.ethereum
netId = web3.utils.isHex(chainId) ? web3.utils.hexToNumber(chainId) : chainId
} else {
netId = await web3.eth.net.getId()
}
return netId
}

export async function enableWallet(onAccountChange) {
if (window.ethereum) {
try {
await window.ethereum.enable()
await window.ethereum.request({ method: 'eth_requestAccounts' })
} catch (e) {
await onAccountChange(null)
throw Error(messages.userDeniedAccessToAccount)
}

const web3 = new Web3(window.ethereum)
const accounts = await web3.eth.getAccounts()
const accounts = await getAccounts(web3)

await onAccountChange(accounts[0])
}
Expand All @@ -28,7 +49,11 @@ export default async function getWeb3(netId, onAccountChange) {
if (window.ethereum) {
web3 = new Web3(window.ethereum)
console.log('Injected web3 detected.')
window.ethereum.autoRefreshOnNetworkChange = true
if (!window.ethereum.autoRefreshOnNetworkChange) {
window.ethereum.on('chainChanged', () => {
window.location.reload()
})
}
} else if (window.web3) {
web3 = new Web3(window.web3.currentProvider)
console.log('Injected web3 detected.')
Expand All @@ -38,7 +63,7 @@ export default async function getWeb3(netId, onAccountChange) {
// Load for the first time in the current browser's session
if (web3) {
// MetaMask (or another plugin) is injected
netId = await web3.eth.net.getId()
netId = await getNetId(web3)
if (!(netId in constants.NETWORKS)) {
// If plugin's netId is unsupported, try to use
// the previously chosen netId
Expand Down Expand Up @@ -67,25 +92,33 @@ export default async function getWeb3(netId, onAccountChange) {
let networkMatch = false

if (web3) {
const accounts = await web3.eth.getAccounts()
const accounts = await getAccounts(web3)
defaultAccount = accounts[0] || null

if (!defaultAccount) {
console.error('Unlock your wallet')
console.log('Unlock your wallet')
}

if (web3.currentProvider.publicConfigStore) {
let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : ''
web3.currentProvider.publicConfigStore.on('update', async function(obj) {
const account = obj.selectedAddress
if (account && account !== currentAccount) {
currentAccount = account
await onAccountChange(account)
}
let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : null
async function onUpdateAccount(account) {
if (account && account !== currentAccount) {
currentAccount = account
await onAccountChange(account)
}
}
if (window.ethereum) {
window.ethereum.on('accountsChanged', async accs => {
const account = accs && accs.length > 0 ? accs[0].toLowerCase() : null
await onUpdateAccount(account)
})
} else if (web3.currentProvider.publicConfigStore) {
web3.currentProvider.publicConfigStore.on('update', async obj => {
const account = obj.selectedAddress ? obj.selectedAddress.toLowerCase() : null
await onUpdateAccount(account)
})
}

const web3NetId = await web3.eth.net.getId()
const web3NetId = await getNetId(web3)
if (web3NetId === netId) {
networkMatch = true
} else {
Expand Down

0 comments on commit 74ad1ff

Please sign in to comment.