Hello!
Looks like provider.getNetwork() (where provider is new providers.Web3Provider(window.web3.currentProvider)) always returns the same value, even if the network is switched in Metamask. I suppose this is the case of the problem (the same promise is returned for all calls).
Thanks!
If the network is switched in MetaMask, I think it should refresh the page, no? Or has this changed?
MetaMask's auto refresh on network change can be disabled https://metamask.github.io/metamask-docs/API_Reference/Ethereum_Provider#ethereum.autorefreshonnetworkchange
I take advantage of this feature in my Scratch extension for Ethereum
Looks like that my version of Metamask doesn鈥檛 reload the page. Well, it reloads Remix IDE, but doesn鈥檛 reload other DApps. Might be an issue of some kind of old APIs used there.
Anyway, new/the majority of DApps doesn鈥檛 refresh at their own for me, which is expected.
This is going to have to be an issue that is addressed at a higher level. The intention of a provider is that it is immutable; I wasn't planning to incorporate multi-honed providers until the Eth2.0 versions (since it will be required for cross-shard communications).
The issue is that this is not actually possible to handle at this level. We will need to build a ProviderManager which will dispatch a new provider on changes, and block (or more likely add conditional transactions) to non-focused providers.
The on("network") event is asynchronous, so it is not safe/possible to prevent terrible things from maybe happening. First, imagine if you send a transaction which is approved in a UI, but the network changes, which network it "should" vs "will" go to could be confusing no matter what is chosen. Another issue is any changes while a transaction is being populated, for example, when you send a transaction to an ENS name, it resolves it; also the gas price and nonce are populated asynchronously. If the network changes during this population, the nonce/ENS name/gas estimate and so on may be from one network, while wrong on the other.
I will put more thought into the easiest way to support this sort of thing for now, but I do not think it will be easy. Likely, it will require an `ethereum.on("network", teardownExistingAppAndRebuildNewAppWithNewProvider(network));'...
i want to know whith ethereum node when i use ethers.js. thanks
I've added preliminary support for networks that can change. When creating a Provider that you wish to be able to change networks spontaneously, you must specify the network parameter as "any".
If the "any" network is not specified, errors will occur when trying to use the Provider on a network it was initially connected to and any events running that were intended for one network will fail during polling when a different network.
When supporting changing networks, you should probably call removeAllListeners on the provider before registering new ones.
This will be published in the next beta release and I'd love some feedback. :)
This has been published in 5.0.0-beta.191. Any feedback is welcome. :)
I'm closing this issue now, but have started a new issue to keep all the discussion across these related issues together. If you have any comments, please feel free to chime in on #866. :)
Thanks! :)