Skip to content

Commit

Permalink
first sdk commit
Browse files Browse the repository at this point in the history
Signed-off-by: emmdim <[email protected]>
  • Loading branch information
emmdim committed Sep 7, 2023
1 parent e865947 commit 8714eb1
Show file tree
Hide file tree
Showing 9 changed files with 970 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/contracts-ethers/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@aragon/simple-storage-ethers",
"name": "@vocdoni/offhcain-voting-ethers",
"version": "1.0.0",
"description": "Plugin contract definitions for ethers.js",
"main": "dist/bundle-cjs.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/js-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@
"@aragon/osx-ethers": "1.3.0-rc0",
"@aragon/sdk-client-common": "1.2.0-rc0",
"@aragon/sdk-common": "1.5.0",
"@aragon/simple-storage-ethers": "1.0.0",
"@ethersproject/abstract-signer": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/constants": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@ethersproject/wallet": "^5.7.0",
"graphql": "^16.6.0",
"graphql-request": "4.3.0"
"graphql-request": "4.3.0",
"@vocdoni/offhcain-voting-ethers": "*"
},
"jest": {
"testEnvironment": "./test-environment.js",
Expand Down
24 changes: 24 additions & 0 deletions packages/js-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import {
OffchainVotingClientMethods,
} from './internal';
import { OffchainVotingClientCore } from './internal/core';
import { TokenVotingPluginInstall } from '@aragon/sdk-client';
import {
ClientCore,
Context,
PluginInstallItem,
} from '@aragon/sdk-client-common';
import { Networkish } from '@ethersproject/providers';

export class OffchainVotingClient
extends OffchainVotingClientCore
Expand All @@ -28,4 +35,21 @@ export class OffchainVotingClient
this.encoding = new OffchainVotingClientEncoding(pluginContext);
this.decoding = new OffchainVotingClientDecoding(pluginContext);
}

static encoding = {
/**
* Computes the parameters to be given when creating the DAO,
* so that the plugin is configured
*
* @param {TokenVotingPluginInstall} params
* @param {Networkish} [network="mainnet"]
* @return {*} {PluginInstallItem}
* @memberof OffchainVotingClient
*/
getPluginInstallItem: (
params: TokenVotingPluginInstall,
network: Networkish
): PluginInstallItem =>
OffchainVotingClientEncoding.getPluginInstallItem(params, network),
};
}
109 changes: 107 additions & 2 deletions packages/js-client/src/internal/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,110 @@
export const DEFAULT_OFFCHAIN_VOTING_REPO_ADDRESS = "0x0000000000000000000000000000000000000000";
// import { MetadataAbiInput } from './types';
import { IERC20MintableUpgradeable__factory } from '@aragon/osx-ethers';
import { MetadataAbiInput } from '@aragon/sdk-client-common';
import { VocdoniVoting__factory } from '@vocdoni/offhcain-voting-ethers';

export const DEFAULT_OFFCHAIN_VOTING_REPO_ADDRESS =
'0x0000000000000000000000000000000000000000';

export const DEFAULT_OFFCHAIN_VOTING_BACKEND_URL =
"https://example.otg/offchain-voting/rpc";
'https://example.otg/offchain-voting/rpc';

const majorityVotingInterface = VocdoniVoting__factory.createInterface();

export const AVAILABLE_FUNCTION_SIGNATURES: string[] = [
VocdoniVoting__factory.createInterface()
.getFunction('updatePluginSettings')
.format('minimal'),
IERC20MintableUpgradeable__factory.createInterface()
.getFunction('mint')
.format('minimal'),
];

export const FAILING_PROPOSAL_AVAILABLE_FUNCTION_SIGNATURES = [
majorityVotingInterface.getFunction('updatePluginSettings').format('minimal'),
];

export const INSTALLATION_ABI: MetadataAbiInput[] = [
{
components: [
{
internalType: 'bool',
name: 'onlyCommitteeProposalCreation',
type: 'bool',
description: '',
},
{
internalType: 'uint16',
name: 'minTallyApprovals',
type: 'uint16',
description: '',
},
{
internalType: 'uint32',
name: 'minParticipation',
type: 'uint32',
description: '',
},
{
internalType: 'uint32',
name: 'supportThreshold',
type: 'uint32',
description: '',
},
{
internalType: 'uint64',
name: 'minDuration',
type: 'uint64',
description: '',
},
{
internalType: 'address',
name: 'daoTokenAddress',
type: 'address',
description: '',
},
{
internalType: 'uint256',
name: 'minProposerVotingPower',
type: 'uint256',
description: '',
},
{
internalType: 'string',
name: 'censusStrategy',
type: 'string',
description: '',
},
],
internalType: 'struct VocdoniVoting.PluginSettings',
name: 'pluginSettings',
type: 'tuple',
description: '',
},
{
components: [
{
internalType: 'address',
name: 'to',
type: 'address',
description: '',
},
{
internalType: 'uint256',
name: 'value',
type: 'uint256',
description: '',
},
{
internalType: 'bytes',
name: 'data',
type: 'bytes',
description: '',
},
],
internalType: 'struct IDAO.Action[]',
name: 'actions',
type: 'tuple[]',
description: '',
},
];
25 changes: 21 additions & 4 deletions packages/js-client/src/internal/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { PrepareInstallationParams } from "../types";
import { PrepareInstallationParams } from '../types';
import {
VotingSettings,
MintTokenParams,
TokenVotingPluginPrepareInstallationParams,
} from '@aragon/sdk-client';
import {
GasFeeEstimation,
PrepareInstallationStepValue,
} from "@aragon/sdk-client-common";
DaoAction,
} from '@aragon/sdk-client-common';

