Filters
The six methods that install a filter on the node, poll it and remove it. A filter collects logs, block hashes or pending transaction hashes between two polls.
| Method | JSON-RPC | Returns | Description |
|---|---|---|---|
newFilter |
eth_newFilter |
Hex |
Installs a log filter. |
newBlockFilter |
eth_newBlockFilter |
Hex |
Installs a filter that collects the hashes of new blocks. |
newPendingTransactionFilter |
eth_newPendingTransactionFilter |
Hex |
Installs a filter that collects the hashes of new pending transactions. |
getFilterChanges |
eth_getFilterChanges |
Hash[] or Log[] |
What a filter collected since it was last polled. |
getFilterLogs |
eth_getFilterLogs |
Log[] |
Every log matching a log filter. |
uninstallFilter |
eth_uninstallFilter |
boolean |
Removes a filter. |
The three new… methods return the id of the filter, and the three others take it. Each
returns an Effect<…, Method.Error, Transport>.
Filters work by polling, so they work over HTTP. Over a transport that can push, a subscription gives the same items as a stream.
For logs that are already on chain, use Eth.getLogs.
newFilter
Section titled “newFilter”Installs a log filter on the node and returns its id.
import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const TOKEN = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
const id = Eth.newFilter({ address: TOKEN });- JSON-RPC:
eth_newFilter - Parameters:
filter(Filter.Filter). - Returns:
Hex, the id of the filter. - Without a range, the filter collects the logs of the blocks added after it was installed.
With
fromBlock, the first poll also returns the matching logs from that block on.
newBlockFilter
Section titled “newBlockFilter”Installs a filter that collects the hash of each new block, and returns its id.
import { Effect } from "effect";import { Eth } from "@ethect/core/rpc";
const newBlocks = Effect.gen(function* () { const id = yield* Eth.newBlockFilter(); yield* Effect.sleep("12 seconds"); return yield* Eth.getFilterChanges(id);});- JSON-RPC:
eth_newBlockFilter - Parameters: none.
- Returns:
Hex, the id of the filter.
newPendingTransactionFilter
Section titled “newPendingTransactionFilter”Installs a filter that collects the hash of each transaction entering the pending pool, and returns its id.
import { Effect } from "effect";import { Eth } from "@ethect/core/rpc";
const pending = Effect.gen(function* () { const id = yield* Eth.newPendingTransactionFilter(); yield* Effect.sleep("1 second"); return yield* Eth.getFilterChanges(id);});- JSON-RPC:
eth_newPendingTransactionFilter - Parameters: none.
- Returns:
Hex, the id of the filter.
getFilterChanges
Section titled “getFilterChanges”Returns what a filter collected since it was last polled: logs for a log filter, hashes for a block filter or a pending transaction filter.
import { Effect, Schedule } from "effect";import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const TOKEN = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
const watch = Effect.gen(function* () { const id = yield* Eth.newFilter({ address: TOKEN }); yield* Eth.getFilterChanges(id).pipe( Effect.tap((changes) => Effect.forEach(changes, (change) => // a log filter collects logs, the two others collect hashes typeof change === "string" ? Effect.void : Effect.log(change.transactionHash), ), ), Effect.repeat(Schedule.spaced("2 seconds")), );});- JSON-RPC:
eth_getFilterChanges - Parameters:
id(Hex). - Returns:
ReadonlyArray<Hash>orReadonlyArray<Log.Log>, depending on the filter. An empty array when nothing happened since the last poll. - The type is the union of the two arrays, because the id does not say which kind the filter
is. Narrow each item with
typeof item === "string".
getFilterLogs
Section titled “getFilterLogs”Returns every log matching a log filter, whatever was polled before.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const TOKEN = Address.makeUnsafe("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
const all = Effect.gen(function* () { const id = yield* Eth.newFilter({ address: TOKEN, fromBlock: 0n }); return yield* Eth.getFilterLogs(id);});- JSON-RPC:
eth_getFilterLogs - Parameters:
id(Hex), the id of a log filter. - Returns:
ReadonlyArray<Log.Log>: whatEth.getLogsreturns for the same filter. - It does not consume anything: the next
getFilterChangesstill returns what is new.
uninstallFilter
Section titled “uninstallFilter”Removes a filter from the node.
import { Effect } from "effect";import { Eth } from "@ethect/core/rpc";
const once = Effect.gen(function* () { const id = yield* Eth.newBlockFilter(); const hashes = yield* Eth.getFilterChanges(id); yield* Eth.uninstallFilter(id); return hashes;});- JSON-RPC:
eth_uninstallFilter - Parameters:
id(Hex). - Returns:
boolean.truewhen the filter was removed,falsewhen the node did not know it.
Behavior
Section titled “Behavior”- A poll empties the filter.
getFilterChangesreturns each item once. A second poll right after the first returns an empty array. - A new filter starts empty. The first poll returns what happened since the filter was
installed. Only a log filter with a
fromBlockreaches back. - A filter lives on the node. Its id means nothing to another node, so filters do not work behind a load balancer that spreads calls over several nodes.
- Nodes drop idle filters. A filter that is not polled for a while is removed by the node. Polling it afterwards fails like an unknown id.
- The id is opaque. It is hex, but not a number: pass it back as you got it.
- An unknown id fails.
getFilterChangesandgetFilterLogsfail with anRpcErrorwhose reason isInvalidInputfor an id the node does not know or has removed, as anvil 1.9 reports it.getFilterLogsfails the same way with the id of a block filter.