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

Commit

Permalink
update poll vote fnc to encode option arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
jparklev committed Aug 12, 2020
1 parent 5731edc commit 216caac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/dai-plugin-governance/src/GovPollingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
13 changes: 13 additions & 0 deletions packages/dai-plugin-governance/test/GovPollingService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 216caac

Please sign in to comment.