Skip to content

Commit

Permalink
Adds getVotingSettings, getProposal
Browse files Browse the repository at this point in the history
Signed-off-by: emmdim <[email protected]>
  • Loading branch information
emmdim committed Sep 15, 2023
1 parent 295d65d commit 4096b9c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 76 deletions.
97 changes: 43 additions & 54 deletions packages/js-client/src/internal/modules/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import {
CreateGasslessProposalParams,
GasslessVotingProposal,
PrepareInstallationParams,
VocdoniVotingSettings,
vocdoniProposalParams,
} from '../../types';
import { INSTALLATION_ABI } from '../constants';
import { OffchainVotingClientCore } from '../core';
import { IOffchainVotingClientMethods } from '../interfaces';
import { initParamsToContract, toGaslessVotingProposal } from '../utils';
import {
initParamsToContract,
toGaslessVotingProposal,
votingSettingsfromContract,
} from '../utils';
import { GovernanceWrappedERC20__factory } from '@aragon/osx-ethers';
import {
Erc20TokenDetails,
Expand Down Expand Up @@ -259,26 +264,25 @@ export class OffchainVotingClientMethods
* @return {*} {Promise<TokenVotingProposal>}
* @memberof TokenVotingClient
*/
// public async getProposal(
// pluginAddress: string,
// proposalId: number
// // ): Promise<GasslessVotingProposal | null> {
// ): Promise<GasslessVotingProposal | null> {
// const signer = this.web3.getConnectedSigner();

// const gaslessVotingContract = VocdoniVoting__factory.connect(
// pluginAddress,
// signer
// );
// let proposal = gaslessVotingContract.getProposal(proposalId);
public async getProposal(
pluginAddress: string,
proposalId: number
// ): Promise<GasslessVotingProposal | null> {
): Promise<GasslessVotingProposal | null> {
const signer = this.web3.getConnectedSigner();

// if (!proposal) {
// return null;
const gaslessVotingContract = VocdoniVoting__factory.connect(
pluginAddress,
signer
);
let proposal = await gaslessVotingContract.getProposal(proposalId);

// // TODO
// return toGaslessVotingProposal(proposal);
// }
// }
if (!proposal) {
return null;
}
// TODO
return toGaslessVotingProposal(proposal);
}

/**
* Returns a list of proposals on the Plugin, filtered by the given criteria
Expand Down Expand Up @@ -380,41 +384,26 @@ export class OffchainVotingClientMethods
* @return {*} {Promise<VotingSettings>}
* @memberof TokenVotingClient
*/
// public async getVotingSettings(
// pluginAddress: string,
// blockNumber?: number
// ): Promise<VotingSettings | null> {
// if (!isAddress(pluginAddress)) {
// throw new InvalidAddressError();
// }
// const query = QueryTokenVotingSettings;
// const params = {
// address: pluginAddress.toLowerCase(),
// block: blockNumber ? { number: blockNumber } : null,
// };
// const name = 'TokenVoting settings';
// type T = { tokenVotingPlugin: SubgraphVotingSettings };
// const { tokenVotingPlugin } = await this.graphql.request<T>({
// query,
// params,
// name,
// });
// if (!tokenVotingPlugin) {
// return null;
// }
// return {
// minDuration: parseInt(tokenVotingPlugin.minDuration),
// supportThreshold: decodeRatio(
// BigInt(tokenVotingPlugin.supportThreshold),
// 6
// ),
// minParticipation: decodeRatio(
// BigInt(tokenVotingPlugin.minParticipation),
// 6
// ),
// minProposerVotingPower: BigInt(tokenVotingPlugin.minProposerVotingPower),
// votingMode: tokenVotingPlugin.votingMode,
// };
public async getVotingSettings(
pluginAddress: string,
blockNumber?: number
): Promise<VocdoniVotingSettings | null> {
if (!isAddress(pluginAddress)) {
throw new InvalidAddressError();
}
const signer = this.web3.getConnectedSigner();

const gaslessVotingContract = VocdoniVoting__factory.connect(
pluginAddress,
signer
);
if (!gaslessVotingContract) {
return null;
}
let params = blockNumber ? { blockTag: blockNumber || 0 } : {};
const settings = await gaslessVotingContract.getPluginSettings(params);
return votingSettingsfromContract(settings);
}

/**
* Returns the details of the token used in a specific plugin instance
Expand Down
47 changes: 31 additions & 16 deletions packages/js-client/src/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { BigNumber } from '@ethersproject/bignumber';
import { AddressZero } from '@ethersproject/constants';
import { Contract } from '@ethersproject/contracts';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { VocdoniVoting } from '@vocdoni/offchain-voting-ethers';

// export function votingModeFromContracts(votingMode: number): VotingMode {
// switch (votingMode) {
Expand Down Expand Up @@ -44,19 +45,34 @@ export function mintTokenParamsFromContract(result: Result): MintTokenParams {

export function votingSettingsToContract(
params: VocdoniVotingSettings
): [number, number, BigNumber, BigNumber, number, string, string, boolean] {
): [boolean, number, number, number, BigNumber, string, BigNumber, string] {
return [
true,
params.minTallyApprovals,
params.minDuration,
params.minParticipation,
params.supportThreshold,
params.minDuration,
'',
params.minProposerVotingPower,
params.censusStrategy,
'',
true,
];
}

export function votingSettingsfromContract(
settings: VocdoniVoting.PluginSettingsStructOutput
): VocdoniVotingSettings {
return {
onlyCommitteeProposalCreation: settings[0],
minTallyApprovals: settings[1],
minParticipation: settings[2],
supportThreshold: settings[3],
minDuration: settings[4],
daoTokenAddress: settings[5],
minProposerVotingPower: settings[6],
censusStrategy: settings[7],
};
}

export function initParamsToContract(params: OffchainVotingPluginInstall) {
let token: [string, string, string] = ['', '', ''];
let balances: [string[], BigNumber[]] = [[], []];
Expand Down Expand Up @@ -147,35 +163,34 @@ export async function voteWithSigners(
export function toGaslessVotingProposal(
proposal: ProposalFromSC
): GasslessVotingProposal {
const startDate = new Date(proposal.parameters.startDate);
const endDate = new Date(proposal.parameters.endDate);
const expirationDate = new Date(proposal.parameters.expirationDate);
proposal.parameters.startDate;
const startDate = new Date(proposal.parameters.startDate.toString());
const endDate = new Date(proposal.parameters.endDate.toString());
const expirationDate = new Date(
proposal.parameters.expirationDate.toString()
);
return {
executed: proposal.executed,
// TODO FIX
// approvers: proposal.approvals,
approvers: [],
vochainProposalId: proposal.vochainProposalId,
parameters: {
censusBlock: proposal.parameters.censusBlock,
censusBlock: proposal.parameters.censusBlock.toNumber(),
securityBlock: 0,
startDate,
endDate,
expirationDate,
},
//TODO to number
allowFailureMap: 0,
// allowFailureMap: proposal.allowFailureMap,
// TODO to number[][]
tally: [[1, 2, 3]],
// tally: proposal.tally,
allowFailureMap: proposal.allowFailureMap.toNumber(),
tally: proposal.tally.map((int) => {
return int.map((x) => x.toNumber());
}),
actions: proposal.actions.map((action): DaoAction => {
return {
data: hexToBytes(action.data),
to: action.to,
//TODO to bigint
value: BigInt(action.value.toBigInt()),
// value: action.value,
};
}),
};
Expand Down
12 changes: 6 additions & 6 deletions packages/js-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ export const ONE_YEAR = 365 * ONE_DAY;

export type VocdoniVotingSettings = {
minTallyApprovals: number;
minDuration: number;
minParticipation: BigNumber;
supportThreshold: BigNumber;
minProposerVotingPower: number;
minDuration: BigNumber;
minParticipation: number;
supportThreshold: number;
minProposerVotingPower: BigNumber;
censusStrategy: string;
daoTokenAddress: string;
onlyCommitteeProposalCreation?: boolean;
Expand Down Expand Up @@ -205,9 +205,9 @@ export const UPDATE_INFOS: { [index: string]: UpdateInfo } = {

export type ProposalFromSC = {
executed: boolean;
approvals: number;
approvers: string[];
vochainProposalId: string;
parameters: vocdoniProposalParams;
parameters: VocdoniVoting.ProposalParametersStructOutput;
allowFailureMap: BigNumber;
tally: BigNumber[][];
actions: IDAO.ActionStructOutput[];
Expand Down

0 comments on commit 4096b9c

Please sign in to comment.