NomicFoundation / hardhat

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.
https://hardhat.org
Other
7.06k stars 1.36k forks source link

Doesn't get the correct chainId from ganache #601

Closed PaulRBerg closed 4 years ago

PaulRBerg commented 4 years ago

Description

I spin up my local ganache instance like this:

ganache-cli --deterministic --networkId 1234 --port 8545

Add the "localhost" network under "networks" in buidler.config.ts:

localhost: {
  chainId: 1234,
  gas: "auto",
  gasPrice: "auto",
  url: "http://127.0.0.1:8545",
},

And get this error when running my deployment script:

{
  error: BuidlerError: BDLR101: Buidler was set to use chain id 1234, but connected to a chain with id 1337.
      at /Users/paulrberg/Projects/Sablier/Magellan/Protocol/node_modules/@nomiclabs/buidler/src/internal/core/providers/chainId.ts:18:13
      at processTicksAndRejections (internal/process/task_queues.js:97:5)
      at EthersProviderWrapper.send (/Users/paulrberg/Projects/Sablier/Magellan/Protocol/node_modules/@nomiclabs/buidler-ethers/src/ethers-provider-wrapper.ts:13:20)
      at Object.getSigners (/Users/paulrberg/Projects/Sablier/Magellan/Protocol/node_modules/@nomiclabs/buidler-ethers/src/helpers.ts:6:20)
      at getContractFactoryByName (/Users/paulrberg/Projects/Sablier/Magellan/Protocol/node_modules/@nomiclabs/buidler-ethers/src/helpers.ts:53:21) {
    errorDescriptor: {
      number: 101,
      message: 'Buidler was set to use chain id %configChainId%, but connected to a chain with id %connectionChainId%.',
      title: 'Connected to the wrong network',
      description: 'Your config specifies a chain id for the network you are trying to used, but Buidler detected anotherone. \n' +
        '\n' +
        'Please make sure you are setting your config correctly.'
    },
    number: 101,
    _isBuidlerError: true
  }
}

Environment

alcuadrado commented 4 years ago

How are you running your deployment script?

Can you paste the output of this here?

curl --data '{"method":"eth_chainId","params":[],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" http://localhost:8545
PaulRBerg commented 4 years ago

How are you running your deployment script? This is the command:

yarn buidler run scripts/deploy.ts --network localhost

Can you paste the output of this here? Here you go:

{"id":1,"jsonrpc":"2.0","result":"0x539"}
PaulRBerg commented 4 years ago

Weird .. 0x539 seems to indeed be 1337, not 1234. Maybe I have another local node running.

alcuadrado commented 4 years ago

Oh, I think you are setting the network id, not the chain id.

PaulRBerg commented 4 years ago

Interesting. Are they different? The Ganache CLI only allows me to set a network id.

This thread on StackExchange also indicates that they are the same thing.

alcuadrado commented 4 years ago

Their difference is subtle, but they are not the same thing. For example, ganache uses a random network id by default, and 1337 as chan id.

Here's an article on their difference: https://medium.com/@pedrouid/chainid-vs-networkid-how-do-they-differ-on-ethereum-eec2ed41635b

PaulRBerg commented 4 years ago

These standards change so fast. Thanks! I'll close this issue because with "1337" as the chain id, I can deploy my contracts to my local node.

PaulRBerg commented 4 years ago

Fwiw, this seems to be a bug with ganache. It erroneously returns 1337 in some cases:

https://github.com/trufflesuite/ganache-core/issues/451