Eth.getLogs
Returns the event logs matching a filter: by contract and by topics, over a range of blocks or in one block.
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 objects. The examples below leave the transport out.
The topics and the data of a log are hex. Ev.decode reads them
as the events of a contract, and Ev.logs asks for the logs and
decodes them in one step.
By topics
Section titled “By topics”Topics match by position. A position takes one hash, an array of alternatives, or null for
any value.
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
Section titled “In one block”Name the block by its hash instead of a range.
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
Section titled “From several contracts”address takes one address or an array.
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
Section titled “Returns”Effect<ReadonlyArray<Log.Log>, Method.Error, Transport>
The matching logs. The array is empty when nothing matches.
Parameters
Section titled “Parameters”filter
Section titled “filter”- Type:
Filter.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 |
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
Section titled “Behavior”- No range means the latest block. A filter without
fromBlock,toBlockorblockHashsearches the latest block only. - A range or a block hash. A filter with
blockHashandfromBlockortoBlockis 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.
nullon a pending log. The position fields of aLogarenullwhile its transaction is pending.
Errors
Section titled “Errors”The three 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
Section titled “JSON-RPC method”eth_getLogs