EIPs Explained

ERC-7751 · Final · ERC · 2024-08-06

ERC-7751: Wrapping of bubbled up reverts

A standard WrappedError custom error that contracts throw when re-raising a revert from a call, adding which contract and function failed so tools can show readable nested error traces.

What problem does it solve?

When contract A calls contract B and B reverts, A usually "bubbles up" B's revert data unchanged. By the time the error reaches the user or a block explorer, you see something like Error("insufficient balance") but not which contract or call produced it. In a chain of routers, vaults and tokens that makes debugging slow and guessy.

ERC-7751 standardises a single custom error for wrapping a bubbled-up revert with context. Explorers, debuggers and frontends can then decode it the same way everywhere, and even unwrap nested failures layer by layer.

How it works

A contract that wants to wrap a revert reverts with:

error WrappedError(address target, bytes4 selector, bytes reason, bytes details);
// selector: 0x90bfb865
Field Meaning
target The called contract that reverted
selector The function selector that was called; bytes4(0) for a plain ETH transfer with no data
reason The raw revert bytes returned by the callee
details Optional extra context; empty, or an ABI-encoded custom error declared by the wrapping contract

Because reason can itself be a WrappedError, errors nest naturally. A tool can decode the outer error, then decode reason, and so on, producing a stack-trace-like view from the outermost caller down to the original failure.

Sample code

Wrapping a failing token transfer in Solidity:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IERC20 { function transfer(address to, uint256 v) external returns (bool); }

contract Vault {
    error WrappedError(address target, bytes4 selector, bytes reason, bytes details);
    error TransferFailed(address recipient);

    IERC20 public immutable token;
    constructor(IERC20 t) { token = t; }

    function withdraw(address to, uint256 amount) external {
        try token.transfer(to, amount) {} catch (bytes memory reason) {
            revert WrappedError(
                address(token),
                IERC20.transfer.selector,
                reason,
                abi.encodeWithSelector(TransferFailed.selector, to)
            );
        }
    }
}

Unwrapping nested errors off-chain (ethers v6):

import { Interface } from "ethers";

const iface = new Interface([
  "error WrappedError(address target, bytes4 selector, bytes reason, bytes details)",
]);

function unwrap(data: string, depth = 0): void {
  const err = iface.parseError(data);
  if (!err) { console.log(" ".repeat(depth * 2) + "root revert:", data); return; }
  const [target, selector, reason] = err.args;
  console.log(" ".repeat(depth * 2) + `call ${selector} on ${target} failed`);
  unwrap(reason, depth + 1);
}

Things to know

  • Status: Final. Adoption is optional and incremental; it introduces no backwards incompatibility.
  • The data is self-reported. A contract can drop, suppress or misreport a wrapped revert, so treat it as a debugging aid, not proof.
  • Wrapping changes the outer error selector. Callers that match on a specific inner error must unwrap first.
  • details must be a custom error declared on the wrapping contract, so tools can decode it with that contract's ABI.

#errors #debugging #solidity #tooling

Details

Authors
Daniel Gretzke (@gretzke), Sara Reynolds (@snreynolds), Alice Henshaw (@hensha256), Marko Veniger <[email protected]>, Hadrien Croubois (@Amxx)
Status
Final
Type
Standards Track / ERC
Created
2024-08-06

Machine-readable: Markdown · JSON