# 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.

```ts
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)`](https://ethect.dev/reference/ev/make/) | function | An event: a class to extend. Its `indexed` fields are those marked with `Abi.indexed`. |
| [`Ev.decode(events, logs)`](https://ethect.dev/reference/ev/decode/) | 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?)`](https://ethect.dev/reference/ev/logs/) | function | Past events, in one array. |
| [`Ev.watch(events, options?)`](https://ethect.dev/reference/ev/watch/) | function | Events as they happen, as a `Stream`. With `fromBlock`, the past ones first. |
| [`Ev.pages(events, options?)`](https://ethect.dev/reference/ev/pages/) | function | Past then live, one range of blocks at a time. Each page says where it ends. |
| [`Ev.Source`](https://ethect.dev/reference/ev/source/#source) | reference | How logs arrive. The default picks from what the transport can do. |
| [`Ev.subscription`](https://ethect.dev/reference/ev/source/#subscription) | source | `eth_subscribe`: pushed by the node. |
| [`Ev.filter`](https://ethect.dev/reference/ev/source/#filter) | source | `eth_newFilter`, then `eth_getFilterChanges` at every interval. |
| [`Ev.polling`](https://ethect.dev/reference/ev/source/#polling) | source | `eth_getLogs` for each new block, by its hash. |
| [`Ev.Emitted`](https://ethect.dev/reference/ev/make/#the-log-of-an-event) | type | What every decoded event carries: `log`, where it comes from. |
| [`Ev.Log`](https://ethect.dev/reference/ev/make/#the-log-of-an-event) | 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`](https://ethect.dev/reference/ev/logs/#events) | type | One event, or several. |
| [`Ev.Of<E>`](https://ethect.dev/reference/ev/logs/#returns) | type | What the events decode to: an instance of one of them. |
| [`Ev.Filter<E>`](https://ethect.dev/reference/ev/logs/#parameters) | type | Which logs to ask for: `address`, `args`, `fromBlock`, `toBlock`. |
| [`Ev.WatchOptions<E>`](https://ethect.dev/reference/ev/watch/#parameters) | type | A filter without `toBlock`, with `confirmations` and `interval`. |
| [`Ev.Page<E>`](https://ethect.dev/reference/ev/pages/#a-page) | type | The events of a range of blocks: `fromBlock`, `toBlock`, `hash`, `events`. |

## Under the hood

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

**`Ev.logs(events, filter)`**

- **your node** `eth_getLogs({ address, topics, fromBlock, toBlock })`: the range, in pieces when the node refuses it whole

**`Ev.watch(events)`**

- **your node** `eth_subscribe("logs")`: over a transport that pushes
- **your node** `eth_newFilter`: otherwise, then `eth_getFilterChanges`
- **your node** `eth_getLogs`: where the node has no filter, at each block

**`Ev.pages(events, options)`**

- **your node** `eth_getLogs({ address, topics, fromBlock, toBlock })`: the past, range by range
- **your node** `eth_getLogs({ address, topics, blockHash })`: the head, block by block, at each new block

**`Ev.decode(events, logs)`**

- **your program**: no request: the logs are the ones you give

See [`Ev.logs`](https://ethect.dev/reference/ev/logs/#under-the-hood), [`Ev.watch`](https://ethect.dev/reference/ev/watch/#under-the-hood),
[`Ev.pages`](https://ethect.dev/reference/ev/pages/#under-the-hood) and [`Ev.Source`](https://ethect.dev/reference/ev/source/).
