Skip to content

Abi.struct

Several values as one, by name. A value is a readonly object, and the fields are laid out in the order you write them.

import { Effect } from "effect";
import { Abi, Address } from "@ethect/core";
// struct MarketParams { address loanToken; address collateralToken; address oracle; address irm; uint256 lltv; }
const MarketParams = Abi.struct({
loanToken: Abi.address,
collateralToken: Abi.address,
oracle: Abi.address,
irm: Abi.address,
lltv: Abi.uint256,
});
const TOKEN = Address.makeUnsafe("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48");
const program = Effect.gen(function* () {
const market = { loanToken: TOKEN, collateralToken: TOKEN, oracle: TOKEN, irm: TOKEN, lltv: 860000000000000000n };
const data = yield* Abi.encode(MarketParams, market);
return yield* Abi.decode(MarketParams, data);
});

A struct is a type like another: put it in an array, in another struct, or in the inputs of a function.

Abi.Reference<{ readonly [name]: T }>

The struct type. A value is an object with every field, each of its field’s type. It is an Abi.Type that says it is a reference type.

  • Type: Abi.Fields

The types of the fields, by name. The order of the keys is the order of the struct.

import { Abi } from "@ethect/core";
const Order = Abi.struct({ maker: Abi.address, amount: Abi.uint256 });
const Fill = Abi.struct({ order: Order, proof: Abi.array(Abi.bytes32) });
// @ts-expect-error a field is named by a Solidity identifier
const Broken = Abi.struct({ amount: Abi.uint256, 0: Abi.address });

The layout is the keys of fields, read once when the struct is defined. JavaScript keeps the keys of an object in the order they were written, so the order you see is the order encoded.

Where an order comes from Kept
the object literal given to Abi.struct yes: it is the layout
the keys of a value given to Abi.encode not read: fields are taken by name
a value that was spread, built by a class, or read with JSON.parse not read either
a decoded value built by position, with its keys in the order of the layout
a key that reads as a number, such as 0 or "12" no: JavaScript puts it first. It does not compile
import { Effect } from "effect";
import { Abi } from "@ethect/core";
const Range = Abi.struct({ from: Abi.uint48, to: Abi.uint48 });
const same = Effect.all([
Abi.encode(Range, { from: 1, to: 2 }),
Abi.encode(Range, { to: 2, from: 1 }), // the same bytes
]);
A field is missing from a value Checked: the encoding fails, and the types refuse it
A field has a value its type refuses Checked: the message names the field, at ["lltv"]
A value has a key the struct does not Left out of the encoding
A name is no Solidity identifier Checked: it does not compile. See Behavior
Two fields of the same type are written in the wrong order Not checked. Nothing can tell loanToken from oracle

For the last row, compare a definition written by hand with the ABI the compiler gives, in a test.

  • A struct and a tuple of the same types are the same bytes. Names are not encoded.
  • The definition never throws. A name that got past the types, such as a numeric key in an object typed any, gives a type whose every use fails with an AbiError that names the key: A field is named by a Solidity identifier: "0" is not one.
  • __proto__ is not a field name. It is the one identifier Solidity takes and a JavaScript object does not hold.
  • A struct has a field. Abi.struct({}) gives a type whose every use fails too.
  • The fields are read once, when the struct is defined. Changing the object you gave afterwards changes nothing.
Error When
AbiError encoding a value that is not an object, or that lacks a field
AbiError encoding a field its type refuses
AbiError any use of a struct whose names are not Solidity identifiers