Skip to content

useAccount

The useAccount hook is used to create a new LightAccount or MultiOwnerModularAccount contract using the AlchemySigner provided by the Accounts Context. This hook is mainly useful if you just want to use information from the account. In most cases, however, the useSmartAccountClient hook is more useful since the resulting client contains the account for you to use as well.

Import

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

Usage

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

export function ComponentUsingAccount() {
  // 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 { isLoadingAccount, account } = useAccount({
    type: "LightAccount", // alternatively pass in "MultiOwnerModularAccount",
    accountParams: {}, // optional param for overriding any account specific properties
    skipCreate: false, // optional param to skip creating the account
    onSuccess: (account) => {
      // [optional] Do something with the account
    },
    onError: (error) => {
      // [optional] Do something with the error
    },
  });

  if (isLoadingAccount || !account) {
    return <div>Loading...</div>;
  }

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

Parameters

ts
import { type UseAccountProps } 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>;

skipCreate

An optional param that allows you to avoid creating a new instance of the account. This is useful if you know your account has already been created and cached locally.

...mutationArgs

You can optionally specify any of the React Query useMutation parameters as parameters to this hook.

Return Type

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

account

LightAccount<AlchemySigner> | MultiOwnerModularAccount<AlchemySigner>

An instance of the account specified by the type parameter.

isLoadingAccount

boolean

Indicates whether or not the account is still being created.