From 882e63e5c52d4267bbc034d47d84ba353f0923b1 Mon Sep 17 00:00:00 2001 From: Kirill Klimenko Date: Wed, 17 Jan 2024 14:38:07 +0100 Subject: [PATCH] Fix votes breakdown --- .../SnapshotProposalVotes.tsx | 2 +- .../loaders/snapshot/useSnapshotProposal.ts | 25 +++++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/components/Proposals/SnapshotProposalDetails/SnapshotProposalVotes.tsx b/src/components/Proposals/SnapshotProposalDetails/SnapshotProposalVotes.tsx index 76e81239b8..bb63523b6a 100644 --- a/src/components/Proposals/SnapshotProposalDetails/SnapshotProposalVotes.tsx +++ b/src/components/Proposals/SnapshotProposalDetails/SnapshotProposalVotes.tsx @@ -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 diff --git a/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts b/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts index f8cdcbe648..f42d265327 100644 --- a/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts +++ b/src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts @@ -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, }; @@ -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) { @@ -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], }; }