ApeWorX / ape

The smart contract development tool for Pythonistas, Data Scientists, and Security Professionals
https://apeworx.io
Apache License 2.0
868 stars 133 forks source link

Add error type support to `ape.reverts` #1223

Closed fubuloubu closed 1 year ago

fubuloubu commented 1 year ago

Overview

Solidity v0.8.x series added a new type of item to the abi called custom errors: https://docs.soliditylang.org/en/v0.8.17/structure-of-a-contract.html#errors

We have support for parsing these errors from the ABI as of ethpm-types v0.2.1

The basic gist is that when a revert happens, Solidity will return abi-encoded dated corresponding to the error type with additional information about the error. Some of the built-in Solidity errors are as follows (it might be good to add an enum with these to ape-ethereum or ape-solidity):

image

Specification

It would be nice to use these error types in tests w/ ape.reverts. Some examples might include:

# succeds if `CustomError` is raised with any args
with ape.reverts(my_contract.CustomError):
    my_contract.call_that_raises_CustomError(...)

# succeds if `CustomError` is raised with `arg1 = some_value`, fails otherwise
with ape.reverts(my_contract.CustomError, arg1=some_value):
    my_contract.call_that_raises_CustomErrorWithArgs(...)

Dependencies

n/a

pcaversaccio commented 1 year ago

To be specific, Solidity 0.8.4 introduced Custom Errors.

fubuloubu commented 1 year ago

Alternative syntax:

with my_contract.CustomError(_, _, ..., _):
    my_contract.call_that_raises_CustomError(...)

with my_contract.CustomError(some_value, _, ..., _):
    my_contract.call_that_raises_CustomErrorWithArgs(...)

But the use of the wildcard _ might be complex

pcaversaccio commented 1 year ago

KISS - I would definitely go with version 1.

fubuloubu commented 1 year ago

KISS - I would definitely go with version 1.

Yeah I agree, but I did like the ability to do with CustomErrorType: ... which I think it's a bit nicer looking syntax

pcaversaccio commented 1 year ago

I see - maybe worth introducing an alias later?

istankovic commented 1 year ago

@fubuloubu any progress/news on this?

antazoey commented 1 year ago

@fubuloubu any progress/news on this?

PR open https://github.com/ApeWorX/ape/pull/1380