Skip to content

Commit

Permalink
🐛 Better stat indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Sep 2, 2024
1 parent a1a8e1d commit 26926f7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/ponder/src/interactions/pressInteractions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ponder.on("ProductInteraction:ArticleRead", async ({ event, context }) => {
// Update the current campaigns stats
await increaseCampaignsInteractions({
interactionEmitter: event.log.address,
blockNumber: event.block.number,
context,
increments: {
readInteractions: 1n,
Expand All @@ -43,6 +44,7 @@ ponder.on("ProductInteraction:ArticleOpened", async ({ event, context }) => {
// Update the current campaigns stats
await increaseCampaignsInteractions({
interactionEmitter: event.log.address,
blockNumber: event.block.number,
context,
increments: {
openInteractions: 1n,
Expand Down
2 changes: 2 additions & 0 deletions packages/ponder/src/interactions/referralInteractions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ponder.on(
// Update the current campaigns stats
await increaseCampaignsInteractions({
interactionEmitter: event.log.address,
blockNumber: event.block.number,
context,
increments: {
createReferredLinkInteractions: 1n,
Expand All @@ -47,6 +48,7 @@ ponder.on("ProductInteraction:UserReferred", async ({ event, context }) => {
// Update the current campaigns stats
await increaseCampaignsInteractions({
interactionEmitter: event.log.address,
blockNumber: event.block.number,
context,
increments: {
referredInteractions: 1n,
Expand Down
35 changes: 34 additions & 1 deletion packages/ponder/src/interactions/stats.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Context, Schema } from "@/generated";
import type { Address } from "viem";
import { interactionCampaignAbi } from "../../abis/frak-campaign-abis";

/**
* Get the rewarding contract for the given event emitter
Expand All @@ -11,9 +12,11 @@ export async function increaseCampaignsInteractions({
interactionEmitter,
context,
increments,
blockNumber,
}: {
interactionEmitter: Address;
context: Context;
blockNumber: bigint;
increments: Partial<
Pick<
Schema["PressCampaignStats"],
Expand Down Expand Up @@ -48,13 +51,43 @@ export async function increaseCampaignsInteractions({
return;
}

// Ensure the given campaign was active at this block
let isActiveDuringInteraction: boolean[] = [];
try {
isActiveDuringInteraction = await context.client.multicall({
allowFailure: false,
contracts: campaigns.items.map(
(campaign) =>
({
address: campaign.id,
abi: interactionCampaignAbi,
functionName: "isActive",
}) as const
),
blockNumber: blockNumber,
});
} catch (error) {
console.error("Error during campaign.isActive multicall check", error);
return;
}

// Perform the increments
// todo: Should use an `updateMany` if we are sure that campaign stats are created
for (const campaign of campaigns.items) {
for (const [index, campaign] of campaigns.items.entries()) {
if (!campaign.id) {
console.error("Campaign id not found", campaign);
continue;
}

// Check if the campaign was active during the interaction
if (!isActiveDuringInteraction[index]) {
console.log("Campaign was not active during the interaction", {
campaign,
interactionEmitter,
});
continue;
}

try {
// Create the stats if not found
await PressCampaignStats.upsert({
Expand Down

0 comments on commit 26926f7

Please sign in to comment.