-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathuseStakingHistory.ts
62 lines (54 loc) · 1.62 KB
/
useStakingHistory.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { useQuery } from "@tanstack/react-query";
// dynamic atlasUri would go here
const atlasUri = "https://url.example/graphql";
const AUTH_TOKEN = "Bearer tokenExampleGoesHere";
export const useStakingHistory = (take: number, lastCursorId?: number) => {
const variables = {
pagination: { take, lastCursorId: lastCursorId ?? null },
};
return useQuery({
queryKey: ["stakingHistoryQuery", take, lastCursorId],
enabled: true,
staleTime: 60000,
queryFn: async () => {
console.log("Fetching with variables:", variables);
try {
const response = await fetch(atlasUri, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: AUTH_TOKEN,
},
body: JSON.stringify({
query: `
query GetStakingEvents($pagination: PaginationArgs) {
userStakingEvents(pagination: $pagination) {
edges {
node {
name
args
blockTimestamp
transactionHash
}
cursor
}
count
hasNextPage
}
}
`,
variables,
}),
});
const result = await response.json();
if (!response.ok) {
throw new Error(`GraphQL error: ${JSON.stringify(result)}`);
}
return result;
} catch (error) {
console.error("GraphQL Fetch Error:", error);
throw error;
}
},
});
};