Skip to content

Commit

Permalink
Merge pull request #1321 from decent-dao/fix/cast-snapshot-vote-on-te…
Browse files Browse the repository at this point in the history
…stnet

Fix votes breakdown for weighted voting
  • Loading branch information
mudrila authored Jan 17, 2024
2 parents 3cece78 + 3388682 commit abb5109
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
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

0 comments on commit abb5109

Please sign in to comment.