From f540f7c88f2b597ce49b8a25d50939a72b7d1b79 Mon Sep 17 00:00:00 2001 From: bitbeckers Date: Sat, 8 Jun 2024 10:22:13 +0200 Subject: [PATCH] feat(parse): batchwise parsing claimstored logs --- src/indexer/indexClaimsStored.ts | 39 ++++++++++++++++++++++++-------- src/parsing/claimStoredEvent.ts | 24 +++++++++++--------- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/indexer/indexClaimsStored.ts b/src/indexer/indexClaimsStored.ts index e4fb0bd..5cc1b65 100644 --- a/src/indexer/indexClaimsStored.ts +++ b/src/indexer/indexClaimsStored.ts @@ -1,4 +1,7 @@ -import { parseClaimStoredEvent } from "@/parsing/claimStoredEvent.js"; +import { + parseClaimStoredEvent, + ParsedClaimStoredEvent, +} from "@/parsing/claimStoredEvent.js"; import { IndexerConfig } from "@/types/types.js"; import { getContractEventsForChain } from "@/storage/getContractEventsForChain.js"; import { updateLastBlockIndexedContractEvents } from "@/storage/updateLastBlockIndexedContractEvents.js"; @@ -50,16 +53,27 @@ export const indexClaimsStoredEvents = async ({ contractEvent, }); - // parse logs to get claimID, contractAddress and cid - const events = await Promise.all(logs.map(parseClaimStoredEvent)); + // Split logs into chunks + const logChunks = chunkArray(logs, 10); - const claims = events.map((claim) => ({ - ...claim, - contracts_id: contractEvent.contracts_id, - })); + // Initialize an empty array to store all claims + let allClaims: ParsedClaimStoredEvent[] = []; + + // Process each chunk one by one + for (const logChunk of logChunks) { + const events = await Promise.all(logChunk.map(parseClaimStoredEvent)); + + const claims = events.map((claim) => ({ + ...claim, + contracts_id: contractEvent.contracts_id, + })); + + // Add the claims from the current chunk to the allClaims array + allClaims = [...allClaims, ...claims]; + } return { - claims, + claims: allClaims, contractEventUpdate: { ...contractEvent, last_block_indexed: toBlock, @@ -67,7 +81,6 @@ export const indexClaimsStoredEvents = async ({ }; }), ); - const claims = results.flatMap((result) => result.claims); const contractEventUpdates = results.flatMap((result) => [ @@ -82,3 +95,11 @@ export const indexClaimsStoredEvents = async ({ }), ); }; + +const chunkArray = (array, size) => { + const result = []; + for (let i = 0; i < array.length; i += size) { + result.push(array.slice(i, i + size)); + } + return result; +}; diff --git a/src/parsing/claimStoredEvent.ts b/src/parsing/claimStoredEvent.ts index 9acf1a3..52442c5 100644 --- a/src/parsing/claimStoredEvent.ts +++ b/src/parsing/claimStoredEvent.ts @@ -17,11 +17,20 @@ export const ClaimStoredEventSchema = z.object({ export type ClaimStoredEvent = z.infer; +export type ParsedClaimStoredEvent = { + owner_address: string; + creator_address: string; + token_id: bigint; + uri: string; + block_number: bigint; + units: bigint; +}; + /** * Parses a ClaimStoredEvent and retrieves additional information about the transaction. * * @param {unknown} event - The event to parse. - * @returns {Promise} A promise that resolves to an object containing the parsed event data. + * @returns {Promise} A promise that resolves to an object containing the parsed event data. * @throws {z.ZodError} If the event does not match the ClaimStoredEventSchema, a Zod validation error is thrown. */ export const parseClaimStoredEvent = async (event: unknown) => { @@ -33,16 +42,7 @@ export const parseClaimStoredEvent = async (event: unknown) => { hash: transactionHash, }); - // const owner = await client.readContract({ - // address, - // abi: parseAbi([ - // `function ownerOf(uint256 tokenId) view returns (address owner)`, - // ]), - // functionName: "ownerOf", - // args: [args.claimID], - // }); - - return { + const parsedEvent: ParsedClaimStoredEvent = { owner_address: "0x0000000000000000000000000000000000000000", creator_address: transaction.from, token_id: args.claimID, @@ -50,6 +50,8 @@ export const parseClaimStoredEvent = async (event: unknown) => { block_number: blockNumber, units: args.totalUnits, }; + + return parsedEvent; } catch (error) { console.error( "[ParseClaimStoredEvent] Error parsing claim stored event",