@latticexyz/block-logs-stream
TypeScript icon, indicating that this package has built-in type declarations

2.0.11 • Public • Published

Block logs stream

A set of utilities for efficiently retrieving blockchain event logs. Built on top of viem and RxJS.

Example

import { filter, map, mergeMap } from "rxjs";
import { createPublicClient, parseAbi } from "viem";
import { createBlockStream, groupLogsByBlockNumber, blockRangeToLogs } from "@latticexyz/block-logs-stream";

const publicClient = createPublicClient({
  // your viem public client config here
});

const latestBlock$ = await createBlockStream({ publicClient, blockTag: "latest" });
const latestBlockNumber$ = latestBlock$.pipe(map((block) => block.number));

latestBlockNumber$
  .pipe(
    map((latestBlockNumber) => ({ startBlock: 0n, endBlock: latestBlockNumber })),
    blockRangeToLogs({
      publicClient,
      address,
      events: parseAbi([
        "event Store_SetRecord(bytes32 indexed tableId, bytes32[] keyTuple, bytes staticData, bytes32 encodedLengths, bytes dynamicData)",
        "event Store_SpliceStaticData(bytes32 indexed tableId, bytes32[] keyTuple, uint48 start, bytes data)",
        "event Store_SpliceDynamicData(bytes32 indexed tableId, bytes32[] keyTuple, uint48 start, uint40 deleteCount, bytes32 encodedLengths, bytes data)",
        "event Store_DeleteRecord(bytes32 indexed tableId, bytes32[] keyTuple)",
      ]),
    }),
    mergeMap(({ logs }) => from(groupLogsByBlockNumber(logs))),
  )
  .subscribe((block) => {
    console.log("got events for block", block);
  });

Readme

Keywords

none

Package Sidebar

Install

npm i @latticexyz/block-logs-stream

Weekly Downloads

1,898

Version

2.0.11

License

MIT

Unpacked Size

50.9 kB

Total Files

15

Last publish

Collaborators

  • holic
  • alvarius
  • l_udens