Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider.getNetwork() Always Returns the Same Network #495

Closed
nikitaeverywhere opened this issue Apr 19, 2019 · 8 comments
Closed

provider.getNetwork() Always Returns the Same Network #495

nikitaeverywhere opened this issue Apr 19, 2019 · 8 comments
Labels
discussion Questions, feedback and general information.

Comments

@nikitaeverywhere
Copy link

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!

@ricmoo
Copy link
Member

ricmoo commented May 24, 2019

If the network is switched in MetaMask, I think it should refresh the page, no? Or has this changed?

@naddison36
Copy link

naddison36 commented May 24, 2019

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

@nikitaeverywhere
Copy link
Author

Looks like that my version of Metamask doesn’t reload the page. Well, it reloads Remix IDE, but doesn’t reload other DApps. Might be an issue of some kind of old APIs used there.

Anyway, new/the majority of DApps doesn’t refresh at their own for me, which is expected.

@ricmoo ricmoo added discussion Questions, feedback and general information. next version labels May 24, 2019
@ricmoo
Copy link
Member

ricmoo commented May 24, 2019

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));'...

@zyjblockchain
Copy link

i want to know whith ethereum node when i use ethers.js. thanks

@ricmoo
Copy link
Member

ricmoo commented Jun 3, 2020

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. :)

@ricmoo
Copy link
Member

ricmoo commented Jun 3, 2020

This has been published in 5.0.0-beta.191. Any feedback is welcome. :)

@ricmoo
Copy link
Member

ricmoo commented Jun 4, 2020

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! :)

@ricmoo ricmoo closed this as completed Jun 4, 2020
michaeltout pushed a commit to michaeltout/ethers.js that referenced this issue Aug 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Questions, feedback and general information.
Projects
None yet
Development

No branches or pull requests

4 participants