Skip to content

Commit

Permalink
refactor: change the names of the plugin decorators and add example u…
Browse files Browse the repository at this point in the history
…sages (#308)
  • Loading branch information
moldy530 authored Dec 5, 2023
1 parent 2a267ed commit f65970e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
10 changes: 5 additions & 5 deletions packages/accounts/scripts/plugingen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ export function plugingen({
name: "${name}",
version: "${version}",
},
accountDecorators: (account: ISmartContractAccount) => ({ ${accountFunctions.join(
accountMethods: (account: IMSCA<any, any>) => ({ ${accountFunctions.join(
",\n\n"
)} }),
providerDecorators: <
providerMethods: <
TTransport extends SupportedTransports,
P extends ISmartAccountProvider<TTransport> & { account: IMSCA<TTransport> }
>(
Expand All @@ -153,9 +153,9 @@ export function plugingen({
export const ${contract.name}: Plugin<ReturnType<typeof ${
contract.name
}_["accountDecorators"]>, ReturnType<typeof ${
}_["accountMethods"]>, ReturnType<typeof ${
contract.name
}_["providerDecorators"]>> = ${contract.name}_;
}_["providerMethods"]>> = ${contract.name}_;
`);

// add the abi at the end so it's easier to read the actual plugin code output
Expand All @@ -170,7 +170,7 @@ export function plugingen({
import { type GetFunctionArgs, encodeFunctionData } from "viem";
import type { Plugin } from "./types";
import type { IMSCA } from "../builder";
import type { ISmartContractAccount, ISmartAccountProvider, SupportedTransports } from "@alchemy/aa-core";
import type { ISmartAccountProvider, SupportedTransports } from "@alchemy/aa-core";
`;

return {
Expand Down
2 changes: 2 additions & 0 deletions packages/accounts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ export {
} from "./msca/multi-owner-account.js";
export { pluginManagerDecorator } from "./msca/plugin-manager/decorator.js";
export type * from "./msca/plugin-manager/installPlugin.js";
export { installPlugin } from "./msca/plugin-manager/installPlugin.js";
export type * from "./msca/plugin-manager/types.js";
export type * from "./msca/plugin-manager/uninstallPlugin.js";
export { uninstallPlugin } from "./msca/plugin-manager/uninstallPlugin.js";
export {
MultiOwnerPlugin,
MultiOwnerPluginExecutionFunctionAbi,
Expand Down
4 changes: 2 additions & 2 deletions packages/accounts/src/msca/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ export class MSCABuilder {
extendWithPluginMethods = <AD, PD>(
plugin: Plugin<AD, PD>
): DynamicMSCA<TProviderDecorators & PD> & AD => {
const methods = plugin.accountDecorators(this);
const methods = plugin.accountMethods(this);
const result = Object.assign(this, methods) as unknown as DynamicMSCA<
TProviderDecorators & PD
> &
AD;
result.providerDecorators_.push(plugin.providerDecorators);
result.providerDecorators_.push(plugin.providerMethods);

return result as unknown as DynamicMSCA<TProviderDecorators & PD> & AD;
};
Expand Down
9 changes: 4 additions & 5 deletions packages/accounts/src/msca/plugins/multi-owner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { type GetFunctionArgs, encodeFunctionData } from "viem";
import type { Plugin } from "./types";
import type { IMSCA } from "../builder";
import type {
ISmartContractAccount,
ISmartAccountProvider,
SupportedTransports,
} from "@alchemy/aa-core";
Expand All @@ -16,7 +15,7 @@ const MultiOwnerPlugin_ = {
name: "Multi Owner Plugin",
version: "1.0.0",
},
accountDecorators: (account: ISmartContractAccount) => ({
accountMethods: (account: IMSCA<any, any>) => ({
encodeUpdateOwnersData: ({
args,
}: GetFunctionArgs<
Expand Down Expand Up @@ -114,7 +113,7 @@ const MultiOwnerPlugin_ = {
});
},
}),
providerDecorators: <
providerMethods: <
TTransport extends SupportedTransports,
P extends ISmartAccountProvider<TTransport> & {
account: IMSCA<TTransport>;
Expand All @@ -140,8 +139,8 @@ const MultiOwnerPlugin_ = {
};

export const MultiOwnerPlugin: Plugin<
ReturnType<(typeof MultiOwnerPlugin_)["accountDecorators"]>,
ReturnType<(typeof MultiOwnerPlugin_)["providerDecorators"]>
ReturnType<(typeof MultiOwnerPlugin_)["accountMethods"]>,
ReturnType<(typeof MultiOwnerPlugin_)["providerMethods"]>
> = MultiOwnerPlugin_;

export const MultiOwnerPluginExecutionFunctionAbi = [
Expand Down
29 changes: 26 additions & 3 deletions packages/accounts/src/msca/plugins/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
import type {
ISmartAccountProvider,
ISmartContractAccount,
SupportedTransports,
} from "@alchemy/aa-core";
import type { IMSCA } from "../builder";

export interface Plugin<AD, PD> {
meta: { name: string; version: string };
accountDecorators: (a: ISmartContractAccount) => AD;
providerDecorators: <
/**
* Decorator functions that can be used to read data from an MSCA contract instance
* These methods can be used on their own or with the `account.extend` method to add them to the account instance
* @example
* const account = new MSCA(...);
* const extendedAccount = account.extend(plugin.accountMethods);
* // OR
* const accountReadMethods = plugin.accountMethods(account);
*
* @param a - the MSCA contract instance we want to read from
* @returns the various read methods this plugin provides for this MSCA contract instance
*/
accountMethods: (a: IMSCA<any, any>) => AD;
/**
* Decorator functions that can be used to write data to an MSCA contract instance
*
* @example
* const provider = new SmartAccountProvider(...).connect(rpcClient => new MSCA(...));
* const extendedProvider = provider.extend(plugin.providerMethods);
* // OR
* const accountWriteMethods = plugin.providerMethods(provider);
*
* @param p - a provider instance connected to an MSCA that we want to send user ops to
* @returns the various write methods this plugin provides for this MSCA contract instance
*/
providerMethods: <
TTransport extends SupportedTransports,
P extends ISmartAccountProvider<TTransport> & { account: IMSCA<TTransport> }
>(
Expand Down

0 comments on commit f65970e

Please sign in to comment.