-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
147 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { getUnixTime } from "date-fns"; | ||
|
||
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); | ||
|
||
transactionResponse.forEach(({ blockTime, transferDestination }) => { | ||
if (receiverAddress == transferDestination ) { | ||
if (Number(blockTime) > limitDate) { | ||
isAllowed = false; | ||
} | ||
} | ||
}); | ||
return isAllowed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
export default async function getLatestTransactions(): Promise<PartialTransaction[]> { | ||
if (!process.env.NEXT_PUBLIC_EXPLORER_API_URL || !process.env.NEXT_PUBLIC_SENDER_ADDRESS) { | ||
throw new Error('NEXT_PUBLIC_EXPLORER_API_URL or NEXT_PUBLIC_SENDER_ADDRESS env vars undefined.'); | ||
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.', | ||
); | ||
} | ||
|
||
const latestTransactionsPath = `/accTransactions/${process.env.NEXT_PUBLIC_SENDER_ADDRESS}?limit=5&order=descending&includeRawRejectReason`; | ||
|
||
try { | ||
const response = await fetch(`${process.env.NEXT_PUBLIC_EXPLORER_API_URL}${latestTransactionsPath}`); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to fetch transactions: ${response.statusText}`); | ||
} | ||
|
||
const transactionResponse: TransactionsResponse = await response.json(); | ||
return transactionResponse.transactions.map(({ blockTime, transactionHash }) => ({ | ||
blockTime, | ||
transactionHash, | ||
})); | ||
} catch (error) { | ||
console.error('Error fetching transactions:', error); | ||
throw error; | ||
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}`); | ||
} | ||
|
||
const transactionResponse: TransactionsResponse = await response.json(); | ||
return transactionResponse.transactions.map(({ blockTime, transactionHash, details }) => ({ | ||
blockTime, | ||
transactionHash, | ||
transferDestination: details.transferDestination | ||
})); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import getLatestTransactions from '@/lib/getLatestTransactions'; | ||
|
||
type Data = { | ||
transactions?: PartialTransaction[]; | ||
error?: string; | ||
}; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse<Data>) { | ||
if (req.method !== 'GET') { | ||
return res.status(405).json({ error: 'Method Not Allowed. Please use GET.' }); | ||
} | ||
try { | ||
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}` }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import checkUsageLimit from '@/lib/checkUsageLimit'; | ||
|
||
interface IBody { | ||
hoursLimit: number; | ||
receiver: string; | ||
} | ||
|
||
type Data = { | ||
isAllowed?: boolean; | ||
error?: string; | ||
}; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse<Data>) { | ||
if (req.method !== 'POST') { | ||
return res.status(405).json({ error: 'Method Not Allowed. Please use POST.' }); | ||
} | ||
const { hoursLimit, receiver } = req.body as IBody; | ||
|
||
if (!hoursLimit || !receiver) { | ||
return res.status(400).json({ | ||
error: 'Missing parameters. Please provide hoursLimit and receiver params.', | ||
}); | ||
} | ||
try { | ||
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}` }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters