Skip to content

Ev.logs

Past events, over a range of blocks, in one array. A range the node refuses is read in pieces it accepts.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Erc20, Ev, Transport } from "@ethect/core";
const { Transfer } = Erc20.ev;
const USDC = Address.makeUnsafe("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48");
const program = Effect.gen(function* () {
// every transfer of the token in a hundred blocks
const transfers = yield* Ev.logs(Transfer, {
address: USDC,
fromBlock: 19_000_000n,
toBlock: 19_000_099n,
});
return transfers.map((transfer) => [transfer.from, transfer.to, transfer.value]);
});
const moved = await program.pipe(
Effect.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);

Each event is an instance of its class: its fields, and the log it comes from. The examples below leave the transport out.

args takes the fields the event declares indexed, and no other: a node can only be asked to match what is in a topic. Give one value, or several as alternatives.

import { Erc20, Ev, type Address } from "@ethect/core";
const { Transfer } = Erc20.ev;
// what one account received
const received = (token: Address.Address, user: Address.Address) =>
Ev.logs(Transfer, { address: token, args: { to: user }, fromBlock: 19_000_000n });
// what either of two accounts sent to a third
const sent = (token: Address.Address, senders: ReadonlyArray<Address.Address>, to: Address.Address) =>
Ev.logs(Transfer, {
address: token,
args: { from: senders, to },
fromBlock: 19_000_000n,
});

A field that is not indexed does not compile.

import { Erc20, Ev } from "@ethect/core";
const { Transfer } = Erc20.ev;
const large = Ev.logs(Transfer, {
// @ts-expect-error `value` is in the data of a log, not in a topic
args: { value: 1_000_000n },
});

Give an array. The logs of all of them are asked for in one request, and come in the order of the chain. _tag tells them apart.

import { Effect } from "effect";
import { Erc20, Ev, type Address } from "@ethect/core";
const { Approval, Transfer } = Erc20.ev;
const activity = (token: Address.Address) =>
Effect.map(Ev.logs([Transfer, Approval], { address: token, fromBlock: 19_000_000n }), (events) =>
events.map((event) => (event._tag === "Transfer" ? event.to : event.spender)),
);

args is for one event: with several, it does not compile.

Most nodes refuse a range that is too wide. You write the range you want, and it is read in pieces.

import { Erc20, Ev, type Address } from "@ethect/core";
const { Transfer } = Erc20.ev;
// every transfer since the first block, however many requests that takes
const history = (token: Address.Address) =>
Ev.logs(Transfer, { address: token, fromBlock: "earliest", toBlock: "latest" });

The result is one array, held in memory. For a history that does not fit, or that goes on, use Ev.pages: it gives one range of blocks at a time.

Effect<ReadonlyArray<Ev.Of<E>>, Transport.Error | AbiError, Transport>

The events, in the order of the chain. Ev.Of<E> is an instance of the event, or of one of the events of an array.

  • Type: Ev.Any or ReadonlyArray<Ev.Any>

The events to ask for, each a class of Ev.make.

The contract the logs come from, or several. Without it, the logs of every contract are asked for.

  • Type: the indexed fields of the event, each a value or a ReadonlyArray of values

Values to match, by field. A list is an alternative: one of its values. For one event.

A field marked Abi.indexed(type, "hash") holds a hash in its topic, and is matched by that hash.

The first block of the range: a number, a tag, or an object that names the block by its hash.

The last block of the range.

  • One request when the node reads the range. The range is asked for at once, as you wrote it, tags included.
  • A range the node refuses is halved, and asked for again, down to one block. One block that fails is the failure of the whole.
  • After a refusal, the pieces grow with care. The first width the node reads is a threshold. Each piece after it is an eighth wider than the one before, until the node refuses again. A node that does not say its limit is then refused about once in seven requests, and not once in two.
  • A block that holds too many logs narrows what is next to it. A node that limits the number of logs it gives refuses the blocks that hold many. The pieces are narrow there, and grow back after them.
  • A node that says its limit is asked for that many. Geth, reth, Erigon and Besu say how many blocks they read at once when they refuse a range, and so do some gateways. The pieces are that wide from then on, and no wider: such a node refuses one request.
  • In the order of the chain. The pieces are read one after the other.
  • A list of no value matches nothing, and the node is not asked. args: { to: [] } gives no event, and so does address: []. A node reads an empty list as “any”.
  • A value its field does not take fails before anything is sent, with an AbiError: an address that is no address, a number for a bytes32.
  • A block named by its hash is read for its number. The range is then asked for by number.
  • Only logs that are in a block. A log of a pending transaction is left out.
Error When
AbiError a value of args is not one its field takes: nothing was sent
RpcError the node refused a range of one block
RpcError the node has no logs for these blocks, such as history it pruned
TransportError the node could not be reached, or did not answer. The range is not halved for that.
TransportError a block named by a tag or by its hash is one the node does not have

One service is asked: the Transport.

  1. Ev.logs(events, { address, args, fromBlock, toBlock })

    1. the filtertopics from the events and args. No request
    2. the rangeeth_getLogs({ address, topics, fromBlock, toBlock })
    3. the node reads it: done

    4. refused

      1. your nodeeth_getBlockByNumber(tag, false)for an end that is a tag
      2. in pieceseth_getLogs({ …, fromBlock: n, toBlock: m })half as many blocks
      3. refused again: half of that, down to one block

      4. read: the next piece, an eighth wider

The node answers The next request
the logs the blocks after them, an eighth more than this time
an error that says how many blocks it reads, such as query exceeds max block range 10000 or limited to a 10,000 range that many blocks, and never more
any other error, for more than one block half as many blocks
any error, for one block none: it is the failure

The words that say a limit are those of Geth, reth, Erigon and Besu, and of four gateways whose answers were recorded. A node with other words is halved like any other.

A block named by its hash costs one eth_getBlockByHash before the first request.