diff --git a/packages/block-logs-stream/src/fetchBlockLogs.ts b/packages/block-logs-stream/src/fetchBlockLogs.ts index 885592e45f..cefcbdb9a0 100644 --- a/packages/block-logs-stream/src/fetchBlockLogs.ts +++ b/packages/block-logs-stream/src/fetchBlockLogs.ts @@ -1,6 +1,7 @@ -import { AbiEvent, Log } from "viem"; +import { AbiEvent } from "viem"; import { GroupLogsByBlockNumberResult, groupLogsByBlockNumber } from "./groupLogsByBlockNumber"; import { FetchLogsOptions, FetchLogsResult, fetchLogs } from "./fetchLogs"; +import { iteratorToArray } from "@latticexyz/common/utils"; /** * Fetches all logs from the blockchain for the given range, grouped by block number. @@ -12,14 +13,14 @@ import { FetchLogsOptions, FetchLogsResult, fetchLogs } from "./fetchLogs"; * * @param {FetchLogsOptions} options See `FetchLogsOptions`. * + * @returns {GroupLogsByBlockNumberResult} See `GroupLogsByBlockNumberResult`. + * * @throws Will throw an error if the block range can't be reduced any further. */ export async function fetchBlockLogs( opts: FetchLogsOptions, -): Promise>> { - const results: FetchLogsResult[] = []; - for await (const result of fetchLogs(opts)) { - results.push(result); - } - return groupLogsByBlockNumber(results.flatMap((result) => result.logs)); +): Promise["logs"][number]>> { + const fetchedLogs = await iteratorToArray(fetchLogs(opts)); + const logs = fetchedLogs.flatMap(({ logs }) => logs); + return groupLogsByBlockNumber(logs); } diff --git a/packages/block-logs-stream/src/groupLogsByBlockNumber.ts b/packages/block-logs-stream/src/groupLogsByBlockNumber.ts index b5bf5ad483..82f6b8ba6e 100644 --- a/packages/block-logs-stream/src/groupLogsByBlockNumber.ts +++ b/packages/block-logs-stream/src/groupLogsByBlockNumber.ts @@ -1,11 +1,11 @@ import { BlockNumber } from "viem"; import { bigIntSort, isDefined } from "@latticexyz/common/utils"; -type PartialLog = { blockNumber: bigint; logIndex: number }; +type PartialLog = { readonly blockNumber: bigint; readonly logIndex: number }; export type GroupLogsByBlockNumberResult = { - blockNumber: log["blockNumber"]; - logs: readonly log[]; + readonly blockNumber: log["blockNumber"]; + readonly logs: readonly log[]; }[]; /** diff --git a/packages/common/src/utils/iteratorToArray.ts b/packages/common/src/utils/iteratorToArray.ts index e1164f7d92..0ad050624f 100644 --- a/packages/common/src/utils/iteratorToArray.ts +++ b/packages/common/src/utils/iteratorToArray.ts @@ -1,4 +1,4 @@ -export async function iteratorToArray(iterator: AsyncIterable): Promise { +export async function iteratorToArray(iterator: AsyncIterable): Promise { const items: T[] = []; for await (const item of iterator) { items.push(item);