Skip to content

Commit

Permalink
* Implements pinMetadata
Browse files Browse the repository at this point in the history
* Implements first version of updatePluginSettingsAction for encoding and
decoding
* Minor fixes
  • Loading branch information
emmdim committed Oct 12, 2023
1 parent 8f0bbcd commit 6c52945
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 30 deletions.
7 changes: 7 additions & 0 deletions packages/js-client/src/internal/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
PrepareInstallationStepValue,
DaoAction,
InterfaceParams,
ProposalMetadata,
} from '@aragon/sdk-client-common';

export interface IOffchainVotingClient {
Expand Down Expand Up @@ -87,6 +88,7 @@ export interface IOffchainVotingClientMethods {
pluginAddress: string,
proposalId: number
): AsyncGenerator<ExecuteProposalStepValue>;
pinMetadata(params: ProposalMetadata): Promise<string>;
}
export interface IOffchainVotingClientEstimation {
// prepareInstallation(
Expand All @@ -111,10 +113,15 @@ export interface IOffchainVotingClientEncoding {
minterAddress: string,
params: MintTokenParams
) => DaoAction;
updatePluginSettingsAction(
pluginAddress: string,
params: GaslessPluginVotingSettings
): DaoAction;
}
export interface IOffchainVotingClientDecoding {
// Fill with methods that encode actions that can be passed to a proposal
// encodeAction(data: Uint8Array): params;
findInterface(data: Uint8Array): InterfaceParams | null;
updatePluginSettingsAction(data: Uint8Array): GaslessPluginVotingSettings;
mintTokenAction(data: Uint8Array): MintTokenParams;
}
34 changes: 30 additions & 4 deletions packages/js-client/src/internal/modules/decoding.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { GaslessPluginVotingSettings } from '../../types';
import { AVAILABLE_FUNCTION_SIGNATURES } from '../constants';
import { OffchainVotingClientCore } from '../core';
import { IOffchainVotingClientDecoding } from '../interfaces';
import { mintTokenParamsFromContract } from '../utils';
import {
mintTokenParamsFromContract,
votingSettingsfromContract,
} from '../utils';
import { IERC20MintableUpgradeable__factory } from '@aragon/osx-ethers';
import { MintTokenParams } from '@aragon/sdk-client';
import {
InterfaceParams,
getFunctionFragment,
} from '@aragon/sdk-client-common';
import { bytesToHex } from '@aragon/sdk-common';
import {
VocdoniVoting,
VocdoniVoting__factory,
} from '@vocdoni/offchain-voting-ethers';

export class OffchainVotingClientDecoding
extends OffchainVotingClientCore
Expand All @@ -22,9 +30,27 @@ export class OffchainVotingClientDecoding
* @return {*} {VotingSettings}
* @memberof OffchainVotingClientDecoding
*/
// public updatePluginSettingsAction(data: Uint8Array): VotingSettings {
// return decodeUpdatePluginSettingsAction(data);
// }
public updatePluginSettingsAction(
data: Uint8Array
): GaslessPluginVotingSettings {
return this.decodeUpdatePluginSettingsAction(data);
}

private decodeUpdatePluginSettingsAction(
data: Uint8Array
): GaslessPluginVotingSettings {
const votingInterface = VocdoniVoting__factory.createInterface();
const hexBytes = bytesToHex(data);
const expectedfunction = votingInterface.getFunction(
'updatePluginSettings'
);
const result = votingInterface.decodeFunctionData(
expectedfunction,
hexBytes
) as VocdoniVoting.PluginSettingsStructOutput;

return votingSettingsfromContract(result);
}

/**
* Decodes the mint token params from an encoded mint token action
Expand Down
68 changes: 51 additions & 17 deletions packages/js-client/src/internal/modules/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import metadata from '../../../../contracts/src/build-metadata.json';
import { OffchainVotingPluginInstall } from '../../types';
import {
GaslessPluginVotingSettings,
OffchainVotingPluginInstall,
} from '../../types';
import { DEFAULT_ADDRESSES } from '../constants';
import { OffchainVotingClientCore } from '../core';
import { IOffchainVotingClientEncoding } from '../interfaces';
import { mintTokenParamsToContract, initParamsToContract } from '../utils';
import {
mintTokenParamsToContract,
initParamsToContract,
gaslessVotingSettingsToContract,
} from '../utils';
import { IERC20MintableUpgradeable__factory } from '@aragon/osx-ethers';
import { MintTokenParams } from '@aragon/sdk-client';
import {
Expand All @@ -21,6 +28,7 @@ import {
import { defaultAbiCoder } from '@ethersproject/abi';
import { isAddress } from '@ethersproject/address';
import { Networkish, getNetwork } from '@ethersproject/providers';
import { VocdoniVoting__factory } from '@vocdoni/offchain-voting-ethers';

const prepareInstallationDataTypes = getNamedTypesFromMetadata(
metadata.pluginSetup.prepareInstallation.inputs
Expand Down Expand Up @@ -64,24 +72,50 @@ export class OffchainVotingClientEncoding
* Computes the parameters to be given when creating a proposal that updates the governance configuration
*
* @param {string} pluginAddress
* @param {VotingSettings} params
* @param {GaslessPluginVotingSettings} params
* @return {*} {DaoAction}
* @memberof OffchainVotingClientEncoding
*/
// public updatePluginSettingsAction(
// pluginAddress: string,
// params: VotingSettings
// ): DaoAction {
// if (!isAddress(pluginAddress)) {
// throw new InvalidAddressError();
// }
// // TODO: check if to and value are correct
// return {
// to: pluginAddress,
// value: BigInt(0),
// data: encodeUpdateVotingSettingsAction(params),
// };
// }
public updatePluginSettingsAction(
pluginAddress: string,
params: GaslessPluginVotingSettings
): DaoAction {
if (!isAddress(pluginAddress)) {
throw new InvalidAddressError();
}
// TODO: check if to and value are correct
return {
to: pluginAddress,
value: BigInt(0),
data: this.encodeUpdateVotingSettingsAction(params),
};
}

