From 9a7cd6b9d60841313ed9b934525e2443b09e80cf Mon Sep 17 00:00:00 2001 From: benya7 Date: Fri, 9 Aug 2024 18:26:43 +0200 Subject: [PATCH] fix: read usageLimit from env vars --- testnet-faucet/src/lib/checkUsageLimit.ts | 9 +++++++-- testnet-faucet/src/pages/api/usageLimit.ts | 9 ++++----- testnet-faucet/src/pages/api/validateAndClaim.ts | 13 ++++++------- testnet-faucet/src/pages/index.tsx | 11 +++++------ 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/testnet-faucet/src/lib/checkUsageLimit.ts b/testnet-faucet/src/lib/checkUsageLimit.ts index e740f989..a34c1b38 100644 --- a/testnet-faucet/src/lib/checkUsageLimit.ts +++ b/testnet-faucet/src/lib/checkUsageLimit.ts @@ -1,11 +1,16 @@ import { getUnixTime } from 'date-fns'; +import { usageLimit } from '@/constants'; + import getLatestTransactions from './getLatestTransactions'; import { shiftDateBackwards } from './utils'; -export default async function checkUsageLimit(hoursLimit: number, receiverAddress: string): Promise { +export default async function checkUsageLimit(receiverAddress: string): Promise { + if (!usageLimit) { + throw new Error('NEXT_PUBLIC_USAGE_LIMIT_IN_HOURS env var undefined.'); + } let isAllowed = true; - const limitDate = getUnixTime(shiftDateBackwards(hoursLimit)); + const limitDate = getUnixTime(shiftDateBackwards(usageLimit)); const transactionResponse: PartialTransaction[] = await getLatestTransactions(1000); transactionResponse.forEach(({ blockTime, transferDestination }) => { diff --git a/testnet-faucet/src/pages/api/usageLimit.ts b/testnet-faucet/src/pages/api/usageLimit.ts index a292bd5f..1f676b16 100644 --- a/testnet-faucet/src/pages/api/usageLimit.ts +++ b/testnet-faucet/src/pages/api/usageLimit.ts @@ -3,7 +3,6 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import checkUsageLimit from '@/lib/checkUsageLimit'; interface IBody { - hoursLimit: number; receiver: string; } @@ -16,15 +15,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< if (req.method !== 'POST') { return res.status(405).json({ error: 'Method Not Allowed. Please use POST.' }); } - const { hoursLimit, receiver } = req.body as IBody; + const { receiver } = req.body as IBody; - if (!hoursLimit || !receiver) { + if (!receiver) { return res.status(400).json({ - error: 'Missing parameters. Please provide hoursLimit and receiver params.', + error: 'Missing parameters. Please provide and receiver params.', }); } try { - const isAllowed = await checkUsageLimit(hoursLimit, receiver); + const isAllowed = await checkUsageLimit(receiver); return res.status(200).json({ isAllowed }); } catch (e) { return res.status(500).json({ error: `An unexpected error has occurred: ${e}` }); diff --git a/testnet-faucet/src/pages/api/validateAndClaim.ts b/testnet-faucet/src/pages/api/validateAndClaim.ts index 4d4fa13e..4218a555 100644 --- a/testnet-faucet/src/pages/api/validateAndClaim.ts +++ b/testnet-faucet/src/pages/api/validateAndClaim.ts @@ -2,14 +2,13 @@ import { AccountTransactionSignature, signTransaction } from '@concordium/web-sd import type { NextApiRequest, NextApiResponse } from 'next'; import { Rettiwt } from 'rettiwt-api'; -import { extraKeywordToVerify } from '@/constants'; +import { extraKeywordToVerify, usageLimit } from '@/constants'; import checkUsageLimit from '@/lib/checkUsageLimit'; import createAccountTransaction from '@/lib/createAccountTrasantion'; import createGRPCNodeClient from '@/lib/createGPRCClient'; import getSenderAccountSigner from '@/lib/getSenderAccountSigner'; interface IBody { - hoursLimit: number; receiver: string; XPostId: string; } @@ -29,18 +28,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< throw new Error('SENDER_ADDRESS env vars undefined.'); } - const { hoursLimit, XPostId, receiver } = req.body as IBody; + const { XPostId, receiver } = req.body as IBody; - if (!hoursLimit || !XPostId || !receiver) { + if (!XPostId || !receiver) { return res.status(400).json({ - error: 'Missing parameters. Please provide hoursLimit XPostId, and receiver params.', + error: 'Missing parameters. Please provide XPostId, and receiver params.', }); } try { - const isAllowed = await checkUsageLimit(hoursLimit, receiver); + const isAllowed = await checkUsageLimit(receiver); if (!isAllowed) { return res.status(401).json({ - error: `You already get tokens in the last ${hoursLimit} ${hoursLimit > 1 ? 'hours' : 'hour'}. Please try again later.`, + error: `You already get tokens in the last ${usageLimit} ${usageLimit > 1 ? 'hours' : 'hour'}. Please try again later.`, }); } const rettiwt = new Rettiwt(); diff --git a/testnet-faucet/src/pages/index.tsx b/testnet-faucet/src/pages/index.tsx index 8e8b6120..9260f5b8 100644 --- a/testnet-faucet/src/pages/index.tsx +++ b/testnet-faucet/src/pages/index.tsx @@ -47,7 +47,7 @@ const getLatestTransactions = async () => { } }; -const validateAndClaim = async (hoursLimit: number, XPostId: string | undefined, receiver: string) => { +const validateAndClaim = async (XPostId: string | undefined, receiver: string) => { try { const response = await fetch('/api/validateAndClaim', { method: 'POST', @@ -55,7 +55,6 @@ const validateAndClaim = async (hoursLimit: number, XPostId: string | undefined, 'Content-Type': 'application/json', }, body: JSON.stringify({ - hoursLimit, XPostId, receiver, }), @@ -69,14 +68,14 @@ const validateAndClaim = async (hoursLimit: number, XPostId: string | undefined, } }; -const checkUsageLimit = async (hoursLimit: number, receiver: string) => { +const checkUsageLimit = async (receiver: string) => { try { const response = await fetch('/api/usageLimit', { method: 'POST', headers: { 'Content-Type': 'application/json', }, - body: JSON.stringify({ hoursLimit, receiver }), + body: JSON.stringify({ receiver }), }); const data = await response.json(); @@ -133,7 +132,7 @@ export default function Home() { setTurnstileOpen(false); setIsVerifyLoading(true); try { - const response = await validateAndClaim(usageLimit, XPostId, address); + const response = await validateAndClaim(XPostId, address); if (response.ok && !response.data.error) { setIsValidVerification(true); @@ -158,7 +157,7 @@ export default function Home() { } const isWithinUsageLimit = async () => { try { - const { ok, data } = await checkUsageLimit(usageLimit, address); + const { ok, data } = await checkUsageLimit(address); if (ok && !data.isAllowed) { setAddressValidationError( `You already get tokens in the last ${usageLimit} ${usageLimit > 1 ? 'hours' : 'hour'}. Please try again later.`,