-
Notifications
You must be signed in to change notification settings - Fork 13
feat(overlord): compute proposal's scores_total_value
#595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
03d0346
feat: compute proposal's `scores_total_value`
wa0x6e 32e0172
fix: remove invalid data validation
wa0x6e 878bd8d
feat: compute proposal's `scores_total_value`
wa0x6e 6133822
fix: compute value for active proposals
wa0x6e fb4c1bd
refactor:
wa0x6e 25b5e9a
feat: use zod for validation
wa0x6e f435cad
fix: better constant name, and compute scores for active proposals
wa0x6e 67d7b95
fix: update CB only if when ready
wa0x6e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { capture } from '@snapshot-labs/snapshot-sentry'; | ||
| import snapshot from '@snapshot-labs/snapshot.js'; | ||
| import { z } from 'zod'; | ||
| import db from './mysql'; | ||
| import { CB } from '../constants'; | ||
|
|
||
| type Proposal = { | ||
| id: string; | ||
| scoresState: string; | ||
| vpValueByStrategy: number[]; | ||
| scoresByStrategy: number[][]; | ||
| }; | ||
|
|
||
| const REFRESH_INTERVAL = 10 * 1000; | ||
| const BATCH_SIZE = 25; | ||
|
|
||
| const proposalSchema = z | ||
| .object({ | ||
| id: z.string(), | ||
| scoresState: z.string(), | ||
| vpValueByStrategy: z.array(z.number().finite()), | ||
| scoresByStrategy: z.array(z.array(z.number().finite())) | ||
| }) | ||
| .refine( | ||
| data => { | ||
| if (data.scoresByStrategy.length === 0 || data.vpValueByStrategy.length === 0) { | ||
| return true; | ||
| } | ||
| // Ensure all scoresByStrategy arrays have the same length as vpValueByStrategy | ||
| return data.scoresByStrategy.every( | ||
| voteScores => voteScores.length === data.vpValueByStrategy.length | ||
| ); | ||
| }, | ||
| { | ||
| message: 'Array size mismatch: voteScores length does not match vpValueByStrategy length' | ||
| } | ||
| ); | ||
|
|
||
| async function getProposals(): Promise<Proposal[]> { | ||
| const query = ` | ||
| SELECT id, scores_state, vp_value_by_strategy, scores_by_strategy | ||
| FROM proposals | ||
| WHERE cb = ? | ||
| ORDER BY created ASC | ||
| LIMIT ? | ||
| `; | ||
| const proposals = await db.queryAsync(query, [CB.PENDING_COMPUTE, BATCH_SIZE]); | ||
|
|
||
| return proposals.map((p: any) => ({ | ||
| id: p.id, | ||
| scoresState: p.scores_state, | ||
| vpValueByStrategy: JSON.parse(p.vp_value_by_strategy), | ||
| scoresByStrategy: JSON.parse(p.scores_by_strategy) | ||
| })); | ||
| } | ||
|
|
||
| export function getScoresTotalValue(proposal: Proposal): number { | ||
| const { scoresByStrategy, vpValueByStrategy } = proposalSchema.parse(proposal); | ||
|
|
||
| return vpValueByStrategy.reduce((totalValue, strategyValue, strategyIndex) => { | ||
| const strategyTotal = scoresByStrategy.reduce( | ||
| (sum, voteScores) => sum + voteScores[strategyIndex], | ||
| 0 | ||
| ); | ||
| return totalValue + strategyTotal * strategyValue; | ||
| }, 0); | ||
| } | ||
|
|
||
| async function refreshProposalsScoresTotalValue(proposals: Proposal[]) { | ||
| const query: string[] = []; | ||
| const params: any[] = []; | ||
|
|
||
| proposals.forEach(proposal => { | ||
| query.push('UPDATE proposals SET scores_total_value = ?, cb = ? WHERE id = ? LIMIT 1'); | ||
|
|
||
| try { | ||
| const scoresTotalValue = getScoresTotalValue(proposal); | ||
| params.push( | ||
| scoresTotalValue, | ||
| proposal.scoresState === 'final' ? CB.FINAL : CB.PENDING_FINAL, | ||
| proposal.id | ||
| ); | ||
| } catch (e) { | ||
| capture(e); | ||
| params.push(0, CB.INELIGIBLE, proposal.id); | ||
| } | ||
| }); | ||
|
|
||
| if (query.length) { | ||
| await db.queryAsync(query.join(';'), params); | ||
| } | ||
| } | ||
|
|
||
| export default async function run() { | ||
| while (true) { | ||
| const proposals = await getProposals(); | ||
|
|
||
| if (proposals.length) { | ||
| await refreshProposalsScoresTotalValue(proposals); | ||
| } | ||
|
|
||
| if (proposals.length < BATCH_SIZE) { | ||
| await snapshot.utils.sleep(REFRESH_INTERVAL); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.