diff --git a/src/parser/processor.ts b/src/parser/processor.ts index 54719476..b1d3704c 100644 --- a/src/parser/processor.ts +++ b/src/parser/processor.ts @@ -34,26 +34,28 @@ export class Processor { return this; } + _getRewardsLimit() { + let taskReward = Infinity; + if (!this._configuration.limitRewards) { + return taskReward; + } + for (const item of Object.keys(this._result)) { + if (this._result[item].task) { + taskReward = this._result[item].task.reward * this._result[item].task.multiplier; + return taskReward; + } + } + return taskReward; + } + async run(data: Readonly) { - let taskReward = 0; for (const transformer of this._transformers) { if (transformer.enabled) { this._result = await transformer.transform(data, this._result); } // Aggregate total result for (const item of Object.keys(this._result)) { - this._result[item].total = this._sumRewards(this._result[item]); - if (this._result[item].task) { - taskReward = this._result[item].task.reward * this._result[item].task.multiplier; - } - } - } - if (this._configuration.limitRewards) { - this._context.logger.info(`Limiting rewards to the task reward: ${taskReward}.`); - for (const item of Object.keys(this._result)) { - if (!this._result[item].task) { - this._result[item].total = Math.min(this._result[item].total, taskReward); - } + this._result[item].total = this._sumRewards(this._result[item], this._getRewardsLimit()); } } return this._result; @@ -70,14 +72,16 @@ export class Processor { return result; } - _sumRewards(obj: Record) { + _sumRewards(obj: Record, taskRewardLimit = Infinity) { let totalReward = new Decimal(0); for (const [key, value] of Object.entries(obj)) { if (key === "reward" && typeof value === "number") { - totalReward = totalReward.add(value); + totalReward = totalReward.add(Math.min(value, taskRewardLimit)); } else if (typeof value === "object") { - totalReward = totalReward.add(this._sumRewards(value as Record)); + totalReward = totalReward.add( + Math.min(this._sumRewards(value as Record, taskRewardLimit), taskRewardLimit) + ); } } diff --git a/src/web/api/index.ts b/src/web/api/index.ts index 564af644..400446d0 100644 --- a/src/web/api/index.ts +++ b/src/web/api/index.ts @@ -34,7 +34,11 @@ const baseApp = createPlugin( const app = { fetch: async (request: Request, env: object, ctx: ExecutionContext) => { - if (request.method === "POST" && new URL(request.url).pathname === "/") { + if ( + request.method === "POST" && + new URL(request.url).pathname === "/" && + request.headers.get("origin") === "http://localhost:4000" + ) { try { const originalBody = await request.json(); const modifiedBody = await getPayload( @@ -108,7 +112,7 @@ app.get("/openai/*", () => { return Response.json("OpenAI GET"); }); -const port = 3000; +const port = 4000; export default { fetch: app.fetch, diff --git a/src/web/api/payload.ts b/src/web/api/payload.ts index a72e23dc..5e7a095b 100644 --- a/src/web/api/payload.ts +++ b/src/web/api/payload.ts @@ -10,7 +10,7 @@ export async function getPayload(ownerRepo: string, issueId: number, useOpenAi: if (!useOpenAi) { cfgFile.incentives.contentEvaluator.openAi = { - endpoint: "http://localhost:3000/openai", + endpoint: "http://localhost:4000/openai", }; } @@ -21,6 +21,7 @@ export async function getPayload(ownerRepo: string, issueId: number, useOpenAi: eventName: "issues.closed", action: "closed", env: process.env, + command: null, settings: { ...cfgFile, evmPrivateEncrypted: cfgFile.evmPrivateEncrypted ?? process.env.EVM_PRIVATE_ENCRYPTED, diff --git a/src/web/main.tsx b/src/web/main.tsx index f491f9cf..a0cb2585 100644 --- a/src/web/main.tsx +++ b/src/web/main.tsx @@ -21,7 +21,7 @@ function Form() { useCache, }; try { - const result = await fetch("http://localhost:3000", { + const result = await fetch("http://localhost:4000", { method: "POST", headers: { "Content-Type": "application/json", diff --git a/tests/process.issue.test.ts b/tests/process.issue.test.ts index 8180085b..189ae7f6 100644 --- a/tests/process.issue.test.ts +++ b/tests/process.issue.test.ts @@ -300,4 +300,67 @@ describe("Modules tests", () => { const postBody = await githubCommentModule.getBodyContent(githubCommentAltResults as unknown as Result); expect(postBody).not.toContain("whilefoo"); }); + + describe("Reward limits", () => { + it("Should return infinity if disabled", () => { + const processor = new Processor({ + ...ctx, + config: { + ...ctx.config, + incentives: { + ...ctx.config.incentives, + limitRewards: false, + }, + }, + }); + const result = processor._getRewardsLimit(); + expect(result).toBe(Infinity); + }); + }); + + it("Should return the max corresponding to the label of the issue if enabled", async () => { + const processor = new Processor({ + ...ctx, + config: { + ...ctx.config, + incentives: { + ...ctx.config.incentives, + limitRewards: true, + }, + }, + }); + processor["_transformers"] = [ + new UserExtractorModule(ctx), + new DataPurgeModule(ctx), + new FormattingEvaluatorModule(ctx), + new ContentEvaluatorModule(ctx), + ]; + processor["_result"] = { + user1: { + total: 999, + task: { + multiplier: 0.5, + reward: 18.5, + }, + userId: 0, + }, + user2: { + total: 11111111, + userId: 1, + }, + }; + const result = processor._getRewardsLimit(); + expect(result).toBe(9.25); + const total = await processor.run(activity); + expect(total).toMatchObject({ + user1: { total: 9.25, task: { multiplier: 0.5, reward: 18.5 }, userId: 0 }, + user2: { total: 0, userId: 1 }, + "0x4007": { + total: 9.25, + }, + whilefoo: { + total: 9.25, + }, + }); + }); });