-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuseTransactionHistory.ts
34 lines (28 loc) · 1.04 KB
/
useTransactionHistory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { useQuery } from "@tanstack/react-query";
import { CovalentClient } from "@covalenthq/client-sdk";
const fetchTransactionHistory = async (address: string) => {
const client = new CovalentClient(
process.env.NEXT_PUBLIC_COVALENT_API_KEY as string
);
const response = await client.TransactionService.getTransactionsForAddressV3(
"polygon-zkevm-testnet",
address,
0
);
// Filter transactions where the address is the sender or receiver
const filteredTransactions = response.data.items.sort(
(a, b) => b?.block_signed_at.getTime() - a?.block_signed_at.getTime()
);
// Limit to the 10 latest transactions
const latestTransactions = filteredTransactions.slice(0, 5);
return latestTransactions;
};
export default function useTransactionHistory(address: string | undefined) {
return useQuery(
["transaction-history", address],
() => fetchTransactionHistory(address as string),
{
enabled: address !== undefined, // Only fetch when the address is defined
}
);
}