Alchemy Logo

Configure client

A smart wallet client is the main interface used to interact with smart wallets and take actions like sending transactions, batching transactions, swapping, sponsoring gas, and more.

The SmartWalletClient is a EIP-1193 compatible extension of viem's Client which allows you to interact with smart wallets.

Use the createSmartWalletClient function to create a smart wallet client.

  1. Replace the placeholders a. API key from your Alchemy dashboard b. Policy ID from your gas policy c. Signer from authentication or your own signer
  2. Create the initial client using createSmartWalletClient
  3. Generate your smart account by calling requestAccount()
  4. Create the final client with your account attached
client.ts
import { LocalAccountSigner } from "@aa-sdk/core";
import { alchemy, sepolia } from "@account-kit/infra";
import {
  createSmartWalletClient,
  type SmartWalletClientParams,
} from "@account-kit/wallet-client";
 
const clientParams: SmartWalletClientParams = {
  transport: alchemy({ apiKey: "your-alchemy-api-key"}),
  chain: sepolia,
  signer: LocalAccountSigner.privateKeyToAccountSigner("0x-your-wallet-private-key"),
  policyId: "your-policy-id",
};
 
const clientWithoutAccount = createSmartWalletClient(clientParams);
 
const account = await clientWithoutAccount.requestAccount();
 
export const client = createSmartWalletClient({
  ...clientParams,
  account: account.address,
});

Override default account type

By default, the Smart Wallet Client will use ModularAccountV2(MAv2). This is the cheapest and most advanced Smart Account, but you can specify other smart contract account types as needed. Learn more about the different smart accounts here.

Changing the account type will deploy a different account. If you've already deployed an account for a user and want to change the underlying account type, you'll need to upgrade it. Learn how to upgrade here.

Javascript

Specify the account type when calling requestAccount using the creationHint parameter. See all parameter options.

const account = await clientWithoutAccount.requestAccount(
  creationHint: {
    accountType: "sma-b"
  },
);
 
export const client = createSmartWalletClient({
  ...clientParams,
  account: account.address,
});

API

Pass an accountType to creationHint when calling wallet_requestAccount.

Using 7702

To use EIP-7702, please see this guide. You'll need to set additional parameters on the client.

Connect to an existing account address

By default, account addresses are deterministically generated from the signer address. To connect to an existing account that doesn't match the deterministic address of your signer, specify the account address when creating the client.

Javascript

Pass the address to the createSmartWalletClient directly rather than calling requestAccount.

export const client = createSmartWalletClient({
  ...clientParams,
  account: "0xYOUR_SMART_ACCOUNT_ADDR",
});

API

Pass the account address directly when preparing calls instead of calling wallet_requestAccount. See this guide and skip step 2.

Extend client with custom actions

Because the Smart Wallet Clients are extensions of viem's clients, they support extensions via the .extend method. The base client already includes a number of actions by default. You can find additional details about these actions in the @aa-sdk/core documentation.

Use one client for multiple accounts

Typically, the smart account client uses the default account or the account passed into the client constructor for all of the actions you perform with the client - also known as account hositing.

If you want to manage multiple instances of an account but want to use one client for all of them, you can pass an account to the client on every action.

import {
  createAlchemySmartAccountClient,
  sepolia,
  alchemy,
} from "@account-kit/infra";
import { createLightAccount } from "@account-kit/smart-contracts";
import { LocalAccountSigner } from "@aa-sdk/core";
import { http } from "viem";
import { generatePrivateKey } from "viem/accounts";
 
// with account hoisting
const transport = alchemy({ apiKey: "your-api-key" });
const hoistedClient = createAlchemySmartAccountClient({
  transport,
  chain: sepolia,
  account: await createLightAccount({
    signer: LocalAccountSigner.privateKeyToAccountSigner(generatePrivateKey()),
    chain: sepolia,
    transport,
  }),
});
 
const signature = await hoistedClient.signMessage({ message: "Hello world! " });
 
// without account hoisting
const nonHoistedClient = createAlchemySmartAccountClient({
  transport,
  chain: sepolia,
});
 
const lightAccount = await createLightAccount({
  signer: LocalAccountSigner.privateKeyToAccountSigner(generatePrivateKey()),
  chain: sepolia,
  transport,
});
 
const signature2 = await nonHoistedClient.signMessage({
  message: "Hello world! ",
  account: lightAccount,
});

Was this page helpful?