Update src/utils/getWeb3.js
due to MetaMask breaking changes: https://docs.metamask.io/guide/provider-migration.html
This commit is contained in:
parent
89feb07a88
commit
889fa30e7c
|
@ -5,17 +5,39 @@ import messages from './messages'
|
||||||
|
|
||||||
const defaultNetId = helpers.netIdByBranch(constants.CORE)
|
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) {
|
export async function enableWallet(updateKeys) {
|
||||||
if (window.ethereum) {
|
if (window.ethereum) {
|
||||||
try {
|
try {
|
||||||
await window.ethereum.enable()
|
await window.ethereum.request({ method: 'eth_requestAccounts' })
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await updateKeys(null)
|
await updateKeys(null)
|
||||||
throw Error(messages.USER_DENIED_ACCOUNT_ACCESS)
|
throw Error(messages.USER_DENIED_ACCOUNT_ACCESS)
|
||||||
}
|
}
|
||||||
|
|
||||||
const web3 = new Web3(window.ethereum)
|
const web3 = new Web3(window.ethereum)
|
||||||
const accounts = await web3.eth.getAccounts()
|
const accounts = await getAccounts(web3)
|
||||||
|
|
||||||
await updateKeys(accounts[0])
|
await updateKeys(accounts[0])
|
||||||
}
|
}
|
||||||
|
@ -28,7 +50,9 @@ export default async function getWeb3(netId, updateKeys) {
|
||||||
if (window.ethereum) {
|
if (window.ethereum) {
|
||||||
web3 = new Web3(window.ethereum)
|
web3 = new Web3(window.ethereum)
|
||||||
console.log('Injected web3 detected.')
|
console.log('Injected web3 detected.')
|
||||||
window.ethereum.autoRefreshOnNetworkChange = true
|
window.ethereum.on('chainChanged', () => {
|
||||||
|
window.location.reload()
|
||||||
|
})
|
||||||
} else if (window.web3) {
|
} else if (window.web3) {
|
||||||
web3 = new Web3(window.web3.currentProvider)
|
web3 = new Web3(window.web3.currentProvider)
|
||||||
console.log('Injected web3 detected.')
|
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
|
// Load for the first time in the current browser's session
|
||||||
if (web3) {
|
if (web3) {
|
||||||
// MetaMask (or another plugin) is injected
|
// MetaMask (or another plugin) is injected
|
||||||
netId = await web3.eth.net.getId()
|
netId = await getNetId(web3)
|
||||||
if (!(netId in constants.NETWORKS)) {
|
if (!(netId in constants.NETWORKS)) {
|
||||||
// If plugin's netId is unsupported, try to use
|
// If plugin's netId is unsupported, try to use
|
||||||
// the previously chosen netId
|
// the previously chosen netId
|
||||||
|
@ -67,25 +91,33 @@ export default async function getWeb3(netId, updateKeys) {
|
||||||
let networkMatch = false
|
let networkMatch = false
|
||||||
|
|
||||||
if (web3) {
|
if (web3) {
|
||||||
const accounts = await web3.eth.getAccounts()
|
const accounts = await getAccounts(web3)
|
||||||
defaultAccount = accounts[0] || null
|
defaultAccount = accounts[0] || null
|
||||||
|
|
||||||
if (!defaultAccount) {
|
if (!defaultAccount) {
|
||||||
console.error('Unlock your wallet')
|
console.log('Unlock your wallet')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (web3.currentProvider.publicConfigStore) {
|
let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : null
|
||||||
let currentAccount = defaultAccount ? defaultAccount.toLowerCase() : null
|
function onUpdateAccount(account) {
|
||||||
web3.currentProvider.publicConfigStore.on('update', function(obj) {
|
if (account && account !== currentAccount) {
|
||||||
const account = obj.selectedAddress
|
currentAccount = account
|
||||||
if (account !== currentAccount) {
|
updateKeys(account)
|
||||||
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) {
|
if (web3NetId === netId) {
|
||||||
networkMatch = true
|
networkMatch = true
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue