Skip to content

Commit ed31f78

Browse files
committed
feat: set proposal strategies value on proposal creation
fix: fix botched merge conflict chore: remove typo fix: fix typescript error fix: fix lint error fix: delete unused file
1 parent f7a87bd commit ed31f78

File tree

3 files changed

+31
-34
lines changed

3 files changed

+31
-34
lines changed

src/helpers/vpValue.ts renamed to src/helpers/strategiesValue.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,14 @@
1-
import hubDB from './mysql';
21
import { fetchWithKeepAlive } from './utils';
32

43
type Proposal = {
5-
id: string;
64
network: string;
75
strategies: any[];
86
start: number;
97
};
108

119
const OVERLORD_URL = 'https://overlord.snapshot.org';
12-
const CB_LAST = 5;
13-
const CB_ERROR = 0;
1410

15-
export async function setProposalVpValue(proposal: Proposal) {
16-
if (proposal.start > Date.now()) {
17-
return;
18-
}
19-
20-
try {
21-
const vpValue = await getVpValue(proposal);
22-
23-
await hubDB.queryAsync('UPDATE proposals SET vp_value = ?, cb = ? WHERE id = ? LIMIT 1', [
24-
vpValue,
25-
CB_LAST,
26-
proposal.id
27-
]);
28-
} catch {
29-
await hubDB.queryAsync('UPDATE proposals SET cb = ? WHERE id = ? LIMIT 1', [
30-
CB_ERROR,
31-
proposal.id
32-
]);
33-
return;
34-
}
35-
}
36-
37-
async function getVpValue(proposal: Proposal) {
11+
export default async function getStrategiesValue(proposal: Proposal): Promise<number[]> {
3812
const init = {
3913
method: 'POST',
4014
headers: {

src/writer/proposal.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { containsFlaggedLinks, flaggedAddresses } from '../helpers/moderation';
99
import { isMalicious } from '../helpers/monitoring';
1010
import db from '../helpers/mysql';
1111
import { getLimits, getSpaceType } from '../helpers/options';
12+
import getStrategiesValue from '../helpers/strategiesValue';
1213
import { captureError, getQuorum, jsonParse, validateChoices } from '../helpers/utils';
13-
import { setProposalVpValue } from '../helpers/vpValue';
1414

1515
const scoreAPIUrl = process.env.SCORE_API_URL || 'https://score.snapshot.org';
1616
const broviderUrl = process.env.BROVIDER_URL || 'https://rpc.snapshot.org';
@@ -242,9 +242,34 @@ export async function verify(body): Promise<any> {
242242
if (msg.payload.choices.length > choicesLimit) {
243243
return Promise.reject(`number of choices can not exceed ${choicesLimit}`);
244244
}
245+
246+
let strategiesValue: number[] = [];
247+
248+
try {
249+
strategiesValue = await getStrategiesValue({
250+
network: space.network,
251+
start: msg.payload.start,
252+
strategies: space.strategies
253+
});
254+
255+
// Handle unlikely case where strategies value array length does not match strategies length
256+
if (strategiesValue.length !== space.strategies.length) {
257+
capture(new Error('Strategies value length mismatch'), {
258+
space: space.id,
259+
strategiesLength: space.strategies.length,
260+
strategiesValue: JSON.stringify(strategiesValue)
261+
});
262+
return Promise.reject('failed to get strategies value');
263+
}
264+
} catch (e: any) {
265+
console.log('unable to get strategies value', e.message);
266+
return Promise.reject('failed to get strategies value');
267+
}
268+
269+
return { strategiesValue };
245270
}
246271

247-
export async function action(body, ipfs, receipt, id): Promise<void> {
272+
export async function action(body, ipfs, receipt, id, context): Promise<void> {
248273
const msg = jsonParse(body.msg);
249274
const space = msg.space;
250275

@@ -302,7 +327,7 @@ export async function action(body, ipfs, receipt, id): Promise<void> {
302327
scores_state: 'pending',
303328
scores_total: 0,
304329
scores_updated: 0,
305-
vp_value_by_strategy: JSON.stringify([]),
330+
vp_value_by_strategy: JSON.stringify(context.strategiesValue),
306331
votes: 0,
307332
validation,
308333
flagged: +containsFlaggedLinks(msg.payload.body)
@@ -317,6 +342,4 @@ export async function action(body, ipfs, receipt, id): Promise<void> {
317342
`;
318343

319344
await db.queryAsync(query, [proposal, space, author, space]);
320-
321-
setProposalVpValue({ ...proposal, strategies: spaceSettings.strategies });
322345
}

test/integration/writer/proposal.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('writer/proposal', () => {
4545
expect.hasAssertions();
4646
mockContainsFlaggedLinks.mockReturnValueOnce(true);
4747
const id = '0x01-flagged';
48-
expect(await action(input, 'ipfs', 'receipt', id)).toBeUndefined();
48+
expect(await action(input, 'ipfs', 'receipt', id, {})).toBeUndefined();
4949
expect(mockContainsFlaggedLinks).toBeCalledTimes(1);
5050

5151
const [proposal] = await db.queryAsync('SELECT * FROM proposals WHERE id = ?', [id]);
@@ -57,7 +57,7 @@ describe('writer/proposal', () => {
5757
it('creates and does not flag proposal', async () => {
5858
expect.hasAssertions();
5959
const id = '0x02-non-flagged';
60-
expect(await action(input, 'ipfs', 'receipt', id)).toBeUndefined();
60+
expect(await action(input, 'ipfs', 'receipt', id, {})).toBeUndefined();
6161
expect(mockContainsFlaggedLinks).toBeCalledTimes(1);
6262

6363
const [proposal] = await db.queryAsync('SELECT * FROM proposals WHERE id = ?', [id]);

0 commit comments

Comments
 (0)