trufflesuite / drizzle

Reactive Ethereum dapp UI suite
906 stars 238 forks source link

update accounts by listening to web3 changes #65

Open alanarvelo opened 4 years ago

alanarvelo commented 4 years ago

Related to Issue #48 (Polls->account: intempestive refresh) though not experiencing intempestive refresh thus making a new Issue.

During development, polling accounts saturates the console with frequent state refreshes. Adjusting polling frequency when you change from testing DApp as a user (high frequency, 1.5s) to debugging (lower frequency, 90s) and vice versa, is cumbersome.

In a recent project (code) I tracked account changes with:

// listens for updates on the MetaMask active account.
  listenToActiveAccountUpdates() {
    try {
      this.props.drizzle.web3.currentProvider.publicConfigStore.on('update', ({ selectedAddress }) => {
        this.props.drizzle.store.dispatch(setActiveEOA(selectedAddress))
      });
    } catch (error) {
      console.error("No web3 account tracking: ", error)
    }
  }

There is also the possibility to track network changes with:

ethereum.on('chainChanged', chainId => {
  // handle the new network
})

This is an improvement that has been talked about. I understand that the ramifications of this change in the source code are deep and complex. Any tips on how to compartmentalize? where to start? Is it ok if it works with MetaMask and not other web3 providers? Should we do this now?

A relevant MetaMask article on network changes.

cds-amal commented 4 years ago

Hi @alanarvelo . We'll want to coordinate this when the new MM changes arrives. The account change is resolved in the drizzle-middleware. My hunch is to modify the accounts saga to dispatch the appropriate event when the EOA is updated.