Abi.indexed
Marks a field of an event as stored in a topic, which is what lets a filter match it.
import { Abi, Ev } from "@ethect/core";
// event Transfer(address indexed from, address indexed to, uint256 value)class Transfer extends Ev.make("Transfer", { from: Abi.indexed(Abi.address), to: Abi.indexed(Abi.address), value: Abi.uint256,}) {}An event is defined from such fields. The mark says where each one is read from: a topic, or the data of the log.
A type a topic cannot hold
Section titled “A type a topic cannot hold”A topic is one word, 32 bytes. An address, a number, a bool or a bytesN fits in one, and
the topic holds it. A string, bytes, an array, a tuple or a struct does not: Solidity
stores the hash of its value, and the value itself is not in the log.
Abi knows which of its types those are, so you write the same thing. The field is then the
hash, typed as hex.
import { Abi, Ev, type Hex } from "@ethect/core";
// event Posted(string indexed topic, uint256[] indexed ids, string text)class Posted extends Ev.make("Posted", { topic: Abi.indexed(Abi.string), ids: Abi.indexed(Abi.array(Abi.uint256)), text: Abi.string,}) {}const hashes = (post: Posted): readonly [Hex.Hex, Hex.Hex] => [post.topic, post.ids];| The type is | A topic holds | The field is |
|---|---|---|
address, bool, uintN, intN, bytesN |
the value | the value |
string, bytes |
keccak256 of the text or the bytes |
that hash, a Hex.Hex |
| an array, a tuple, a struct | keccak256 of its items, each in whole words |
that hash, a Hex.Hex |
A type of your own
Section titled “A type of your own”A transformation of a type is a new type, and it no longer says that it is a
reference type. For a type of your own built on a
string, bytes, an array, a tuple or a struct, say "hash".
import { Schema } from "effect";import { Abi, Ev } from "@ethect/core";
const Label = Abi.string.pipe(Schema.brand("Label"));
// event Tagged(string indexed label, address by)class Tagged extends Ev.make("Tagged", { label: Abi.indexed(Label, "hash"), by: Abi.address,}) {}A type of your own built on a type a topic holds needs nothing: a uint256 you read as a date
is stored as it is, and Abi.indexed(When) is the date.
Returns
Section titled “Returns”A type with indexed: true. It is an Abi.Type<T>, where T is what a topic gives.
| Given | Returns | It is |
|---|---|---|
| a type a topic holds | Abi.Indexed<T> |
the type you gave. It encodes and decodes as that type |
one of Abi’s reference types |
Abi.Indexed<Hex.Hex> |
the 32 bytes of a hash. Its hashed is the type you gave, which the signature of the event names |
any type, with "hash" |
Abi.Indexed<Hex.Hex> |
the same |
Parameters
Section titled “Parameters”- Type:
Abi.Type<T>
The type of the field, as the contract declares it.
import { Abi } from "@ethect/core";
const owner = Abi.indexed(Abi.address);const yes: true = owner.indexed;const untouched = "indexed" in Abi.address; // false: the type you gave is not changedstored (optional)
Section titled “stored (optional)”- Type:
"hash"
Says that the topic holds the hash of the value. A reference type of Abi does not need it.
A type of your own built on one does.
Behavior
Section titled “Behavior”- A type marked the wrong way is not a type. A type of your own built on a
string, marked without"hash", is typed as its values, which no topic holds.Abi.indexeddoes not throw: every use of such a type fails with anAbiErrorthat says how to mark it, and an event with such a field decodes no log. The same goes forAbi.indexed(Abi.address, "hash"). - A filter takes what the topic holds. For a field stored as its hash, that is the hash: hash the text yourself to match it.
- A value type encodes the same.
Abi.encode(Abi.indexed(Abi.address), owner)gives the bytes ofAbi.encode(Abi.address, owner). - The mark is on the value returned. A transformation of an indexed type is a new type, and is not indexed: mark it last.
- It is for the fields of an event. Leave the inputs of a function and the fields of a struct unmarked.