Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
benya7 authored and DOBEN committed Aug 11, 2024
1 parent 31cfc6c commit ad8780c
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 30 deletions.
30 changes: 15 additions & 15 deletions testnet-faucet/src/lib/checkUsageLimit.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { getUnixTime } from "date-fns";
import { getUnixTime } from 'date-fns';

import getLatestTransactions from "./getLatestTransactions";
import { shiftDateBackwards } from "./utils";
import getLatestTransactions from './getLatestTransactions';
import { shiftDateBackwards } from './utils';

export default async function checkUsageLimit(hoursLimit: number, receiverAddress: string): Promise<boolean> {
let isAllowed = true;
const limitDate = getUnixTime(shiftDateBackwards(hoursLimit));
const transactionResponse: PartialTransaction[] = await getLatestTransactions(1000);
let isAllowed = true;
const limitDate = getUnixTime(shiftDateBackwards(hoursLimit));
const transactionResponse: PartialTransaction[] = await getLatestTransactions(1000);

transactionResponse.forEach(({ blockTime, transferDestination }) => {
if (receiverAddress == transferDestination ) {
if (Number(blockTime) > limitDate) {
isAllowed = false;
}
}
});
return isAllowed
}
transactionResponse.forEach(({ blockTime, transferDestination }) => {
if (receiverAddress == transferDestination) {
if (Number(blockTime) > limitDate) {
isAllowed = false;
}
}
});
return isAllowed;
}
12 changes: 5 additions & 7 deletions testnet-faucet/src/lib/getLatestTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
export default async function getLatestTransactions(limit: number): Promise<PartialTransaction[]> {
const explorerApiUrl = process.env.EXPLORER_API_URL;
const senderAddress = process.env.SENDER_ADDRESS;

if (!senderAddress || !explorerApiUrl) {
throw new Error(
'EXPLORER_API_URL, SENDER_ADDRESS env vars undefined.',
);
throw new Error('EXPLORER_API_URL, SENDER_ADDRESS env vars undefined.');
}
const latestTransactionsPath = `/accTransactions/${senderAddress}?limit=${limit}&order=descending&includeRawRejectReason`;

const response = await fetch(`${explorerApiUrl}${latestTransactionsPath}`);

if (!response.ok) {
throw new Error(`Failed to fetch transactions: ${response.statusText}`);
}
Expand All @@ -19,6 +17,6 @@ export default async function getLatestTransactions(limit: number): Promise<Part
return transactionResponse.transactions.map(({ blockTime, transactionHash, details }) => ({
blockTime,
transactionHash,
transferDestination: details.transferDestination
transferDestination: details.transferDestination,
}));
}
2 changes: 1 addition & 1 deletion testnet-faucet/src/lib/getSenderAccountSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export default function getSenderAccountSigner(): AccountSigner {
throw new Error('SENDER_PRIVATE_KEY env var undefined');
}
return buildBasicAccountSigner(SENDER_PRIVATE_KEY);
};
}
2 changes: 1 addition & 1 deletion testnet-faucet/src/pages/api/latestTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
return res.status(405).json({ error: 'Method Not Allowed. Please use GET.' });
}
try {
const transactions = await getLatestTransactions(5)
const transactions = await getLatestTransactions(5);
return res.status(200).json({ transactions });
} catch (e) {
return res.status(500).json({ error: `An unexpected error has occurred: ${e}` });
Expand Down
2 changes: 1 addition & 1 deletion testnet-faucet/src/pages/api/usageLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
});
}
try {
const isAllowed = await checkUsageLimit(hoursLimit, receiver)
const isAllowed = await checkUsageLimit(hoursLimit, receiver);
return res.status(200).json({ isAllowed });
} catch (e) {
return res.status(500).json({ error: `An unexpected error has occurred: ${e}` });
Expand Down
6 changes: 3 additions & 3 deletions testnet-faucet/src/pages/api/validateAndClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
});
}
try {
const isAllowed = await checkUsageLimit(hoursLimit, receiver)
const isAllowed = await checkUsageLimit(hoursLimit, 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 ${hoursLimit} ${hoursLimit > 1 ? 'hours' : 'hour'}. Please try again later.`,
});
}
const rettiwt = new Rettiwt();
Expand All @@ -60,7 +60,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
error: 'X Post verification failed. Please make sure you do not modify the template text and that your address is present.',
});
}

const client = createGRPCNodeClient();
const signer = getSenderAccountSigner();

Expand Down
4 changes: 2 additions & 2 deletions testnet-faucet/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const getLatestTransactions = async () => {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
},
});
const data = await response.json();

Expand All @@ -57,7 +57,7 @@ const validateAndClaim = async (hoursLimit: number, XPostId: string | undefined,
body: JSON.stringify({
hoursLimit,
XPostId,
receiver
receiver,
}),
});

Expand Down

0 comments on commit ad8780c

Please sign in to comment.