Skip to content

Commit

Permalink
chore(store-sync): use indexer v2 api for logs
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
JamesLefrere committed Jan 21, 2025
1 parent 6b17f8b commit ec4c0c0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
16 changes: 12 additions & 4 deletions packages/store-sync/src/createStoreSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SyncOptions,
SyncResult,
internalTableIds,
schemasTable,
WaitForTransactionResult,
} from "./common";
import { createBlockStream } from "@latticexyz/block-logs-stream";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -72,10 +73,17 @@ export async function createStoreSync({
initialBlockLogs,
indexerUrl: initialIndexerUrl,
}: CreateStoreSyncOptions): Promise<SyncResult> {
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 =>
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions packages/store-sync/src/getRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ export async function getRecords<table extends Table>(
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 ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit ec4c0c0

Please sign in to comment.