Skip to content

Commit

Permalink
Merge pull request #91 from hypercerts-org/fix/remove_ownerOf_call
Browse files Browse the repository at this point in the history
feat(parse): batchwise parsing claimstored logs
  • Loading branch information
bitbeckers authored Jun 8, 2024
2 parents 687b4aa + f540f7c commit e2788f7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
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

0 comments on commit e2788f7

Please sign in to comment.