In ethers npm

I want to only allow payments If the users in on the metamask Eth mainnet and not ropsten

So after they make a payment I can logged it in the database As paid,

But I don’t want to log it in the Database if the user has paid with Ropsten ,

So something like

Network = ethers Get network()
If (network === ropsten) { return }

If (network === mainnet){
Database. Log user with id 727!he3 

Because now the users can pay with Ropsten and I can’t tell this From The transaction receipt or when calling getsigner() ??? If it was ropsten or mainnet

1 Answer 1


You can read the current network chain ID from metamask like this:

console.log(window.ethereum.networkVersion, 'window.ethereum.networkVersion');// that will be of type string

So your logic will look something like this:

network = window.ethereum.networkVersion 

if (network !== "1") {return;} else { Database. Log user with id 727!he3 Paid }

And these are the chain IDs for the most used ethereum networks:

  • Mainnet: 1
  • Kovan: 42
  • Ropsten: 3
  • Rinkeby: 4
  • Goerli: 5

If you are even going to different chains you cn find a list of the ids here.

More discussion about it can be found on this other question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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