From 58117bf5e814b502701e4ccfb3ad6d599e9cd9f2 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Tue, 26 Nov 2019 10:21:56 -0500 Subject: [PATCH] add listener for account change --- src/dapp.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/dapp.js b/src/dapp.js index e40e47b..eed547d 100644 --- a/src/dapp.js +++ b/src/dapp.js @@ -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()