Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
handle irv tallying the case where there are no more preferences avai…
Browse files Browse the repository at this point in the history
…lable
  • Loading branch information
jparklev committed May 22, 2020
1 parent 0436bbe commit 79361e9
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions packages/dai-plugin-governance/src/GovPollingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,21 @@ export default class GovPollingService extends PrivateService {
tally.rounds++;

// eliminate the weakest candidate
const [optionToEliminate] = Object.entries(tally.options)
.filter(([, optionDetails]) => !optionDetails.eliminated)
.reduce((prv, cur) => {
const [, prvVotes] = prv;
const [, curVotes] = cur;
if (
curVotes.firstChoice
.plus(curVotes.transfer)
.lt(prvVotes.firstChoice.plus(prvVotes.transfer))
)
return cur;
return prv;
});
const filteredOptions = Object.entries(tally.options).filter(
([, optionDetails]) => !optionDetails.eliminated
);

const [optionToEliminate] = filteredOptions.reduce((prv, cur) => {
const [, prvVotes] = prv;
const [, curVotes] = cur;
if (
curVotes.firstChoice
.plus(curVotes.transfer)
.lt(prvVotes.firstChoice.plus(prvVotes.transfer))
)
return cur;
return prv;
});

tally.options[optionToEliminate].eliminated = true;
tally.options[optionToEliminate].transfer = BigNumber(0);
Expand Down Expand Up @@ -208,8 +210,12 @@ export default class GovPollingService extends PrivateService {
}
);

//if there's no more rounds, the winner is the option with the most votes
if (tally.rounds > MAX_ROUNDS && !tally.winner) {
//if there's no more rounds, or if there's only one option that hasn't been eliminated
// the winner is the option with the most votes
if (
(tally.rounds > MAX_ROUNDS && !tally.winner) ||
(filteredOptions.length === 1 && !tally.winner)
) {
let max = BigNumber(0);
let maxOption;
Object.entries(tally.options).forEach(
Expand Down

0 comments on commit 79361e9

Please sign in to comment.