Skip to content

Commit

Permalink
Simplified maninet balance check RPC (#133)
Browse files Browse the repository at this point in the history
* prod configs
  • Loading branch information
rajranjan0608 committed Dec 29, 2023
1 parent 332273d commit abb21a6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 3 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down
14 changes: 8 additions & 6 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
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 abb21a6

Please sign in to comment.