Skip to content

Commit

Permalink
🐛 Fix campaign distribution stats
Browse files Browse the repository at this point in the history
  • Loading branch information
KONFeature committed Sep 30, 2024
1 parent 0dbdf00 commit 69a0876
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 48 deletions.
1 change: 1 addition & 0 deletions packages/ponder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"serve": "ponder --config config/config-local.ts serve",
"serve:dev": "ponder --config config/config-dev.ts serve",
"start": "ponder --config config/config-local.ts start",
"start:dev": "ponder --config config/config-dev.ts start",
"typecheck": "tsc"
},
"dependencies": {
Expand Down
48 changes: 30 additions & 18 deletions packages/ponder/ponder.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,46 @@ export default createSchema((p) => ({
}),

// Product related stuff
ProductAdministrator: p.createTable({
id: p.hex(),
ProductAdministrator: p.createTable(
{
id: p.hex(),

productId: p.bigint().references("Product.id"),
product: p.one("productId"),
productId: p.bigint().references("Product.id"),
product: p.one("productId"),

isOwner: p.boolean(),
roles: p.bigint(),
user: p.hex(),
isOwner: p.boolean(),
roles: p.bigint(),
user: p.hex(),

createdTimestamp: p.bigint(),
}),
createdTimestamp: p.bigint(),
},
{
productIndex: p.index("productId"),
userIndex: p.index("user"),
}
),

/* -------------------------------------------------------------------------- */
/* Interaction related stuff */
/* -------------------------------------------------------------------------- */

ProductInteractionContract: p.createTable({
id: p.hex(), // address
ProductInteractionContract: p.createTable(
{
id: p.hex(), // address

productId: p.bigint().references("Product.id"),
product: p.one("productId"),
productId: p.bigint().references("Product.id"),
product: p.one("productId"),

referralTree: p.hex(),
referralTree: p.hex(),

createdTimestamp: p.bigint(),
lastUpdateTimestamp: p.bigint().optional(),
removedTimestamp: p.bigint().optional(),
}),
createdTimestamp: p.bigint(),
lastUpdateTimestamp: p.bigint().optional(),
removedTimestamp: p.bigint().optional(),
},
{
productIndex: p.index("productId"),
}
),

InteractionEvent: p.createTable(
{
Expand Down Expand Up @@ -252,6 +263,7 @@ export default createSchema((p) => ({
},
{
rewardIndex: p.index("rewardId"),
emitterIndex: p.index("emitter"),
}
),
RewardClaimedEvent: p.createTable(
Expand Down
22 changes: 7 additions & 15 deletions packages/ponder/src/campaign/campaignReward.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ponder } from "@/generated";
import { emptyCampaignStats } from "../interactions/stats";
import { increaseCampaignsStats } from "../interactions/stats";

ponder.on("CampaignBanks:RewardAdded", async ({ event, context }) => {
const { BankingContract, Reward, RewardAddedEvent, ReferralCampaignStats } =
context.db;
const { BankingContract, Reward, RewardAddedEvent } = context.db;

// Try to find a rewarding contract for the given event emitter
const bankingContract = await BankingContract.findUnique({
Expand Down Expand Up @@ -52,20 +51,13 @@ ponder.on("CampaignBanks:RewardAdded", async ({ event, context }) => {
});

// Update the current campaigns stats for the distributed amount
await ReferralCampaignStats.upsert({
id: event.log.address,
create: {
...emptyCampaignStats,
campaignId: event.log.address,
await increaseCampaignsStats({
productId: bankingContract.productId,
context,
blockNumber: event.block.number,
increments: {
totalRewards: event.args.amount,
},
// Update the given field by incrementing them
update: ({ current }) => {
return {
...current,
totalRewards: current.totalRewards + (event.args.amount ?? 0n),
};
},
});
});

Expand Down
6 changes: 3 additions & 3 deletions packages/ponder/src/interactions/pressInteractions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ponder } from "@/generated";
import { increaseCampaignsInteractions } from "./stats";
import { increaseCampaignsStats } from "./stats";

ponder.on("ProductInteraction:ArticleRead", async ({ event, context }) => {
const { InteractionEvent } = context.db;
Expand All @@ -17,7 +17,7 @@ ponder.on("ProductInteraction:ArticleRead", async ({ event, context }) => {
});

// Update the current campaigns stats
await increaseCampaignsInteractions({
await increaseCampaignsStats({
interactionEmitter: event.log.address,
blockNumber: event.block.number,
context,
Expand All @@ -42,7 +42,7 @@ ponder.on("ProductInteraction:ArticleOpened", async ({ event, context }) => {
});

// Update the current campaigns stats
await increaseCampaignsInteractions({
await increaseCampaignsStats({
interactionEmitter: event.log.address,
blockNumber: event.block.number,
context,
Expand Down
6 changes: 3 additions & 3 deletions packages/ponder/src/interactions/purchaseInteractions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ponder } from "@/generated";
import { increaseCampaignsInteractions } from "./stats";
import { increaseCampaignsStats } from "./stats";

ponder.on("ProductInteraction:PurchaseStarted", async ({ event, context }) => {
const { InteractionEvent } = context.db;
Expand All @@ -17,7 +17,7 @@ ponder.on("ProductInteraction:PurchaseStarted", async ({ event, context }) => {
});

// Update the current campaigns stats
await increaseCampaignsInteractions({
await increaseCampaignsStats({
interactionEmitter: event.log.address,
blockNumber: event.block.number,
context,
Expand All @@ -44,7 +44,7 @@ ponder.on(
});

// Update the current campaigns stats
await increaseCampaignsInteractions({
await increaseCampaignsStats({
interactionEmitter: event.log.address,
blockNumber: event.block.number,
context,
Expand Down
6 changes: 3 additions & 3 deletions packages/ponder/src/interactions/referralInteractions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ponder } from "@/generated";
import { increaseCampaignsInteractions } from "./stats";
import { increaseCampaignsStats } from "./stats";

ponder.on(
"ProductInteraction:ReferralLinkCreation",
Expand All @@ -19,7 +19,7 @@ ponder.on(
});

// Update the current campaigns stats
await increaseCampaignsInteractions({
await increaseCampaignsStats({
interactionEmitter: event.log.address,
blockNumber: event.block.number,
context,
Expand All @@ -46,7 +46,7 @@ ponder.on("ProductInteraction:UserReferred", async ({ event, context }) => {
});

// Update the current campaigns stats
await increaseCampaignsInteractions({
await increaseCampaignsStats({
interactionEmitter: event.log.address,
blockNumber: event.block.number,
context,
Expand Down
30 changes: 24 additions & 6 deletions packages/ponder/src/interactions/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const emptyCampaignStats = {
};

export type StatsIncrementsParams = Partial<
Omit<Schema["ReferralCampaignStats"], "id" | "campaignId" | "totalRewards">
Omit<Schema["ReferralCampaignStats"], "id" | "campaignId">
>;

/**
Expand All @@ -26,13 +26,15 @@ export type StatsIncrementsParams = Partial<
* @param context
* @param increments fields to increments
*/
export async function increaseCampaignsInteractions({
export async function increaseCampaignsStats({
interactionEmitter,
productId,
context,
increments,
blockNumber,
}: {
interactionEmitter: Address;
interactionEmitter?: Address;
productId?: bigint;
context: Context;
blockNumber: bigint;
increments: StatsIncrementsParams;
Expand All @@ -41,9 +43,25 @@ export async function increaseCampaignsInteractions({
context.db;

// Find the interaction contract
const interactionContract = await ProductInteractionContract.findUnique({
id: interactionEmitter,
});
let interactionContract: Schema["ProductInteractionContract"] | null = null;
if (interactionEmitter) {
interactionContract = await ProductInteractionContract.findUnique({
id: interactionEmitter,
});
} else if (productId) {
const interactions = await ProductInteractionContract.findMany({
where: { productId },
});
interactionContract = interactions?.items?.[0] ?? null;
}

if (!interactionContract) {
console.log("Interaction contract not found for stats update", {
interactionEmitter,
productId,
});
return;
}

if (!interactionContract) {
console.log("Interaction contract not found for stats update", {
Expand Down

0 comments on commit 69a0876

Please sign in to comment.