Skip to main content
add hardhat tag
Link
sola24
  • 1.2k
  • 4
  • 21
reworded as have narrowed it down to a local hardhat issue
Source Link
sola24
  • 1.2k
  • 4
  • 21

is this a bug? the compiler insists a function should still be a pure function even if it has custom errors in itthe code below, but if a revert using the custom error is executed,inside the pure function it actually throwsreverts with a completely different error (below). I presume because the VM has to go to If I do the state to retrievesame things without pure, it reverts with the custom error and that's not on for a pure function?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.

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 couldshould 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]

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?

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.

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.

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]
added something else I discovered
Source Link
sola24
  • 1.2k
  • 4
  • 21

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.

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")'

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.

Source Link
sola24
  • 1.2k
  • 4
  • 21
Loading