-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathuseGetApplicationStakes.ts
More file actions
93 lines (83 loc) · 2.11 KB
/
useGetApplicationStakes.ts
File metadata and controls
93 lines (83 loc) · 2.11 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { useQuery } from "@tanstack/react-query";
import { gql } from "graphql-request";
const endpoint = "https://indexer.hyperindex.xyz/98cb471/v1/graphql";
const getStakesQuery = gql`
query getStakes($chainId: numeric!, $poolId: numeric!, $recipient: String!) {
TokenLock_Locked(
where: {
chainId: { _eq: $chainId }
poolId: { _eq: $poolId }
recipient: { _eq: $recipient }
}
) {
amount
chainId
poolId
recipient
sender
}
}
`;
export const useGetApplicationStakes = (
chainId: number,
poolId: number,
recipient: string,
isStakableRound: boolean
) => {
const query = useQuery({
enabled: isStakableRound,
queryKey: ["getApplicationStakes", chainId, poolId, recipient],
queryFn: () => getApplicationStakes(chainId, poolId, recipient),
});
return {
data: query.data,
isLoading: query.isLoading,
isError: query.isError,
error: query.error,
refetch: query.refetch,
};
};
const GET = async (chainId: number, poolId: number, recipient: string) => {
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: getStakesQuery,
variables: { chainId, poolId, recipient },
}),
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(
`Error: ${response.status} - ${errorData.message || "Unknown error"}`
);
}
return response.json();
};
interface ApplicationStake {
amount: string;
chainId: string;
poolId: string;
recipient: string;
sender: string;
}
export async function getApplicationStakes(
chainId: number,
poolId: number,
recipient: string
): Promise<string> {
try {
const response = (await GET(chainId, poolId, recipient)).data
.TokenLock_Locked as ApplicationStake[];
const totalStakes = response.reduce(
(acc, stake) => acc + Number(stake.amount),
0
);
return (totalStakes / 10 ** 18).toFixed(3);
} catch (error) {
console.error("Error fetching pool info and stakes:", error);
throw error;
}
}