dazito
dazito

Reputation: 7980

Why is waffle showing as undefined when using hardhat

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?

Upvotes: 2

Views: 1551

Answers (3)

Skar
Skar

Reputation: 91

@user3358107 answered in a comment:

add this line to hardhat.config.js:

require("@nomiclabs/hardhat-waffle");

Upvotes: 2

Stierney1127
Stierney1127

Reputation: 1

Try changing const mainnetForkProvider = waffle.provider; to:

const mainnetForkProvider = network.provider;

Surprised you didn't have to change receipt to:

const txReceipt = txSwap.wait();

Upvotes: 0

Malena
Malena

Reputation: 1

Try these versions:

npm install --save-dev hardhat@^2.9.3 @nomiclabs/hardhat-waffle@^2.0.0 ethereum-waffle@^3.0.0 chai@^4.2.0 @nomiclabs/hardhat-ethers@^2.0.0 ethers@^5.0.0

Upvotes: 0

Related Questions