Skip to main content

Watch events and blocks

Use the WebSocket transport for eth_subscribe-based watchers:

const anvil = useAnvil(
new AnvilContainer().withTestClient((client) =>
client.withAccount(0).withTransport("webSocket"),
),
{ setup: async (node) => void (token = await deployToken(node.testClient)) },
);

Tested in examples/test/e2e/events-and-subscriptions.e2e.test.ts:37

Contract events​

const received: Log[] = [];
const errors: Error[] = [];
const unwatch = anvil.testClient.watchContractEvent({
address: token,
abi: exampleToken.abi,
eventName: "Transfer",
onLogs: (logs) => received.push(...logs),
onError: (error) => errors.push(error),
});
try {
// The subscription starts asynchronously: mint until a log arrives.
await triggerUntil(
() => mint(anvil.testClient, token, alice, parseEther("5")),
() => received.length > 0,
);
} finally {
unwatch();
}

expect(errors).toEqual([]);
expect(received[0]).toMatchObject({
eventName: "Transfer",
args: { to: alice, value: parseEther("5") },
});

Tested in examples/test/e2e/events-and-subscriptions.e2e.test.ts:49

New blocks​

const start = await anvil.testClient.getBlockNumber();
const seen = new Set<bigint>();
const unwatch = anvil.testClient.watchBlocks({
// viem fetches each announced block separately, so blocks can be
// reported out of order: collect them, don't assume a sequence.
onBlock: (block: Block) => seen.add(block.number!),
});
try {
await triggerUntil(
() => anvil.mine(),
() => seen.size >= 2,
);
} finally {
unwatch();
}

const latest = await anvil.testClient.getBlockNumber();
for (const number of seen) {
expect(number).toBeGreaterThan(start); // only newly mined blocks
expect(number).toBeLessThanOrEqual(latest);
}

Tested in examples/test/e2e/events-and-subscriptions.e2e.test.ts:101

Pending transactions​

await anvil.cheats.setAutomine(false); // keep transactions pending
const seen = new Set<Hash>();
const sent: Hash[] = [];
const unwatch = anvil.testClient.watchPendingTransactions({
onTransactions: (hashes) => hashes.forEach((hash) => seen.add(hash)),
});
try {
await triggerUntil(
async () =>
sent.push(
await anvil.testClient.sendTransaction({ to: alice, value: 1n }),
),
() => sent.some((hash) => seen.has(hash)),
);
} finally {
unwatch();
await anvil.cheats.setAutomine(true); // node setting: restore
}

Tested in examples/test/e2e/events-and-subscriptions.e2e.test.ts:128

Your own WebSocket clients​

const client = createPublicClient({
chain: anvil.chain,
transport: webSocket(anvil.wsUrl),
});
const start = await anvil.testClient.getBlockNumber();
let announced: bigint | undefined;
const unwatch = client.watchBlockNumber({
onBlockNumber: (blockNumber) => (announced = blockNumber),
});
try {
await triggerUntil(
() => anvil.mine(),
() => announced !== undefined,
);
expect(announced).toBeGreaterThan(start);
} finally {
unwatch();
// Clients you create are yours to close (the node closes its own).
(await client.transport.getRpcClient()).close();
}

Tested in examples/test/e2e/events-and-subscriptions.e2e.test.ts:189

Don't sleep: trigger until observed​

Watchers subscribe asynchronously and viem has no "ready" signal. Repeat the trigger until the watcher reports:

/**
* Runs `trigger` (e.g. mine a block, send a transaction), then checks
* `observed`, repeating until it returns `true`.
*
* @example
* const seen = new Set<bigint>();
* const unwatch = client.watchBlocks({ onBlock: (block) => seen.add(block.number!) });
* await triggerUntil(() => anvil.mine(), () => seen.size >= 2);
* unwatch();
*/
export async function triggerUntil(
trigger: () => Promise<unknown>,
observed: () => boolean,
{ intervalMs = 50, timeoutMs = 10_000 }: TriggerUntilOptions = {},
): Promise<void> {
const deadline = Date.now() + timeoutMs;
let attempts = 0;
while (!observed()) {
if (Date.now() > deadline) {
throw new Error(
`triggerUntil: not observed after ${attempts} trigger(s) in ${timeoutMs} ms`,
);
}
await trigger();
attempts++;
// Give the watcher a moment to report before triggering again.
for (let waited = 0; waited < intervalMs && !observed(); waited += 10) {
await new Promise((resolve) => setTimeout(resolve, 10));
}
}
}

Tested in examples/src/watchers.ts:20

Concepts​