Skip to content

Commit

Permalink
fix(reward-limits): adjust rewards limit calculation
Browse files Browse the repository at this point in the history
Refactor reward limits logic and fix endpoint URLs for local server.
  • Loading branch information
gentlementlegen committed Nov 26, 2024
1 parent 0ff3824 commit 171f523
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 20 deletions.
36 changes: 20 additions & 16 deletions src/parser/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IssueActivity>) {
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;
Expand All @@ -70,14 +72,16 @@ export class Processor {
return result;
}

_sumRewards(obj: Record<string, unknown>) {
_sumRewards(obj: Record<string, unknown>, 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<string, unknown>));
totalReward = totalReward.add(
Math.min(this._sumRewards(value as Record<string, unknown>, taskRewardLimit), taskRewardLimit)
);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/web/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ const baseApp = createPlugin<PluginSettings, EnvConfig, SupportedEvents>(

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(
Expand Down Expand Up @@ -108,7 +112,7 @@ app.get("/openai/*", () => {
return Response.json("OpenAI GET");
});

const port = 3000;
const port = 4000;

export default {
fetch: app.fetch,
Expand Down
3 changes: 2 additions & 1 deletion src/web/api/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
}

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/web/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
63 changes: 63 additions & 0 deletions tests/process.issue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
});
});

0 comments on commit 171f523

Please sign in to comment.