diff --git a/packages/engine/paima-rest/src/controllers/BasicControllers.ts b/packages/engine/paima-rest/src/controllers/BasicControllers.ts index fe81aa48..03c1e357 100644 --- a/packages/engine/paima-rest/src/controllers/BasicControllers.ts +++ b/packages/engine/paima-rest/src/controllers/BasicControllers.ts @@ -413,6 +413,49 @@ export class GetLogsController extends Controller { @Post() public async post(@Body() params: GetLogsParams): Promise { const gameStateMachine = EngineService.INSTANCE.getSM(); + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const eventDefinition = ((): any => { + const appEvents = gameStateMachine.getAppEvents(); + + for (const defs of Object.values(appEvents)) { + for (const def of defs) { + if (def.topicHash === params.topic) { + return def; + } + } + } + + return undefined; + })(); + + if (!eventDefinition) { + this.setStatus(StatusCodes.NOT_FOUND); + return { + success: false, + errorMessage: 'Topic not found', + }; + } + + if (params.filters) { + const indexedFields = new Set(); + for (const field of eventDefinition.definition.fields) { + if (field.indexed) { + indexedFields.add(field.name); + } + } + + for (const fieldName of Object.keys(params.filters)) { + if (!indexedFields.has(fieldName)) { + this.setStatus(StatusCodes.NOT_FOUND); + return { + success: false, + errorMessage: `Field is not indexed: ${fieldName}`, + }; + } + } + } + try { const DBConn = gameStateMachine.getReadonlyDbConn(); const base: Record & { topic: string } = { diff --git a/packages/engine/paima-sm/src/index.ts b/packages/engine/paima-sm/src/index.ts index c4904765..edff3767 100644 --- a/packages/engine/paima-sm/src/index.ts +++ b/packages/engine/paima-sm/src/index.ts @@ -325,6 +325,9 @@ const SM: GameStateMachineInitializer = { await blockHeightDone.run({ block_height: latestChainData.blockNumber }, dbTx); return processedCount; }, + getAppEvents(): ReturnType { + return events; + }, }; }, }; diff --git a/packages/engine/paima-sm/src/types.ts b/packages/engine/paima-sm/src/types.ts index 1be10b25..d64ae56c 100644 --- a/packages/engine/paima-sm/src/types.ts +++ b/packages/engine/paima-sm/src/types.ts @@ -695,4 +695,5 @@ export interface GameStateMachine { presyncProcess: (dbTx: PoolClient, latestCdeData: PresyncChainData) => Promise; markPresyncMilestone: (blockHeight: number, network: string) => Promise; dryRun: (gameInput: string, userAddress: string) => Promise; + getAppEvents: () => ReturnType; }