Skip to content

Commit 84efdb7

Browse files
committed
feat: set proposal vp value on proposal creation
chore: fix types
1 parent 228bf79 commit 84efdb7

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/helpers/vpValue.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import hubDB from './mysql';
2+
import { fetchWithKeepAlive } from './utils';
3+
4+
type Proposal = {
5+
id: string;
6+
network: string;
7+
strategies: any[];
8+
start: number;
9+
};
10+
11+
const OVERLORD_URL = 'https://overlord.snapshot.org';
12+
const CB_LAST = 5;
13+
const CB_ERROR = 0;
14+
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) {
38+
const init = {
39+
method: 'POST',
40+
headers: {
41+
Accept: 'application/json',
42+
'Content-Type': 'application/json'
43+
},
44+
body: JSON.stringify({
45+
jsonrpc: '2.0',
46+
method: 'get_vp_value_by_strategy',
47+
params: {
48+
network: proposal.network,
49+
strategies: proposal.strategies,
50+
snapshot: proposal.start
51+
},
52+
id: Math.random().toString(36).substring(7)
53+
})
54+
};
55+
const res = await fetchWithKeepAlive(OVERLORD_URL, init);
56+
const { result } = await res.json();
57+
return result;
58+
}

src/writer/proposal.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { isMalicious } from '../helpers/monitoring';
1010
import db from '../helpers/mysql';
1111
import { getLimits, getSpaceType } from '../helpers/options';
1212
import { captureError, getQuorum, jsonParse, validateChoices } from '../helpers/utils';
13+
import { setProposalVpValue } from '../helpers/vpValue';
1314

1415
const scoreAPIUrl = process.env.SCORE_API_URL || 'https://score.snapshot.org';
1516
const broviderUrl = process.env.BROVIDER_URL || 'https://rpc.snapshot.org';
@@ -316,4 +317,6 @@ export async function action(body, ipfs, receipt, id): Promise<void> {
316317
`;
317318

318319
await db.queryAsync(query, [proposal, space, author, space]);
320+
321+
setProposalVpValue({ ...proposal, strategies: spaceSettings.strategies });
319322
}

0 commit comments

Comments
 (0)