# Ev.decode

Decodes logs to events, in one pass: one event or several, in the order of the logs or sorted
by event.

## Usage

```ts
import { Effect } from "effect";
import { Erc4626, Ev, type Hash } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const { Deposit } = Erc4626.ev;

const deposits = (transaction: Hash.Hash) =>
  Effect.map(Eth.getTransactionReceipt(transaction), (receipt) =>
    Ev.decode(Deposit, receipt?.logs ?? []),
  );
// Effect<ReadonlyArray<Deposit>, …>: each has `assets`, `shares`, `sender`, `owner` and `log`
```

It takes the logs of a receipt, or any logs a node sent. A log that is none of the events is
left out.

### Several events, in order

Give an array of events. The result follows the order of the logs, and `_tag` tells the events
apart.

```ts
import { Erc4626, Ev } from "@ethect/core";
import type { Receipt } from "@ethect/core/rpc";

const { Deposit, Transfer } = Erc4626.ev;

const moved = (receipt: Receipt.Receipt) => {
  let total = 0n;
  for (const event of Ev.decode([Transfer, Deposit], receipt.logs)) {
    if (event._tag === "Transfer") total += event.value;
    else total -= event.assets;
  }
  return total;
};
```

### Several events, sorted

Give a record. The result has the same keys, each with the logs of its event.

```ts
import { Erc4626, Ev } from "@ethect/core";
import type { Receipt } from "@ethect/core/rpc";

const { Deposit, Transfer } = Erc4626.ev;

const sorted = (receipt: Receipt.Receipt) => {
  const events = { transfers: Transfer, deposited: Deposit };
  const { transfers, deposited } = Ev.decode(events, receipt.logs);
  return { transfers: transfers.length, shares: deposited.map((deposit) => deposit.shares) };
};
```

### Two events of one signature

ERC-20 and ERC-721 both declare `Transfer(address,address,uint256)`, so their logs have the
same first topic. They differ by how many fields are indexed, which is how many topics a log
has after the first.

```ts
import { Erc20, Erc721, Ev } from "@ethect/core";
import type { Receipt } from "@ethect/core/rpc";

// a log with two topics after the first is the transfer of a token, one with three of an NFT
const both = (receipt: Receipt.Receipt) =>
  Ev.decode({ tokens: Erc20.ev.Transfer, nfts: Erc721.ev.Transfer }, receipt.logs);
```

In an array, both have the tag `"Transfer"`: tell them apart with `instanceof`, or by a field.

## Returns

| `events` is | The result is |
| --- | --- |
| one event | `ReadonlyArray<Instance>`, in the order of the logs |
| an array of events | `ReadonlyArray<Instance of one of them>`, in the order of the logs |
| a record of events | an object with the same keys, each a `ReadonlyArray` of its event |

It is a plain value, not an effect: decoding cannot fail.

## Parameters

### events

- **Type:** `Ev.Any`, `ReadonlyArray<Ev.Any>`, or `Readonly<Record<string, Ev.Any>>`

The events to look for, each a class of [`Ev.make`](https://ethect.dev/reference/ev/make/).

### logs

- **Type:** `ReadonlyArray<Log.Log>`

The logs, as a node sends them: those of a [receipt](https://ethect.dev/rpc/types/receipt/), or the answer of
[`Eth.getLogs`](https://ethect.dev/rpc/eth/get-logs/).

## Behavior

- **One pass.** The events are put in a table by first topic and number of indexed fields.
  Each log is then looked up once, however many events you give.
- **A log that matches no event is left out.** So is a log that has the topic of an event and
  does not decode as it: data cut short, or a topic missing.
- **A log that is not in a block yet is left out.** A node sends such a log with a `null` block
  for a pending transaction. A decoded event always has its place: see
  [the log of an event](https://ethect.dev/reference/ev/make/#the-log-of-an-event).
- **A log a reorg took back is kept**, with `log.removed` set.
- **A log is given to the first event it decodes as.** The same event twice in a list, or
  under two keys, gets each log once.
- **Nothing throws.** A class that is no event is skipped.
- **To see why a log is left out**, compare its first topic with the
  [`topic`](https://ethect.dev/reference/ev/make/#what-an-event-is-known-by) of the event, then the number of
  topics that follow with the number of indexed fields.
