Skip to content

Commit

Permalink
allow get block in event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
boudra committed Feb 27, 2024
1 parent 2a1bd9a commit 46c552b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/eventProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { AsyncEventEmitter } from "@/asyncEventEmitter";
import { Indexer, IndexerEvents } from "@/indexer";
import { EventHandler } from "@/types";
import { SubscriptionStore } from "./subscriptionStore";
import { Cache } from "@/cache";
import { RpcClient } from "@/rpc";

export async function processEvents<
TAbis extends Record<string, Abi>,
Expand All @@ -30,6 +32,8 @@ export async function processEvents<
readContract: Indexer<TAbis, TContext>["readContract"];
subscribeToContract: Indexer<TAbis, TContext>["subscribeToContract"];
unsubscribeFromContract: Indexer<TAbis, TContext>["unsubscribeFromContract"];
cache: Cache | null;
rpcClient: RpcClient;
}) {
const {
chainId,
Expand All @@ -43,6 +47,8 @@ export async function processEvents<
subscriptionStore,
subscribeToContract,
unsubscribeFromContract,
cache,
rpcClient,
} = args;

const subscriptionCount = subscriptions.size;
Expand Down Expand Up @@ -85,6 +91,38 @@ export async function processEvents<
unsubscribeFromContract: (opts) => {
return unsubscribeFromContract(opts);
},
getBlock: async () => {
const cachedBlock = await cache?.getBlockByNumber({
chainId: chainId,
blockNumber: event.blockNumber,
});

if (cachedBlock) {
return cachedBlock;
}

const rpcBlock = await rpcClient.getBlockByNumber({
number: event.blockNumber,
});

// should not happen
if (!rpcBlock) {
throw new Error(`Block ${event.blockNumber} not found`);
}

const block = {
chainId: chainId,
blockNumber: event.blockNumber,
blockHash: rpcBlock.hash,
timestamp: rpcBlock.timestamp,
};

if (cache) {
await cache?.insertBlock(block);
}

return block;
},
subscribeToContract: (opts) => {
return subscribeToContract({
...opts,
Expand Down
2 changes: 2 additions & 0 deletions src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ export function createIndexer<
readContract: readContract,
subscribeToContract: subscribeToContract,
unsubscribeFromContract,
cache,
rpcClient,
});

for (const id of subscriptionIds) {
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ExtractAbiFunctionNames,
} from "abitype";
import { Address, GetEventArgs } from "viem";
import { Block } from "@/cache";

export type Hex = `0x${string}`;
export type ToBlock = "latest" | bigint;
Expand Down Expand Up @@ -83,6 +84,8 @@ export type EventHandlerArgs<
address: Address;
}
): void;

getBlock(this: void): Promise<Block>;
};

export type EventHandler<
Expand Down

0 comments on commit 46c552b

Please sign in to comment.