Ev.make
Defines an event of a contract. The result is a class to extend, and its instances are the decoded logs of the event.
import { Abi, Ev } from "@ethect/core";import type { Receipt } from "@ethect/core/rpc";
// 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,}) {}
const moved = (receipt: Receipt.Receipt) => { let total = 0n; for (const transfer of Ev.decode(Transfer, receipt.logs)) { total += transfer.value; // transfer.from, transfer.to, transfer.log } return total;};Extend the result, so the event has a name of its own in your types. The first argument is the
name the contract gives it: it is the _tag of the instances, and the first topic of its logs
is hashed from it and from the types of the fields.
Indexed fields
Section titled “Indexed fields”Mark a field with Abi.indexed when the contract declares it
indexed. An indexed field is read from a topic of the log, the others from its data, and a
filter can only match the indexed ones.
import { Abi, Ev } from "@ethect/core";
// event Deposit(address indexed sender, address indexed owner, uint256 assets, uint256 shares)class Deposit extends Ev.make("Deposit", { sender: Abi.indexed(Abi.address), owner: Abi.indexed(Abi.address), assets: Abi.uint256, shares: Abi.uint256,}) {}Which fields are indexed is part of what an event is. ERC-20 and ERC-721 both have a
Transfer(address,address,uint256): the first indexes two fields, the second all three, and
Ev.decode tells them apart by that.
A field stored as its hash
Section titled “A field stored as its hash”A topic is one word. A string, bytes, an array or a struct does not fit in one, so
Solidity stores the hash of its value. Such a field is the hash, typed as
Hex.Hex.
import { Abi, Ev, type Hex } from "@ethect/core";
// event NameRegistered(string indexed name, address owner)class NameRegistered extends Ev.make("NameRegistered", { name: Abi.indexed(Abi.string), owner: Abi.address,}) {}
const hashOf = (event: NameRegistered): Hex.Hex => event.name; // keccak256("alice"), not "alice"The text itself is not in the log. See
Abi.indexed for which types are
hashed, and for a type of your own built on one
of them.
What an event is known by
Section titled “What an event is known by”The class says the signature of the event, and the topic hashed from it: the first topic of every log of the event.
import { Abi, Ev } from "@ethect/core";
class Transfer extends Ev.make("Transfer", { from: Abi.indexed(Abi.address), to: Abi.indexed(Abi.address), value: Abi.uint256,}) {}
Transfer.signature; // "Transfer(address,address,uint256)"Transfer.topic; // "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"Use them to check a definition against a contract, or against a log that does not decode:
compare the first topic of the log with Transfer.topic.
Returns
Section titled “Returns”(new (value) => Fields & Ev.Emitted & { readonly _tag: Name }) & { signature, topic, indexed }
A class. An instance holds the fields of the event, its _tag, and log: where it comes from.
| On the class | Type | Is |
|---|---|---|
signature |
string |
the name of the event and the types of its fields: Transfer(address,address,uint256) |
topic |
Hash.Hash |
the hash of the signature: the first topic of its logs |
indexed |
there for its type: the indexed fields, with the value a filter takes for each |
Parameters
Section titled “Parameters”- Type:
string
The name of the event in the contract.
fields
Section titled “fields”- Type:
Abi.Fields
The Solidity types of the fields, by name, in the order the contract declares them. The names are yours: only the order, the types and which are indexed matter.
The log of an event
Section titled “The log of an event”Every decoded event has a log, an Ev.Log: the log it was decoded from. It is in a block, so
its place in the chain is never null.
log. |
Type | Is |
|---|---|---|
address |
Address.Address |
the contract that emitted the event |
blockNumber |
bigint |
the block it is in |
blockHash |
Hash.Hash |
the hash of that block |
transactionHash |
Hash.Hash |
the transaction that emitted it |
logIndex |
number |
its position among the logs of the block |
removed |
boolean |
true when a reorg took the log back |
topics, data |
the log as the node sent it |
Ev.Emitted is what every event has: { log }. Ev.Any is any event class, Ev.Events one
event or an array of them, and Ev.Of<E> what they decode to.
Behavior
Section titled “Behavior”newchecks nothing. An instance is built from a decoded log without a check that could throw. You rarely build one:new Transfer({ from, to, value, log })takes the fields and the log.- An event that is not a definition decodes nothing. A field that is no Solidity type, a field named by a number, or an indexed type of your own that is marked the wrong way: no log is an event of it, and nothing throws.
- The names of the fields are not in the signature, and neither is which are indexed. Two events of one name and the same types have one signature and one topic.
- Name no field
logor_tag. The event has its own, and a class of such an event does not compile. Any other name reads the same logs: only the order and the types of the fields are in the signature. - Anonymous events are not supported. Their logs have no topic for the signature.