Skip to content

Commit

Permalink
chore: summary comment of txs
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Aug 29, 2024
1 parent 8fe261d commit 9de4117
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/handlers/gas-subsidize.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ethers } from "ethers";
import { Context } from "../types";
import { throwError } from "../utils/logger";
import { faucet } from "./faucet";
Expand All @@ -24,7 +25,7 @@ export async function gasSubsidize(context: Context) {

users.push(issue.user);

const txs = [];
const txs: Record<string, ethers.providers.TransactionReceipt[]> = {};

for (const user of users) {
if (!user?.login) continue;
Expand All @@ -38,7 +39,11 @@ export async function gasSubsidize(context: Context) {
continue;
}

txs.push(await faucet(context, { recipient: userWallet, networkId, amount: gasSubsidyAmount }));
txs[user.login] ??= [];
const tx = await faucet(context, { recipient: userWallet, networkId, amount: gasSubsidyAmount });
if (tx) {
txs[user.login].push(tx);
}
}

return txs;
Expand Down
19 changes: 18 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,29 @@ import { gasSubsidize } from "./handlers/gas-subsidize";
import { isIssueClosedEvent } from "./types/typeguards";
import { createAdapters } from "./adapters";
import { createClient } from "@supabase/supabase-js";
import { logAndComment } from "./utils/logger";

export async function runPlugin(context: Context) {
const { logger, eventName } = context;

if (isIssueClosedEvent(context)) {
return await gasSubsidize(context);
const txs = await gasSubsidize(context);

if (!txs) {
logger.info("No gas subsidy transactions were sent.");
return;
}

const comment = `
${Object.entries(txs).forEach(([user, txs]) => {
let cmt = `Gas subsidy sent to ${user}:\n`;
txs.forEach((tx) => {
cmt += `- [\`${tx.transactionHash.slice(0, 8)}\`](https://blockscan.com/tx/${tx.transactionHash})\n`;
});
return cmt + "\n";
})}`;

await logAndComment(context, "info", comment, { txs });
}

logger.info(`Ignoring event ${eventName}`);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LogLevel, Logs } from "@ubiquity-dao/ubiquibot-logger";
import { LogLevel, Logs, Metadata } from "@ubiquity-dao/ubiquibot-logger";
import { Context } from "../types";

// typed as never so that ts registers that it cannot return undefined
Expand All @@ -8,7 +8,7 @@ export function throwError(err: string, rest?: object): never {
throw new Error(`${error?.logMessage.diff}\n${JSON.stringify(error?.metadata, null, 2)}`);
}

export async function logAndComment(context: Context, type: LogLevel, message: string, metadata?: object) {
export async function logAndComment(context: Context, type: LogLevel, message: string, metadata?: Metadata) {
const { logger, octokit, payload } = context;
const log = logger[type](message, metadata);

Expand Down

0 comments on commit 9de4117

Please sign in to comment.