From 0ecf1eaf631ebbd8a895eb01475c29379f351a39 Mon Sep 17 00:00:00 2001 From: Shreerang Kale Date: Thu, 24 Oct 2024 12:52:29 +0530 Subject: [PATCH] Disable rate limit --- src/index.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 47c2daa..4ca39fa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ 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: { @@ -44,7 +45,7 @@ async function main (): Promise { app.use(cors()); app.post('/faucet', async (req, res) => { - const { address: accountAddress } = req.body; + const { address: accountAddress, amount } = req.body; if (!accountAddress) { return res.status(400).json({ error: 'address is required' }); @@ -54,22 +55,24 @@ async function main (): Promise { return res.status(400).json({ error: 'invalid address' }); } - // Check rate limit const now = Date.now(); const today = new Date(now).toISOString().split('T')[0]; const faucetStoreKey = `${accountAddress}:${today}`; const amountSentToAddress = await faucetDataStore.get(faucetStoreKey) || '0'; - if (BigInt(amountSentToAddress) + BigInt(config.server.transferAmount) > BigInt(config.server.periodTransferLimit)) { - return res.status(429).json({ error: 'Limit exceeded' }); + if (!DISABLE_RATE_LIMIT) { + // Check rate limit + if (BigInt(amountSentToAddress) + BigInt(amount) > BigInt(config.server.periodTransferLimit)) { + return res.status(429).json({ error: 'Limit exceeded' }); + } } try { - const txHash = await sendTokens(config, accountAddress, String(config.server.transferAmount)); + const txHash = await sendTokens(config, accountAddress, String(amount)); console.log(`Sent tokens to address: ${accountAddress}, txHash: ${txHash}`); // Update rate limit - await faucetDataStore.set(faucetStoreKey, (BigInt(amountSentToAddress) + BigInt(config.server.transferAmount)).toString(), FAUCET_DATA_TTL); + !DISABLE_RATE_LIMIT && await faucetDataStore.set(faucetStoreKey, (BigInt(amountSentToAddress) + BigInt(amount)).toString(), FAUCET_DATA_TTL); res.json({ success: true, txHash }); } catch (error) {