Skip to content

Commit

Permalink
Amending the JS example
Browse files Browse the repository at this point in the history
  • Loading branch information
brickpop committed Sep 28, 2023
1 parent c62435c commit 10df8f7
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 114 deletions.
86 changes: 58 additions & 28 deletions packages/js-client/src/context.ts
Original file line number Diff line number Diff line change
@@ -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<MyPluginContextParams>,
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
Expand All @@ -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;
}
}
31 changes: 24 additions & 7 deletions packages/js-client/src/internal/core.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
11 changes: 5 additions & 6 deletions packages/js-client/src/internal/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,14 +19,13 @@ export interface IMyPluginClient {
export interface IMyPluginClientMethods {
// fill with methods
prepareInstallation(
params: PrepareInstallationParams
params: PrepareInstallationParams,
): AsyncGenerator<PrepareInstallationStepValue>;
getNumber(daoAddressOrEns: string): Promise<bigint>;
getNumbers(params: NumbersQueryParams): Promise<NumberListItem[]>;
doSomething(): Promise<void>;
}
export interface IMyPluginClientEstimation {
prepareInstallation(
params: PrepareInstallationParams
params: PrepareInstallationParams,
): Promise<GasFeeEstimation>;
}
export interface IMyPluginClientEncoding {
Expand Down
15 changes: 7 additions & 8 deletions packages/js-client/src/internal/modules/decoding.ts
Original file line number Diff line number Diff line change
@@ -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]);
}
}
37 changes: 24 additions & 13 deletions packages/js-client/src/internal/modules/encoding.ts
Original file line number Diff line number Diff line change
@@ -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),
};
Expand Down
68 changes: 44 additions & 24 deletions packages/js-client/src/internal/modules/estimation.ts
Original file line number Diff line number Diff line change
@@ -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<GasFeeEstimation> {
let version = params.version;
// if not specified use the lates version
Expand All @@ -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;
}
Expand All @@ -44,13 +64,13 @@ export class SimpleStoragClientEstimation
});
}

public async storeNumber(number: bigint): Promise<GasFeeEstimation> {
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<GasFeeEstimation> {
// 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());
// }
}
Loading

0 comments on commit 10df8f7

Please sign in to comment.