Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
feat: selectivly enable incentives
Browse files Browse the repository at this point in the history
  • Loading branch information
me505 committed Sep 19, 2023
1 parent dadcde8 commit d3e8230
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/handlers/payout/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
generatePermit2Signature,
getAllIssueComments,
getAllPullRequestReviews,
getIncentivizedUsers,
getIssueDescription,
getTokenSymbol,
parseComments,
Expand Down Expand Up @@ -70,13 +71,13 @@ export const incentivizeComments = async () => {
logger.info("incentivizeComments: skipping payment permit generation because `assignee` is `undefined`.");
return;
}

const users = await getIncentivizedUsers(issue.number);
const issueComments = await getAllIssueComments(issue.number, "full");
logger.info(`Getting the issue comments done. comments: ${JSON.stringify(issueComments)}`);
const issueCommentsByUser: Record<string, { id: string; comments: string[] }> = {};
for (const issueComment of issueComments) {
const user = issueComment.user;
if (user.type == UserType.Bot || user.login == assignee) continue;
if (user.type == UserType.Bot) continue;
const commands = commentParser(issueComment.body);
if (commands.length > 0) {
logger.info(`Skipping to parse the comment because it contains commands. comment: ${JSON.stringify(issueComment)}`);
Expand All @@ -103,26 +104,30 @@ export const incentivizeComments = async () => {
const fallbackReward: Record<string, Decimal> = {};
let comment = `#### Conversation Rewards\n`;
for (const user of Object.keys(issueCommentsByUser)) {
const commentsByUser = issueCommentsByUser[user];
const commentsByNode = await parseComments(commentsByUser.comments, ItemsToExclude);
const rewardValue = calculateRewardValue(commentsByNode, incentives);
if (rewardValue.equals(0)) {
logger.info(`Skipping to generate a permit url because the reward value is 0. user: ${user}`);
continue;
}
logger.debug(`Comment parsed for the user: ${user}. comments: ${JSON.stringify(commentsByNode)}, sum: ${rewardValue}`);
const account = await getWalletAddress(user);
const amountInETH = rewardValue.mul(baseMultiplier);
if (amountInETH.gt(paymentPermitMaxPrice)) {
logger.info(`Skipping comment reward for user ${user} because reward is higher than payment permit max price`);
continue;
}
if (account) {
const { payoutUrl } = await generatePermit2Signature(account, amountInETH, issue.node_id, commentsByUser.id, "ISSUE_COMMENTER");
comment = `${comment}### [ **${user}: [ CLAIM ${amountInETH} ${tokenSymbol.toUpperCase()} ]** ](${payoutUrl})\n`;
reward[user] = payoutUrl;
if (users.users.includes(user) || users.enable) {
const commentsByUser = issueCommentsByUser[user];
const commentsByNode = await parseComments(commentsByUser.comments, ItemsToExclude);
const rewardValue = calculateRewardValue(commentsByNode, incentives);
if (rewardValue.equals(0)) {
logger.info(`Skipping to generate a permit url because the reward value is 0. user: ${user}`);
continue;
}
logger.debug(`Comment parsed for the user: ${user}. comments: ${JSON.stringify(commentsByNode)}, sum: ${rewardValue}`);
const account = await getWalletAddress(user);
const amountInETH = rewardValue.mul(baseMultiplier);
if (amountInETH.gt(paymentPermitMaxPrice)) {
logger.info(`Skipping comment reward for user ${user} because reward is higher than payment permit max price`);
continue;
}
if (account) {
const { payoutUrl } = await generatePermit2Signature(account, amountInETH, issue.node_id, commentsByUser.id, "ISSUE_COMMENTER");
comment = `${comment}### [ **${user}: [ CLAIM ${amountInETH} ${tokenSymbol.toUpperCase()} ]** ](${payoutUrl})\n`;
reward[user] = payoutUrl;
} else {
fallbackReward[user] = amountInETH;
}
} else {
fallbackReward[user] = amountInETH;
continue;
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/helpers/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ export const listAllIssuesForRepo = async (state: "open" | "closed" | "all" = "o
return issuesArr;
};

export const getIncentivizedUsers = async (issue_number: number) => {
const comments = await getAllIssueComments(issue_number);
const incentiveComment = comments.filter((comment) => comment.body.startsWith("/comment-incentives"));
const parts = incentiveComment[0].body.split("");
parts.shift();
let users: { enable: boolean; users: string[] } = { enable: false, users: [] };

Check failure on line 103 in src/helpers/issue.ts

View workflow job for this annotation

GitHub Actions / build

'users' is never reassigned. Use 'const' instead
for (const part of parts) {
if (part.startsWith("@")) {
users.users.push(part.substring(1));
} else if (part == "true") {
users.enable = part == "true";
}
}
return users;
};

export const addCommentToIssue = async (msg: string, issue_number: number) => {
const context = getBotContext();
const logger = getLogger();
Expand Down

0 comments on commit d3e8230

Please sign in to comment.