Skip to content

Events (Ev)

Events of a contract. Each one is a class whose instances are its decoded logs. Events are decoded from the logs of a receipt, asked for over a range of blocks, or watched as they happen.

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,
}) {}
// the transfers of a transaction, from its receipt
const transfers = (receipt: Receipt.Receipt) =>
Ev.decode(Transfer, receipt.logs).map((event) => [event.from, event.to, event.value]);
Export Kind Description
Ev.make(name, fields) function An event: a class to extend. Its indexed fields are those marked with Abi.indexed.
Ev.decode(events, logs) function Decodes logs in one pass: as one list in the order of the logs, or sorted by event when given a record.
Ev.logs(events, filter?) function Past events, in one array.
Ev.watch(events, options?) function Events as they happen, as a Stream. With fromBlock, the past ones first.
Ev.pages(events, options?) function Past then live, one range of blocks at a time. Each page says where it ends.
Ev.Source reference How logs arrive. The default picks from what the transport can do.
Ev.subscription source eth_subscribe: pushed by the node.
Ev.filter source eth_newFilter, then eth_getFilterChanges at every interval.
Ev.polling source eth_getLogs for each new block, by its hash.
Ev.Emitted type What every decoded event carries: log, where it comes from.
Ev.Log type A log that is in a block: its block, its transaction and its index are never null.
Ev.Any type Any event class.
Ev.Events type One event, or several.
Ev.Of<E> type What the events decode to: an instance of one of them.
Ev.Filter<E> type Which logs to ask for: address, args, fromBlock, toBlock.
Ev.WatchOptions<E> type A filter without toBlock, with confirmations and interval.
Ev.Page<E> type The events of a range of blocks: fromBlock, toBlock, hash, events.

Only the node is asked. Each function has its requests on its page.

  1. Ev.logs(events, filter)

    1. your nodeeth_getLogs({ address, topics, fromBlock, toBlock })the range, in pieces when the node refuses it whole
  2. Ev.watch(events)

    1. your nodeeth_subscribe("logs")over a transport that pushes
    2. your nodeeth_newFilterotherwise, then eth_getFilterChanges
    3. your nodeeth_getLogswhere the node has no filterat each block
  3. Ev.pages(events, options)

    1. your nodeeth_getLogs({ address, topics, fromBlock, toBlock })the past, range by range
    2. your nodeeth_getLogs({ address, topics, blockHash })the head, block by blockat each new block
  4. Ev.decode(events, logs)

    1. your programno request: the logs are the ones you give

See Ev.logs, Ev.watch, Ev.pages and Ev.Source.