Skip to content

Commit

Permalink
logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rajranjan0608 committed Dec 28, 2023
1 parent 892a4c6 commit fa6eb1c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
16 changes: 10 additions & 6 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ router.post('/sendToken', captcha.middleware, async (req: any, res: any) => {
!pipelineValidity.isValid && couponCheckEnabled && await checkCouponPipeline(couponService, pipelineValidity, faucetConfigId, coupon)

// don't check mainnet balance, if coupon is provided
!pipelineValidity.isValid && !coupon && mainnetCheckEnabledRPC && await checkMainnetBalancePipeline(pipelineValidity, faucetConfigId, mainnetCheckEnabledRPC, address)
!pipelineValidity.isValid && !coupon && mainnetCheckEnabledRPC && await checkMainnetBalancePipeline(pipelineValidity, mainnetCheckEnabledRPC, address)

if (
(mainnetCheckEnabledRPC || couponCheckEnabled) &&
Expand All @@ -163,11 +163,15 @@ router.post('/sendToken', captcha.middleware, async (req: any, res: any) => {

// logging requests (if enabled)
DEBUG && console.log(JSON.stringify({
"type": "NewFaucetRequest",
"address": address,
"chain": chain,
"erc20": erc20,
"ip": req.headers["cf-connecting-ip"] || req.ip
type: "NewFaucetRequest",
faucetConfigId,
address,
chain,
erc20,
checkPassedType: pipelineValidity.checkPassedType,
dripAmount: pipelineValidity.dripAmount,
mainnetBalance: pipelineValidity.mainnetBalance,
ip: req.headers["cf-connecting-ip"] || req.ip
}))

// send request
Expand Down
16 changes: 5 additions & 11 deletions utils/mainnetBalanceCheck.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from "axios"

export async function checkMainnetBalance(faucetConfigId: string, rpc: string, address: string, threshold = 0): Promise<boolean> {
export async function checkMainnetBalance(rpc: string, address: string, threshold = 0): Promise<{isValid: boolean, balance: number}> {
const response = {isValid: false, balance: 0}
try {
const response = await axios.post<any, any>(rpc, {
jsonrpc: "2.0",
Expand All @@ -10,18 +11,11 @@ export async function checkMainnetBalance(faucetConfigId: string, rpc: string, a
})
const balance = parseInt(response.data.result)
if (balance > threshold) {
console.log(JSON.stringify({
type: "FaucetMainnetBalanceCheckSuccess",
chain: faucetConfigId,
address,
balance,
rpc,
}))
return true
return {isValid: true, balance}
}
} catch(err) {
console.error('ERROR: checkMainnetBalance', err)
return false
return response
}
return false
return response
}
6 changes: 4 additions & 2 deletions utils/pipelineChecks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type PipelineCheckValidity = {
checkPassedType?: PIPELINE_CHECKS,
errorMessage?: string,
dripAmount: number,
mainnetBalance?: number,
}

export function pipelineFailureMessage(mainnetBalanceCheckEnabled: boolean | string, couponCheckEnabled: boolean): string {
Expand Down Expand Up @@ -47,11 +48,12 @@ export async function checkCouponPipeline(
}
}

export async function checkMainnetBalancePipeline(pipelineCheckValidity: PipelineCheckValidity, faucetConfigId: string, rpc: string, address: string) {
const isValid = await checkMainnetBalance(faucetConfigId, rpc, address)
export async function checkMainnetBalancePipeline(pipelineCheckValidity: PipelineCheckValidity, rpc: string, address: string) {
const {isValid, balance} = await checkMainnetBalance(rpc, address)
if (isValid) {
pipelineCheckValidity.isValid = true
pipelineCheckValidity.checkPassedType = PIPELINE_CHECKS.MAINNET_BALANCE
pipelineCheckValidity.mainnetBalance = balance
} else {
pipelineCheckValidity.errorMessage = "Mainnet balance check failed! "
}
Expand Down

0 comments on commit fa6eb1c

Please sign in to comment.