Skip to content

Commit

Permalink
feat(block-logs-stream): add more logging (#1426)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Sep 11, 2023
1 parent 1d60930 commit b519d96
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/block-logs-stream/src/blockRangeToLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EMPTY, OperatorFunction, concatMap, from, pipe, tap } from "rxjs";
import { FetchLogsResult, fetchLogs } from "./fetchLogs";
import { AbiEvent } from "abitype";
import { Address, BlockNumber, PublicClient } from "viem";
import { debug } from "./debug";

export type BlockRangeToLogsOptions<TAbiEvents extends readonly AbiEvent[]> = {
/**
Expand Down Expand Up @@ -50,9 +51,10 @@ export function blockRangeToLogs<TAbiEvents extends readonly AbiEvent[]>({
toBlock = endBlock;
}),
// concatMap only processes the next emission once the inner observable completes,
// so it always uses the latest`toBlock` value.
// so it always uses the latest `toBlock` value.
concatMap(() => {
if (fromBlock > toBlock) return EMPTY;
debug("fetching logs for block range", { fromBlock, toBlock });
return from(
fetchLogs({
publicClient,
Expand Down
2 changes: 2 additions & 0 deletions packages/block-logs-stream/src/fetchLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ export async function* fetchLogs<TAbiEvents extends readonly AbiEvent[]>({
while (fromBlock <= getLogsOpts.toBlock) {
try {
const toBlock = fromBlock + blockRange;
debug("getting logs", { fromBlock, toBlock });
const logs = await publicClient.getLogs({ ...getLogsOpts, fromBlock, toBlock, strict: true });
yield { fromBlock, toBlock, logs };
fromBlock = toBlock + 1n;
blockRange = bigIntMin(maxBlockRange, getLogsOpts.toBlock - fromBlock);
} catch (error: unknown) {
debug("error getting logs:", String(error));
if (!(error instanceof Error)) throw error;

// TODO: figure out actual rate limit message for RPCs
Expand Down

0 comments on commit b519d96

Please sign in to comment.