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

return a default options obj with tally when no MKR support #313

Open
wants to merge 1 commit into
base: dev
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
31 changes: 21 additions & 10 deletions packages/dai-plugin-governance/src/GovPollingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,16 +326,27 @@ export default class GovPollingService extends PrivateService {

const numVoters = currentVotes.length;

const resultsObject = currentVotes.reduce((acc, cur) => {
if (acc[cur.optionIdRaw]) {
acc[cur.optionIdRaw] = new BigNumber(acc[cur.optionIdRaw]).plus(
cur.mkrSupport
);
} else {
acc[cur.optionIdRaw] = new BigNumber(cur.mkrSupport);
}
return acc;
}, {});
// We assume all plurality votes have 3 options including abstain.
const pluralityDefaultOptions = {
'0': new BigNumber(0),
'1': new BigNumber(0),
'2': new BigNumber(0)
};

const resultsObject = currentVotes.reduce(
(acc, cur) => {
if (acc[cur.optionIdRaw]) {
acc[cur.optionIdRaw] = new BigNumber(acc[cur.optionIdRaw]).plus(
cur.mkrSupport
);
} else {
acc[cur.optionIdRaw] = new BigNumber(cur.mkrSupport);
}
return acc;
},
// Return a default state if currentVotes is empty.
pluralityDefaultOptions
);

const summedSupport = Object.keys(resultsObject).map(option => ({
optionId: option,
Expand Down
37 changes: 37 additions & 0 deletions packages/dai-plugin-governance/test/GovPollingService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,50 @@ test('plurality tally', async () => {
'1': {
mkrSupport: '700',
winner: true
},
'2': {
mkrSupport: '0',
winner: false
}
}
};

expect(JSON.parse(JSON.stringify(tally))).toEqual(expectedResult);
});

test('plurality tally with NO MKR support', async () => {
govPollingService._getPoll = jest.fn(() => ({
endDate: 123
}));

// Return an empty array when there is no support
govQueryApiService.getMkrSupportRankedChoice = jest.fn(() => []);

const tally = await govPollingService.getTallyPlurality();

const expectedResult = {
winner: '0',
totalMkrParticipation: '0',
numVoters: 0,
options: {
'0': {
mkrSupport: '0',
winner: true
},
'1': {
mkrSupport: '0',
winner: false
},
'2': {
mkrSupport: '0',
winner: false
}
}
};

expect(JSON.stringify(tally)).toEqual(JSON.stringify(expectedResult));
});

test('plurality tally with adjusted votes', async () => {
govQueryApiService.getMkrSupportRankedChoice = jest.fn(
() => dummyMkrGetMkrSupportRCForPluralityDataAdjusted
Expand Down