From ae25e9bd86637c232a902da563f8447d6c87db75 Mon Sep 17 00:00:00 2001 From: Sniezka Date: Wed, 11 Dec 2024 11:54:11 +0100 Subject: [PATCH] Enhance event source validation --- src/snap-points.ts | 24 +++++++++++++----------- src/utils.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/snap-points.ts b/src/snap-points.ts index 8f88cd7dc7..f248f99f9f 100644 --- a/src/snap-points.ts +++ b/src/snap-points.ts @@ -20,6 +20,7 @@ import { processNewOpen, processNewClosed, processNewOpenClosed, + validateLogs, } from "./utils"; import { IActive, IConfig, IPoints, IPoolAndTicks, IPositions } from "./types"; import { @@ -94,23 +95,24 @@ export const createSnapshotForNetwork = async (network: Network) => { MAX_SIGNATURES_PER_CALL ); - const finalLogs = txLogs.flat(); const previousData = JSON.parse(fs.readFileSync(eventsSnapFilename, "utf-8")); const eventsObject: Record = { ...convertJson(previousData), }; - const eventLogs: string[] = []; + // const finalLogs = txLogs.flat(); + // const eventLogs: string[] = []; + // finalLogs.map((log, index) => { + // if ( + // log.startsWith("Program data:") && + // finalLogs[index + 1].startsWith( + // `Program ${market.program.programId.toBase58()}` + // ) + // ) + // eventLogs.push(log.split("Program data: ")[1]); + // }); - finalLogs.map((log, index) => { - if ( - log.startsWith("Program data:") && - finalLogs[index + 1].startsWith( - `Program ${market.program.programId.toBase58()}` - ) - ) - eventLogs.push(log.split("Program data: ")[1]); - }); + const eventLogs = validateLogs(txLogs, market.program.programId); const decodedEvents = eventLogs .map((log) => market.eventDecoder.decode(log)) diff --git a/src/utils.ts b/src/utils.ts index 8c169efb46..58ce0499b6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -298,3 +298,29 @@ export const processNewOpenClosed = ( return updatedNewOpenClosed; }; + +export const validateLogs = (logs: string[][], programId: PublicKey) => { + const rawEvents: string[] = []; + for (const txLog of logs) { + txLog.map((log, index) => { + if ( + log.slice(0, -4) === `Program ${programId.toBase58()} invoke` && + (txLog[index + 1] === "Program log: Instruction: CreatePosition" || + txLog[index + 1] === "Program log: Instruction: RemovePosition") + ) { + for (let i = index; i < txLog.length; i++) { + if (txLog[i] === `Program ${programId.toBase58()} success`) { + const associatedSlice = txLog.slice(index, i + 1); + const event = associatedSlice.find((log) => + log.startsWith("Program data:") + ); + if (event) { + rawEvents.push(event); + } + } + } + } + }); + } + return rawEvents; +};