Skip to content

Commit bc36caf

Browse files
committed
feat: new hook to get total stakes for a round application
1 parent 3b9a961 commit bc36caf

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { useQuery } from "@tanstack/react-query";
2+
import { gql } from "graphql-request";
3+
4+
const endpoint = "https://indexer.hyperindex.xyz/98cb471/v1/graphql";
5+
6+
const getStakesQuery = gql`
7+
query getStakes($chainId: numeric!, $poolId: numeric!, $recipient: String!) {
8+
TokenLock_Locked(
9+
where: {
10+
chainId: { _eq: $chainId }
11+
poolId: { _eq: $poolId }
12+
recipient: { _eq: $recipient }
13+
}
14+
) {
15+
amount
16+
chainId
17+
poolId
18+
recipient
19+
sender
20+
}
21+
}
22+
`;
23+
24+
export const useGetApplicationStakes = (
25+
chainId: number,
26+
poolId: number,
27+
recipient: string,
28+
isStakableRound: boolean
29+
) => {
30+
const query = useQuery({
31+
enabled: isStakableRound,
32+
queryKey: ["getApplicationStakes", chainId, poolId, recipient],
33+
queryFn: () => getApplicationStakes(chainId, poolId, recipient),
34+
});
35+
36+
return {
37+
data: query.data,
38+
isLoading: query.isLoading,
39+
isError: query.isError,
40+
error: query.error,
41+
refetch: query.refetch,
42+
};
43+
};
44+
45+
const GET = async (chainId: number, poolId: number, recipient: string) => {
46+
const response = await fetch(endpoint, {
47+
method: "POST",
48+
headers: {
49+
"Content-Type": "application/json",
50+
},
51+
body: JSON.stringify({
52+
query: getStakesQuery,
53+
variables: { chainId, poolId, recipient },
54+
}),
55+
});
56+
57+
if (!response.ok) {
58+
const errorData = await response.json();
59+
throw new Error(
60+
`Error: ${response.status} - ${errorData.message || "Unknown error"}`
61+
);
62+
}
63+
64+
return response.json();
65+
};
66+
67+
interface ApplicationStake {
68+
amount: string;
69+
chainId: string;
70+
poolId: string;
71+
recipient: string;
72+
sender: string;
73+
}
74+
75+
export async function getApplicationStakes(
76+
chainId: number,
77+
poolId: number,
78+
recipient: string
79+
): Promise<string> {
80+
try {
81+
const response = (await GET(chainId, poolId, recipient)).data
82+
.TokenLock_Locked as ApplicationStake[];
83+
const totalStakes = response.reduce(
84+
(acc, stake) => acc + Number(stake.amount),
85+
0
86+
);
87+
88+
return (totalStakes / 10 ** 18).toFixed(3);
89+
} catch (error) {
90+
console.error("Error fetching pool info and stakes:", error);
91+
throw error;
92+
}
93+
}

0 commit comments

Comments
 (0)