Skip to content

Commit 64949fd

Browse files
fix: update round / application stats for all strategies (#110)
# 🤖 Linear Closes PAR-XXX ## Description - update round donation stats for direct allocation ## Checklist before requesting a review - [ ] I have conducted a self-review of my code. - [ ] I have conducted a QA. - [ ] If it is a core feature, I have included comprehensive tests. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Enhanced donation processing by incorporating round-based donation statistics tracking, providing a more comprehensive view of donation performance across rounds. - Added functionality to track unique donors for both applications and rounds, improving insights into donor engagement. - Introduced new operations for tracking total distributed amounts for specific rounds, enhancing event handling logic. - Added assertions in tests to verify the correct handling of new donation statistics events. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents ac766f4 + f132065 commit 64949fd

File tree

6 files changed

+76
-1
lines changed

6 files changed

+76
-1
lines changed

packages/processors/src/processors/strategy/directAllocation/handlers/directAllocated.handler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ export class DirectAllocatedHandler implements IEventHandler<"Strategy", "Direct
8686
type: "InsertDonation",
8787
args: { donation },
8888
},
89+
{
90+
type: "IncrementRoundDonationStats",
91+
args: {
92+
chainId: this.chainId,
93+
roundId: round.id,
94+
amountInUsd,
95+
},
96+
},
8997
];
9098
}
9199
}

packages/processors/src/processors/strategy/directGrantsLite/handlers/allocated.handler.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class DGLiteAllocatedHandler implements IEventHandler<"Strategy", "Alloca
3030

3131
/**
3232
* Handles the AllocatedWithToken event for the Direct Grants Lite strategy.
33-
* @returns The changeset with an InsertApplicationPayout operation.
33+
* @returns The changeset with an InsertApplicationPayout and IncrementRoundTotalDistributed operation.
3434
* @throws RoundNotFound if the round is not found.
3535
* @throws ApplicationNotFound if the application is not found.
3636
* @throws TokenNotFound if the token is not found.
@@ -104,6 +104,14 @@ export class DGLiteAllocatedHandler implements IEventHandler<"Strategy", "Alloca
104104
},
105105
},
106106
},
107+
{
108+
type: "IncrementRoundTotalDistributed",
109+
args: {
110+
chainId: this.chainId,
111+
roundId: round.id,
112+
amount: amountInRoundMatchToken,
113+
},
114+
},
107115
];
108116
}
109117
}

packages/processors/test/strategy/directAllocation/handlers/directAllocated.handler.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ describe("DirectAllocatedHandler", () => {
109109
},
110110
},
111111
},
112+
{
113+
type: "IncrementRoundDonationStats",
114+
args: {
115+
chainId,
116+
roundId: mockRound.id,
117+
amountInUsd: "20000",
118+
},
119+
},
112120
]);
113121
});
114122

packages/processors/test/strategy/directGrantsLite/handlers/allocated.handler.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ describe("DGLiteAllocatedHandler", () => {
105105
},
106106
},
107107
});
108+
109+
expect(result[1]).toEqual({
110+
type: "IncrementRoundTotalDistributed",
111+
args: {
112+
chainId,
113+
roundId: "round1",
114+
amount: BigInt(amount),
115+
},
116+
});
108117
});
109118

110119
it("doesn't fetch token price if amount is 0", async () => {
@@ -159,6 +168,15 @@ describe("DGLiteAllocatedHandler", () => {
159168
expect(changeset.args.applicationPayout.amount).toBe(0n);
160169
expect(changeset.args.applicationPayout.amountInUsd).toBe("0");
161170
expect(changeset.args.applicationPayout.amountInRoundMatchToken).toBe(0n);
171+
172+
expect(result[1]).toEqual({
173+
type: "IncrementRoundTotalDistributed",
174+
args: {
175+
chainId,
176+
roundId: "round1",
177+
amount: BigInt(0),
178+
},
179+
});
162180
});
163181

164182
it("throws RoundNotFound if round is not found", async () => {
@@ -369,5 +387,13 @@ describe("DGLiteAllocatedHandler", () => {
369387
args: { applicationPayout: { amountInRoundMatchToken: bigint } };
370388
};
371389
expect(changeset.args.applicationPayout.amountInRoundMatchToken).toBe(parseEther("0.005"));
390+
expect(result[1]).toEqual({
391+
type: "IncrementRoundTotalDistributed",
392+
args: {
393+
chainId,
394+
roundId: "round1",
395+
amount: parseEther("0.005"),
396+
},
397+
});
372398
});
373399
});

packages/repository/src/kysely/repositories/application.repository.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,24 @@ export class KyselyApplicationRepository implements IApplicationRepository<Kysel
169169
): Promise<void> {
170170
try {
171171
const queryBuilder = (tx || this.db).withSchema(this.schemaName);
172+
173+
const uniqueDonorsResult = await queryBuilder
174+
.withSchema(this.schemaName)
175+
.selectFrom("donations")
176+
.where("chainId", "=", where.chainId)
177+
.where("roundId", "=", where.roundId)
178+
.where("applicationId", "=", where.id)
179+
.select((qb) => qb.fn.count("donorAddress").distinct().as("uniqueDonorsCount"))
180+
.executeTakeFirst();
181+
182+
const uniqueDonorsCount = Number(uniqueDonorsResult?.uniqueDonorsCount ?? 0);
183+
172184
await queryBuilder
173185
.updateTable("applications")
174186
.set((eb) => ({
175187
totalDonationsCount: eb("totalDonationsCount", "+", 1),
176188
totalAmountDonatedInUsd: eb("totalAmountDonatedInUsd", "+", amountInUsd),
189+
uniqueDonorsCount,
177190
}))
178191
.where("id", "=", where.id)
179192
.where("chainId", "=", where.chainId)

packages/repository/src/kysely/repositories/round.repository.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,23 @@ export class KyselyRoundRepository implements IRoundRepository<KyselyTransaction
213213
): Promise<void> {
214214
try {
215215
const queryBuilder = (tx || this.db).withSchema(this.schemaName);
216+
217+
const uniqueDonorsResult = await queryBuilder
218+
.withSchema(this.schemaName)
219+
.selectFrom("donations")
220+
.where("chainId", "=", where.chainId)
221+
.where("roundId", "=", where.roundId)
222+
.select((qb) => qb.fn.count("donorAddress").distinct().as("uniqueDonorsCount"))
223+
.executeTakeFirst();
224+
225+
const uniqueDonorsCount = Number(uniqueDonorsResult?.uniqueDonorsCount ?? 0);
226+
216227
await queryBuilder
217228
.updateTable("rounds")
218229
.set((eb) => ({
219230
totalDonationsCount: eb("totalDonationsCount", "+", 1),
220231
totalAmountDonatedInUsd: eb("totalAmountDonatedInUsd", "+", amountInUsd),
232+
uniqueDonorsCount,
221233
}))
222234
.where("chainId", "=", where.chainId)
223235
.where("id", "=", where.roundId)

0 commit comments

Comments
 (0)