From 9de41173cf350bee653082b29f5a37cf8c98eace Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Thu, 29 Aug 2024 18:04:38 +0100 Subject: [PATCH] chore: summary comment of txs --- src/handlers/gas-subsidize.ts | 9 +++++++-- src/plugin.ts | 19 ++++++++++++++++++- src/utils/logger.ts | 4 ++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/handlers/gas-subsidize.ts b/src/handlers/gas-subsidize.ts index c803eed..e209765 100644 --- a/src/handlers/gas-subsidize.ts +++ b/src/handlers/gas-subsidize.ts @@ -1,3 +1,4 @@ +import { ethers } from "ethers"; import { Context } from "../types"; import { throwError } from "../utils/logger"; import { faucet } from "./faucet"; @@ -24,7 +25,7 @@ export async function gasSubsidize(context: Context) { users.push(issue.user); - const txs = []; + const txs: Record = {}; for (const user of users) { if (!user?.login) continue; @@ -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; diff --git a/src/plugin.ts b/src/plugin.ts index c918172..667d78f 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -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}`); diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 2204458..e6c6e9a 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -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 @@ -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);