From 216caacb18918cb08bcb23f9cf8e5b2dd50a6d88 Mon Sep 17 00:00:00 2001 From: Josh Levine Date: Wed, 12 Aug 2020 02:41:13 -0400 Subject: [PATCH] update poll vote fnc to encode option arrays --- .../src/GovPollingService.js | 15 +++++++++++++-- .../test/GovPollingService.test.js | 13 +++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/dai-plugin-governance/src/GovPollingService.js b/packages/dai-plugin-governance/src/GovPollingService.js index 69585af58..e60ab22d3 100644 --- a/packages/dai-plugin-governance/src/GovPollingService.js +++ b/packages/dai-plugin-governance/src/GovPollingService.js @@ -34,11 +34,22 @@ export default class GovPollingService extends PrivateService { return this._pollingContract().withdrawPoll(pollId); } - vote(pollIds, optionIds) { - if (pollIds.length !== optionIds.length || pollIds.length === 0) + vote(pollIds, options) { + if (pollIds.length !== options.length || pollIds.length === 0) throw new Error( 'poll id array and option id array must be the same length and have a non-zero number of elements' ); + + const optionIds = options.map(option => { + if (!Array.isArray(option)) return option; + if (option.length === 1) return option[0]; + const byteArray = new Uint8Array(32); + option.forEach((optionIndex, i) => { + byteArray[byteArray.length - i - 1] = optionIndex; + }); + return fromBuffer(byteArray).toString(); + }); + if (pollIds.length === 1) { const func = 'vote(uint256,uint256)'; return this._batchPollingContract()[func](pollIds[0], optionIds[0]); diff --git a/packages/dai-plugin-governance/test/GovPollingService.test.js b/packages/dai-plugin-governance/test/GovPollingService.test.js index 57bbaf0e2..afcbc9864 100644 --- a/packages/dai-plugin-governance/test/GovPollingService.test.js +++ b/packages/dai-plugin-governance/test/GovPollingService.test.js @@ -95,6 +95,19 @@ test('can vote in batches', async () => { expect(loggedOptionIds[1]).toBe(OPTION_IDS[1]); }); +test('can vote in batches with array options (ranked choice)', async () => { + const POLL_IDS = [0, 1]; + const OPTION_IDS = [3, [1, 4]]; + const txo = await govPollingService.vote(POLL_IDS, OPTION_IDS); + const loggedOptionIds = [ + parseInt(txo.receipt.logs[0].topics[3]), + parseInt(txo.receipt.logs[1].topics[3]) + ]; + // this will fail if the event was not emitted + expect(loggedOptionIds[0]).toBe(OPTION_IDS[0]); + expect(loggedOptionIds[1]).toBe(1025); +}); + test('can withdraw poll', async () => { const POLL_ID = 0; const txo = await govPollingService.withdrawPoll(POLL_ID);