4

I am attempting to expectRevert on a custom error with MULTIPLE parameters by following the example in the office Foundry. I am able to get it to work with a single parameter. But as soon as I add a second parameter to the custom error I get an error in the forge test runner.

https://book.getfoundry.sh/cheatcodes/expect-revert

Does anyone know what could be causing this issue? Any direction would be greatly appreciated.

Contract

error MaximumContributionExceeded(uint256 isIndividual, uint256 temp);
...
revert MaximumContributionExceeded({isIndividual: 1, temp: 2});

Test (Foundry Test Contract)

bytes4 selector = bytes4(keccak256("MaximumContributionExceeded(uint256, uint256)"));
vm.expectRevert(abi.encodeWithSelector(selector, 1, 2));
ico.contribute{value: 1501 ether}();

Error

[FAIL. Reason: Error != expected error: 0xd38ddc2300000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002 != 0x1d3d86b7] 

Full code https://gist.github.com/bd9b4964bc96e88cb6da5a2d59e5672c

3 Answers 3

4

I had to remove the space between uint256 and uint256 on this line: bytes4 selector = bytes4(keccak256("MaximumContributionExceeded(uint256,uint256)"));

1
  • Very nice! Thanks
    – Russo
    Commented Nov 1, 2023 at 3:58
0

While the selected answer by currenthandle should fix your issue, I would suggest programmatically using the error selector.

vm.expectRevert(MyContract.MaximumContributionExceeded.selector, 1, 2)
0

If your custom errors are declared inside the contract, and they do not have params, you can simply do:

vm.expectRevert(MyContract.MaximumContributionExceeded.selector);

If they do have params, you do:

vm.expectRevert(
    abi.encodeWithSelector(
        MyContract.MaximumContributionExceeded.selector,
        1,
        2
    )
);

See these examples in the Foundry Book.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.