Skip to content

Commit

Permalink
fix: read usageLimit from env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
benya7 authored and DOBEN committed Aug 11, 2024
1 parent ad8780c commit 9a7cd6b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
9 changes: 7 additions & 2 deletions testnet-faucet/src/lib/checkUsageLimit.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
export default async function checkUsageLimit(receiverAddress: string): Promise<boolean> {
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 }) => {
Expand Down
9 changes: 4 additions & 5 deletions testnet-faucet/src/pages/api/usageLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import checkUsageLimit from '@/lib/checkUsageLimit';

interface IBody {
hoursLimit: number;
receiver: string;
}

Expand All @@ -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}` });
Expand Down
13 changes: 6 additions & 7 deletions testnet-faucet/src/pages/api/validateAndClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
Expand Down
11 changes: 5 additions & 6 deletions testnet-faucet/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ 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',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
hoursLimit,
XPostId,
receiver,
}),
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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.`,
Expand Down

0 comments on commit 9a7cd6b

Please sign in to comment.