Skip to content

Commit cba84ce

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
1 parent f7a87bd commit cba84ce

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

src/helpers/strategiesValue.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { fetchWithKeepAlive } from './utils';
2+
3+
type Proposal = {
4+
network: string;
5+
strategies: any[];
6+
start: number;
7+
};
8+
9+
const OVERLORD_URL = 'https://overlord.snapshot.org';
10+
11+
export default async function getStrategiesValue(proposal: Proposal): Promise<number[]> {
12+
const init = {
13+
method: 'POST',
14+
headers: {
15+
Accept: 'application/json',
16+
'Content-Type': 'application/json'
17+
},
18+
body: JSON.stringify({
19+
jsonrpc: '2.0',
20+
method: 'get_vp_value_by_strategy',
21+
params: {
22+
network: proposal.network,
23+
strategies: proposal.strategies,
24+
snapshot: proposal.start
25+
},
26+
id: Math.random().toString(36).substring(7)
27+
})
28+
};
29+
const res = await fetchWithKeepAlive(OVERLORD_URL, init);
30+
const { result } = await res.json();
31+
return result;
32+
}

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)