Elementary types
The Solidity types that hold one value, and what each one is in TypeScript.
import { Effect } from "effect";import { Abi, Address } from "@ethect/core";
const OWNER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Effect.gen(function* () { const encoded = yield* Abi.encode(Abi.address, OWNER); const decimals = yield* Abi.decode(Abi.uint8, `0x${"00".repeat(31)}12`); // 18 return { encoded, decimals };});Every type is a constant. Compose them with Abi.array,
Abi.tuple and Abi.struct.
What each type is
Section titled “What each type is”| Solidity | TypeScript | Checked when encoding |
|---|---|---|
address |
Address.Address |
20 bytes of lowercase hex, which Address.make gives |
bool |
boolean |
a boolean |
string |
string |
a string |
bytes |
Hex.Hex |
whole bytes: an even number of digits |
bytesN |
Hex.Hex |
exactly N bytes |
uintN, intN, N ≤ 48 |
number |
a whole number, in range |
uintN, intN, N > 48 |
bigint |
in range |
T[], T[k] |
ReadonlyArray<T> |
k items for a fixed length |
| a tuple | a readonly tuple | one value per component |
| a struct | a readonly object | every field present |
A value that fails its check fails the encoding with an
AbiError. What decoding checks is on the page of
Abi.decode.
address
Section titled “address”Abi.address is an address. Its values are Address.Address:
validated, and always lowercase.
import { Abi, Address } from "@ethect/core";
const OWNER = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const encoded = Abi.encode(Abi.address, OWNER);A plain string is a type error. Decoding gives the address in lowercase: call
Address.checksum to display it.
Abi.bool is a bool. Its values are true and false.
string
Section titled “string”Abi.string is a string, written in UTF-8.
- Half a surrogate pair is no character. It is written as U+FFFD, as
TextEncoderdoes. - Bytes that are not UTF-8 decode to U+FFFD, each invalid sequence to one. Decoding a
stringdoes not fail on its content.
Abi.bytes is a bytes: any number of bytes, as hex. "0x" is none.
import { Abi } from "@ethect/core";
// @ts-expect-error hex starts with 0xconst encoded = Abi.encode(Abi.bytes, "1234");Encoding takes letters of either case. Decoding gives lowercase.
bytesN
Section titled “bytesN”Bytes of a fixed length, from 1 to 32. A value has exactly that many bytes.
| Size | Types |
|---|---|
| 1 to 8 | Abi.bytes1, Abi.bytes2, Abi.bytes3, Abi.bytes4, Abi.bytes5, Abi.bytes6, Abi.bytes7, Abi.bytes8 |
| 9 to 16 | Abi.bytes9, Abi.bytes10, Abi.bytes11, Abi.bytes12, Abi.bytes13, Abi.bytes14, Abi.bytes15, Abi.bytes16 |
| 17 to 24 | Abi.bytes17, Abi.bytes18, Abi.bytes19, Abi.bytes20, Abi.bytes21, Abi.bytes22, Abi.bytes23, Abi.bytes24 |
| 25 to 32 | Abi.bytes25, Abi.bytes26, Abi.bytes27, Abi.bytes28, Abi.bytes29, Abi.bytes30, Abi.bytes31, Abi.bytes32 |
import { Effect } from "effect";import { Abi } from "@ethect/core";
// a function selectorconst selector = Abi.encode(Abi.bytes4, "0x70a08231");
// three bytes are not a bytes4: an AbiErrorconst short = Effect.flip(Abi.encode(Abi.bytes4, "0x70a082"));uintN and intN
Section titled “uintN and intN”Every size Solidity has: a multiple of 8, from 8 to 256.
- Up to 48 bits, a value is a
number. Auint48always fits one exactly: it holds a timestamp, a fee, a tick. - Above, a value is a
bigint.
| Bits | Unsigned | Signed | TypeScript |
|---|---|---|---|
| 8 to 48 | Abi.uint8, Abi.uint16, Abi.uint24, Abi.uint32, Abi.uint40, Abi.uint48 |
Abi.int8, Abi.int16, Abi.int24, Abi.int32, Abi.int40, Abi.int48 |
number |
| 56 to 96 | Abi.uint56, Abi.uint64, Abi.uint72, Abi.uint80, Abi.uint88, Abi.uint96 |
Abi.int56, Abi.int64, Abi.int72, Abi.int80, Abi.int88, Abi.int96 |
bigint |
| 104 to 144 | Abi.uint104, Abi.uint112, Abi.uint120, Abi.uint128, Abi.uint136, Abi.uint144 |
Abi.int104, Abi.int112, Abi.int120, Abi.int128, Abi.int136, Abi.int144 |
bigint |
| 152 to 192 | Abi.uint152, Abi.uint160, Abi.uint168, Abi.uint176, Abi.uint184, Abi.uint192 |
Abi.int152, Abi.int160, Abi.int168, Abi.int176, Abi.int184, Abi.int192 |
bigint |
| 200 to 256 | Abi.uint200, Abi.uint208, Abi.uint216, Abi.uint224, Abi.uint232, Abi.uint240, Abi.uint248, Abi.uint256 |
Abi.int200, Abi.int208, Abi.int216, Abi.int224, Abi.int232, Abi.int240, Abi.int248, Abi.int256 |
bigint |
import { Effect } from "effect";import { Abi } from "@ethect/core";
const fee = Abi.encode(Abi.uint24, 3000); // a numberconst amount = Abi.encode(Abi.uint256, 10n ** 18n); // a bigint
// @ts-expect-error a uint256 is a bigint, even a small oneconst wrong = Abi.encode(Abi.uint256, 1);const tooLarge = Effect.flip(Abi.encode(Abi.uint8, 256)); // "256 does not fit in uint8"uint and int alone are not exported: write Abi.uint256 and Abi.int256.
Behavior
Section titled “Behavior”- A type is a schema.
Schema.is(Abi.uint8)is a guard for a whole number from 0 to 255. See A type is a schema. - One 32-byte word each, except
bytesandstring, which are a length followed by their bytes. - Nothing is rounded.
1.5is refused by every integer type, and so is abigintwhere anumberis expected.