/
scores.ts
185 lines (164 loc) · 5.95 KB
/
scores.ts
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import snapshot from '@snapshot-labs/snapshot.js';
import db from './helpers/mysql';
import { hasStrategyOverride, sha256 } from './helpers/utils';
import log from './helpers/log';
import { getDecryptionKey } from './helpers/shutter';
const scoreAPIUrl = process.env.SCORE_API_URL || 'https://score.snapshot.org';
async function getProposal(id: string): Promise<any | undefined> {
const query = 'SELECT * FROM proposals WHERE id = ? LIMIT 1';
const [proposal] = await db.queryAsync(query, [id]);
if (!proposal) return;
proposal.strategies = JSON.parse(proposal.strategies);
proposal.plugins = JSON.parse(proposal.plugins);
proposal.choices = JSON.parse(proposal.choices);
proposal.scores = JSON.parse(proposal.scores);
proposal.scores_by_strategy = JSON.parse(proposal.scores_by_strategy);
let proposalState = 'pending';
const ts = parseInt((Date.now() / 1e3).toFixed());
if (ts > proposal.start) proposalState = 'active';
if (ts > proposal.end) proposalState = 'closed';
proposal.state = proposalState;
return proposal;
}
async function getVotes(proposalId: string): Promise<any[] | undefined> {
const query =
'SELECT id, choice, voter, vp, vp_by_strategy, vp_state FROM votes WHERE proposal = ?';
const votes = await db.queryAsync(query, [proposalId]);
return votes.map(vote => {
vote.choice = JSON.parse(vote.choice);
vote.vp_by_strategy = JSON.parse(vote.vp_by_strategy);
vote.balance = vote.vp;
vote.scores = vote.vp_by_strategy;
return vote;
});
}
async function updateVotesVp(votes: any[], vpState: string, proposalId: string) {
const votesWithChange = votes.filter(vote => {
const key1 = sha256(JSON.stringify([vote.balance, vote.scores, vpState]));
const key2 = sha256(JSON.stringify([vote.vp, vote.vp_by_strategy, vote.vp_state]));
return key1 !== key2;
});
if (votesWithChange.length === 0) return;
const max = 200;
const pages = Math.ceil(votesWithChange.length / max);
const votesInPages: any = [];
Array.from(Array(pages)).forEach((x, i) => {
votesInPages.push(votesWithChange.slice(max * i, max * (i + 1)));
});
let i = 0;
for (const votesInPage of votesInPages) {
const params: any = [];
let query = '';
votesInPage.forEach((vote: any) => {
query += `UPDATE votes
SET vp = ?, vp_by_strategy = ?, vp_state = ?
WHERE id = ? AND proposal = ? LIMIT 1; `;
params.push(vote.balance);
params.push(JSON.stringify(vote.scores));
params.push(vpState);
params.push(vote.id);
params.push(proposalId);
});
await db.queryAsync(query, params);
if (i) await snapshot.utils.sleep(200);
i++;
}
log.info(`[scores] updated votes vp, ${votesWithChange.length}/${votes.length} on ${proposalId}`);
}
async function updateProposalScores(proposalId: string, scores: any, votes: number) {
const ts = (Date.now() / 1e3).toFixed();
const query = `
UPDATE proposals
SET scores_state = ?,
scores = ?,
scores_by_strategy = ?,
scores_total = ?,
scores_updated = ?,
votes = ?
WHERE id = ? LIMIT 1;
`;
await db.queryAsync(query, [
scores.scores_state,
JSON.stringify(scores.scores),
JSON.stringify(scores.scores_by_strategy),
scores.scores_total,
ts,
votes,
proposalId
]);
}
const pendingRequests = {};
export async function updateProposalAndVotes(proposalId: string, force = false) {
const proposal = await getProposal(proposalId);
if (!proposal || proposal.state === 'pending') return false;
if (proposal.scores_state === 'final') return true;
if (!force && proposal.privacy === 'shutter' && proposal.state === 'closed') {
await getDecryptionKey(proposal.id);
return true;
}
// Ignore score calculation if proposal have more than 100k votes and scores_updated greater than 5 minute
const ts = Number((Date.now() / 1e3).toFixed());
if (
(proposal.votes > 20000 && proposal.scores_updated > ts - 300) ||
pendingRequests[proposalId]
) {
console.log(
'ignore score calculation',
proposal.space,
proposalId,
proposal.votes,
proposal.scores_updated
);
return false;
}
if (proposal.votes > 20000) pendingRequests[proposalId] = true;
try {
// Get votes
let votes: any = await getVotes(proposalId);
const isFinal = votes.every(vote => vote.vp_state === 'final');
let vpState = 'final';
if (!isFinal) {
log.info(`[scores] Get scores', ${proposalId}`);
// Get scores
const { scores, state } = await snapshot.utils.getScores(
proposal.space,
proposal.strategies,
proposal.network,
votes.map(vote => vote.voter),
parseInt(proposal.snapshot),
scoreAPIUrl,
{ returnValue: 'all' }
);
vpState = state;
// Add vp to votes
votes = votes.map((vote: any) => {
vote.scores = proposal.strategies.map((strategy, i) => scores[i][vote.voter] || 0);
vote.balance = vote.scores.reduce((a, b: any) => a + b, 0);
return vote;
});
}
// Get results
const voting = new snapshot.utils.voting[proposal.type](proposal, votes, proposal.strategies);
const results = {
scores_state: proposal.state === 'closed' ? 'final' : 'pending',
scores: voting.getScores(),
scores_by_strategy: voting.getScoresByStrategy(),
scores_total: voting.getScoresTotal()
};
// Check if voting power is final
const withOverride = hasStrategyOverride(proposal.strategies);
if (vpState === 'final' && withOverride && proposal.state !== 'closed') vpState = 'pending';
// Update votes voting power
if (!isFinal) await updateVotesVp(votes, vpState, proposalId);
// Store scores
await updateProposalScores(proposalId, results, votes.length);
log.info(
`[scores] Proposal updated ${proposal.id}, ${proposal.space}, ${results.scores_state}, ${votes.length}`
);
delete pendingRequests[proposalId];
return true;
} catch (e) {
delete pendingRequests[proposalId];
throw e;
}
}