diff --git a/discord-bot/src/index.ts b/discord-bot/src/index.ts index a3a2bd7..6d17ffd 100644 --- a/discord-bot/src/index.ts +++ b/discord-bot/src/index.ts @@ -2,7 +2,7 @@ import { Client, IntentsBitField } from 'discord.js' import { config, CommandNames, - transactions, + requestTokens, queries, log, sendSlackMessage, @@ -21,24 +21,27 @@ const client = new Client({ }) let faucetRequestsCount = 0 +const requesterAddresses: string[] = [] let slackMessageId: string | undefined = undefined let lastLowWarningMessage: Date | undefined = undefined -const incrementFaucetRequestsCount = async () => { +const incrementFaucetRequestsCount = async (address: string) => { faucetRequestsCount++ + if (!requesterAddresses.find((a) => a === address)) requesterAddresses.push(address) // If it's Monday and there was requests, send a new slack message with the stats from last week and reset the counter // If it's not Monday, update the slack message with the new stats if (new Date().getDay() === 1 && faucetRequestsCount > 0) { slackMessageId = undefined slackMessageId = await sendSlackMessage( 'Last week evm-faucet requests', - buildSlackStatsMessage('weekRecap', faucetRequestsCount), + buildSlackStatsMessage('weekRecap', faucetRequestsCount, requesterAddresses.length), ) faucetRequestsCount = 0 + requesterAddresses.length = 0 } else { slackMessageId = await sendSlackMessage( 'Last week evm-faucet requests', - buildSlackStatsMessage('update', faucetRequestsCount), + buildSlackStatsMessage('update', faucetRequestsCount, requesterAddresses.length), slackMessageId, ) } @@ -100,7 +103,7 @@ client.on('interactionCreate', async (interaction) => { log('withdrawalAmount', formattedAmount) // request tokens - const tx = await transactions.requestTokens(addressOption.value) + const tx = await requestTokens(addressOption.value) if (tx && tx.hash) { interaction.followUp( 'We just sent you ' + @@ -112,7 +115,7 @@ client.on('interactionCreate', async (interaction) => { '/tx/' + tx.hash, ) - await incrementFaucetRequestsCount() + await incrementFaucetRequestsCount(addressOption.value) } else interaction.followUp('Transaction failed') } else { const timeToWait = nextAccessTime.sub(currentTime) diff --git a/discord-bot/src/utils/contracts.ts b/discord-bot/src/utils/contracts.ts index 6b2f56f..0a4d8d4 100644 --- a/discord-bot/src/utils/contracts.ts +++ b/discord-bot/src/utils/contracts.ts @@ -34,18 +34,14 @@ const nextAccessTime = async (address: string): Promise => { return await contract.nextAccessTime(address) } -const requestTokens = async (address: string): Promise => { - const contract = await getFaucetWithSigner() - return await contract.requestTokens(address) -} - const verifyFaucetBalance = async (): Promise => { const provider = await getProvider() return await provider.getBalance(config.CONTRACT_ADDRESS) } -export const transactions = { - requestTokens, +export const requestTokens = async (address: string): Promise => { + const contract = await getFaucetWithSigner() + return await contract.requestTokens(address) } export const queries = { diff --git a/discord-bot/src/utils/slack.ts b/discord-bot/src/utils/slack.ts index 8ee1682..8775e6b 100644 --- a/discord-bot/src/utils/slack.ts +++ b/discord-bot/src/utils/slack.ts @@ -34,17 +34,25 @@ export const sendSlackMessage = async (message: string, blocks: TBlocks, message } } -export const buildSlackStatsMessage = (type: 'weekRecap' | 'update', requestCount: number): TBlocks => { +export const buildSlackStatsMessage = ( + type: 'weekRecap' | 'update', + requestCount: number, + uniqueAddresses: number, +): TBlocks => { const blocks = [slackBuilder.buildSimpleSlackHeaderMsg('Faucet stats')] switch (type) { case 'weekRecap': - blocks.push(slackBuilder.buildSimpleSectionMsg(`Last week total requests: ${requestCount} :subspace-hype:`)) + blocks.push( + slackBuilder.buildSimpleSectionMsg(`Last week total requests: ${requestCount} :subspace-hype:`), + slackBuilder.buildSimpleSectionMsg(`Unique addresses: ${uniqueAddresses} :subheart-white:`), + ) return blocks case 'update': blocks.push( slackBuilder.buildSimpleSectionMsg( `Current total requests: ${requestCount} ${requestCount > 100 ? ':subspace-hype:' : ':subheart-black:'}`, ), + slackBuilder.buildSimpleSectionMsg(`Unique addresses: ${uniqueAddresses} :subheart-white:`), ) return blocks }