I'm using hardhat to forking the Ethereum mainnet and run some tests on it. In one terminal I start the server with:
npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/$API_KEY
In another terminal I have Node.js code running. I need to periodically resync the network with mainnet. What is the best way to launch and terminate the hardhat server directly from the Node.js code?
I'm currently using the hardhat_reset command to force resync with the latest block, however this is not reliable enough, as the server code appears to enter bad state and transactions stop working.
const hre = require('hardhat');
async function test() {
const block = parseInt(await web3.eth.getBlockNumber());
await hre.network.provider.request({
method: "hardhat_reset",
params: [{forking: {
jsonRpcUrl: ALCHEMY_URL,
blockNumber: block,
}}]
});
}