Skip to content

Commit

Permalink
log unique addresses and use requestTokens directly
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-aurele-besner committed Sep 13, 2023
1 parent d3a5cd9 commit aae3661
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
15 changes: 9 additions & 6 deletions discord-bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Client, IntentsBitField } from 'discord.js'
import {
config,
CommandNames,
transactions,
requestTokens,
queries,
log,
sendSlackMessage,
Expand All @@ -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,
)
}
Expand Down Expand Up @@ -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 ' +
Expand All @@ -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)
Expand Down
10 changes: 3 additions & 7 deletions discord-bot/src/utils/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,14 @@ const nextAccessTime = async (address: string): Promise<BigNumber> => {
return await contract.nextAccessTime(address)
}

const requestTokens = async (address: string): Promise<Transaction> => {
const contract = await getFaucetWithSigner()
return await contract.requestTokens(address)
}

const verifyFaucetBalance = async (): Promise<BigNumber> => {
const provider = await getProvider()
return await provider.getBalance(config.CONTRACT_ADDRESS)
}

export const transactions = {
requestTokens,
export const requestTokens = async (address: string): Promise<Transaction> => {
const contract = await getFaucetWithSigner()
return await contract.requestTokens(address)
}

export const queries = {
Expand Down
12 changes: 10 additions & 2 deletions discord-bot/src/utils/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit aae3661

Please sign in to comment.