From 10df8f71e05acc18aa44b791efab061b4ca9bf76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8r=E2=88=82=C2=A1?= Date: Thu, 28 Sep 2023 19:07:58 +0200 Subject: [PATCH] Amending the JS example --- packages/js-client/src/context.ts | 86 ++++++++++++------ packages/js-client/src/internal/core.ts | 31 +++++-- packages/js-client/src/internal/interfaces.ts | 11 +-- .../src/internal/modules/decoding.ts | 15 ++- .../src/internal/modules/encoding.ts | 37 +++++--- .../src/internal/modules/estimation.ts | 68 +++++++++----- .../js-client/src/internal/modules/methods.ts | 91 +++++++++++++++---- packages/js-client/src/internal/types.ts | 19 ++-- packages/js-client/src/types.ts | 15 ++- 9 files changed, 259 insertions(+), 114 deletions(-) diff --git a/packages/js-client/src/context.ts b/packages/js-client/src/context.ts index 54dfc25..b9a9202 100644 --- a/packages/js-client/src/context.ts +++ b/packages/js-client/src/context.ts @@ -1,36 +1,37 @@ import { MyPluginContextState, MyPluginOverriddenState, -} from './internal/types'; -import { MyPluginContextParams } from './types'; -import { Context, ContextCore } from '@aragon/sdk-client-common'; +} from "./internal/types"; +import { MyPluginContextParams } from "./types"; +import { Context, ContextCore } from "@aragon/sdk-client-common"; -// set your defaults here or import them from a package -const DEFAULT_SIMPLE_STORAGE_PLUGIN_ADDRESS = - '0x1234567890123456789012345678901234567890'; -const DEFAULT_SIMPLE_STORAGE_Repo_ADDRESS = - '0x2345678901234567890123456789012345678901'; +const DEFAULT_SPACE_PLUGIN_REPO_ADDRESS = "..."; +const DEFAULT_MEMBER_ACCESS_PLUGIN_REPO_ADDRESS = "..."; +const DEFAULT_MAIN_VOTING_PLUGIN_REPO_ADDRESS = "..."; export class MyPluginContext extends ContextCore { // super is called before the properties are initialized // so we initialize them to the value of the parent class protected state: MyPluginContextState = this.state; - // TODO - // fix typo in the overridden property name + + // Keeps track of what values are not the default protected overriden: MyPluginOverriddenState = this.overriden; + constructor( contextParams?: Partial, - aragonContext?: Context + aragonContext?: Context, ) { // call the parent constructor // so it does not complain and we // can use this super(); + // set the context params inherited from the context if (aragonContext) { // copy the context properties to this Object.assign(this, aragonContext); } + // contextParams have priority over the aragonContext if (contextParams) { // overide the context params with the ones passed to the constructor @@ -42,37 +43,66 @@ export class MyPluginContext extends ContextCore { // the super function will call this set // so we need to call the parent set first super.set(contextParams); + // set the default values for the new params this.setDefaults(); + // override default params if specified in the context - if (contextParams.myPluginPluginAddress) { - // override the myPluginPluginAddress value - this.state.myPluginPluginAddress = contextParams.myPluginPluginAddress; + if (contextParams.spacePluginAddress) { + // override the spacePluginAddress value + this.state.spacePluginAddress = contextParams.spacePluginAddress; // set the overriden flag to true in case set is called again - this.overriden.myPluginPluginAddress = true; + this.overriden.spacePluginAddress = true; } - - if (contextParams.myPluginRepoAddress) { - this.state.myPluginRepoAddress = contextParams.myPluginRepoAddress; - this.overriden.myPluginRepoAddress = true; + if (contextParams.memberAccessPluginAddress) { + this.state.memberAccessPluginAddress = + contextParams.memberAccessPluginAddress; + this.overriden.memberAccessPluginAddress = true; + } + if (contextParams.mainVotingPluginAddress) { + this.state.mainVotingPluginAddress = + contextParams.mainVotingPluginAddress; + this.overriden.mainVotingPluginAddress = true; } } private setDefaults() { - if (!this.overriden.myPluginPluginAddress) { - // set the default value for myPluginPluginAddress - this.state.myPluginPluginAddress = DEFAULT_SIMPLE_STORAGE_PLUGIN_ADDRESS; + // Optional: Set any settings that may have a default value here + + if (!this.overriden.spacePluginRepoAddress) { + this.state.spacePluginRepoAddress = DEFAULT_SPACE_PLUGIN_REPO_ADDRESS; } - if (!this.overriden.myPluginPluginAddress) { - this.state.myPluginPluginAddress = DEFAULT_SIMPLE_STORAGE_Repo_ADDRESS; + if (!this.overriden.memberAccessPluginRepoAddress) { + this.state.memberAccessPluginRepoAddress = + DEFAULT_MEMBER_ACCESS_PLUGIN_REPO_ADDRESS; } + if (!this.overriden.mainVotingPluginRepoAddress) { + this.state.mainVotingPluginRepoAddress = + DEFAULT_MAIN_VOTING_PLUGIN_REPO_ADDRESS; + } + } + + get spacePluginAddress(): string { + return this.state.spacePluginAddress; + } + + get memberAccessPluginAddress(): string { + return this.state.memberAccessPluginAddress; + } + + get mainVotingPluginAddress(): string { + return this.state.mainVotingPluginAddress; + } + + get spacePluginRepoAddress(): string { + return this.state.spacePluginRepoAddress; } - get myPluginPluginAddress(): string { - return this.state.myPluginPluginAddress; + get memberAccessPluginRepoAddress(): string { + return this.state.memberAccessPluginRepoAddress; } - get myPluginRepoAddress(): string { - return this.state.myPluginRepoAddress; + get mainVotingPluginRepoAddress(): string { + return this.state.mainVotingPluginRepoAddress; } } diff --git a/packages/js-client/src/internal/core.ts b/packages/js-client/src/internal/core.ts index 58da75d..c4dfbdc 100644 --- a/packages/js-client/src/internal/core.ts +++ b/packages/js-client/src/internal/core.ts @@ -1,13 +1,30 @@ -import { MyPluginContext } from '../context'; -import { ClientCore } from '@aragon/sdk-client-common'; +import { MyPluginContext } from "../context"; +import { ClientCore } from "@aragon/sdk-client-common"; +import { + IMyPluginClientDecoding, + IMyPluginClientEncoding, + IMyPluginClientEstimation, + IMyPluginClientMethods, +} from "./interfaces"; +import { + MyPluginClientDecoding, + MyPluginClientEncoding, + MyPluginClientEstimation, + MyPluginClientMethods, +} from "./modules"; -export class MyPluginClientCore extends ClientCore { - public myPluginPluginAddress: string; - public myPluginRepoAddress: string; +export class StandardSpaceClientCore extends ClientCore { + public methods: IMyPluginClientMethods; + public encoding: IMyPluginClientEncoding; + public decoding: IMyPluginClientDecoding; + public estimation: IMyPluginClientEstimation; constructor(pluginContext: MyPluginContext) { super(pluginContext); - this.myPluginPluginAddress = pluginContext.myPluginPluginAddress; - this.myPluginRepoAddress = pluginContext.myPluginRepoAddress; + + this.methods = new MyPluginClientMethods(pluginContext); + this.encoding = new MyPluginClientEncoding(pluginContext); + this.decoding = new MyPluginClientDecoding(pluginContext); + this.estimation = new MyPluginClientEstimation(pluginContext); } } diff --git a/packages/js-client/src/internal/interfaces.ts b/packages/js-client/src/internal/interfaces.ts index d65bf77..9265034 100644 --- a/packages/js-client/src/internal/interfaces.ts +++ b/packages/js-client/src/internal/interfaces.ts @@ -2,12 +2,12 @@ import { NumberListItem, NumbersQueryParams, PrepareInstallationParams, -} from '../types'; +} from "../types"; import { DaoAction, GasFeeEstimation, PrepareInstallationStepValue, -} from '@aragon/sdk-client-common'; +} from "@aragon/sdk-client-common"; export interface IMyPluginClient { methods: IMyPluginClientMethods; @@ -19,14 +19,13 @@ export interface IMyPluginClient { export interface IMyPluginClientMethods { // fill with methods prepareInstallation( - params: PrepareInstallationParams + params: PrepareInstallationParams, ): AsyncGenerator; - getNumber(daoAddressOrEns: string): Promise; - getNumbers(params: NumbersQueryParams): Promise; + doSomething(): Promise; } export interface IMyPluginClientEstimation { prepareInstallation( - params: PrepareInstallationParams + params: PrepareInstallationParams, ): Promise; } export interface IMyPluginClientEncoding { diff --git a/packages/js-client/src/internal/modules/decoding.ts b/packages/js-client/src/internal/modules/decoding.ts index bd8ccac..29f720a 100644 --- a/packages/js-client/src/internal/modules/decoding.ts +++ b/packages/js-client/src/internal/modules/decoding.ts @@ -1,14 +1,13 @@ -import { MyPluginClientCore } from '../core'; -import { IMyPluginClientDecoding } from '../interfaces'; -import { MyPlugin__factory } from '@aragon/simple-storage-ethers'; +import { ClientCore } from "@aragon/sdk-client-common"; +// import { MyPluginContext } from "../../context"; +import { IMyPluginClientDecoding } from "../interfaces"; +import { MyPlugin__factory } from "@aragon/simple-storage-ethers"; -export class MyPluginClientDecoding - extends MyPluginClientCore - implements IMyPluginClientDecoding -{ +export class MyPluginClientDecoding extends ClientCore + implements IMyPluginClientDecoding { public storeNumberAction(data: Uint8Array): bigint { const iface = MyPlugin__factory.createInterface(); - const res = iface.decodeFunctionData('storeNumber', data); + const res = iface.decodeFunctionData("storeNumber", data); return BigInt(res[0]); } } diff --git a/packages/js-client/src/internal/modules/encoding.ts b/packages/js-client/src/internal/modules/encoding.ts index 0ce15af..0a13568 100644 --- a/packages/js-client/src/internal/modules/encoding.ts +++ b/packages/js-client/src/internal/modules/encoding.ts @@ -1,19 +1,30 @@ -import { MyPluginClientCore } from '../core'; -import { IMyPluginClientEncoding } from '../interfaces'; -import { DaoAction } from '@aragon/sdk-client-common'; -import { hexToBytes } from '@aragon/sdk-common'; -import { MyPlugin__factory } from '@aragon/simple-storage-ethers'; +import { IMyPluginClientEncoding } from "../interfaces"; +import { ClientCore, DaoAction } from "@aragon/sdk-client-common"; +import { hexToBytes } from "@aragon/sdk-common"; +import { SpacePlugin__factory } from "../../../../contracts/typechain"; +import { MyPluginContext } from "../../context"; + +export class MyPluginClientEncoding extends ClientCore + implements IMyPluginClientEncoding { + private spacePluginAddress: string; + private memberAccessPluginAddress: string; + private mainVotingPluginAddress: string; + + constructor(pluginContext: MyPluginContext) { + super(pluginContext); + + this.spacePluginAddress = pluginContext.spacePluginAddress; + this.memberAccessPluginAddress = pluginContext.memberAccessPluginAddress; + this.mainVotingPluginAddress = pluginContext.mainVotingPluginAddress; + } -export class MyPluginClientEncoding - extends MyPluginClientCore - implements IMyPluginClientEncoding -{ // implementation of the methods in the interface - public storeNumberAction(number: bigint): DaoAction { - const iface = MyPlugin__factory.createInterface(); - const data = iface.encodeFunctionData('storeNumber', [number]); + public storeNumberAction(): DaoAction { + const iface = SpacePlugin__factory.createInterface(); + const data = iface.encodeFunctionData("setContent", [1, 4, "ipfs://...."]); + return { - to: this.myPluginPluginAddress, + to: this.spacePluginAddress, value: BigInt(0), data: hexToBytes(data), }; diff --git a/packages/js-client/src/internal/modules/estimation.ts b/packages/js-client/src/internal/modules/estimation.ts index f77bd71..00d3832 100644 --- a/packages/js-client/src/internal/modules/estimation.ts +++ b/packages/js-client/src/internal/modules/estimation.ts @@ -1,20 +1,40 @@ -import * as BUILD_METADATA from '../../../../contracts/src/build-metadata.json'; -import { PrepareInstallationParams } from '../../types'; -import { MyPluginClientCore } from '../core'; -import { IMyPluginClientEstimation } from '../interfaces'; -import { PluginRepo__factory } from '@aragon/osx-ethers'; +import * as BUILD_METADATA from "../../../../contracts/src/build-metadata.json"; +import { MyPluginContext } from "../../context"; +import { PrepareInstallationParams } from "../../types"; +import { IMyPluginClientEstimation } from "../interfaces"; +import { PluginRepo__factory } from "@aragon/osx-ethers"; import { + ClientCore, GasFeeEstimation, prepareGenericInstallationEstimation, -} from '@aragon/sdk-client-common'; -import { MyPlugin__factory } from '@aragon/simple-storage-ethers'; +} from "@aragon/sdk-client-common"; + +export class MyPluginClientEstimation extends ClientCore + implements IMyPluginClientEstimation { + private spacePluginAddress: string; + private memberAccessPluginAddress: string; + private mainVotingPluginAddress: string; + + private spacePluginRepoAddress: string; + private memberAccessPluginRepoAddress: string; + private mainVotingPluginRepoAddress: string; + + constructor(pluginContext: MyPluginContext) { + super(pluginContext); + + this.spacePluginAddress = pluginContext.spacePluginAddress; + this.memberAccessPluginAddress = pluginContext.memberAccessPluginAddress; + this.mainVotingPluginAddress = pluginContext.mainVotingPluginAddress; + // repos + this.spacePluginRepoAddress = pluginContext.spacePluginRepoAddress; + this.memberAccessPluginRepoAddress = + pluginContext.memberAccessPluginRepoAddress; + this.mainVotingPluginRepoAddress = + pluginContext.mainVotingPluginRepoAddress; + } -export class SimpleStoragClientEstimation - extends MyPluginClientCore - implements IMyPluginClientEstimation -{ public async prepareInstallation( - params: PrepareInstallationParams + params: PrepareInstallationParams, ): Promise { let version = params.version; // if not specified use the lates version @@ -24,13 +44,13 @@ export class SimpleStoragClientEstimation // connect to the plugin repo const pluginRepo = PluginRepo__factory.connect( this.myPluginRepoAddress, - signer + signer, ); // get latest release const currentRelease = await pluginRepo.latestRelease(); // get latest version - const latestVersion = await pluginRepo['getLatestVersion(uint8)']( - currentRelease + const latestVersion = await pluginRepo["getLatestVersion(uint8)"]( + currentRelease, ); version = latestVersion.tag; } @@ -44,13 +64,13 @@ export class SimpleStoragClientEstimation }); } - public async storeNumber(number: bigint): Promise { - const signer = this.web3.getConnectedSigner(); - const myPlugin = MyPlugin__factory.connect( - this.myPluginPluginAddress, - signer - ); - const estimation = await myPlugin.estimateGas.storeNumber(number); - return this.web3.getApproximateGasFee(estimation.toBigInt()); - } + // public async storeNumber(number: bigint): Promise { + // const signer = this.web3.getConnectedSigner(); + // const myPlugin = MyPlugin__factory.connect( + // this.myPluginPluginAddress, + // signer, + // ); + // const estimation = await myPlugin.estimateGas.storeNumber(number); + // return this.web3.getApproximateGasFee(estimation.toBigInt()); + // } } diff --git a/packages/js-client/src/internal/modules/methods.ts b/packages/js-client/src/internal/modules/methods.ts index 473e286..514c3c3 100644 --- a/packages/js-client/src/internal/modules/methods.ts +++ b/packages/js-client/src/internal/modules/methods.ts @@ -1,28 +1,55 @@ -import * as BUILD_METADATA from '../../../../contracts/src/build-metadata.json'; +import * as BUILD_METADATA from "../../../../contracts/src/build-metadata.json"; import { NumberListItem, NumbersQueryParams, NumbersSortBy, PrepareInstallationParams, -} from '../../types'; -import { MyPluginClientCore } from '../core'; -import { QueryNumber, QueryNumbers } from '../graphql-queries'; -import { IMyPluginClientMethods } from '../interfaces'; -import { SubgraphNumber, SubgraphNumberListItem } from '../types'; -import { toNumber, toNumberListItem } from '../utils'; +} from "../../types"; import { + MainVotingPlugin__factory, + MemberAccessPlugin__factory, + // PersonalSpaceAdminPlugin__factory, + SpacePlugin__factory, +} from "../../../../contracts/typechain"; +import { QueryNumber, QueryNumbers } from "../graphql-queries"; +import { IMyPluginClientMethods } from "../interfaces"; +import { SubgraphNumber, SubgraphNumberListItem } from "../types"; +import { toNumber, toNumberListItem } from "../utils"; +import { + ClientCore, prepareGenericInstallation, PrepareInstallationStepValue, SortDirection, -} from '@aragon/sdk-client-common'; +} from "@aragon/sdk-client-common"; +import { MyPluginContext } from "../../context"; + +export class MyPluginClientMethods extends ClientCore + implements IMyPluginClientMethods { + private spacePluginAddress: string; + private memberAccessPluginAddress: string; + private mainVotingPluginAddress: string; + + private spacePluginRepoAddress: string; + private memberAccessPluginRepoAddress: string; + private mainVotingPluginRepoAddress: string; + + constructor(pluginContext: MyPluginContext) { + super(pluginContext); + + this.spacePluginAddress = pluginContext.spacePluginAddress; + this.memberAccessPluginAddress = pluginContext.memberAccessPluginAddress; + this.mainVotingPluginAddress = pluginContext.mainVotingPluginAddress; + // repos + this.spacePluginRepoAddress = pluginContext.spacePluginRepoAddress; + this.memberAccessPluginRepoAddress = + pluginContext.memberAccessPluginRepoAddress; + this.mainVotingPluginRepoAddress = + pluginContext.mainVotingPluginRepoAddress; + } -export class MyPluginClientMethods - extends MyPluginClientCore - implements IMyPluginClientMethods -{ // implementation of the methods in the interface public async *prepareInstallation( - params: PrepareInstallationParams + params: PrepareInstallationParams, ): AsyncGenerator { // do any additionall custom operations here before you prepare your plugin @@ -30,16 +57,46 @@ export class MyPluginClientMethods yield* prepareGenericInstallation(this.web3, { daoAddressOrEns: params.daoAddressOrEns, - pluginRepo: this.myPluginRepoAddress, + pluginRepo: this.spacePluginRepoAddress, version: params.version, installationAbi: BUILD_METADATA.pluginSetup.prepareInstallation.inputs, installationParams: [params.settings.number], }); } + async doSomething() { + const spaceClient = new SpacePlugin__factory().attach( + this.spacePluginAddress, + ); + const memberAccessClient = new MemberAccessPlugin__factory().attach( + this.memberAccessPluginAddress, + ); + const mainVotingClient = new MainVotingPlugin__factory().attach( + this.mainVotingPluginAddress, + ); + + // complex operation + const tx1 = await memberAccessClient.proposeNewMember( + "ipfs://...", + "0x0.....", + ); + await tx1.wait(); + + const tx2 = await mainVotingClient.createProposal( + "", + [], + 0, + 0, + 0, + 1, + false, + ); + await tx2.wait(); + } + public async getNumber(daoAddressOrEns: string): Promise { const query = QueryNumber; - const name = 'Numbers'; + const name = "Numbers"; type T = { dao: SubgraphNumber }; const { dao } = await this.graphql.request({ query, @@ -62,7 +119,7 @@ export class MyPluginClientMethods direction, sortBy, }; - const name = 'Numbers'; + const name = "Numbers"; type T = { daos: SubgraphNumberListItem[] }; const { daos } = await this.graphql.request({ query, @@ -72,7 +129,7 @@ export class MyPluginClientMethods return Promise.all( daos.map(async (number) => { return toNumberListItem(number); - }) + }), ); } } diff --git a/packages/js-client/src/internal/types.ts b/packages/js-client/src/internal/types.ts index b8ed767..82c6e57 100644 --- a/packages/js-client/src/internal/types.ts +++ b/packages/js-client/src/internal/types.ts @@ -1,4 +1,4 @@ -import { ContextState, OverriddenState } from '@aragon/sdk-client-common'; +import { ContextState, OverriddenState } from "@aragon/sdk-client-common"; export type SubgraphNumberListItem = { id: string; @@ -17,10 +17,17 @@ export type SubgraphNumber = { export type MyPluginContextState = ContextState & { // extend the Context state with a new state for storing // the new parameters - myPluginPluginAddress: string; - myPluginRepoAddress: string; -}; + spacePluginAddress: string; + memberAccessPluginAddress: string; + mainVotingPluginAddress: string; -export type MyPluginOverriddenState = OverriddenState & { - [key in keyof MyPluginContextState]: boolean; + spacePluginRepoAddress: string; + memberAccessPluginRepoAddress: string; + mainVotingPluginRepoAddress: string; }; + +export type MyPluginOverriddenState = + & OverriddenState + & { + [key in keyof MyPluginContextState]: boolean; + }; diff --git a/packages/js-client/src/types.ts b/packages/js-client/src/types.ts index f074968..5ea17d2 100644 --- a/packages/js-client/src/types.ts +++ b/packages/js-client/src/types.ts @@ -2,12 +2,17 @@ import { ContextParams, Pagination, VersionTag, -} from '@aragon/sdk-client-common'; +} from "@aragon/sdk-client-common"; export type MyPluginContextParams = ContextParams & { // optional so we can set default values for the parameter - myPluginPluginAddress?: string; - myPluginRepoAddress?: string; + spacePluginAddress?: string; + memberAccessPluginAddress?: string; + mainVotingPluginAddress?: string; + + spacePluginRepoAddress?: string; + memberAccessPluginRepoAddress?: string; + mainVotingPluginRepoAddress?: string; // add custom params }; @@ -25,8 +30,8 @@ export type NumbersQueryParams = Pagination & { }; export enum NumbersSortBy { - NUMBER = 'number', - CREATED_AT = 'createdAt', + NUMBER = "number", + CREATED_AT = "createdAt", } export type NumberListItem = {