in the code below, if a revert using the custom error inside the pure function it reverts with a different error (below). If I do the same things without pure, it reverts with the custom error as expected
After @hroussille comment I tried it in remix but couldn't recreate the issue, so it seems I'm having an issue with hardhat.
test contract
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;
contract RevertProblem {
error aCustomError(string errorText);
function test1() public pure {
revert ("BadHappened");
}
function test2() public {
revert aCustomError("BadHappened");
}
function test3() public pure {
revert aCustomError("BadHappened");
}
}
test script
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Should revert with expected message", async function() {
it("not so pure", async function () {
const rpContractFactory = await ethers.getContractFactory('RevertProblem')
const rpContract = await rpContractFactory.deploy()
await rpContract.deployed()
await expect(rpContract.test1()).to.be.revertedWith("BadHappened")
await expect(rpContract.test2()).to.be.revertedWith('aCustomError("BadHappened")')
await expect(rpContract.test3()).to.be.revertedWith('aCustomError("BadHappened")')
})
});
the first two functions are fine. the third one reverts with
AssertionError: Expected transaction to be reverted with aCustomError("BadHappened"), but other exception was thrown: Error: call revert exception [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="test3()", data="0x062089cf0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b42616448617070656e6564000000000000000000000000000000000000000000", errorArgs=["BadHappened"], errorName="aCustomError", errorSignature="aCustomError(string)", reason=null, code=CALL_EXCEPTION, version=abi/5.6.3)
It should revert with something like
Error: VM Exception while processing transaction: reverted with custom error 'aCustomError("BadHappened")'
If I take the pure keyword out of my non-test code the function reverts correctly, but even though it compiles OK (just get a warning about should use pure), the non-test function which calculates and returns a number went crazy haywire.
├── @nomiclabs/[email protected]
├── @nomiclabs/[email protected]
├── @openzeppelin/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
pure
is allowed and advised. What version of hardhat do you use ? Your test works fine on my side (hardhat 2.9.1 and 2.9.7).