diff --git a/EIPS/eip-838.md b/EIPS/eip-838.md new file mode 100644 index 00000000000000..b60cd87c325291 --- /dev/null +++ b/EIPS/eip-838.md @@ -0,0 +1,96 @@ +--- +eip: 838 +title: ABI specification for REVERT reason +author: Federico Bond (@federicobond), Leonid Logvinov (@LogvinovLeon) +discussions-to: +status: Draft +type: ERC +category: ERC +created: 2019-01-31 +--- + + + +## Simple Summary + + + +A proposal to extend the ABI specification to include typed errors in the REVERT reason. + +## Abstract + + + +This proposal specifies how to encode potential error conditions in the JSON ABI of a smart contract. A high-level language could then provide a syntax for declaring and throwing these errors. The compiler will encode these errors in the reason parameter of the REVERT opcode in a way that can be easily reconstructed by libraries such as `ethers` or `web3`. + +## Motivation + + + +It's important to provide clear feedback to users (and developers) about what went wrong with their Ethereum transactions. The `REVERT` opcode is a step in the right direction, as it allows smart contract developers to encode a message describing the failure in the reason parameter. There is an implementation released by Solidity that accepts a `string`, thus providing a low-level interface to this parameter. However, standardizing a method for passing errors from this parameter back to clients will bring many benefits to both users and developers. + +The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119. + +## Specification + + + +To conform to this specification, compilers producing JSON ABIs SHOULD include error declarations alongside functions and events. Each error object MUST contain the keys `name` (string) and `arguments` (same types as the function’s `inputs` list). The value of `type` MUST be "error". +Example: + +```json +{ + "type": "error", + "name": "InsufficientBalance", + "arguments": [ + { + "name": "amount", + "type": "uint256" + } + ] +} +``` + +A selector for this error can be computed from its signature (`InsufficientBalance(uint256)` for the example above) in the same way that it's currently done for public functions and events. Any arguments for the error together with the selector are ABI encoded in the same way as call data for functions. + +A high-level language like Solidity can then implement a syntax like this: + +```solidity +contract MyToken { + error InsufficientFunds(uint256 amount); + + function transfer(address _to, uint256 _amount) { + if (balances[msg.sender] <= _amount) { + revert InsufficientFunds(_amount); + } + } +} +``` + +## Rationale + + + +This specific encoding was choosen because it is the same one Solidity uses at this moment for REVERTs. This PR just proposes to allow for user-defined errro ABIs instead of hard-coding `Error(string)`. + +In terms of previous work - there is one other [EIP 207](https://github.com/ethereum/EIPs/pull/207). It proposes to use **CBOR** encoding. In my oponion - there is no need to introduce another encoding scheme because the ABI encoding is flexible fast cheap and supported already. + +## Backwards Compatibility + + + +Apps and tools that have not implemented this spec are checking for the old error selector. The only way to break that is to define an error ABI `Error(string)` (or to find a hash collision), but in that case - our data is a string - so they will work correctly. This EIP is a superset of a current solidity feature and therefore is backwards compatible. + +## Test Cases + + + +## Implementation + + + +Not started yet. + +## Copyright + +Copyright and related rights waived via [CC0](https://creativecommons.org/publicdomain/zero/1.0/).