matter-labs/hardhat-zksync

`reverted` and `revertedWith` does not working using @matterlabs/hardhat-zksync-chai-matchers

tkowalczyk opened this issue · 7 comments

I have to following function in smart contract:

function mintNft() external payable {
        require(
            _tokenIdCounter.current() <= MAX_SUPPLY,
            "Supply reached: no more mint allowed"
        );
        require(
            balanceOf(msg.sender) == 0,
            "Balance reached: one mint per wallet allowed"
        );
        require(
            msg.value >= ethFee,
            "Insufficient fee: the required fee must be covered"
        );
        _tokenIdCounter.increment();
        uint256 tokenId = _tokenIdCounter.current();

        _safeMint(msg.sender, tokenId);

        uint256 dust = msg.value - ethFee;
        (bool sent, ) = address(msg.sender).call{value: dust}("");
        require(sent, "Failed to return overpayment");
    }

which I would like to test to check if reverts properly:

it("Should revert when msg.value lower than 0.0005", async function () {
        const { deployer } = await deployContract();
        const zkNFT = await deployzkNFT(deployer);
        const mintTxn = await zkNFT.mintNft({
            value: ethers.utils.parseEther("0.0004")
        });

        const txn = await mintTxn.wait();

        expect(txn).to.be.reverted;
    });

but this test is always giving me a wrong result:

obraz

Just add gasLimit: 3e7 after value in the mintNft funtion call to make the Tx go through

const mintTxn = await zkNFT.mintNft({
            value: ethers.utils.parseEther("0.0004"),
            gasLimit: 3e7
        });

Its not working for me either. If i include both @matterlabs/hardhat-zksync-chai-matchers, @nomicfoundation/hardhat-chai-matchers or only nomicfoundation plugin in hardhat config, i get the same error as the op.

If i only include the zksync plugin i get the error Error: Invalid Chai property: revertedWithCustomError

Hello, can you send me a hardhat config file? Since the tasks are overridden, '@matterlabs/hardhat-zksync-chai-matchers' should be imported after you import '@nomicfoundation/hardhat-chai-matchers'.

reverted seems to work with the following imports. But revertedWith is still giving Invalid Chai property

import "@matterlabs/hardhat-zksync-verify";
import "@matterlabs/hardhat-zksync-solc";
import "@matterlabs/hardhat-zksync-chai-matchers";
import "@matterlabs/hardhat-zksync-upgradable";

import "solidity-coverage";
import "@typechain/hardhat";
import "hardhat-gas-reporter";
import "hardhat-packager";
import "@primitivefi/hardhat-dodoc";
import "@openzeppelin/hardhat-upgrades";
import "@nomiclabs/hardhat-waffle";

Everything works for me with your imports with my dummy project, could you give me your repo so I have the whole picture?

the repo is in a private monorepo 😕 can you share your dummy repo?

There is my contract and test, I use your imports on hardhat config.

`// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

contract Greeter {

    string greeting;

    constructor() {
        greeting = "Hello, World!";
    }

    function greet() public view returns (string memory) {
        return greeting;
    }

    function revertWithMesage() public pure {
        require(
            false,
            "Revert with a payable error message string"
        );
    }
}
`
  it("Should revert", async function () {
    const p = new Provider("http://localhost:3050");
    const w = new Wallet("0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110", p);
    const deploy = new Deployer(hre, w);

    const artifact = await deploy.loadArtifact("Greeter");
    const contract = await deploy.deploy(artifact, []);

    const sender = new Wallet("0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110", p);
    const receiver = Wallet.createRandom();
    
    await expect(() =>
    sender.transfer({
      to: receiver.address,
      amount: 2000,
    })
  ).to.changeEtherBalance(sender.address, BigInt("-2000"));

  await expect(contract.revertWithMesage()).to.be.revertedWith("Revert with a payable error message string");
});