add listener for account change

This commit is contained in:
Barry Gitarts 2019-11-26 10:21:56 -05:00
parent f1eafb8d40
commit 58117bf5e8
1 changed files with 17 additions and 1 deletions

View File

@ -37,13 +37,17 @@ class App extends React.Component {
const network = process.env.REACT_APP_NETWORK || 'ropsten'
this.setCurrencies(network)
this.setGraphClient(network)
this.grabAddress()
}
grabAddress = () => {
if (window.ethereum) {
this.accountListener()
const { selectedAddress: account } = window.ethereum
if (account) this.setState({ account })
} else {
console.log('window.ethreum not found :', {window})
}
}
setCurrencies = async network => {
@ -75,6 +79,18 @@ class App extends React.Component {
this.setState({ loading: false })
}
accountListener = () => {
let self = this
try {
window.ethereum.on('accountsChanged', function (accounts) {
const [account] = accounts
self.setState({ account })
})
} catch (error) {
console.error('accountsChanged listener : ', {error})
}
}
enableEthereum = async () => {
try {
await window.ethereum.enable()