This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
action.ts
204 lines (181 loc) · 8.28 KB
/
action.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { BigNumber, ethers } from "ethers";
import { getPenalty, getWalletAddress, getWalletMultiplier, removePenalty } from "../../adapters/supabase";
import { getBotConfig, getBotContext, getLogger } from "../../bindings";
import {
addLabelToIssue,
checkUserPermissionForRepoAndOrg,
clearAllPriceLabelsOnIssue,
deleteLabel,
generatePermit2Signature,
getAllIssueComments,
getTokenSymbol,
savePermitToDB,
wasIssueReopened,
getAllIssueAssignEvents,
} from "../../helpers";
import { UserType, Payload, StateReason } from "../../types";
import { shortenEthAddress } from "../../utils";
import { bountyInfo } from "../wildcard";
import Decimal from "decimal.js";
import { GLOBAL_STRINGS } from "../../configs";
import { isParentIssue } from "../pricing";
export const handleIssueClosed = async () => {
const context = getBotContext();
const {
payout: { paymentToken, rpc, permitBaseUrl, networkId, privateKey },
mode: { paymentPermitMaxPrice },
accessControl,
} = getBotConfig();
const logger = getLogger();
const payload = context.payload as Payload;
const issue = payload.issue;
const { repository, organization } = payload;
const id = organization?.id || repository?.id; // repository?.id as fallback
if (!issue) return;
if (accessControl.organization) {
const userHasPermission = await checkUserPermissionForRepoAndOrg(payload.sender.login, context);
if (!userHasPermission) return "Permit generation skipped because this issue has been closed by an external contributor.";
}
const comments = await getAllIssueComments(issue.number);
const wasReopened = await wasIssueReopened(issue.number);
const claimUrlRegex = new RegExp(`\\((${permitBaseUrl}\\?claim=\\S+)\\)`);
const permitCommentIdx = comments.findIndex((e) => e.user.type === UserType.Bot && e.body.match(claimUrlRegex));
if (wasReopened && permitCommentIdx !== -1) {
const permitComment = comments[permitCommentIdx];
const permitUrl = permitComment.body.match(claimUrlRegex);
if (!permitUrl || permitUrl.length < 2) {
logger.error(`Permit URL not found`);
return;
}
const url = new URL(permitUrl[1]);
const claimBase64 = url.searchParams.get("claim");
if (!claimBase64) {
logger.error(`Permit claim search parameter not found`);
return;
}
let networkId = url.searchParams.get("network");
if (!networkId) {
networkId = "1";
}
let claim;
try {
claim = JSON.parse(Buffer.from(claimBase64, "base64").toString("utf-8"));
} catch (err: unknown) {
logger.error(`${err}`);
return;
}
const amount = BigNumber.from(claim.permit.permitted.amount);
const tokenAddress = claim.permit.permitted.token;
// extract assignee
const events = await getAllIssueAssignEvents(issue.number);
if (events.length === 0) {
logger.error(`No assignment found`);
return;
}
const assignee = events[0].assignee.login;
try {
await removePenalty(assignee, payload.repository.full_name, tokenAddress, networkId, amount);
} catch (err) {
logger.error(`Failed to remove penalty: ${err}`);
return;
}
logger.info(`Penalty removed`);
return;
}
if (privateKey == "") {
logger.info("Permit generation skipped because wallet private key is not set");
return "Permit generation skipped because wallet private key is not set";
}
if (issue.state_reason !== StateReason.COMPLETED) {
logger.info("Permit generation skipped because the issue was not closed as completed");
return "Permit generation skipped because the issue was not closed as completed";
}
logger.info(`Checking if the issue is a parent issue.`);
if (issue.body && isParentIssue(issue.body)) {
logger.error("Permit generation skipped since the issue is identified as parent issue.");
await clearAllPriceLabelsOnIssue();
return "Permit generation skipped since the issue is identified as parent issue.";
}
logger.info(`Handling issues.closed event, issue: ${issue.number}`);
for (const botComment of comments.filter((cmt) => cmt.user.type === UserType.Bot).reverse()) {
const botCommentBody = botComment.body;
if (botCommentBody.includes(GLOBAL_STRINGS.autopayComment)) {
const pattern = /\*\*(\w+)\*\*/;
const res = botCommentBody.match(pattern);
if (res) {
if (res[1] === "false") {
logger.info(`Skipping to generate permit2 url, reason: autoPayMode for this issue: false`);
return `Permit generation skipped since automatic payment for this issue is disabled.`;
}
break;
}
}
}
if (paymentPermitMaxPrice == 0 || !paymentPermitMaxPrice) {
logger.info(`Skipping to generate permit2 url, reason: { paymentPermitMaxPrice: ${paymentPermitMaxPrice}}`);
return `Permit generation skipped since paymentPermitMaxPrice is 0`;
}
const issueDetailed = bountyInfo(issue);
if (!issueDetailed.isBounty) {
logger.info(`Skipping... its not a bounty`);
return `Permit generation skipped since this issue didn't qualify as bounty`;
}
const assignees = issue?.assignees ?? [];
const assignee = assignees.length > 0 ? assignees[0] : undefined;
if (!assignee) {
logger.info("Skipping to proceed the payment because `assignee` is undefined");
return `Permit generation skipped since assignee is undefined`;
}
if (!issueDetailed.priceLabel) {
logger.info("Skipping to proceed the payment because price not set");
return `Permit generation skipped since price label is not set`;
}
const recipient = await getWalletAddress(assignee.login);
if (!recipient || recipient?.trim() === "") {
logger.info(`Recipient address is missing`);
return;
}
const { value: multiplier } = await getWalletMultiplier(assignee.login, id?.toString());
if (multiplier === 0) {
const errMsg = "Refusing to generate the payment permit because " + `@${assignee.login}` + "'s payment `multiplier` is `0`";
logger.info(errMsg);
return errMsg;
}
let priceInEth = new Decimal(issueDetailed.priceLabel.substring(7, issueDetailed.priceLabel.length - 4)).mul(multiplier);
if (priceInEth.gt(paymentPermitMaxPrice)) {
logger.info("Skipping to proceed the payment because bounty payout is higher than paymentPermitMaxPrice");
return `Permit generation skipped since issue's bounty is higher than ${paymentPermitMaxPrice}`;
}
// if bounty hunter has any penalty then deduct it from the bounty
const penaltyAmount = await getPenalty(assignee.login, payload.repository.full_name, paymentToken, networkId.toString());
if (penaltyAmount.gt(0)) {
logger.info(`Deducting penalty from bounty`);
const bountyAmount = ethers.utils.parseUnits(priceInEth.toString(), 18);
const bountyAmountAfterPenalty = bountyAmount.sub(penaltyAmount);
if (bountyAmountAfterPenalty.lte(0)) {
await removePenalty(assignee.login, payload.repository.full_name, paymentToken, networkId.toString(), bountyAmount);
const msg = `Permit generation skipped because bounty amount after penalty is 0`;
logger.info(msg);
return msg;
}
priceInEth = new Decimal(ethers.utils.formatUnits(bountyAmountAfterPenalty, 18));
}
const { txData, payoutUrl } = await generatePermit2Signature(recipient, priceInEth, issue.node_id, assignee.node_id, "ISSUE_ASSIGNEE");
const tokenSymbol = await getTokenSymbol(paymentToken, rpc);
const shortenRecipient = shortenEthAddress(recipient, `[ CLAIM ${priceInEth} ${tokenSymbol.toUpperCase()} ]`.length);
logger.info(`Posting a payout url to the issue, url: ${payoutUrl}`);
const comment =
`#### Task Assignee Reward\n### [ **[ CLAIM ${priceInEth} ${tokenSymbol.toUpperCase()} ]** ](${payoutUrl})\n` + "```" + shortenRecipient + "```";
const permitComments = comments.filter((content) => content.body.includes("https://pay.ubq.fi?claim=") && content.user.type == UserType.Bot);
if (permitComments.length > 0) {
logger.info(`Skip to generate a permit url because it has been already posted`);
return `Permit generation skipped because it was already posted to this issue.`;
}
await deleteLabel(issueDetailed.priceLabel);
await addLabelToIssue("Permitted");
await savePermitToDB(assignee.id, txData);
if (penaltyAmount.gt(0)) {
await removePenalty(assignee.login, payload.repository.full_name, paymentToken, networkId.toString(), penaltyAmount);
}
return comment;
};