8

I am using truffle console to deploy contracts and using Nodejs/web3js to interact with it. I wanted to use ethereumjs-tx for signing transactions, here is the reference link. I am not sure how can I find the chain id and network id of the local blockchain for which I need to create rawtransaction. i.e

const customCommon = Common.forCustomChain(
  'randomchainNmae',
  {
    name: 'my-network',
    networkId: ???,
    chainId: ???,
  },
  'petersburg',
)
4
  • networkId and chainId are the same thing AFAIK. And it's not on Truffle side, but on Ganache side (or whatever you're using as a blockchain provider). On Truffle side you can filter specific network IDs via the Truffle configuration file (truffle.js or truffle-config.js). But the configuration of the actual ID is on the provider side. For example, if you ARE using Ganache, then you can use -i or --networkId in order to specify the network id that ganache-cli will use to identify itself (see here). Oct 10, 2019 at 8:14
  • 1
    You can probably also obtain it programmatically from the web3 instance which you're initializing in your script. Oct 10, 2019 at 8:15
  • 2
    With web3 v1.0 the following works web3.eth.net.getId().
    – Ismael
    Oct 10, 2019 at 16:28
  • 4
    no, networkId and chainId are NOT the same, and the purpose of chainId is to resist transaction replay attacks. So this distinction is actually super-important but little-understood. e.g. see medium.com/@pedrouid/… Dec 18, 2019 at 2:52

3 Answers 3

14

You can get both using the web3 instance of web3.js:

const networkId = await web3.eth.net.getId();
const chainId = await web3.eth.getChainId();
1
  • 1
    You can just write these down to your truffle cli like "await web3.eth.getChainId()" and you will get your chain ID.
    – myuce
    May 10, 2021 at 23:21
4

To answer the title directly, the chain_id of ganache is 0x539 (or 1337).

1

If you don't know network_id then you can use the following network. Check your truffle.js

   development: {
          host: '127.0.0.1',
          port: 8545,
          network_id: "*",
          from: "", // default address to use for any transaction Truffle makes 
        }

Your Answer

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

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