From 889fa30e7c09b9502aef04e0c1fb4a29059a2acf Mon Sep 17 00:00:00 2001 From: PoaMan <33550681+poaman@users.noreply.github.com> Date: Mon, 16 Nov 2020 15:19:21 +0300 Subject: [PATCH] Update src/utils/getWeb3.js due to MetaMask breaking changes: https://docs.metamask.io/guide/provider-migration.html --- src/utils/getWeb3.js | 62 +++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/src/utils/getWeb3.js b/src/utils/getWeb3.js index 6c3fc92..1b6407d 100644 --- a/src/utils/getWeb3.js +++ b/src/utils/getWeb3.js @@ -5,17 +5,39 @@ import messages from './messages' const defaultNetId = helpers.netIdByBranch(constants.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) { + netId = web3.utils.isHex(window.ethereum.chainId) + ? web3.utils.hexToNumber(window.ethereum.chainId) + : window.ethereum.chainId + } else { + netId = await web3.eth.net.getId() + } + return netId +} + export async function enableWallet(updateKeys) { if (window.ethereum) { try { - await window.ethereum.enable() + await window.ethereum.request({ method: 'eth_requestAccounts' }) } catch (e) { await updateKeys(null) throw Error(messages.USER_DENIED_ACCOUNT_ACCESS) } const web3 = new Web3(window.ethereum) - const accounts = await web3.eth.getAccounts() + const accounts = await getAccounts(web3) await updateKeys(accounts[0]) } @@ -28,7 +50,9 @@ export default async function getWeb3(netId, updateKeys) { if (window.ethereum) { web3 = new Web3(window.ethereum) console.log('Injected web3 detected.') - window.ethereum.autoRefreshOnNetworkChange = true + window.ethereum.on('chainChanged', () => { + window.location.reload() + }) } else if (window.web3) { web3 = new Web3(window.web3.currentProvider) console.log('Injected web3 detected.') @@ -38,7 +62,7 @@ export default async function getWeb3(netId, updateKeys) { // 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 @@ -67,25 +91,33 @@ export default async function getWeb3(netId, updateKeys) { 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() : null - web3.currentProvider.publicConfigStore.on('update', function(obj) { - const account = obj.selectedAddress - if (account !== currentAccount) { - currentAccount = account - updateKeys(account) - } + let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : null + function onUpdateAccount(account) { + if (account && account !== currentAccount) { + currentAccount = account + updateKeys(account) + } + } + if (window.ethereum) { + window.ethereum.on('accountsChanged', accs => { + const account = accs && accs.length > 0 ? accs[0].toLowerCase() : null + onUpdateAccount(account) + }) + } else if (web3.currentProvider.publicConfigStore) { + web3.currentProvider.publicConfigStore.on('update', obj => { + const account = obj.selectedAddress ? obj.selectedAddress.toLowerCase() : null + onUpdateAccount(account) }) } - const web3NetId = await web3.eth.net.getId() + const web3NetId = await getNetId(web3) if (web3NetId === netId) { networkMatch = true } else {