export interface IOffchainVotingClient {
methods: IOffchainVotingClientMethods;
Expand All @@ -18,13 +24,13 @@ export interface IOffchainVotingClientMethods {
// the version of the plugin, the dao address and the plugin
// repo if its not specified in the state of the client
prepareInstallation(
params: PrepareInstallationParams,
params: TokenVotingPluginPrepareInstallationParams
): AsyncGenerator<PrepareInstallationStepValue>;
// Add any methods that you need
}
export interface IOffchainVotingClientEstimation {
prepareInstallation(
params: PrepareInstallationParams,
params: PrepareInstallationParams
): Promise<GasFeeEstimation>;
// Add any estimation methods that you need
}
Expand All @@ -36,3 +42,14 @@ export interface IOffchainVotingClientDecoding {
// Fill with methods that encode actions that can be passed to a proposal
// encodeAction(data: Uint8Array): params;
}

export interface ITokenVotingClientEncoding {
updatePluginSettingsAction: (
pluginAddress: string,
params: VotingSettings
) => DaoAction;
mintTokenAction: (
minterAddress: string,
params: MintTokenParams
) => DaoAction;
}
129 changes: 124 additions & 5 deletions packages/js-client/src/internal/modules/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,126 @@
import { OffchainVotingClientCore } from "../core";
import { IOffchainVotingClientEncoding } from "../interfaces";
import metadata from '../../../../contracts/src/build-metadata.json';
import { INSTALLATION_ABI } from '../constants';
import { ITokenVotingClientEncoding } from '../interfaces';
import { OffchainVotingPluginInstall } from '../types';
import {
mintTokenParamsToContract,
tokenVotingInitParamsToContract,
} from '../utils';
import { IERC20MintableUpgradeable__factory } from '@aragon/osx-ethers';
import {
MintTokenParams,
TokenVotingPluginInstall,
encodeUpdateVotingSettingsAction,
VotingSettings,
} from '@aragon/sdk-client';
import {
ClientCore,
DaoAction,
getNamedTypesFromMetadata,
LIVE_CONTRACTS,
PluginInstallItem,
SupportedNetwork,
SupportedNetworksArray,
} from '@aragon/sdk-client-common';
import {
hexToBytes,
InvalidAddressError,
UnsupportedNetworkError,
} from '@aragon/sdk-common';
import { defaultAbiCoder } from '@ethersproject/abi';
import { isAddress } from '@ethersproject/address';
import { Networkish, getNetwork } from '@ethersproject/providers';

export class OffchainVotingClientEncoding extends OffchainVotingClientCore
implements IOffchainVotingClientEncoding {
// add your action encoders here
const prepareInstallationDataTypes = getNamedTypesFromMetadata(
metadata.pluginSetup.prepareInstallation.inputs
);

/**
* Encoding module the SDK TokenVoting Client
*/
export class OffchainVotingClientEncoding
extends ClientCore
implements ITokenVotingClientEncoding
{
/**
* Computes the parameters to be given when creating the DAO,
* so that the plugin is configured
*
* @param {TokenVotingPluginInstall} params
* @param {Networkish} network
* @return {*} {PluginInstallItem}
* @memberof TokenVotingClientEncoding
*/
static getPluginInstallItem(
params: OffchainVotingPluginInstall,
network: Networkish
): PluginInstallItem {
const networkName = getNetwork(network).name as SupportedNetwork;
if (!SupportedNetworksArray.includes(networkName)) {
throw new UnsupportedNetworkError(networkName);
}
const args = tokenVotingInitParamsToContract(params);
const hexBytes = defaultAbiCoder.encode(prepareInstallationDataTypes,
args
//[
// Object.values([params.daoAddress]),
// Object.values(params.votingSettings),
// [params.useToken?.tokenAddress, '', ''],
// Object.values(params.newToken?.),
]
//);
return {
id: LIVE_CONTRACTS[networkName].tokenVotingRepo,
data: hexToBytes(hexBytes),
};
}
/**
* Computes the parameters to be given when creating a proposal that updates the governance configuration
*
* @param {string} pluginAddress
* @param {VotingSettings} params
* @return {*} {DaoAction}
* @memberof TokenVotingClientEncoding
*/
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),
};
}

/**
* Computes the parameters to be given when creating a proposal that mints an amount of ERC-20 tokens to an address
*
* @param {string} minterAddress
* @param {MintTokenParams} params
* @return {*} {DaoAction}
* @memberof TokenVotingClientEncoding
*/
public mintTokenAction(
minterAddress: string,
params: MintTokenParams
): DaoAction {
if (!isAddress(minterAddress) || !isAddress(params.address)) {
throw new InvalidAddressError();
}
const votingInterface =
IERC20MintableUpgradeable__factory.createInterface();
const args = mintTokenParamsToContract(params);
// get hex bytes
const hexBytes = votingInterface.encodeFunctionData('mint', args);
return {
to: minterAddress,
value: BigInt(0),
data: hexToBytes(hexBytes),
};
}
}
Loading

0 comments on commit 8714eb1

Please sign in to comment.