import { useMutation } from "@tanstack/react-query"; import { useCallback } from "react"; import type { Chain, Client, Transport } from "viem"; import type { SupportedAccounts } from "../../config"; import { ClientUndefinedHookError } from "../errors.js"; import type { UseSmartAccountClientResult } from "./useSmartAccountClient"; export type UseClientActionsProps< TTransport extends Transport = Transport, TChain extends Chain | undefined = Chain | undefined, TActions extends { [x: string]: (...args: any[]) => unknown } = { [x: string]: (...args: any[]) => unknown; } > = { client?: UseSmartAccountClientResult< TTransport, TChain, SupportedAccounts >["client"]; actions: (client: Client) => TActions; }; export type UseClientActionsResult< TActions extends { [x: string]: (...args: any[]) => unknown } = { [x: string]: (...args: any[]) => unknown; } > = { executeAction: >( params: ClientActionParameters ) => void; executeActionAsync: >( params: ClientActionParameters ) => Promise>; data: ReturnType | undefined; isExecutingAction: boolean; error?: Error | null; }; export type ExecutableFunctionName< TActions extends { [x: string]: (...args: any[]) => unknown } = { [x: string]: (...args: any[]) => unknown; } > = keyof TActions extends infer functionName extends string ? [functionName] extends [never] ? string : functionName : string; export type ExecuteableFunctionResult< TFunctionName extends ExecutableFunctionName, TActions extends { [x: string]: (...args: any[]) => unknown } = { [x: string]: (...args: any[]) => unknown; } > = ReturnType; export type ExecutableFunctionArgs< TActions extends { [x: string]: (...args: any[]) => unknown } = { [x: string]: (...args: any[]) => unknown; }, TFunctionName extends ExecutableFunctionName = ExecutableFunctionName > = Parameters; // All of this is based one how viem's `encodeFunctionData` works export type ClientActionParameters< TActions extends { [x: string]: (...args: any[]) => unknown } = { [x: string]: (...args: any[]) => unknown; }, TFunctionName extends ExecutableFunctionName = ExecutableFunctionName, allArgs = ExecutableFunctionArgs< TActions, TFunctionName extends ExecutableFunctionName ? TFunctionName : ExecutableFunctionName > > = { functionName: TFunctionName; args: allArgs; }; /** * A hook that allows you to leverage client decorators to execute actions * and await them in your UX. This is particularly useful for using Plugins * with Modular Accounts. * * @example * ```tsx * const Foo = () => { * const { client } = useSmartAccountClient({ type: "MultiOwnerModularAccount" }); * const { executeAction } = useClientActions({ * client, * pluginActions: sessionKeyPluginActions, * }); * * executeAction({ * functionName: "isAccountSessionKey", * args: [{ key: "0x0" }], * }); * }; * ``` * * @param args the hooks arguments highlighted below * @param args.client the smart account client returned from {@link useSmartAccountClient} * @param args.actions the smart account client decorator you want to execute actions from * @returns an object containing methods to execute the actions as well loading and error states (see: {@link UseClientActionsResult}) */ export function useClientActions< TTransport extends Transport = Transport, TChain extends Chain | undefined = Chain | undefined, TActions extends { [x: string]: (...args: any[]) => any } = { [x: string]: (...args: any[]) => any; } >( args: UseClientActionsProps ): UseClientActionsResult { const { client, actions } = args; const { mutate, isPending: isExecutingAction, error, mutateAsync, data, } = useMutation< ReturnType, Error, ClientActionParameters> >({ mutationFn: async >({ functionName, args, }: ClientActionParameters) => { if (!client) { throw new ClientUndefinedHookError("useClientActions"); } const actions_ = actions(client); return actions_[functionName](...args); }, }); const executeAction = useCallback( >( params: ClientActionParameters ) => { const { functionName, args } = params; return mutate({ functionName, args }); }, [mutate] ); const executeActionAsync = useCallback( async >( params: ClientActionParameters ) => { const { functionName, args } = params; return mutateAsync({ functionName, args }); }, [mutateAsync] ); return { executeAction, executeActionAsync, data, isExecutingAction, error, }; }