Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parse): batchwise parsing claimstored logs #91

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions src/indexer/indexClaimsStored.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -50,24 +53,34 @@ 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,
},
};
}),
);

const claims = results.flatMap((result) => result.claims);

const contractEventUpdates = results.flatMap((result) => [
Expand All @@ -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;
};
24 changes: 13 additions & 11 deletions src/parsing/claimStoredEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ export const ClaimStoredEventSchema = z.object({

export type ClaimStoredEvent = z.infer<typeof ClaimStoredEventSchema>;

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<Object>} A promise that resolves to an object containing the parsed event data.
* @returns {Promise<ParsedClaimStoredEvent>} 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) => {
Expand All @@ -33,23 +42,16 @@ 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,
uri: args.uri,
block_number: blockNumber,
units: args.totalUnits,
};

return parsedEvent;
} catch (error) {
console.error(
"[ParseClaimStoredEvent] Error parsing claim stored event",
Expand Down