Skip to content

Commit

Permalink
Implements getVotingSettings from subgraph
Browse files Browse the repository at this point in the history
Signed-off-by: emmdim <[email protected]>
  • Loading branch information
emmdim committed Nov 14, 2023
1 parent fa58676 commit 2c7143f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { gql } from 'graphql-request';

export const QueryPluginSettings = gql`
query PluginSettings($address: String!, $block: Block_height) {
plugins(address: $address, block: $block) {
plugins(block: $block, where: { address: $address}) {
id
onlyExecutionMultisigProposalCreation
minTallyApprovals
Expand Down
80 changes: 57 additions & 23 deletions packages/js-client/src/internal/modules/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../../types';
import { INSTALLATION_ABI } from '../constants';
import { GaslessVotingClientCore } from '../core';
import { QueryPluginSettings } from '../graphql-queries';
import { IGaslessVotingClientMethods } from '../interfaces';
import {
initParamsToContract,
Expand All @@ -30,10 +31,10 @@ import {
TokenVotingMember,
} from '@aragon/sdk-client';
import {
findLog,
findLog,
InvalidAddressOrEnsError,
NoProviderError,
prepareGenericInstallation,
prepareGenericInstallation,
PrepareInstallationStepValue,
ProposalMetadata,
SupportedNetwork,
Expand All @@ -52,10 +53,13 @@ prepareGenericInstallation,
SortDirection,
} from '@aragon/sdk-client-common';
import { isAddress } from '@ethersproject/address';
// import { Wallet } from '@ethersproject/wallet';
import { VocdoniVoting__factory } from '@vocdoni/gasless-voting-ethers';
import { ErrElectionNotFound, ElectionAPI } from '@vocdoni/sdk';
import axios from 'axios';

// import { providers } from 'ethers';

export class GaslessVotingClientMethods
extends GaslessVotingClientCore
implements IGaslessVotingClientMethods
Expand Down Expand Up @@ -119,15 +123,14 @@ export class GaslessVotingClientMethods
const endTimestamp = params.endDate?.getTime() || 0;
const minTallyDurationTimestamp = params.tallyEndDate?.getTime() || 0;


const votingParams: GaslessProposalParametersContractStruct = {
startDate: BigInt(Math.round(startTimestamp / 1000)),
voteEndDate: BigInt(Math.round(endTimestamp / 1000)),
tallyEndDate: BigInt(Math.round(minTallyDurationTimestamp / 1000)),
securityBlock: BigInt(0),
totalVotingPower: params.totalVotingPower,
censusURI: params.censusURI,
censusRoot: hexToBytes(params.censusRoot)
censusRoot: hexToBytes(params.censusRoot),
};
const tx = await gaslessVotingContract.createProposal(
// toUtf8Bytes(params.metadataUri),
Expand Down Expand Up @@ -258,7 +261,7 @@ export class GaslessVotingClientMethods
public async getProposal(
proposalId: string,
daoName?: string,
daoAddress?: string,
daoAddress?: string
): Promise<GaslessVotingProposal | null> {
try {
const { pluginAddress, id } = decodeProposalId(proposalId);
Expand Down Expand Up @@ -286,7 +289,10 @@ export class GaslessVotingClientMethods
const vochainProposal = await this.vocdoniSDK.fetchElection(
parsedSCProposal.vochainProposalId
);
const votesList = await ElectionAPI.votesList(this.vocdoniSDK.url,parsedSCProposal.vochainProposalId)
const votesList = await ElectionAPI.votesList(
this.vocdoniSDK.url,
parsedSCProposal.vochainProposalId
);
const voters = votesList.votes.map((vote) => vote.voterID);

const census3token = await this.vocdoniCensus3.getToken(
Expand All @@ -301,7 +307,7 @@ export class GaslessVotingClientMethods
census3token,
voters,
daoName,
daoAddress,
daoAddress
);
} catch (error) {
if (error instanceof ErrElectionNotFound) return null;
Expand All @@ -319,12 +325,11 @@ export class GaslessVotingClientMethods
public async getProposals({
daoAddressOrEns,
pluginAddress,
skip= 0,
skip = 0,
limit = 10,
status=undefined,
status = undefined,
direction = SortDirection.ASC,
}:
// sortBy = ProposalSortBy.CREATED_AT,
}: // sortBy = ProposalSortBy.CREATED_AT,
ProposalQueryParams & { pluginAddress: string }): Promise<
GaslessVotingProposal[]
> {
Expand Down Expand Up @@ -358,14 +363,14 @@ export class GaslessVotingClientMethods
proposal = await this.getProposal(
encodeProposalId(pluginAddress, id),
daoAddressOrEns || '',
address || '',
address || ''
);
if (proposal) proposals.push(proposal);
id += 1;
} while ((proposal != null) && (id < limit)) ;
} while (proposal != null && id < limit);
if (direction == SortDirection.DESC) proposals.reverse();
if (status) {
return proposals.filter(prop => prop.status == status)
return proposals.filter((prop) => prop.status == status);
}
return proposals;
}
Expand All @@ -385,6 +390,40 @@ export class GaslessVotingClientMethods
if (!isAddress(pluginAddress)) {
Promise.reject(new InvalidAddressError());
}

const query = QueryPluginSettings;
const params = {
address: pluginAddress.toLowerCase(),
block: blockNumber ? { number: blockNumber } : null,
};
const name = 'GaslessVoting settings';
type T = { plugins: GaslessPluginVotingSettings[] };
const { plugins } = await this.graphql.request<T>({
query,
params,
name,
});
if (!plugins.length) {
return null;
}
return plugins[0];
}

/**
* Returns the settings of a plugin given the address of the plugin instance
*
* @param {string} pluginAddress
* @param {number} blockNumber
* @return {*} {Promise<VotingSettings>}
* @memberof GaslessVotingClientMethods
*/
public async getVotingSettings2(
pluginAddress: string,
blockNumber?: number
): Promise<GaslessPluginVotingSettings | null> {
if (!isAddress(pluginAddress)) {
Promise.reject(new InvalidAddressError());
}
const signer = this.web3.getConnectedSigner();

const gaslessVotingContract = VocdoniVoting__factory.connect(
Expand Down Expand Up @@ -488,18 +527,16 @@ export class GaslessVotingClientMethods
* @memberof GaslessVotingClientMethods
*/
public async approve(
proposalId: string,
proposalId: string
): Promise<AsyncGenerator<ApproveTallyStepValue>> {
const signer = this.web3.getConnectedSigner();


const { pluginAddress, id } = decodeProposalId(proposalId);
if (!isAddress(pluginAddress)) {
Promise.reject(new InvalidAddressError());
}
if (isNaN(id)) Promise.reject(new InvalidProposalIdError());


let isMultisigMember = await this.isMultisigMember(
pluginAddress,
await signer.getAddress()
Expand Down Expand Up @@ -594,10 +631,7 @@ export class GaslessVotingClientMethods
signer
);

const tx = await gaslessVotingContract.approveTally(
id,
tryExecution
);
const tx = await gaslessVotingContract.approveTally(id, tryExecution);

yield {
key: ApproveTallyStep.EXECUTING,
Expand Down Expand Up @@ -626,11 +660,11 @@ export class GaslessVotingClientMethods
if (isNaN(id)) Promise.reject(new InvalidProposalIdError());
const signer = this.web3.getConnectedSigner();

const tokenVotingContract = VocdoniVoting__factory.connect(
const gaslessVotingContract = VocdoniVoting__factory.connect(
pluginAddress,
signer
);
const tx = await tokenVotingContract.executeProposal(id);
const tx = await gaslessVotingContract.executeProposal(id);

yield {
key: ExecuteProposalStep.EXECUTING,
Expand Down
5 changes: 4 additions & 1 deletion packages/js-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ export type GaslessPluginVotingSettings = {
censusStrategy: string;
daoTokenAddress?: string; // calculated during the DAO installation
onlyExecutionMultisigProposalCreation?: boolean;
id?: string;
executionMultisigMembers?: string[];
};

export type GaslessProposalParametersStruct = {
securityBlock?: number; // calculated internally in the smart contract
startDate?: Date; // UNIX timestamp (ms)
Expand Down Expand Up @@ -182,7 +185,7 @@ export type GaslessVotingProposal = ProposalBase & {
decimals: number;
type: string;
};
voters?: string[];
voters?: string[];
};

export type CreateGasslessProposalParams = CreateProposalBaseParams &
Expand Down

0 comments on commit 2c7143f

Please sign in to comment.