diff --git a/server.ts b/server.ts index 127334f..f128a44 100644 --- a/server.ts +++ b/server.ts @@ -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) && @@ -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 diff --git a/utils/mainnetBalanceCheck.ts b/utils/mainnetBalanceCheck.ts index 22e347f..643edb9 100644 --- a/utils/mainnetBalanceCheck.ts +++ b/utils/mainnetBalanceCheck.ts @@ -1,6 +1,7 @@ import axios from "axios" -export async function checkMainnetBalance(faucetConfigId: string, rpc: string, address: string, threshold = 0): Promise { +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(rpc, { jsonrpc: "2.0", @@ -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 } diff --git a/utils/pipelineChecks.ts b/utils/pipelineChecks.ts index 32ed2a4..f047f77 100644 --- a/utils/pipelineChecks.ts +++ b/utils/pipelineChecks.ts @@ -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 { @@ -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! " }