This repository was archived by the owner on Mar 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvote-utils.ts
More file actions
95 lines (83 loc) · 2.87 KB
/
vote-utils.ts
File metadata and controls
95 lines (83 loc) · 2.87 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
94
95
// Helper function to format votes with appropriate suffixes
export function formatVotes(votes: number): string {
if (isNaN(votes)) throw new Error("Invalid vote value: NaN");
if (votes === 0) return "0";
// Simply return the number divided by 1e8 as requested
return (votes / 1e8).toString();
}
// const url =
// process.env.NEXT_PUBLIC_STACKS_NETWORK === "testnet"
// ? process.env.NEXT_PUBLIC_CACHE_URL_TESTNET
// : process.env.NEXT_PUBLIC_CACHE_URL;
export async function getProposalVotes(
contractPrincipal: string,
proposalId: number,
bustCache = false
) {
// Parse the contract principal to extract address and name
const [contractAddress, contractName] = contractPrincipal.split(".");
if (!contractAddress || !contractName) {
throw new Error("Invalid contract principal format");
}
try {
// `${url}/contract-calls/read-only/${contractAddress}/${contractName}/get-proposal`,
const response = await fetch(
`https://aibtcdev-cache-preview.hosting-962.workers.dev/contract-calls/read-only/${contractAddress}/${contractName}/get-proposal`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
functionArgs: [
{
type: "uint",
value: proposalId.toString(),
},
],
network:
process.env.NEXT_PUBLIC_STACKS_NETWORK === "mainnet"
? "mainnet"
: "testnet",
// Add cache control in the request body
cacheControl: bustCache
? {
bustCache: true, // Force a fresh request
ttl: 3600, // Cache for 1 hour
}
: undefined,
}),
}
);
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status}: ${errorText}`);
}
const responseData = await response.json();
// Check if the data is nested inside a data property
const voteData = responseData.data || responseData;
if (!voteData) {
throw new Error("No vote data returned from API");
}
// Validate that we have the required vote fields
const votesFor = voteData.votesFor;
const votesAgainst = voteData.votesAgainst;
const liquidTokens = voteData.liquidTokens;
if (votesFor === undefined || votesAgainst === undefined) {
throw new Error("Invalid vote data structure - missing vote fields");
}
return {
...responseData,
votesFor: votesFor,
votesAgainst: votesAgainst,
liquidTokens: liquidTokens,
fetchedAt: Date.now(), // Add timestamp for debugging
wasCacheBusted: bustCache,
};
} catch (error) {
// Re-throw with more context
throw new Error(
`Failed to fetch proposal votes: ${error instanceof Error ? error.message : String(error)}`
);
}
}