Query logs and filters
Query past events with getContractEvents / getLogs, or poll a filter.
By indexed argument
const toBob = await anvil.testClient.getContractEvents({
address: token,
abi: exampleToken.abi,
eventName: "Transfer",
args: { to: bob }, // indexed topic filter
fromBlock: 0n,
});
// JSON ABIs are untyped (`Abi`), so decoded args need a type here.
const values = toBob.map((log) => (log.args as { value: bigint }).value);
expect(values).toEqual([2n, 3n]);
Tested in examples/test/e2e/events-and-subscriptions.e2e.test.ts:82
Typed arguments from an event signature
JSON ABIs are untyped; a human-readable signature gives full inference:
// A human-readable event signature gives full type inference for log args,
// even though the JSON ABIs are untyped.
const TRANSFER = parseAbiItem(
"event Transfer(address indexed from, address indexed to, uint256 value)",
);
Tested in examples/test/e2e/events-and-subscriptions.e2e.test.ts:29
const mints = await anvil.testClient.getLogs({
address: token,
event: TRANSFER,
args: { from: zeroAddress },
fromBlock: 0n,
toBlock: "latest",
});
const last = mints.at(-1)!;
expect(last.args.to).toBe(alice); // typed: `0x${string}` | undefined
expect(last.args.value).toBe(7n); // typed: bigint | undefined
Tested in examples/test/e2e/events-and-subscriptions.e2e.test.ts:174
Poll a filter
const http = await anvil.createTestClient((client) =>
client.withAccount(0).withTransport("http"),
);
const filter = await http.createEventFilter({
address: token,
event: TRANSFER,
args: { to: bob }, // indexed argument filter
});
await mint(http, token, alice, 1n);
await mint(http, token, bob, 2n);
const changes = await http.getFilterChanges({ filter });
expect(changes).toHaveLength(1); // only the transfer to bob
expect(await http.getFilterChanges({ filter })).toHaveLength(0); // consumed
await http.uninstallFilter({ filter });
Tested in examples/test/e2e/events-and-subscriptions.e2e.test.ts:152
Concepts
- JSON ABIs are typed as
Abi - Reference: test client actions