From 19db751e1c266f61b2f7922771fba99f2dae62c5 Mon Sep 17 00:00:00 2001 From: Tomasz Kopacki Date: Fri, 29 Nov 2024 12:28:52 +0100 Subject: [PATCH] feat(core.indexing-store): introduce mode param to indexing functions --- packages/core/src/indexing-store/historical.test.ts | 2 ++ packages/core/src/indexing-store/historical.ts | 3 +++ packages/core/src/indexing-store/index.ts | 8 ++++---- packages/core/src/indexing-store/realtime.test.ts | 2 ++ packages/core/src/indexing-store/realtime.ts | 3 +++ packages/core/src/indexing/service.test.ts | 4 ++++ packages/core/src/indexing/service.ts | 4 ++++ 7 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/core/src/indexing-store/historical.test.ts b/packages/core/src/indexing-store/historical.test.ts index d3205a039..163fd7304 100644 --- a/packages/core/src/indexing-store/historical.test.ts +++ b/packages/core/src/indexing-store/historical.test.ts @@ -36,6 +36,8 @@ test("find", async (context) => { initialCheckpoint: encodeCheckpoint(zeroCheckpoint), }); + expect(indexingStore.mode).toBe("historical"); + // empty let result = await indexingStore.find(schema.account, { diff --git a/packages/core/src/indexing-store/historical.ts b/packages/core/src/indexing-store/historical.ts index 4c29a9783..09bb1ec46 100644 --- a/packages/core/src/indexing-store/historical.ts +++ b/packages/core/src/indexing-store/historical.ts @@ -1001,6 +1001,9 @@ export const createHistoricalIndexingStore = ({ isCacheFull() { return cacheBytes > maxBytes; }, + get mode() { + return "historical" as const; + }, } satisfies IndexingStore<"historical">; // @ts-ignore diff --git a/packages/core/src/indexing-store/index.ts b/packages/core/src/indexing-store/index.ts index 8c25ee4a3..676e1cef0 100644 --- a/packages/core/src/indexing-store/index.ts +++ b/packages/core/src/indexing-store/index.ts @@ -9,14 +9,14 @@ import type { Schema } from "@/drizzle/index.js"; import type { Db } from "@/types/db.js"; export type IndexingStore = - policy extends "realtime" - ? Db - : Db & { + policy extends "historical" + ? Db & { mode: policy } & { /** Persist the cache to the database. */ flush: () => Promise; /** Return `true` if the cache size in bytes is above the limit specified by `option.indexingCacheMaxBytes`. */ isCacheFull: () => boolean; - }; + } + : Db & { mode: policy }; export const parseSqlError = (e: any): Error => { let error = getBaseError(e); diff --git a/packages/core/src/indexing-store/realtime.test.ts b/packages/core/src/indexing-store/realtime.test.ts index d35438046..b3505ae91 100644 --- a/packages/core/src/indexing-store/realtime.test.ts +++ b/packages/core/src/indexing-store/realtime.test.ts @@ -36,6 +36,8 @@ test("find", async (context) => { schema, }); + expect(indexingStore.mode).toBe("realtime"); + // empty let result = await indexingStore.find(schema.account, { diff --git a/packages/core/src/indexing-store/realtime.ts b/packages/core/src/indexing-store/realtime.ts index 4918c4c15..fe53bba4d 100644 --- a/packages/core/src/indexing-store/realtime.ts +++ b/packages/core/src/indexing-store/realtime.ts @@ -411,6 +411,9 @@ export const createRealtimeIndexingStore = ({ }), { schema, casing: "snake_case" }, ), + get mode() { + return "realtime" as const; + }, } satisfies IndexingStore<"realtime">; // @ts-ignore diff --git a/packages/core/src/indexing/service.test.ts b/packages/core/src/indexing/service.test.ts index e46d9c038..6b3af9dcf 100644 --- a/packages/core/src/indexing/service.test.ts +++ b/packages/core/src/indexing/service.test.ts @@ -152,6 +152,7 @@ test("processSetupEvents()", async (context) => { expect(indexingFunctions["Erc20:setup"]).toHaveBeenCalledWith({ context: { network: { chainId: 1, name: "mainnet" }, + store: { mode: "realtime" }, contracts: { Erc20: { abi: expect.any(Object), @@ -242,6 +243,7 @@ test("processEvent() log events", async (context) => { }, context: { network: { chainId: 1, name: "mainnet" }, + store: { mode: "realtime" }, contracts: { Erc20: { abi: expect.any(Object), @@ -317,6 +319,7 @@ test("processEvents() block events", async (context) => { }, context: { network: { chainId: 1, name: "mainnet" }, + store: { mode: "realtime" }, contracts: { Erc20: { abi: expect.any(Object), @@ -397,6 +400,7 @@ test("processEvents() call trace events", async (context) => { }, context: { network: { chainId: 1, name: "mainnet" }, + store: { mode: "realtime" }, contracts: { Erc20: { abi: expect.any(Object), diff --git a/packages/core/src/indexing/service.ts b/packages/core/src/indexing/service.ts index f427ec376..a006e139e 100644 --- a/packages/core/src/indexing/service.ts +++ b/packages/core/src/indexing/service.ts @@ -33,6 +33,7 @@ import { type ReadOnlyClient, buildCachedActions } from "./ponderActions.js"; export type Context = { network: { chainId: number; name: string }; + store: Pick, "mode">; client: ReadOnlyClient; db: Db; contracts: Record< @@ -171,6 +172,7 @@ export const create = ({ contextState, context: { network: { name: undefined!, chainId: undefined! }, + store: { mode: undefined! }, contracts: undefined!, client: undefined!, db: undefined!, @@ -367,6 +369,8 @@ export const setIndexingStore = ( delete: indexingStore.delete, sql: indexingStore.sql, }; + + indexingService.currentEvent.context.store.mode = indexingStore.mode; }; export const kill = (indexingService: Service) => {