ERC-838 · Draft · ERC · 2020-08-20
ERC-838: ABI specification for REVERT reason string
A Draft proposal to declare typed errors in a contract's JSON ABI and encode them in REVERT data with a selector, so clients can decode why a transaction failed.
What problem does it solve?
The REVERT opcode (EIP-140) lets a contract stop execution, undo its changes, and return some bytes explaining why. At first the only convention for those bytes was a free-form string. A string is fine for people, but software cannot reliably branch on it, it cannot carry structured values, and long messages cost gas.
ERC-838 proposes treating errors like functions and events: declare them in the ABI with a name and typed arguments, and encode a selector plus the arguments in the revert data. Wallets and libraries can then look up the selector and show a precise, decoded error.
How it works
- Compilers that produce a JSON ABI should include error entries next to functions and events, with
"type": "error", aname, and typed arguments. - A selector is computed from the error's signature, the same way as for a public function.
- That selector must appear in the revert data so clients can look it up, followed by the encoded arguments.
- A high-level language can then offer syntax for declaring and throwing errors.
Possible extensions in the spec include a NatSpec @notice on an error to give a human-readable message, and listing the errors a function may throw in that function's ABI entry.
The Draft leaves the exact byte format, the rationale and security considerations "to be defined" or "needs discussion".
Sample code
The ABI entry shape from the proposal:
{ "type": "error", "name": "InsufficientBalance",
"arguments": [ { "name": "amount", "type": "uint256" } ] }
What this looks like in practice with Solidity custom errors, which follow the same idea:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Vault {
/// @notice You don't have enough funds to withdraw `amount`.
error InsufficientBalance(uint256 available, uint256 amount);
mapping(address => uint256) public balance;
function withdraw(uint256 amount) external {
uint256 bal = balance[msg.sender];
if (bal < amount) revert InsufficientBalance(bal, amount);
balance[msg.sender] = bal - amount;
payable(msg.sender).transfer(amount);
}
}
Decoding the error off chain with ethers v6:
import { Interface } from "ethers";
const iface = new Interface(["error InsufficientBalance(uint256 available, uint256 amount)"]);
const parsed = iface.parseError(revertData); // revertData: hex string from the failed call
console.log(parsed?.name, parsed?.args); // "InsufficientBalance", [10n, 25n]
Things to know
- This proposal is still a Draft and was never finalized. It has no binding byte format.
- In practice the ecosystem settled on what Solidity shipped: custom errors encoded as a 4-byte selector followed by standard ABI-encoded arguments, and ABI entries of
"type": "error"with aninputsarray (notarguments, as in the Draft). - Plain
require(cond, "msg")strings are encoded as the built-inError(string)error, selector0x08c379a0. - ERC-6093 standardizes common custom error names for token contracts.