Skip to content

Commit

Permalink
Merge pull request #99 from ava-labs/raj/nits
Browse files Browse the repository at this point in the history
logging and bug fixes
  • Loading branch information
rajranjan0608 authored May 10, 2023
2 parents bbd2c9b + 4445978 commit 4a85732
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
12 changes: 12 additions & 0 deletions middlewares/parseURI.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import bodyParser from "body-parser";

export const parseURI = (req: any, res: any, next: any) => {
var err = null;
try {
Expand All @@ -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();
}
});
}
16 changes: 14 additions & 2 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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])

Expand Down Expand Up @@ -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) => {
Expand Down
14 changes: 12 additions & 2 deletions vms/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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.")
}
}
Expand Down Expand Up @@ -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()
}
Expand All @@ -446,4 +452,8 @@ export default class EVM {
config
})
}

getFaucetUsage(): number {
return 100 * (this.requestCount / MEMPOOL_LIMIT)
}
}

0 comments on commit 4a85732

Please sign in to comment.