Skip to content

feat: add vp value to leaderboard #571

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

Open
wants to merge 5 commits into
base: feat-assign-fiat-value-to-vote
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/writer/delete-proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ export async function verify(body): Promise<any> {
}

export async function action(body): Promise<void> {
const BATCH_SIZE = 1000;

const msg = jsonParse(body.msg);
const proposal = await getProposal(msg.space, msg.payload.proposal);

const voters = await db.queryAsync(`SELECT voter FROM votes WHERE proposal = ?`, [
const voters = await db.queryAsync(`SELECT voter, vp_value FROM votes WHERE proposal = ?`, [
msg.payload.proposal
]);
const id = msg.payload.proposal;
Expand Down Expand Up @@ -53,4 +55,21 @@ export async function action(body): Promise<void> {
}

await db.queryAsync(queries, parameters);

const votersWithVpValue = voters.filter(v => v.vp_value > 0);
if (votersWithVpValue.length > 0) {
for (let i = 0; i < votersWithVpValue.length; i += BATCH_SIZE) {
const batch = votersWithVpValue.slice(i, i + BATCH_SIZE);
const vpQueries = batch
.map(
() =>
`UPDATE leaderboard SET vp_value = GREATEST(vp_value - ?, 0) WHERE user = ? AND space = ?;`
)
.join('\n ');

const vpParams = batch.flatMap(voter => [voter.vp_value, voter.voter, msg.space]);

await db.queryAsync(vpQueries, vpParams);
}
}
}
9 changes: 5 additions & 4 deletions src/writer/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export async function action(body, ipfs, receipt, id, context): Promise<void> {
proposalId,
msg.space,
created,
vpValue,
voter,
msg.space
]
Expand All @@ -196,12 +197,12 @@ export async function action(body, ipfs, receipt, id, context): Promise<void> {
await db.queryAsync(
`
INSERT INTO votes SET ?;
INSERT INTO leaderboard (space, user, vote_count, last_vote)
VALUES(?, ?, 1, ?)
ON DUPLICATE KEY UPDATE vote_count = vote_count + 1, last_vote = ?;
INSERT INTO leaderboard (space, user, vote_count, last_vote, vp_value)
VALUES(?, ?, 1, ?, ?)
ON DUPLICATE KEY UPDATE vote_count = vote_count + 1, last_vote = VALUES(last_vote), vp_value = vp_value + VALUES(vp_value);
UPDATE spaces SET vote_count = vote_count + 1 WHERE id = ?;
`,
[params, msg.space, voter, created, created, msg.space]
[params, msg.space, voter, created, vpValue, msg.space]
);
}

Expand Down
4 changes: 3 additions & 1 deletion test/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,13 @@ CREATE TABLE leaderboard (
vote_count SMALLINT UNSIGNED NOT NULL DEFAULT '0',
proposal_count SMALLINT UNSIGNED NOT NULL DEFAULT '0',
last_vote BIGINT,
vp_value DECIMAL(13,3) NOT NULL DEFAULT 0.000,
PRIMARY KEY user_space (user,space),
INDEX space (space),
INDEX vote_count (vote_count),
INDEX proposal_count (proposal_count),
INDEX last_vote (last_vote)
INDEX last_vote (last_vote),
INDEX vp_value (vp_value)
);

CREATE TABLE skins (
Expand Down