I have installed hardhat and forked mainnet using
npx hardhat node --fork https://eth-mainnet.g.alchemy.com/v2/my-api-key
Now while developing a very simple test like:
const { expect, assert} = require("chai");
const { ethers, waffle } = require("hardhat")
describe("Read and write to the blockchain", () => {
it('should send a transaction (swap a token)', async function () {
const [ownerSigner] = await ethers.getSigners()
const mainNetForkUniswapRouter = new ethers.Contract(addressRouter, routerABI, ownerSigner);
const amountOut = await getAmountOut()
const myAddress = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
const txSwap = await mainNetForkUniswapRouter.swapExactTokensForTokens(
amountIn, // amountIn
amountOut, // amountOut
[addressFrom, addressTo], // path
myAddress, // address to
Date.now() + 1000 + 60 + 5, // deadline
{
gasLimit: 200000,
gasPrice: ethers.utils.parseUnits("5.5", "gwei")
}// gas
);
assert(txSwap.hash)
console.log("The waffle")
console.log(waffle)
console.log("--- --- --- --- --- --- ---")
const mainnetForkProvider = waffle.provider;
const txReceipt = await mainnetForkProvider.getTransactionReceipt(txSwap.hash)
console.log("")
console.log("Swap tx")
console.log(txSwap)
console.log("")
console.log("Tx Receipt")
console.log(txReceipt)
});
});
When running this test with npx hardhat test I am getting the following error message:
TypeError: Cannot read properties of undefined (reading 'provider')
at Context.<anonymous> (test/sendSwapTx.js:84:44)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at runNextTicks (node:internal/process/task_queues:65:3)
at listOnTimeout (node:internal/timers:528:9)
at processTimers (node:internal/timers:502:7)
The stacktrace points to line const mainnetForkProvider = waffle.provider;
It seems for some reason waffle is undefined but I cannot understand why because I am importing it at the top of the code.
Any help on how to fix this error?