Skip to content
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

Fix votes breakdown for weighted voting #1321

Merged
merged 2 commits into from
Jan 17, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function SnapshotProposalVotes({ proposal }: ISnapshotProposalVot
>
{choices.map((choice, i) => {
const votesBreakdownChoice =
type === 'weighted' ? votesBreakdown[i] : votesBreakdown[choice];
type === 'weighted' ? votesBreakdown[i + 1] : votesBreakdown[choice];
const votesBreakdownChoiceTotal =
votesBreakdownChoice && votesBreakdownChoice?.total
? votesBreakdownChoice?.total
Expand Down
25 changes: 14 additions & 11 deletions src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u

if (type === 'weighted') {
Object.keys(choices).forEach((choice: string, choiceIndex) => {
votesBreakdown[choiceIndex] = {
votesBreakdown[choiceIndex + 1] = {
votes: [],
total: 0,
};
Expand All @@ -136,7 +136,7 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
if (type === 'weighted') {
const voteChoices = vote.choice as SnapshotWeightedVotingChoice;
if (typeof voteChoices === 'number') {
// Means vote casted for single option, and Snapshot API returns just just number then =/
// Means vote casted for single option, and Snapshot API returns just number then =/
const voteChoice = voteChoices - 1;
const existingChoiceType = votesBreakdown[voteChoice];
if (existingChoiceType) {
Expand All @@ -146,24 +146,27 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
};
} else {
votesBreakdown[voteChoice] = {
total: voteChoices[voteChoice],
total: vote.votingWeight,
votes: [vote],
};
}
} else {
let totalVotingWeightDistributon = 0;
Object.keys(voteChoices).forEach(
(choice: any) => (totalVotingWeightDistributon += voteChoices[choice])
);
Object.keys(voteChoices).forEach((choiceIndex: any) => {
// In Snapshot API choices are indexed 1-based. The first choice has index 1.
// https://docs.snapshot.org/tools/api#vote
const voteChoice = choices[choiceIndex - 1];
const existingChoiceType = votesBreakdown[voteChoice];
const voteChoiceValue = voteChoices[choiceIndex] / totalVotingWeightDistributon;
const existingChoiceType = votesBreakdown[choiceIndex];

if (existingChoiceType) {
votesBreakdown[voteChoice] = {
total: existingChoiceType.total + vote.votingWeight,
votesBreakdown[choiceIndex] = {
total: existingChoiceType.total + vote.votingWeight * voteChoiceValue,
votes: [...existingChoiceType.votes, vote],
};
} else {
votesBreakdown[voteChoice] = {
total: vote.votingWeight,
votesBreakdown[choiceIndex] = {
total: vote.votingWeight * voteChoiceValue,
votes: [vote],
};
}
Expand Down
Loading