0

If you write window.ethereum into the browser console it will print a proxy object that looks like this:

window.ethereum: Proxy { <target>: {…}, <handler>: {…} }
<target>:
​​
    ...

    isMetaMask: true
    networkVersion: "1662582417739"
​​
    ...

<handler>: Object { deleteProperty: deleteProperty() }

As you see, there is a networkVersion property which is the equivalent of the web3.eth.net.getId() line of code (of course when you are using web3.js)

But when you are using ethers.js, I do not see any method that provides networkVersion. Sure you can get networkId but not networkVersion.

Is there any method to get networkVersion using ethers itself? Or we should fallback to using window.ethereum.networkId ?

Im actually surprised that there is no documentation on this. This networkVersion is the only way you can get the contract address from its json file (Contract.networks[networkVersion].address). Is this on purpose? A design decision made by the ethers team?

1 Answer 1

0

You can take a look at this tutorial for using Ethers.js library to interact with the Ethereum blockchain and search for "networkversion".

https://jetsoanalin.github.io/EthersJsTutorialJetso/

2
  • 1
    Link only answers are discouraged. It is better to include the main idea in the answer and leave the link for the additional details.
    – Ismael
    Sep 13 at 4:13
  • In that tutorial the author is using window.ethereum.networkId which I knew from the start. Is that the standard thing to do? or should ethers library itself provide a method to get it through it (as web3.js does with web3.eth.net.getId())
    – HHHHHH
    Sep 13 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.