private encodeUpdateVotingSettingsAction(
params: GaslessPluginVotingSettings
): Uint8Array {
const votingInterface = VocdoniVoting__factory.createInterface();
const args = gaslessVotingSettingsToContract(params);
// get hex bytes
const hexBytes = votingInterface.encodeFunctionData(
'updatePluginSettings',
[
{
onlyCommitteeProposalCreation: args[0],
minTallyApprovals: args[1],
minParticipation: args[2],
supportThreshold: args[3],
minDuration: args[4],
expirationTime: args[5],
daoTokenAddress: args[6],
minProposerVotingPower: args[7],
censusStrategy: args[8],
},
]
);
// Strip 0x => encode in Uint8Array
return hexToBytes(hexBytes);
}

/**
* Computes the parameters to be given when creating a proposal that mints an amount of ERC-20 tokens to an address
Expand Down
8 changes: 5 additions & 3 deletions packages/js-client/src/internal/modules/estimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ export class OffchainVotingClientEstimation

const votingParams: GaslessProposalParametersContractStruct = {
censusBlock: [] as string[],
startDate: BigInt(params.startDate / 1000),
endDate: BigInt(params.endDate / 1000),
startDate: params.startDate
? BigInt(Math.floor(params.startDate / 1000))
: BigInt(0),
endDate: BigInt(Math.floor(params.endDate / 1000)),
expirationDate: params.expirationDate
? BigInt(params.expirationDate / 1000)
? BigInt(Math.floor(params.expirationDate / 1000))
: BigInt(0),
securityBlock: BigInt(0),
};
Expand Down
25 changes: 22 additions & 3 deletions packages/js-client/src/internal/modules/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ import {
findLog,
prepareGenericInstallation,
PrepareInstallationStepValue,
ProposalMetadata,
SupportedNetwork,
SupportedNetworksArray,
TokenType,
} from '@aragon/sdk-client-common';
import {
InvalidAddressError,
InvalidProposalIdError,
IpfsPinError,
ProposalCreationError,
SizeMismatchError,
UnsupportedNetworkError,
Expand Down Expand Up @@ -105,10 +107,10 @@ export class OffchainVotingClientMethods
const allowFailureMap = boolArrayToBitmap(params.failSafeActions);
const votingParams: GaslessProposalParametersContractStruct = {
censusBlock: [] as string[],
startDate: BigInt(params.startDate / 1000),
endDate: BigInt(params.endDate / 1000),
startDate: BigInt(Math.floor(params.startDate / 1000)),
endDate: BigInt(Math.floor(params.endDate / 1000)),
expirationDate: params.expirationDate
? BigInt(params.expirationDate / 1000)
? BigInt(Math.floor(params.expirationDate / 1000))
: BigInt(0),
securityBlock: BigInt(0),
};
Expand Down Expand Up @@ -636,4 +638,21 @@ export class OffchainVotingClientMethods

// return tokenVotingContract.canExecute(id);
// }

/**
* Pins a metadata object into IPFS and retruns the generated hash
*
* @param {ProposalMetadata} params
* @return {*} {Promise<string>}
* @memberof ClientMethods
*/
public async pinMetadata(params: ProposalMetadata): Promise<string> {
try {
const cid = await this.ipfs.add(JSON.stringify(params));
await this.ipfs.pin(cid);
return `ipfs://${cid}`;
} catch (e) {
throw new IpfsPinError(e);
}
}
}
7 changes: 4 additions & 3 deletions packages/js-client/src/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function mintTokenParamsFromContract(result: Result): MintTokenParams {
* - The minimum voting power required for a proposer to create a proposal.
* - The census strategy used for the proposal.
*/
export function createProposalVotingSettingsToContract(
export function gaslessVotingSettingsToContract(
params: GaslessPluginVotingSettings
): [
boolean,
Expand All @@ -82,7 +82,7 @@ export function createProposalVotingSettingsToContract(
string
] {
return [
true,
params.onlyCommitteeProposalCreation || true,
params.minTallyApprovals,
encodeRatio(params.minParticipation, 6),
encodeRatio(params.supportThreshold, 6),
Expand Down Expand Up @@ -138,9 +138,10 @@ export function initParamsToContract(params: OffchainVotingPluginInstall) {
params.useToken.wrappedToken.symbol,
];
}
params.votingSettings.onlyCommitteeProposalCreation = true;
return [
params.committee,
createProposalVotingSettingsToContract(params.votingSettings),
gaslessVotingSettingsToContract(params.votingSettings),
token,
balances,
];
Expand Down

0 comments on commit 6c52945

Please sign in to comment.