Skip to content

Commit

Permalink
simplify mainnet balance check config
Browse files Browse the repository at this point in the history
  • Loading branch information
rajranjan0608 committed Dec 28, 2023
1 parent 7809020 commit 7f00e28
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"IS_ENABLED": true,
"MAX_LIMIT_CAP": 5000
},
"MAINNET_BALANCE_CHECK_RPC": "https://api.avax.network/ext/C/rpc",
"evmchains": [
{
"ID": "C",
Expand All @@ -30,7 +31,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": 5,
"WINDOW_SIZE": 60
Expand Down
13 changes: 7 additions & 6 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
GLOBAL_RL,
NATIVE_CLIENT,
DEBUG,
MAINNET_BALANCE_CHECK_RPC,
} from './config.json'
import { CouponService } from './CouponService/couponService'
import {
Expand Down Expand Up @@ -78,7 +79,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 => {
Expand Down Expand Up @@ -143,21 +144,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
}

Expand Down Expand Up @@ -189,7 +190,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 })
})

// GET request for fetching faucet address for the specified chain
Expand Down
4 changes: 2 additions & 2 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 7f00e28

Please sign in to comment.