2

I'm totally new to ethereum and at the moment just finishing my first tutorial from Dapp University. Since the tutorial is from 2019 there is some deprecation code. I was able to find the current solutions, but I got stuck at the part where I need to get the networks id.

In the tutorial I am using the ganache as my testing blockchain and according to json the ganache network id is 5777

"networks": {
    "5777": {
      "events": {},
      "links": {},
      "address": "0xcCAE784C66CF099745F5D1E1BE7c4a76fB39f9F2",
      "transactionHash": "0x6395d9dca26bc4e3e7ef88b2e02c8c0a20f9a870e0cdf895993b40471dc47261"
    }
  },
  "schemaVersion": "3.4.0",
  "updatedAt": "2021-05-05T12:28:30.450Z",
  "networkType": "ethereum",
  "devdoc": {
    "methods": {}
  },
  "userdoc": {
    "methods": {}
  }

Now the way to get the id in the video was to use the method:

const networkId = await web3.eth.net.getId();

However that produced the error:

TypeError: Cannot read property 'net' of undefined

And also on top of that there was a deprecation notice as metamask no longer injects web3.

I found out that instead of the deprecated code I can access metamask methods using window.ethereum instead of web3.eth.

But I couldn't find the corresponding getId() method. When I tried window.ethereum.net.getId(); I got Cannot read property 'getId' of undefined error.

The closest I got was using this method I found in https://metamask.github.io/api-playground/api-documentation/

window.ethereum.request({ method: 'net_version' }) But instead of obtaining ganache id which should be 5777 I got 1.

So now I'm not sure if this is the right method and I'm simply not on ganache or this is something completely else and I just can't find the method I am looking for.

2

Alright I figured it out. I was not connected on my localhost ganache server, but the main ethereum one. Just needed to create my custom local server in metamask and everything works well now. The code itself is correct and now I get 5777 as a network id.

In case other newbies like myself would stumble upon same problem, I solved it this way:

  1. Click on the metamask on your toolbar and than click on the select option at the top.
  2. You can see I already created my own Local Ganache Server so just click on Custom RPC to create your own.

enter image description here

  1. Choose whatever name you like for your network.
  2. Copy past your RPC Server address from Ganache app. You can find it at the top navbar. Looks something like this: HTTP://127.0.0.1:xxxx
  3. Input in the Ganache Chaind ID. According to their docs the default one is 1337.

enter image description here

You are good to go!

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.