1

I want to be able to fork more chains (ethereum, bsc, etc.) than just one on my system.
Hardhat doc shows, how to fork 1 chain npx hardhat node --fork https://... and it works fine.
But I would like to fork them programmatically on a Nodejs script.

When I try to fork a chain on NodeJs like below, it does not work. What can I do?

Thanks!


`hre.config.networks.networks = {
     hardhat: {
        forking: {
          url: http://localhost:8545
        },
    }
}     

await hre.network.provider.request({
    method: "hardhat_reset",
    params: [
      {
        forking: {
          jsonRpcUrl: http://localhost:8545,
          chainId: chainObj.chain_id,
          blockNumber: blockNumber, 
        },
      },
    ],
});

`

Mr.X
  • 31
  • 3

1 Answers1

0

Maybe it would help.

hardhat.config.ts:

networks: {
    hardhat: {
        mining: {
            auto: false,
            interval: [10000, 10000],
        },
        chainId: Number(process.env.CHAIN_ID),
        forking: {
            url: process.env.NETWORK_URL || "",
            // blockNumber: Number(process.env.FROM_BLOCK) || 228,
        },
        accounts: [
            {
                balance: "100000000000000000000000000000",
                privateKey: process.env.PRIVATE_KEY as string,
            },
        ],
    }
}

package.json:

"scripts": {
"bsc": "NETWORK=bsc NETWORK_URL=https://rpc.ankr.com/bsc FROM_BLOCK=22828104 CHAIN_ID=100 npx hardhat node --port 8541 ",
"eth": "NETWORK=eth NETWORK_URL=https://rpc.ankr.com/eth FROM_BLOCK=15720107 CHAIN_ID=101 npx hardhat node --port 8542 ",
"avax": "NETWORK=avax NETWORK_URL=https://api.avax.network/ext/bc/C/rpc CHAIN_ID=102 FROM_BLOCK=20899475 npx hardhat node --port 8543 ",
"ftm": "NETWORK=ftm NETWORK_URL=https://rpc.ankr.com/fantom CHAIN_ID=103 FROM_BLOCK=48883683 npx hardhat node --port 8544 ",
}
dexat0r
  • 136
  • 2
  • 3