NomicFoundation / hardhat

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

HH does not fork mainnet correctly based on specified chainId #3640

Closed Samboy76 closed 1 year ago

Samboy76 commented 1 year ago

Version of Hardhat

2.12.2

What happened?

I am trying to fork 5 environments and config 5 mainnet networks within the same hardhat.config.js.

I have a monitoring bot setup that listens to each one of the above five forks and mainnets depending on the flag that I set in runtime.

My question is this:

For each one of the forks, I would like to test separate contract addresses (tokens and smart contracts) under my backend solidity smart contract but seem to run into problems such as this one: Error HH101: Hardhat was set to use chain id 1, but connected to a chain with id 31337. And when I seem to progress I run into this other error but this time in Solidity back-end "Error: Transaction reverted: function call to a non-contract account" which indicate perhaps I am running under different network or chainId in Hardhat localhost. This is the confusion that I have and would like to toggle between each of the fork networks/chainId for my bot through this toggle.

Upon deploy function call in deploy JS script, in the backend solidity constructor function for the flashloan smart contract, I print out the current chainId of the network I am using and it keeps returning the same chainId value each time 31337. See below print out code snippet:

console.log("block.chainid ", block.chainid);

I would like to have forks and mainnets for each of Ethereum, Polygon, Avalanche, Arbitrum and BSC networks. Not sure if it´s a question of correctly configuring the corresponding chainIds which I have attempted directly in hardhat.config.as as shown above.

This is where I need some help and guidance from your shared wisdom.

Minimal reproduction steps

Attempted to fork 5 mainnet networks including specification of their corresponding config of 5 mainnet networks within the same hardhat.config.js shown below:

require('dotenv').config();
require("@nomicfoundation/hardhat-toolbox");

const FORK_MAINNET = true;
const FORK_POLYGON = false;
const FORK_AVALANCHE = false;
const FORK_ARBITRUM = false;
const FORK_BSC = false;

const MAINNET = false;
const POLYGON = false;
const AVALANCHE = false;
const ARBITRUM = false;
const BSC = false;

const forkingData = FORK_MAINNET
    ? {
        url: process.env.INFURA_URL_API_KEY,
        enabled: true
      }
    : FORK_POLYGON
    ? {
        url: process.env.ALCHEMY_URL_API_KEY,
        enabled: true
      }
    : FORK_AVALANCHE
    ? {
        url: process.env.GETBLOCK_URL_API_KEY,
        enabled: true
      }
    : FORK_ARBITRUM
    ? {
        url: process.env.CHAINSTACK_URL_API_KEY,
        enabled: true
      }
    : FORK_BSC
    ? {
        url: process.env.QUICKNODE_URL,
        enabled: true
      }
    : undefined;

const urlData = MAINNET
    ? process.env.INFURA_URL_API_KEY
    : POLYGON
    ? process.env.ALCHEMY_URL_API_KEY
    : AVALANCHE
    ? process.env.GETBLOCK_URL_API_KEY
    : ARBITRUM
    ? process.env.CHAINSTACK_URL_API_KEY
    : BSC
    ? process.env.QUICKNODE_URL
    : "http://127.0.0.1:8545";
const chainData = MAINNET
    ? 1
    : POLYGON
    ? 137
    : AVALANCHE
    ? 43114
    : ARBITRUM
    ? 42161
    : BSC
    ? 56
    : FORK_MAINNET
    ? 1 // TODO change value
    : FORK_POLYGON
    ? 137 // TODO change value
    : FORK_AVALANCHE
    ? 43114 // TODO change value
    : FORK_ARBITRUM
    ? 42161 // TODO change value
    : FORK_BSC
    ? 56 // TODO change value
    : 31337; // default to Mainnet chainId

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  defaultNetwork: "localhost",
  networks: {
    localhost: {
      url: urlData,
      chainId: chainData,
      throwOnTransactionFailures: true,
      throwOnCallFailures: true,
      forking: forkingData,
      accounts: {
        mnemonic: process.env.MNEMONIC,
        path: "m/44'/60'/0'/0", // HD parent of all the derived keys. Default value: "m/44'/60'/0'/0"
        initialIndex: 0, // The initial index to derive. Default value: 0.
        count: 20, // The number of accounts to derive. Default value: 20.
        passphrase: "" // The passphrase for the wallet. Default value: empty string.
      }
    },
    mainnet: {
      url: urlData,
      chainId: chainData,
      accounts: {
        mnemonic: process.env.MNEMONIC,
        path: "m/44'/60'/0'/0", // HD parent of all the derived keys. Default value: "m/44'/60'/0'/0"
        initialIndex: 0, // The initial index to derive. Default value: 0.
        count: 20, // The number of accounts to derive. Default value: 20.
        passphrase: "" // The passphrase for the wallet. Default value: empty string.
      }
    }
  },
  solidity: {
    compilers: [
      {
        version: "0.8.10"
      }
    ],
    settings: {
      optimizer: {
        enabled: false
      }
    }
  },
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: "./cache",
    artifacts: "./artifacts"
  },
  mocha: {
    timeout: 100000000
  }
};

I have deployed JS script that deploys my smart contract through usage of these code lines:

const Flashloan = await hre.ethers.getContractFactory("Flashloan");
const flashLoan = await Flashloan.deploy(<input arg>);
await flashLoan.deployed();

Then I launch my JS monitoring bot that listens to each one of the above five forks and mainnets depending on the flag that I set in runtime.

The issue I face is this:

For each one of the forks, I would like to test separate contract addresses (tokens and smart contracts) under my backend solidity smart contract but seem to run into problems such as this one: Error HH101: Hardhat was set to use chain id 1, but connected to a chain with id 31337.

And when I seem to progress I run into this other error but this time in Solidity back-end "Error: Transaction reverted: function call to a non-contract account" which indicate perhaps I am running under different network or chainId in Hardhat localhost. This is the confusion that I have and would like to toggle between each of the fork networks/chainId for my bot through this toggle.

Upon deploy function call in deploy JS script, in the backend solidity constructor function for the flashloan smart contract, I print out the current chainId of the network I am using and it keeps returning the same chainId value each time 31337. See below print out code snippet:

console.log("block.chainid ", block.chainid);

What am I missing here or what seems to be the problem.

FYI, I would like to have forks and mainnets for each of Ethereum, Polygon, Avalanche, Arbitrum and BSC networks. Not sure if it´s a question of correctly configuring the corresponding chainIds which I have attempted directly in hardhat.config.as as shown above.

This is where I need some help and guidance from your shared wisdom.

Thank you very much for your assistance on this topic.

Best

Search terms

fork mainnets, hardhat

fvictorio commented 1 year ago

I responded to the same thing here.

Notice that the issue tracker is for bug reports and feature requests, not for questions. Please open a discussion for that.