Obtain outcome of off-chain vote
Submitted by:
mykcryptodev
This function fetches the final outcome of an off-chain vote on the Snapshot.org platform
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
const proposalId = args[0]
// Use snapshot's graphql API to get the final vote outcome
const snapshotRequest = () => Functions.makeHttpRequest({
url: `https://hub.snapshot.org/graphql`,
method: "POST",
data: {
query: `{
proposal(id: "${proposalId}") {
choices
scores
scores_state
}
}`,
},
})
const { data, error } = await snapshotRequest()
if (error) {
throw Error("Snapshot request failed")
}
const { proposal } = data.data
const { choices, scores, scores_state } = proposal
if (scores_state !== "final") {
throw Error("Snapshot vote is not final")
}
const winningChoice = choices[scores.indexOf(Math.max(...scores))]
return Functions.encodeString(winningChoice)