Skip to content

Commit

Permalink
implement the slack stats and low balance warning
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-aurele-besner committed May 21, 2024
1 parent bb189fb commit 6d30402
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion discord-bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,50 @@ import { BigNumber, utils } from 'ethers'
import nacl from 'tweetnacl'
import {
CommandNames,
buildSlackStatsMessage,
config,
createStats,
faucetBalanceLowSlackMessage,
findRequest,
findStats,
formatSeconds,
log,
queries,
requestTokens,
saveRequest,
sendSlackMessage,
updateStats,
} from './utils'

const incrementFaucetRequestsCount = async (address: string, requestDate: string) => {
const stats = await findStats(requestDate)
log('stats', stats)
if (!stats || stats === null || stats.length === 0) {
await createStats(address, requestDate)
const slackMessageId = await sendSlackMessage(
'Current week evm-faucet requests',
buildSlackStatsMessage('update', 1, 1),
)
await createStats(address, slackMessageId, requestDate)
} else {
const statsFound = stats[0].data
const isExistingAddresses = statsFound.addresses.find((a: string) => a === address)
await sendSlackMessage(
'Current week evm-faucet requests',
buildSlackStatsMessage(
'update',
statsFound.requests + 1,
isExistingAddresses ? statsFound.uniqueAddresses : statsFound.uniqueAddresses + 1,
statsFound.requestsByType,
),
statsFound.slackMessageId,
)
await updateStats(stats[0].ref, statsFound, address)
}
}

const sendLowBalanceWarning = async (faucetBalance: BigNumber) =>
await faucetBalanceLowSlackMessage(utils.formatEther(faucetBalance))

const tagUser = (userId: string) => '<@' + userId + '>'

const buildDiscordInteractionResponse = (content: string, originalInteraction: any) =>
Expand Down Expand Up @@ -137,6 +157,10 @@ export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayPr
const withdrawalAmount = await queries.withdrawalAmount()
const faucetBalance = await queries.verifyFaucetBalance()

// if faucetBalance is lower than withdrawalAmount * SLACK_BALANCE_NOTIFICATION_THRESHOLD, then send a slack message
if (faucetBalance.lt(withdrawalAmount.mul(BigNumber.from(config.SLACK_BALANCE_NOTIFICATION_THRESHOLD))))
await sendLowBalanceWarning(faucetBalance)

// if faucetBalance is lower than withdrawalAmount, then the faucet needs to be refilled
if (faucetBalance.lt(withdrawalAmount)) {
await postDiscordMessage(
Expand Down

0 comments on commit 6d30402

Please sign in to comment.