-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.js
69 lines (61 loc) · 1.84 KB
/
snapshot.js
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
import dotenv from 'dotenv';
dotenv.config();
import snapshot from '@snapshot-labs/snapshot.js';
import { request as gqlRequest, gql } from 'graphql-request';
import { ethers } from 'ethers';
const provider = new ethers.providers.AlchemyProvider('mainnet', process.env.ALCHEMY_KEY);
const wallet = new ethers.Wallet(process.env.NANCE_PK, provider);
const hub = 'https://hub.snapshot.org';
const client = new snapshot.Client712(hub);
export async function createProposal(space, proposal, choices, startTimeStamp, endTimeStamp) {
const latestBlock = await provider.getBlockNumber();
const receipt = client.proposal(wallet, wallet.address, {
space: space,
type: 'single-choice',
title: proposal.title,
body: proposal.body,
discussion: proposal.discussion,
choices: choices,
start: startTimeStamp,
end: endTimeStamp,
snapshot: latestBlock,
network: '1',
strategies: JSON.stringify({}),
plugins: JSON.stringify({}),
metadata: JSON.stringify({})
}).then((r) => {
return r.id
}).catch((e) => {
console.log(e)
});
return receipt;
}
// use graphql to get proposal info
export async function getProposalVotes(proposalId) {
const query = gql`
{
proposal(id:"${proposalId}") {
choices
state
votes
scores
scores_state
scores_total
}
}`;
const gqlResults = (await gqlRequest(`${hub}/graphql`, query)).proposal;
// results return as: (make this an interface!)
// { totalVotes: n,
// scoresState: '<state>',
// votes: {
// <choice>: n,
// ...
// <choice>: n
// }
// }
let offChainVotingResults = { totalVotes: gqlResults.votes, scoresState: gqlResults.scores_state, votes: {} };
gqlResults.choices.map((item, index) => {
offChainVotingResults.votes[item] = gqlResults.scores[index];
});
return offChainVotingResults;
}