ABI types (Abi)
Solidity types as values, and the encoding of what they hold. A type is an Effect Schema, so
a type of your own is a transformation of the type it is stored as. Functions, events and
errors are defined with these types, and a whole contract can come from signatures or from a
JSON ABI.
import { Effect } from "effect";import { Abi, Address } from "@ethect/core";
// struct Order { address maker; uint256 amount; uint48 deadline; bytes32[] proof; }const Order = Abi.struct({ maker: Abi.address, amount: Abi.uint256, deadline: Abi.uint48, proof: Abi.array(Abi.bytes32),});
const MAKER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Effect.gen(function* () { const order = { maker: MAKER, amount: 10n ** 18n, deadline: 1_900_000_000, proof: [] }; const data = yield* Abi.encode(Order, order); // "0x0000…": the bytes `abi.encode` gives return yield* Abi.decode(Order, data); // the same order});| Export | Kind | Description |
|---|---|---|
Abi.address |
Solidity type | address, as an Address.Address. |
Abi.bool |
Solidity type | bool, as a boolean. |
Abi.string |
Solidity type | string, as a string. |
Abi.bytes |
Solidity type | bytes, as a Hex.Hex. |
Abi.bytes1 to Abi.bytes32 |
Solidity type | Bytes of a fixed length, as a Hex.Hex. |
Abi.uint8 to Abi.uint256 |
Solidity type | An unsigned integer: a number up to 48 bits, a bigint above. |
Abi.int8 to Abi.int256 |
Solidity type | A signed integer, with the same rule. |
Abi.array(item, length?) |
function | T[], or T[length], as a ReadonlyArray. |
Abi.tuple(items) |
function | Unnamed components, as a readonly tuple. |
Abi.struct(fields) |
function | Named components, as a readonly object. The fields are in the order they are written. |
Abi.Class<Self>(name)(fields) |
function | A struct as a class, to add methods to. |
Abi.indexed(type, stored?) |
function | Marks an event field as stored in a topic: it can be filtered on. A reference type is stored as its hash. |
Abi.encode(type, value) |
function | The ABI encoding of a value, or of several: abi.encode. |
Abi.decode(type, data) |
function | The value that ABI-encoded bytes hold, or the values: abi.decode. |
Abi.parse(signatures) |
function | A Contract from human-readable signatures. |
Abi.fromJson(abi) |
function | A Contract from a JSON ABI, typed when it is declared as const. |
Abi.Type<T> |
type | A Solidity type whose values are a T in TypeScript. |
Abi.Reference<T> |
type | A type that says it is a reference type: a string, bytes, an array, a tuple or a struct. |
Abi.Indexed<T> |
type | A type marked with Abi.indexed. |
Abi.Value |
type | The plain value the ABI stores, which every type encodes to: a bigint, a boolean, a string, or a list or an object of them. |
Abi.Fields |
type | Types by name: the fields of a struct, an event or an error. |
Encoding and decoding fail with an AbiError, as a value.
Nothing here throws, and nothing here asks a node.
A type is a schema
Section titled “A type is a schema”An Abi.Type<T> is an Effect Schema. Its type side is the T you use in code. Its encoded
side is the plain value the ABI stores, an Abi.Value: a bigint for an integer, hex for bytes
and addresses. On top of that it knows how the plain value is laid out in bytes. So an
Abi.Type<T> is a Schema.Codec<T, Abi.Value>, which is what a type of your own shows as.
encode value ── schema ──▶ plain value ── layout ──▶ bytesdecode bytes ── layout ──▶ plain value ── schema ──▶ valueSo a type goes wherever a schema goes: in a Schema.Struct of your own, in a form, in a
property test.
import { Schema } from "effect";import { Abi } from "@ethect/core";
const isFee = Schema.is(Abi.uint24); // a whole number from 0 to 16777215const Pool = Schema.Struct({ name: Schema.String, fee: Abi.uint24 });Reference types
Section titled “Reference types”A string, bytes, an array, a tuple and a struct are what Solidity calls reference types.
Abi gives its own as an Abi.Reference<T>: an Abi.Type<T> that also has reference: true.
It goes wherever a type goes.
Abi gives |
As |
|---|---|
address, bool, uintN, intN, bytesN |
Abi.Type<T> |
string, bytes, array(…), tuple(…), struct(…), Class(…)(…) |
Abi.Reference<T> |
It matters in one place. A topic of a log is one word, which holds no such value: an indexed field of a reference type is the hash of its value, and its type says so.
A transformation of a type is a new type. It is laid out as the type it is built on, and it
does not have the mark: Abi.string.pipe(Schema.brand("Label")) is an Abi.Type and no
longer an Abi.Reference.
Custom types
Section titled “Custom types”A type of your own is a transformation of the type it is stored as. Write it with
Schema.decodeTo, where you use it. The result is still a Solidity type: it encodes, it
decodes, and it goes in a struct, an array or the inputs of a function.
import { Effect, Schema } from "effect";import { Abi } from "@ethect/core";
// An amount of USDC: a bigint that cannot be taken for another number.const Usdc = Schema.BigInt.pipe(Schema.brand("Usdc"));type Usdc = typeof Usdc.Type;
// Stored as a uint256, used as a Usdc.const UsdcAmount = Abi.uint256.pipe(Schema.decodeTo(Usdc));
const Payment = Abi.struct({ to: Abi.address, amount: UsdcAmount });
const program = Effect.gen(function* () { const amount: Usdc = Usdc.make(1_500_000n); const data = yield* Abi.encode(UsdcAmount, amount); const back: Usdc = yield* Abi.decode(UsdcAmount, data); // @ts-expect-error a plain bigint is not an amount of USDC yield* Abi.encode(UsdcAmount, 1_500_000n); return back;});A transformation with a decode and an encode of its own works the same way. Here an amount is a class.
import { Schema, SchemaGetter } from "effect";import { Abi } from "@ethect/core";
class Wei { readonly value: bigint; constructor(value: bigint) { this.value = value; }}
const WeiAmount = Abi.uint256.pipe( Schema.decodeTo(Schema.instanceOf(Wei), { decode: SchemaGetter.transform((value: bigint) => new Wei(value)), encode: SchemaGetter.transform((wei: Wei) => wei.value), }),);- The stored type still checks. A
Weiof-1nfails to encode:-1is not auint256. - Your checks are added.
Abi.uint256.check(Schema.isGreaterThanBigInt(0n))refuses zero, when encoding and when decoding. - A transformation is synchronous. One that throws, or that waits, fails with an
AbiError. - There is no registry. A custom type is a value you import where you need it.
Left out
Section titled “Left out”| Not here | Use instead |
|---|---|
abi.encodePacked |
Nothing yet. Packed encoding has no decoder, and it is not what a call carries. |
The function type |
Abi.bytes24: an address followed by a selector. |
Fixed-point numbers, fixed and ufixed |
Nothing: Solidity cannot assign to them, and no ABI in use has one. |