vyper icon indicating copy to clipboard operation
vyper copied to clipboard

Support Custom Errors

Open skeletor-spaceman opened this issue 3 years ago • 6 comments

Version Information

  • vyper Version (output of vyper --version): 0.3.3
  • OS: linux
  • Python Version (output of python --version): 3.8

What's your issue about?

I want to mimic the behavior of custom errors in solidity: https://blog.soliditylang.org/2021/04/21/custom-errors/ Right now I'm just trying to raise an error without parameters, adding parameters would be nice to have.

Should be able to raise bytes4 | Bytes[4] but encountering following errors:

vyper.exceptions.TypeMismatch: Given reference has type Bytes[4], expected String[1024] vyper.exceptions.InvalidType: Expected String[1024] but literal can only be cast as bytes4

Tried all these different options with no success:

    if msg.sender != self.governance: raise keccak256('Governable_OnlyGovernance()')
    if msg.sender != self.governance: raise convert(method_id('Governable_OnlyGovernance()', output_type=Bytes[4]), String[1024]) # Compiler panic
    if msg.sender != self.governance: raise convert(method_id('Governable_OnlyGovernance()'), String[4])
    if msg.sender != self.governance: raise _abi_encode(method_id=method_id('Governable_OnlyGovernance()'))
    if msg.sender != self.governance: raise method_id('Governable_OnlyGovernance()')
    if msg.sender != self.governance: raise 0xe0ae9db4

skeletor-spaceman avatar Jul 27 '22 13:07 skeletor-spaceman

meeting notes: punt for now as testing frameworks can use other means of error detection. implement if some production smart contracts require handling or issuing of specific custom errors.

charles-cooper avatar Aug 08 '22 16:08 charles-cooper

idea for how we can export for debugging:

# example: x, y and z are local variables
if x > 10:
  raise  # @error My Custom Error, @trace x,y
  # no runtime reason, tooling should print something like "My Custom Error, x = 11, y = ..."

charles-cooper avatar Aug 13 '22 20:08 charles-cooper

Here's a use-case where production smart contracts require issuing of specific custom errors: https://eips.ethereum.org/EIPS/eip-3668

Contracts wishing to support lookup of data from external sources may, instead of returning the data directly, revert using OffchainLookup(address sender, string[] urls, bytes callData, bytes4 callbackFunction, bytes extraData). Clients supporting this specification then make an RPC call to a URL from urls, supplying callData, and getting back an opaque byte string response. Finally, clients call the function specified by callbackFunction on the contract, providing response and extraData. The contract can then decode and verify the returned data using an implementation-specific method.

z80dev avatar Oct 03 '22 03:10 z80dev

raw_revert as implemented in https://github.com/vyperlang/vyper/pull/3136 should address this particular use case

charles-cooper avatar Dec 19 '22 23:12 charles-cooper

We should consider adding custom errors to .vyi files since they are already part of some EIPs: https://eips.ethereum.org/EIPS/eip-6093.

pcaversaccio avatar Feb 28 '24 13:02 pcaversaccio

custom errors is basically a requirement now

fubuloubu avatar Feb 28 '24 23:02 fubuloubu