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

Commit

Permalink
add voteRankedChoice and getOptionVotingForRankedChoice
Browse files Browse the repository at this point in the history
  • Loading branch information
jparklev committed May 6, 2020
1 parent 1c81b31 commit f088ad1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
24 changes: 23 additions & 1 deletion packages/dai-plugin-governance/src/GovPollingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PrivateService } from '@makerdao/services-core';
import { POLLING } from './utils/constants';
import { MKR } from './utils/constants';
import BigNumber from 'bignumber.js';
import { fromBuffer } from './utils/helpers';

const POSTGRES_MAX_INT = 2147483647;

Expand Down Expand Up @@ -29,6 +30,15 @@ export default class GovPollingService extends PrivateService {
return this._pollingContract().vote(pollId, optionId);
}

voteRankedChoice(pollId, rankings) {
const byteArray = new Uint8Array(32);
rankings.forEach((optionIndex, i) => {
byteArray[byteArray.length - i - 1] = optionIndex + 1;
});
const optionId = fromBuffer(byteArray).toString();
return this._pollingContract().vote(pollId, optionId);
}

_pollingContract() {
return this.get('smartContract').getContractByName(POLLING);
}
Expand Down Expand Up @@ -71,6 +81,13 @@ export default class GovPollingService extends PrivateService {
);
}

async getOptionVotingForRankedChoice(address, pollId) {
return this.get('govQueryApi').getOptionVotingForRankedChoice(
address.toLowerCase(),
pollId
);
}

async getNumUniqueVoters(pollId) {
return this.get('govQueryApi').getNumUniqueVoters(pollId);
}
Expand Down Expand Up @@ -105,7 +122,12 @@ export default class GovPollingService extends PrivateService {
BigNumber(0)
);

const tally = { rounds: 1, winner: null, options: {} };
const tally = {
rounds: 1,
winner: null,
totalMkrParticipation,
options: {}
};
const defaultOptionObj = {
firstChoice: BigNumber(0),
transfer: BigNumber(0),
Expand Down
31 changes: 23 additions & 8 deletions packages/dai-plugin-governance/src/GovQueryApiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ export default class QueryApi extends PublicService {
return response.currentVote.nodes[0].optionId;
}

async getOptionVotingForRankedChoice(address, pollId) {
const query = `{
currentVoteRankedChoice(argAddress: "${address}", argPollId: ${pollId}){
nodes{
optionIdRaw
}
}
}`;
const response = await this.getQueryResponse(this.serverUrl, query);
if (!response.currentVote.nodes[0]) return null;
return response.currentVote.nodes[0].optionId;
}

async getBlockNumber(unixTime) {
const query = `{
timeToBlockNumber(argUnix: ${unixTime}){
Expand All @@ -130,14 +143,16 @@ export default class QueryApi extends PublicService {
}`;
const response = await this.getQueryResponseMemoized(this.serverUrl, query);

return response.voteMkrWeightsAtTimeRankedChoice.nodes.map(vote => {
const ballotBuffer = toBuffer(vote.optionIdRaw, { endian: 'little' });
const ballot = paddedArray(32 - ballotBuffer.length, ballotBuffer);
return {
...vote,
ballot
};
});
return response.voteMkrWeightsAtTimeRankedChoice.nodes
.filter(vote => vote.optionIdRaw !== '0')
.map(vote => {
const ballotBuffer = toBuffer(vote.optionIdRaw, { endian: 'little' });
const ballot = paddedArray(32 - ballotBuffer.length, ballotBuffer);
return {
...vote,
ballot
};
});
}

async getMkrSupport(pollId, unixTime) {
Expand Down
3 changes: 3 additions & 0 deletions packages/dai-plugin-governance/umd/index.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions packages/dai-plugin-governance/umd/index.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
object-assign
(c) Sindre Sorhus
@license MIT
*/

/*!
* The buffer module from node.js, for the browser.
*
* @author Feross Aboukhadijeh <[email protected]> <http://feross.org>
* @license MIT
*/
1 change: 1 addition & 0 deletions packages/dai-plugin-governance/umd/index.js.map

Large diffs are not rendered by default.

0 comments on commit f088ad1

Please sign in to comment.