is this a bug?
the compiler insists a function should still be a pure function even if it has custom errors in it, but if a revert using the custom error is executed, it actually throws a completely different error.
I presume because the VM has to go to the state to retrieve the custom error and that's not on for a pure function?
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 a warning about could use pure), the non-test function which calculates and returns a number went crazy haywire.