diff --git a/middlewares/parseURI.ts b/middlewares/parseURI.ts index e2b7150..e31f246 100644 --- a/middlewares/parseURI.ts +++ b/middlewares/parseURI.ts @@ -1,3 +1,5 @@ +import bodyParser from "body-parser"; + export const parseURI = (req: any, res: any, next: any) => { var err = null; try { @@ -9,4 +11,14 @@ export const parseURI = (req: any, res: any, next: any) => { return res.redirect('/') } next(); +} + +export const parseBody = (req: any, res: any, next: () => void) => { + bodyParser.json()(req, res, (error) => { + if (error && error.status >= 400) { + res.status(400).send({message: "Invalid request body"}); + } else { + next(); + } + }); } \ No newline at end of file diff --git a/server.ts b/server.ts index 7c38ac3..9e6a266 100644 --- a/server.ts +++ b/server.ts @@ -5,7 +5,7 @@ import path from 'path' import dotenv from 'dotenv' import { BN } from 'avalanche' -import { RateLimiter, VerifyCaptcha, parseURI } from './middlewares' +import { RateLimiter, VerifyCaptcha, parseBody, parseURI } from './middlewares' import EVM from './vms/evm' import { @@ -29,7 +29,7 @@ const router: any = express.Router() app.use(express.static(path.join(__dirname, "client"))) app.use(cors()) app.use(parseURI) -app.use(bodyParser.json()) +app.use(parseBody) new RateLimiter(app, [GLOBAL_RL]) @@ -151,6 +151,18 @@ router.get('/getBalance', (req: any, res: any) => { }) }) +router.get('/faucetUsage', (req: any, res: any) => { + const chain: string = req.query?.chain + + const evm: EVMInstanceAndConfig = evms.get(chain)! + + const usage: number = evm?.instance?.getFaucetUsage() + + res.status(200).send({ + usage + }) +}) + app.use('/api', router) app.get('/health', (req: any, res: any) => { diff --git a/vms/evm.ts b/vms/evm.ts index 9831924..9c7a537 100644 --- a/vms/evm.ts +++ b/vms/evm.ts @@ -41,6 +41,7 @@ export default class EVM { requestCount: number queuingInProgress: boolean blockFaucetDrips: boolean + recalibrateNowActivated: boolean constructor(config: ChainType, PK: string | undefined) { this.web3 = new Web3(config.RPC) @@ -69,6 +70,7 @@ export default class EVM { this.recalibrate = false this.waitingForRecalibration = false this.queuingInProgress = false + this.recalibrateNowActivated = false this.requestCount = 0 this.waitArr = [] @@ -297,7 +299,8 @@ export default class EVM { this.executeQueue() } else { this.queuingInProgress = false - this.log.warn("Faucet balance too low!" + this.balance) + this.requestCount-- + this.log.warn("Faucet balance too low! " + req.id + " " + this.getBalance(req.id)) this.hasError.set(req.receiver, "Faucet balance too low! Please try after sometime.") } } @@ -428,10 +431,13 @@ export default class EVM { this.pendingTxNonces.clear() this.updateNonceAndBalance() - } else { + } else if (this.recalibrateNowActivated === false) { const recalibrateNow = setInterval(() => { + this.recalibrateNowActivated = true + if(this.pendingTxNonces.size === 0 && this.isUpdating === false && this.queuingInProgress === false) { clearInterval(recalibrateNow) + this.recalibrateNowActivated = false this.waitingForRecalibration = false this.recalibrateNonceAndBalance() } @@ -446,4 +452,8 @@ export default class EVM { config }) } + + getFaucetUsage(): number { + return 100 * (this.requestCount / MEMPOOL_LIMIT) + } } \ No newline at end of file