Skip to content

Err.decode

Reads revert data as one of the errors you give, or as a plain revert. It never fails.

import { Abi, Err, type Hex } from "@ethect/core";
class ExceededMaxDeposit extends Err.make("ERC4626ExceededMaxDeposit", {
receiver: Abi.address,
assets: Abi.uint256,
max: Abi.uint256,
}) {}
// what a node returned for a call that reverted
const explain = (data: Hex.Hex): string => {
const error = Err.decode([ExceededMaxDeposit], data);
return error._tag === "ERC4626ExceededMaxDeposit"
? `The vault takes ${error.max} more`
: error.message;
};

You rarely call it yourself: a read and a send decode a revert with the errors of the function called. It is for revert data you got another way: a trace, an eth_estimateGas that failed, a transaction replayed by hand.

The data starts with It decodes to
the selector of one of the errors an instance of that error
0x08c379a0, Error(string) a Revert.Reason, with the message
0x4e487b71, Panic(uint256) a Revert.Panic, with the code
anything else, or nothing a Revert.Unknown, with the selector and the data
import { Err } from "@ethect/core";
// an overflow
const panic = Err.decode([], `0x4e487b71${"11".padStart(64, "0")}`);
// revert(), with nothing
const nothing = Err.decode([], "0x");
// an error that is not among those given
const unknown = Err.decode([], "0xdeadbeef");
panic.message; // "Panic 0x11: an arithmetic operation overflowed or underflowed"
nothing.message; // "Reverted without data"
unknown.message; // "Reverted with data no declared error matches: 0xdeadbeef"

Err.of gives the errors that calls declare, to decode a revert of one of them.

import { Err, type Call, type Hex } from "@ethect/core";
const reasonOf = <E>(calls: ReadonlyArray<Call.Call<unknown, E>>, data: Hex.Hex) =>
Err.decode(Err.of(calls), data);

InstanceType<Errors[number]> | Revert.Revert

An instance of one of the errors, or one of the three plain reverts. Every one has a _tag and a message.

  • Type: ReadonlyArray<new (value: any) => unknown>

The error classes the data may be. An empty array reads plain reverts only.

The revert data.

  • It never fails and never throws. It is a plain function, not an effect.
  • Data that has the selector of an error and does not decode is a Revert.Unknown. The same goes for an Error(string) or a Panic(uint256) whose data is cut short.
  • The first error that has the selector is the one. Two classes of one signature decode to the first of the list.
  • A class that Err.make did not build is skipped.
  • A selector is compared in lowercase. Data in uppercase decodes the same.