2

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',
)
  • 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). – goodvibration Oct 10 '19 at 8:14
  • 1
    You can probably also obtain it programmatically from the web3 instance which you're initializing in your script. – goodvibration Oct 10 '19 at 8:15
  • 1
    With web3 v1.0 the following works web3.eth.net.getId(). – Ismael Oct 10 '19 at 16:28
  • 1
    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/… – Chan-Ho Suh Dec 18 '19 at 2:52
2

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

const networkId = await web3.eth.net.getId();
const chainId = await web3.eth.getChainId();
| improve this answer | |

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.