From ec4c0c0cd31c2f7dbd86cd0f13be2a84d9ca8638 Mon Sep 17 00:00:00 2001 From: JamesLefrere Date: Tue, 21 Jan 2025 11:28:34 +0100 Subject: [PATCH] chore(store-sync): use indexer v2 api for logs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Use the v2 api for logs in the indexer client - Always add the schemas table to the filters in `createStoreSync`, as this is no longer included by default in the indexer v2 api - Remove redundant filter for schemas table in `getRecords` - The only other instance of getting indexer logs that requires the schemas table—the public SQL API's `getSnapshot`—calls `createStoreSync` internally, so this can remain unchanged. --- packages/store-sync/src/createStoreSync.ts | 16 ++++++++++++---- packages/store-sync/src/getRecords.ts | 5 +---- .../src/indexer-client/createIndexerClient.ts | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/store-sync/src/createStoreSync.ts b/packages/store-sync/src/createStoreSync.ts index 25448dfd78..6e11b70f41 100644 --- a/packages/store-sync/src/createStoreSync.ts +++ b/packages/store-sync/src/createStoreSync.ts @@ -8,6 +8,7 @@ import { SyncOptions, SyncResult, internalTableIds, + schemasTable, WaitForTransactionResult, } from "./common"; import { createBlockStream } from "@latticexyz/block-logs-stream"; @@ -38,7 +39,7 @@ import { getSnapshot } from "./getSnapshot"; import { fromEventSource } from "./fromEventSource"; import { fetchAndStoreLogs } from "./fetchAndStoreLogs"; import { isLogsApiResponse } from "./indexer-client/isLogsApiResponse"; -import { toStorageAdatperBlock } from "./indexer-client/toStorageAdapterBlock"; +import { toStorageAdapterBlock } from "./indexer-client/toStorageAdapterBlock"; import { watchLogs } from "./watchLogs"; import { getAction } from "viem/utils"; import { getChainId, getTransactionReceipt } from "viem/actions"; @@ -72,10 +73,17 @@ export async function createStoreSync({ initialBlockLogs, indexerUrl: initialIndexerUrl, }: CreateStoreSyncOptions): Promise { - const filters: SyncFilter[] = + const filters: SyncFilter[] = ( initialFilters.length || tableIds.length ? [...initialFilters, ...tableIds.map((tableId) => ({ tableId })), ...defaultFilters] - : []; + : [] + ).concat([ + // The schemas table is always added to the filters in order for the storage adapters + // to process table registration logs (necessary for initializing the storage). + { + tableId: schemasTable.tableId, + }, + ]); const logFilter = filters.length ? (log: StoreEventsLog): boolean => @@ -246,7 +254,7 @@ export async function createStoreSync({ if (!isLogsApiResponse(data)) { throw new Error("Received unexpected from indexer:" + messageEvent.data); } - return toStorageAdatperBlock(data); + return toStorageAdapterBlock(data); }), concatMap(async (block) => { await storageAdapter(block); diff --git a/packages/store-sync/src/getRecords.ts b/packages/store-sync/src/getRecords.ts index c831417d4c..20f5a66795 100644 --- a/packages/store-sync/src/getRecords.ts +++ b/packages/store-sync/src/getRecords.ts @@ -52,10 +52,7 @@ export async function getRecords( indexerUrl: options.indexerUrl, filters: [{ tableId: options.table.tableId }], }); - // By default, the indexer includes the `store.Tables` table as part of the snapshot. - // Once we change this default, we can remove the filter here. - // See https://github.com/latticexyz/mud/issues/3386. - return logs?.logs.filter((log) => log.args.tableId === options.table.tableId) ?? []; + return logs?.logs ?? []; } else { const client = options.client ?? diff --git a/packages/store-sync/src/indexer-client/createIndexerClient.ts b/packages/store-sync/src/indexer-client/createIndexerClient.ts index d4a5c774ed..c646639001 100644 --- a/packages/store-sync/src/indexer-client/createIndexerClient.ts +++ b/packages/store-sync/src/indexer-client/createIndexerClient.ts @@ -28,7 +28,7 @@ export function createIndexerClient({ url }: CreateIndexerClientOptions): Indexe try { const input = encodeURIComponent(JSON.stringify(opts)); const urlOrigin = new URL(url).origin; - const response = await fetch(`${urlOrigin}/api/logs?input=${input}`, { method: "GET" }); + const response = await fetch(`${urlOrigin}/api/2/logs?input=${input}`, { method: "GET" }); // TODO: return a readable stream instead of fetching the entire response at once const result = await response.json();