# Eth.getLogs

Returns the event logs matching a filter. Logs are matched by contract and by topics, over a range of blocks or in one block.

Returns the event logs matching a filter: by contract and by topics, over a range of blocks or
in one block.

## Usage

```ts
import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Hash, Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const TOKEN = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");

const program = Eth.getLogs({
  address: TOKEN,
  // the first topic of an event is the hash of its signature
  topics: [Hash.keccak256Utf8("Transfer(address,address,uint256)")],
  fromBlock: 0n,
  toBlock: "latest",
});

const logs = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
```

The result is every `Transfer` event the contract emitted, oldest first, as
[`Log`](https://ethect.dev/rpc/types/log/) objects. The examples below leave the transport out.

The `topics` and the `data` of a log are hex. [`Ev.decode`](https://ethect.dev/reference/ev/decode/) reads them
as the events of a contract, and [`Ev.logs`](https://ethect.dev/reference/ev/logs/) asks for the logs and
decodes them in one step.

### By topics

Topics match by position. A position takes one hash, an array of alternatives, or `null` for
any value.

```ts
import { Address, Hash } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const TOKEN = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
const RECIPIENT = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

const received = Eth.getLogs({
  address: TOKEN,
  fromBlock: 19_000_000n,
  toBlock: 19_000_100n,
  topics: [
    Hash.keccak256Utf8("Transfer(address,address,uint256)"),
    null, // from: anyone
    `0x${RECIPIENT.slice(2).padStart(64, "0")}`, // to: padded to 32 bytes
  ],
});
```

An indexed argument of an event is a topic: here the sender and the recipient of a transfer.

### In one block

Name the block by its hash instead of a range.

```ts
import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";

const logs = Effect.gen(function* () {
  const block = yield* Eth.getBlockByNumber("latest", false);
  return block?.hash ? yield* Eth.getLogs({ blockHash: block.hash }) : [];
});
```

A block hash names one block for good, where a number can name another block after a reorg.

### From several contracts

`address` takes one address or an array.

```ts
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const WETH = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");

const logs = Eth.getLogs({
  address: [WETH, USDC],
  fromBlock: 19_000_000n,
  toBlock: 19_000_100n,
});
```

## Returns

[`Effect<ReadonlyArray<Log.Log>, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)

The matching [logs](https://ethect.dev/rpc/types/log/). The array is empty when nothing matches.

## Parameters

### filter

- **Type:** [`Filter.Filter`](https://ethect.dev/rpc/types/filter/)

Which logs to match. Every field is optional.

| Field | Type | Description |
| --- | --- | --- |
| `address` | `Address` or `ReadonlyArray<Address>` | the contract or contracts that emitted the log |
| `topics` | `Filter.Topics` | the topics to match, by position |
| `fromBlock` | `bigint` or a tag | the first block of the range |
| `toBlock` | `bigint` or a tag | the last block of the range |
| `blockHash` | `Hash` | the one block to search, instead of a range |

```ts
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const TOKEN = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");

const logs = Eth.getLogs({
  address: TOKEN,
  fromBlock: "finalized",
  toBlock: "latest",
});
```

## Behavior

- **No range means the latest block.** A filter without `fromBlock`, `toBlock` or `blockHash`
  searches the latest block only.
- **A range or a block hash.** A filter with `blockHash` and `fromBlock` or `toBlock` is
  rejected by the node.
- **Topics are 32 bytes.** A shorter topic is refused before anything is sent. Pad an address
  or a number on the left.
- **Nodes cap the work.** A hosted node limits the size of a range or the number of logs it
  returns, each in its own way. Ask for a range you can split.
- **`null` on a pending log.** The position fields of a [`Log`](https://ethect.dev/rpc/types/log/) are
  `null` while its transaction is pending.

## Errors

The three [errors](https://ethect.dev/reference/errors/) every method can fail with. The cases specific to this
method, as anvil 1.9 reports them:

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | `InvalidParams` | the filter has `blockHash` and `fromBlock` or `toBlock` |
| `RpcError` | `InvalidParams` | `fromBlock` is after `toBlock` |
| `RpcError` | `InvalidParams` | `toBlock` is above the chain's head |
| `RpcError` | `InvalidInput` | no block has the given `blockHash` |
| `CodecError` | `on: "params"` | a topic is not 32 bytes: nothing is sent |

## JSON-RPC method

`eth_getLogs`
