From abb21a623ec8f2de4264d15c0c75f662f5ccf234 Mon Sep 17 00:00:00 2001 From: Raj Ranjan Date: Fri, 29 Dec 2023 19:17:06 +0530 Subject: [PATCH 1/2] Simplified maninet balance check RPC (#133) * prod configs --- config.json | 4 +++- server.ts | 14 ++++++++------ types.ts | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/config.json b/config.json index 83e1f71..c932e6f 100644 --- a/config.json +++ b/config.json @@ -15,6 +15,8 @@ "IS_ENABLED": true, "MAX_LIMIT_CAP": 5000 }, + "MAINNET_BALANCE_CHECK_RPC": "https://api.avax.network/ext/C/rpc", + "MAINNET_BALANCE_CHECK_CHAIN_ID": 43114, "evmchains": [ { "ID": "C", @@ -30,7 +32,7 @@ "DECIMALS": 18, "RECALIBRATE": 30, "COUPON_REQUIRED": true, - "MAINNET_BALANCE_CHECK_RPC": "https://api.avax.network/ext/C/rpc", + "MAINNET_BALANCE_CHECK_ENABLED": true, "RATELIMIT": { "MAX_LIMIT": 1, "WINDOW_SIZE": 1440 diff --git a/server.ts b/server.ts index f128a44..a8e7ba6 100644 --- a/server.ts +++ b/server.ts @@ -19,6 +19,8 @@ import { GLOBAL_RL, NATIVE_CLIENT, DEBUG, + MAINNET_BALANCE_CHECK_RPC, + MAINNET_BALANCE_CHECK_CHAIN_ID, } from './config.json' import { CouponService } from './CouponService/couponService' import { @@ -78,7 +80,7 @@ const getChainByID = (chains: ChainType[], id: string): ChainType | undefined => return reply } -const separateConfigFields = ['COUPON_REQUIRED', 'MAINNET_BALANCE_CHECK_RPC'] +const separateConfigFields = ['COUPON_REQUIRED', 'MAINNET_BALANCE_CHECK_ENABLED'] // Populates the missing config keys of the child using the parent's config const populateConfig = (child: any, parent: any): any => { @@ -143,21 +145,21 @@ router.post('/sendToken', captcha.middleware, async (req: any, res: any) => { * 3. If no pipeline check is required for a token, then directly process the request * 4. Currently, we have 2 pipeline checks: Coupon Check & Mainnet Balance Check */ - const mainnetCheckEnabledRPC = (erc20Instance ? erc20Instance.config.MAINNET_BALANCE_CHECK_RPC : evm.config.MAINNET_BALANCE_CHECK_RPC) ?? false + const mainnetCheckEnabled = (erc20Instance ? erc20Instance.config.MAINNET_BALANCE_CHECK_ENABLED : evm.config.MAINNET_BALANCE_CHECK_ENABLED) ?? false const couponCheckEnabled = couponConfig.IS_ENABLED && ((erc20Instance ? erc20Instance.config.COUPON_REQUIRED : evm.config.COUPON_REQUIRED) ?? false) let pipelineValidity: PipelineCheckValidity = {isValid: false, dripAmount} !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, mainnetCheckEnabledRPC, address) + !pipelineValidity.isValid && !coupon && mainnetCheckEnabled && await checkMainnetBalancePipeline(pipelineValidity, MAINNET_BALANCE_CHECK_RPC, address) if ( - (mainnetCheckEnabledRPC || couponCheckEnabled) && + (mainnetCheckEnabled || couponCheckEnabled) && !pipelineValidity.isValid ) { // failed - res.status(400).send({message: pipelineValidity.errorMessage + pipelineFailureMessage(mainnetCheckEnabledRPC, couponCheckEnabled)}) + res.status(400).send({message: pipelineValidity.errorMessage + pipelineFailureMessage(MAINNET_BALANCE_CHECK_RPC, couponCheckEnabled)}) return } @@ -189,7 +191,7 @@ router.post('/sendToken', captcha.middleware, async (req: any, res: any) => { // GET request for fetching all the chain and token configurations router.get('/getChainConfigs', (req: any, res: any) => { const configs: any = [...evmchains, ...erc20tokens] - res.send({ configs }) + res.send({ configs, MAINNET_BALANCE_CHECK_RPC, MAINNET_BALANCE_CHECK_CHAIN_ID }) }) // GET request for fetching faucet address for the specified chain diff --git a/types.ts b/types.ts index 71bba96..f5cd3f2 100644 --- a/types.ts +++ b/types.ts @@ -37,7 +37,7 @@ export type ChainType = { DRIP_AMOUNT: number, RECALIBRATE?: number, COUPON_REQUIRED?: boolean, - MAINNET_BALANCE_CHECK_RPC?: string, + MAINNET_BALANCE_CHECK_ENABLED?: boolean, RATELIMIT: { WINDOW_SIZE: number, MAX_LIMIT: number @@ -65,7 +65,7 @@ export type ERC20Type = { MAX_PRIORITY_FEE?: string, MAX_FEE?: string, COUPON_REQUIRED?: boolean, - MAINNET_BALANCE_CHECK_RPC?: string, + MAINNET_BALANCE_CHECK_ENABLED?: boolean, } export type CouponValidity = { From ddc799dd1c71b6b4e280196838ef711b09e25db8 Mon Sep 17 00:00:00 2001 From: Raj Ranjan Date: Mon, 8 Jan 2024 21:28:40 +0530 Subject: [PATCH 2/2] add date to logs (#134) * add date to logs * nits --- CouponService/couponService.ts | 6 +++++- middlewares/verifyCaptcha.ts | 12 ++++++++++-- server.ts | 1 + utils/mainnetBalanceCheck.ts | 6 +++++- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CouponService/couponService.ts b/CouponService/couponService.ts index 984f87e..13ab96d 100644 --- a/CouponService/couponService.ts +++ b/CouponService/couponService.ts @@ -87,7 +87,11 @@ export class CouponService { this.coupons.set(coupon.id, coupon) } } else { - console.log("fetched invalid coupon data:", item) + console.log(JSON.stringify({ + date: new Date(), + type: 'InvalidCouponFetched', + item + })) } }) diff --git a/middlewares/verifyCaptcha.ts b/middlewares/verifyCaptcha.ts index 91d0c82..0f24693 100644 --- a/middlewares/verifyCaptcha.ts +++ b/middlewares/verifyCaptcha.ts @@ -25,7 +25,11 @@ export class VerifyCaptcha { return r }) } catch (err: any) { - console.log("Recaptcha V2 error:", err?.message) + console.log(JSON.stringify({ + date: new Date(), + type: 'RecaptchaV2Error', + item: err?.message + })) } const data = response?.data @@ -45,7 +49,11 @@ export class VerifyCaptcha { try { response = await axios.post(URL) } catch(err: any){ - console.log("Recaptcha V3 error:", err?.message) + console.log(JSON.stringify({ + date: new Date(), + type: 'RecaptchaV3Error', + item: err?.message + })) } const data = response?.data diff --git a/server.ts b/server.ts index a8e7ba6..5749ced 100644 --- a/server.ts +++ b/server.ts @@ -165,6 +165,7 @@ router.post('/sendToken', captcha.middleware, async (req: any, res: any) => { // logging requests (if enabled) DEBUG && console.log(JSON.stringify({ + date: new Date(), type: "NewFaucetRequest", faucetConfigId, address, diff --git a/utils/mainnetBalanceCheck.ts b/utils/mainnetBalanceCheck.ts index 643edb9..7254c87 100644 --- a/utils/mainnetBalanceCheck.ts +++ b/utils/mainnetBalanceCheck.ts @@ -14,7 +14,11 @@ export async function checkMainnetBalance(rpc: string, address: string, threshol return {isValid: true, balance} } } catch(err) { - console.error('ERROR: checkMainnetBalance', err) + console.error(JSON.stringify({ + date: new Date(), + type: 'MainnetBalanceCheckError', + item: err + })) return response } return response