Skip to content

Commit

Permalink
Merge pull request #127 from ava-labs/raj/mainnet-balance-check
Browse files Browse the repository at this point in the history
mainnet balance checks
  • Loading branch information
rajranjan0608 authored Dec 14, 2023
2 parents 39d01a6 + 493cb6f commit debbcfd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"DECIMALS": 18,
"RECALIBRATE": 30,
"COUPON_REQUIRED": true,
"MAINNET_BALANCE_CHECK_RPC": "https://api.avax.network/ext/C/rpc",
"RATELIMIT": {
"MAX_LIMIT": 5,
"WINDOW_SIZE": 60
Expand Down
26 changes: 23 additions & 3 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
DEBUG,
} from './config.json'
import { CouponService } from './CouponService/couponService'
import { checkMainnetBalance } from './utils/mainnetBalanceCheck'

dotenv.config()

Expand Down Expand Up @@ -128,13 +129,32 @@ router.post('/sendToken', captcha.middleware, async (req: any, res: any) => {
// drip amount (native or erc20 token) for this request as per config
const dripAmount = erc20Instance?.config.DRIP_AMOUNT ?? evm.config.DRIP_AMOUNT

/**
* MAINNET BALANCE OR COUPON VALIDATION checks
* 1. If mainnet balance check is enabled, users would be required to have mainnet balance
* 2. If coupon validation is enabled, users would need a specific coupon id to get tokens
* 3. If both are enabled, then any one would be sufficient
*/

// mainnet balance checks
const mainnetCheckEnabledRPC = erc20Instance?.config.MAINNET_BALANCE_CHECK_RPC ?? evm.config.MAINNET_BALANCE_CHECK_RPC ?? false
let mainnetCheckPassed = false
if (mainnetCheckEnabledRPC && (await checkMainnetBalance(mainnetCheckEnabledRPC, address))) {
mainnetCheckPassed = true
}

// validate coupon
let couponValidity: CouponValidity = {isValid: false, amount: dripAmount}

if (
// check coupon validation only if mainnet check failed (either no-balance or check not enabled)
!mainnetCheckPassed &&

// coupon checks
couponConfig.IS_ENABLED &&
(erc20Instance && erc20Instance.config.COUPON_REQUIRED) ||
(erc20Instance === undefined && evm.config.COUPON_REQUIRED)
// if request is for erc20 tokens
((erc20Instance && erc20Instance.config.COUPON_REQUIRED) ||
// if request is for evm native token
(erc20Instance === undefined && evm.config.COUPON_REQUIRED))
) {
// if coupon is required but not passed in request
if (coupon === undefined) {
Expand Down
2 changes: 2 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type ChainType = {
DRIP_AMOUNT: number,
RECALIBRATE?: number,
COUPON_REQUIRED?: boolean,
MAINNET_BALANCE_CHECK_RPC?: string,
RATELIMIT: {
WINDOW_SIZE: number,
MAX_LIMIT: number
Expand Down Expand Up @@ -64,6 +65,7 @@ export type ERC20Type = {
MAX_PRIORITY_FEE?: string,
MAX_FEE?: string,
COUPON_REQUIRED?: boolean,
MAINNET_BALANCE_CHECK_RPC?: string,
}

export type CouponValidity = {
Expand Down
19 changes: 19 additions & 0 deletions utils/mainnetBalanceCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import axios from "axios"

export async function checkMainnetBalance(rpc: string, address: string, threshold = 0): Promise<boolean> {
try {
const response = await axios.post<any, any>(rpc, {
jsonrpc: "2.0",
method: "eth_getBalance",
params: [address, "latest"],
id: 1,
})
if (parseInt(response.data.result) > threshold) {
return true
}
} catch(err) {
console.error('ERROR: checkMainnetBalance', err)
return false
}
return false
}

0 comments on commit debbcfd

Please sign in to comment.