Skip to content

useSmartAccountClient

The useSmartAccountClient hook is used to create a new AlchemySmartAccountClient attached to either a LightAccount or MultiOwnerModularAccount contract using the AlchemySigner.

Import

ts
import { useSmartAccountClient } from "@alchemy/aa-alchemy/react";

Usage

tsx
import { useSmartAccountClient } from "@alchemy/aa-alchemy/react";

export function ComponentUsingClient() {
  // If this is the first time the hook is called, then the client will be undefined until the underlying account is connected to the client
  const { isLoadingClient, client } = useSmartAccountClient({
    type: "MultiOwnerModularAccount", // alternatively pass in "LightAccount",
    accountParams: {}, // optional param for overriding any account specific properties
  });

  if (isLoadingClient || !client) {
    return <div>Loading...</div>;
  }

  return (
    <div>
      <h1>Client is ready!</h1>
      <div>{client.account.address}</div>
    </div>
  );
}

Parameters

ts
import { type UseSmartAccountClientProps } from "@alchemy/aa-alchemy/react";

type

"LightAccount" | "MultiOwnerModularAccount"

The underlying account type you want to use

accountParams

ts
  | Omit<CreateLightAccountParams, "signer" | "transport" | "chain">
  | Omit<CreateMultiOwnerModularAccountParams, "signer" | "transport" | "chain">
  | undefined

An optional param object based on the type property passed in above. It allows for overriding the default account parameters.

CreateLightAccountParams
ts
export type CreateLightAccountParams<
  TTransport extends Transport = Transport,
  TSigner extends SmartAccountSigner = SmartAccountSigner,
  TLightAccountVersion extends GetLightAccountVersion<"LightAccount"> = GetLightAccountVersion<"LightAccount">,
  TEntryPointVersion extends GetEntryPointForLightAccountVersion<
    "LightAccount",
    TLightAccountVersion
  > = GetEntryPointForLightAccountVersion<"LightAccount", TLightAccountVersion>
> = Omit<
  CreateLightAccountBaseParams<
    TTransport,
    TSigner,
    "LightAccount",
    TLightAccountVersion,
    TEntryPointVersion
  >,
  "getAccountInitCode" | "entryPoint" | "version" | "abi" | "accountAddress"
> & {
  salt?: bigint;
  initCode?: Hex;
  accountAddress?: Address;
  factoryAddress?: Address;
  version?: TLightAccountVersion;
  entryPoint?: EntryPointDef<TEntryPointVersion, Chain>;
};
CreateMultiOwnerModularAccountParams
ts
export type CreateMultiOwnerModularAccountParams<
  TTransport extends Transport = Transport,
  TSigner extends SmartAccountSigner = SmartAccountSigner,
  TEntryPointVersion extends "0.6.0" = "0.6.0"
> = Pick<
  ToSmartContractAccountParams<
    "MultiOwnerModularAccount",
    TTransport,
    Chain,
    TEntryPointVersion
  >,
  "transport" | "chain"
> & {
  signer: TSigner;
  salt?: bigint;
  factoryAddress?: Address;
  initCode?: Hex;
  owners?: Address[];
  accountAddress?: Address;
} & EntryPointParameter<TEntryPointVersion, Chain>;

...rest

ts
Omit<
  AlchemySmartAccountClientConfig<
    TTransport,
    TChain,
    SupportedAccount<TAccount>
  >,
  "rpcUrl" | "chain" | "apiKey" | "jwt" | "account"
>;

The remaining parameters that are accepted allow for overriding certain properties of the AlchemySmartAccountClient

AlchemySmartAccountClientConfig
ts
export type AlchemySmartAccountClientConfig<
  transport extends Transport = Transport,
  chain extends Chain | undefined = Chain | undefined,
  account extends SmartContractAccount | undefined =
    | SmartContractAccount
    | undefined,
  context extends UserOperationContext | undefined =
    | UserOperationContext
    | undefined
> = {
  account?: account;
  useSimulation?: boolean;
  gasManagerConfig?: AlchemyGasManagerConfig;
} & AlchemyProviderConfig &
  Pick<
    SmartAccountClientConfig<transport, chain, account, context>,
    "customMiddleware" | "feeEstimator" | "gasEstimator" | "signUserOperation"
  >;

Return Type

ts
import { type UseSmartAccountClientResult } from "@alchemy/aa-alchemy/react";

client

AlchemySmartAccountClient | undefined Once the underlying account is created, this will be an instance of an AlchemySmartAccountClient connected to an instance of the account type specified.

isLoadingClient

boolean Indicates whether the client is still being created.