diff --git a/packages/dai-plugin-governance/src/GovPollingService.ts b/packages/dai-plugin-governance/src/GovPollingService.ts index 8fd59ed11..c5373cd16 100644 --- a/packages/dai-plugin-governance/src/GovPollingService.ts +++ b/packages/dai-plugin-governance/src/GovPollingService.ts @@ -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, diff --git a/packages/dai-plugin-governance/test/GovPollingService.test.js b/packages/dai-plugin-governance/test/GovPollingService.test.js index 2a122df3c..abdfaa116 100644 --- a/packages/dai-plugin-governance/test/GovPollingService.test.js +++ b/packages/dai-plugin-governance/test/GovPollingService.test.js @@ -351,6 +351,10 @@ test('plurality tally', async () => { '1': { mkrSupport: '700', winner: true + }, + '2': { + mkrSupport: '0', + winner: false } } }; @@ -358,6 +362,39 @@ test('plurality tally', async () => { 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