Take rate limit from toml config

This commit is contained in:
Shreerang Kale 2024-10-24 14:31:59 +05:30
parent 0ecf1eaf63
commit 8ac57c3b36
2 changed files with 4 additions and 3 deletions

View File

@ -8,6 +8,7 @@
[server]
port = 3000
enableRateLimit=false
transferAmount = 10000000000 # 1 * 10^10 alnt
periodTransferLimit = 30000000000 # 3 * 10^10 alnt
dbDir = "db"

View File

@ -13,7 +13,6 @@ import KeyvSqlite from '@keyv/sqlite';
const CONFIG_PATH = 'environments/local.toml';
const FAUCET_DATA_FILE = 'faucet_data.sqlite';
const FAUCET_DATA_TTL = 86400000; // 24 hrs
const DISABLE_RATE_LIMIT = true;
interface Config {
upstream: {
@ -26,6 +25,7 @@ interface Config {
},
server: {
port: number
enableRateLimit: boolean
transferAmount: string
periodTransferLimit: string
dbDir: string
@ -60,7 +60,7 @@ async function main (): Promise<void> {
const faucetStoreKey = `${accountAddress}:${today}`;
const amountSentToAddress = await faucetDataStore.get(faucetStoreKey) || '0';
if (!DISABLE_RATE_LIMIT) {
if (config.server.enableRateLimit) {
// Check rate limit
if (BigInt(amountSentToAddress) + BigInt(amount) > BigInt(config.server.periodTransferLimit)) {
return res.status(429).json({ error: 'Limit exceeded' });
@ -72,7 +72,7 @@ async function main (): Promise<void> {
console.log(`Sent tokens to address: ${accountAddress}, txHash: ${txHash}`);
// Update rate limit
!DISABLE_RATE_LIMIT && await faucetDataStore.set(faucetStoreKey, (BigInt(amountSentToAddress) + BigInt(amount)).toString(), FAUCET_DATA_TTL);
config.server.enableRateLimit && await faucetDataStore.set(faucetStoreKey, (BigInt(amountSentToAddress) + BigInt(amount)).toString(), FAUCET_DATA_TTL);
res.json({ success: true, txHash });
} catch (error) {