Skip to content

Abi.decode

Decodes ABI bytes to one value, or to several, as strictly as abi.decode does.

import { Abi, type Hex } from "@ethect/core";
// one type, or a list of types
const read = (data: Hex.Hex) => ({
one: Abi.decode(Abi.string, data), // abi.decode(data, (string))
several: Abi.decode([Abi.uint256, Abi.string], data), // abi.decode(data, (uint256, string))
});

A list gives a readonly tuple, with one value for each type.

import { Effect } from "effect";
import { Abi, type Hex } from "@ethect/core";
// what `getReserves()` returns
const reserves = (data: Hex.Hex) =>
Effect.gen(function* () {
const types = [Abi.uint112, Abi.uint112, Abi.uint32] as const;
const [reserve0, reserve1, updatedAt] = yield* Abi.decode(types, data);
return { reserve0, reserve1, updatedAt }; // bigint, bigint, number
});

A list of types and a tuple of the same types read different data, as in Solidity.

The types are Abi.decode([A, B], data) next to Abi.decode(Abi.tuple([A, B]), data)
static only the same data, the same values
one of them dynamic: bytes, string, T[] the tuple expects one word more at the start: 0x20, its own offset

What a function returns for returns (uint256, string memory) is two values: decode it with a list. A function that returns one struct returns one value: decode it with the struct.

A failure is a value. The message says which bytes, and why.

import { Effect } from "effect";
import { Abi } from "@ethect/core";
const flag = Abi.decode(Abi.bool, `0x${"00".repeat(31)}02`).pipe(
Effect.catchTag("AbiError", (error) => Effect.succeed(error.message)),
);
// succeeds with "Cannot decode bool: The word at byte 0 is not bool: 0x0000…0002"

Effect<T, AbiError>, or Effect<readonly [A, B, …], AbiError> for a list

The value, or for a list of types a readonly tuple of the values. What T is for each type is in this table. It needs no service: no node is asked.

  • Type: Abi.Type<T>, or a list of them

The Solidity type the bytes hold: one of Abi’s, or a type of your own. A list of types reads several values. An empty list reads none, whatever the data.

The bytes, as hex: what Abi.encode gives, or what a node returned. Letters of either case are read.

Decoding is strict, as Solidity’s own decoder is. Data that fails a check fails with an AbiError: nothing is read outside the data, and nothing is guessed.

Rule Refused
An offset stays in the data an offset past the end, or one whose value is cut short
A length stays in the data a bytes longer than what is left, an array of more items than the data can hold
A bool is 0 or 1 2, or any other word
An address is 20 bytes bits above them
A uintN fits N bits bits above them: 0x100 as a uint8
An intN carries its sign to the left 0x80 as an int8: -128 is written with ones all the way
A bytesN is N bytes bits below them
The data is read 32 times over at most data built so that its offsets make the same bytes be read again and again: see Behavior
The hex is whole bytes an odd number of digits, a digit that is not hex

What is let through, as Solidity lets it through:

Let through Read as
Bytes after the last value ignored
An offset that leaves a gap, or that is out of order followed
Offsets that point at the same bytes followed: the same value, twice
Bits in what pads a bytes or a string to a whole word ignored
A string that is not UTF-8 U+FFFD for each invalid sequence
  • Two steps. The layout reads the plain ABI value from the bytes, checking them. Then the type’s schema turns it into the value: a custom type transforms it there.
  • The work is bounded by the data. Decoding reads at most 32 times as many bytes as the data has. A usual encoding reads each byte once, and values that share their bytes are read once each. Data whose offsets all point at the same array, to make it be read a million times, fails instead.
  • Addresses and bytes come back in lowercase. Call Address.checksum to display an address.
  • A struct is built by position, with its keys in the order of the layout. A class gives instances.
  • One type is a list of one. Abi.decode(Abi.string, data) reads what Abi.decode([Abi.string], data) reads, and gives the value alone.
  • Calldata starts with a selector. Its four bytes are not part of the encoding.
Error When
AbiError the data fails one of the checks
AbiError a custom type refuses the decoded value, with a check or a transformation of its own
AbiError type is not a Solidity type