Abi.encode
Encodes one value, or several, to the bytes abi.encode gives.
import { Abi } from "@ethect/core";
// one type and its value, or a list of types and one value for eachconst one = Abi.encode(Abi.uint256, 1n); // abi.encode(x)const several = Abi.encode([Abi.uint256, Abi.string], [1n, "one"]); // abi.encode(a, b)Both are effects. Run one, or yield* it inside another.
import { Effect } from "effect";import { Abi, Address } from "@ethect/core";
const Transfer = Abi.struct({ to: Abi.address, amount: Abi.uint256 });
const TO = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Abi.encode(Transfer, { to: TO, amount: 100n });
const data = await Effect.runPromise(program);// 0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266// 0000000000000000000000000000000000000000000000000000000000000064Several values, or one tuple
Section titled “Several values, or one tuple”A list of types and a tuple of the same types are two things, as they are in Solidity.
import { Abi } from "@ethect/core";
// abi.encode(a, b): two valuesconst several = Abi.encode([Abi.uint256, Abi.string], [1n, "one"]);// abi.encode(pair), for a struct `pair`: one valueconst pair = Abi.encode(Abi.tuple([Abi.uint256, Abi.string]), [1n, "one"]);| The types are | Abi.encode([A, B], …) next to Abi.encode(Abi.tuple([A, B]), …) |
|---|---|
| static only | the same bytes |
one of them dynamic: bytes, string, T[] |
the tuple is one word longer: it starts with 0x20, its own offset |
A tuple with a dynamic component is one dynamic value, and a dynamic value is written by its offset. Hook data, callback data and the keys you hash are most often several values: use the list.
A value that does not fit
Section titled “A value that does not fit”A failure is a value. Handle it with Effect.catchTag.
import { Effect } from "effect";import { Abi } from "@ethect/core";
const data = Abi.encode(Abi.uint8, 256).pipe( Effect.catchTag("AbiError", (error) => Effect.succeed(error.message)),);// succeeds with "Cannot encode uint8: 256 does not fit in uint8"Returns
Section titled “Returns”Effect<Hex.Hex, AbiError>
The encoding, as lowercase hex. It needs no service: no node is asked.
Parameters
Section titled “Parameters”- Type:
Abi.Type<T>, or a list of them
The Solidity type: one of Abi’s, or a
type of your own. A list of types encodes several values. An
empty list encodes to 0x.
- Type:
T, or one value for each type of the list
A value of the type. What T is for each type is in
this table.
import { Abi } from "@ethect/core";
const fee = Abi.encode([Abi.uint24, Abi.bool], [3000, true]);// @ts-expect-error the fee is a number, not a bigintconst wrong = Abi.encode([Abi.uint24, Abi.bool], [3000n, true]);// @ts-expect-error one value for each typeconst short = Abi.encode([Abi.uint24, Abi.bool], [3000]);Behavior
Section titled “Behavior”- Two steps. The type’s schema turns the value into the plain ABI value, checking it. Then the layout writes the bytes: heads first, then tails, each dynamic value by its offset.
- Every value is checked before a byte is written. What each type checks is in this table.
- The message says where. A value deep in a struct is named by its path:
at ["orders"][1]["amount"]. In a list it says which value:value 1 of (address,uint256). - One type is a list of one.
Abi.encode(Abi.string, "one")andAbi.encode([Abi.string], ["one"])are the same bytes. A dynamic value starts with its offset,0x20. - A type is told from a list with
Array.isArray. A type is a schema, never an array. - This is not the calldata of a call. Calldata is a selector followed by the encoding of the arguments.
Errors
Section titled “Errors”| Error | When |
|---|---|
AbiError |
a value is not one its type takes: out of range, of the wrong length, a field missing |
AbiError |
a list of types is given another number of values |
AbiError |
a type is not a Solidity type: a schema that was not built from Abi |
AbiError |
a type is a definition Solidity has no type for: T[0], a tuple of nothing, a field named 0 |