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

Commit

Permalink
Poll votes by address (#285)
Browse files Browse the repository at this point in the history
* add get vote history by array of addresses from gov polling db

* updates for prod db

* move vote history query into gov polling service

* publish dai-plugin-governance 0.14.1-beta4

* Add poll votes by address

* Check for poll before destructure, add test for address votes query

* dai-plugin-governance-v0.14.1-beta9

Co-authored-by: Phil Bain <[email protected]>
  • Loading branch information
adamgoth and b-pmcg authored Sep 1, 2021
1 parent 9cae0ee commit 2a40aba
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/dai-plugin-governance/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@makerdao/dai-plugin-governance",
"description": "A dai.js plugin for adding MKR governance support to dapps.",
"version": "0.14.1-beta5",
"version": "0.14.1-beta9",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
46 changes: 38 additions & 8 deletions packages/dai-plugin-governance/src/GovPollingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ export default class GovPollingService extends PrivateService {
}

async getMkrAmtVoted(pollId) {
const { endDate } = await this._getPoll(pollId);
const endUnix = Math.floor(endDate / 1000);
const poll = await this._getPoll(pollId);
if (!poll) return null;
const endUnix = Math.floor(poll.endDate / 1000);
const weights = await this.get('govQueryApi').getMkrSupport(
pollId,
endUnix
Expand All @@ -271,8 +272,9 @@ export default class GovPollingService extends PrivateService {
}

async getMkrAmtVotedRankedChoice(pollId) {
const { endDate } = await this._getPoll(pollId);
const endUnix = Math.floor(endDate / 1000);
const poll = await this._getPoll(pollId);
if (!poll) return null;
const endUnix = Math.floor(poll.endDate / 1000);
const weights = await this.get('govQueryApi').getMkrSupportRankedChoice(
pollId,
endUnix
Expand All @@ -282,9 +284,36 @@ export default class GovPollingService extends PrivateService {
);
}

async getMkrAmtVotedByAddress(pollId) {
const poll = await this._getPoll(pollId);
if (!poll) return [];
const endUnix = Math.floor(poll.endDate / 1000);
const results = await this.get('govQueryApi').getMkrSupportByAddress(
pollId,
endUnix
);
if (!results) return [];
const votes = results.map(vote => {
let rankedChoiceOption = null;
if (vote.optionIdRaw) {
const ballotBuffer = toBuffer(vote.optionIdRaw, { endian: 'little' });
const ballot = paddedArray(32 - ballotBuffer.length, ballotBuffer);
rankedChoiceOption = ballot
.reverse()
.filter(choice => choice !== 0 && choice !== '0');
}
return {
...vote,
rankedChoiceOption
};
});
return votes;
}

async getTallyRankedChoiceIrv(pollId) {
const { endDate } = await this._getPoll(pollId);
const endUnix = Math.floor(endDate / 1000);
const poll = await this._getPoll(pollId);
if (!poll) return {};
const endUnix = Math.floor(poll.endDate / 1000);
const votes = await this.get('govQueryApi').getMkrSupportRankedChoice(
pollId,
endUnix
Expand Down Expand Up @@ -323,8 +352,9 @@ export default class GovPollingService extends PrivateService {
}

async getWinningProposal(pollId) {
const { endDate } = await this._getPoll(pollId);
const endUnix = Math.floor(endDate / 1000);
const poll = await this._getPoll(pollId);
if (!poll) return null;
const endUnix = Math.floor(poll.endDate / 1000);
const currentVotes = await this.get('govQueryApi').getMkrSupport(
pollId,
endUnix
Expand Down
16 changes: 16 additions & 0 deletions packages/dai-plugin-governance/src/GovQueryApiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@ export default class QueryApi extends PublicService {
return response.timeToBlockNumber.nodes[0];
}

async getMkrSupportByAddress(pollId, unixTime) {
const query = `{voteAddressMkrWeightsAtTime(argPollId: ${pollId}, argUnix: ${unixTime}){
nodes{
voter
optionId
optionIdRaw
mkrSupport
}
}
}`;

const response = await this.getQueryResponse(this.serverUrl, query);
const results = response.voteAddressMkrWeightsAtTime.nodes;
return results;
}

async getMkrSupportRankedChoice(pollId, unixTime) {
const query = `{voteMkrWeightsAtTimeRankedChoice(argPollId: ${pollId}, argUnix: ${unixTime}){
nodes{
Expand Down
10 changes: 10 additions & 0 deletions packages/dai-plugin-governance/test/GovPollingService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
allOptionsExpect,
dummyAllOptionsMany,
allOptionsManyExpect,
dummyMkrVotedByAddress,
mkrVotedByAddressExpect,
dummyWeight,
dummyNumUnique,
dummyBallotNoMajority,
Expand Down Expand Up @@ -182,6 +184,14 @@ test('getAllOptionsVotingForMany', async () => {
expect(options).toEqual(allOptionsManyExpect);
});

test('getMkrAmtVotedByAddress', async () => {
const mockFn = jest.fn(async () => dummyMkrVotedByAddress);
govQueryApiService.getMkrSupportByAddress = mockFn;
const votes = await govPollingService.getMkrAmtVotedByAddress('101');
expect(mockFn).toBeCalled();
expect(votes).toEqual(mkrVotedByAddressExpect);
});

test('getNumUniqueVoters', async () => {
const mockFn = jest.fn(async () => dummyNumUnique);
govQueryApiService.getNumUniqueVoters = mockFn;
Expand Down
32 changes: 32 additions & 0 deletions packages/dai-plugin-governance/test/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,38 @@ export const allOptionsManyExpect = [
}
];

export const dummyMkrVotedByAddress = [
{
voter: '0x14a4ed2000ca405452c140e21c10b3536c1a98e4',
optionId: 1,
optionIdRaw: '1',
mkrSupport: '239.500000000000000000'
},
{
voter: '0x87e6888935180a9b27a9b48b75c9b779bfec1f76',
optionId: 0,
optionIdRaw: '0',
mkrSupport: '1480.609359492058691716'
}
];

export const mkrVotedByAddressExpect = [
{
voter: '0x14a4ed2000ca405452c140e21c10b3536c1a98e4',
optionId: 1,
optionIdRaw: '1',
mkrSupport: '239.500000000000000000',
rankedChoiceOption: [1]
},
{
voter: '0x87e6888935180a9b27a9b48b75c9b779bfec1f76',
optionId: 0,
optionIdRaw: '0',
mkrSupport: '1480.609359492058691716',
rankedChoiceOption: []
}
];

export const dummyEsmData = [
{
txFrom: '0x16fb96a5fa0427af0c8f7cf1eb4870231c8154b6',
Expand Down

0 comments on commit 2a40aba

Please sign in to comment.