Modular Account V2 • Getting started

It is easy to get started with Modular Account v2! Below, you will create a new Modular Account v2 client that will be used to send user operations. Your MAv2 smart account will be deployed on-chain when you send the first User Operation from a unique signer.

Install packages

Prerequisites

Installation

First, install the @account-kit/smart-contracts package.

$yarn add @account-kit/smart-contracts
>yarn add @account-kit/infra
Address calculation

For Modular Account V2, the address of the smart account will be calculated as a combination of the owner and the salt. You will get the same smart account address each time you supply the same owner, the signer(s) used to create the account for the first time. You can also optionally supply salt if you want a different address for the same owner param (the default salt is 0n).

If you want to use a signer to connect to an account whose address does not map to the contract-generated address, you can supply the accountAddress to connect with the account of interest. In that case, the signer address is not used for address calculation, but only for signing the operation.

Creating a Modular Account V2 client

[modular-account-v2.ts]
1import { createModularAccountV2Client } from "@account-kit/smart-contracts";
2import { LocalAccountSigner } from "@aa-sdk/core";
3import { sepolia, alchemy } from "@account-kit/infra";
4import { generatePrivateKey } from "viem/accounts";
5
6const accountClient = await createModularAccountV2Client({
7 mode: "default", // optional param to specify the MAv2 variant (either "default" or "7702")
8 chain: sepolia,
9 transport: alchemy({ apiKey: "your-api-key" }), // Get your API key at https://dashboard.alchemy.com/apps or http("RPC_URL") for non-alchemy infra
10 signer: LocalAccountSigner.privateKeyToAccountSigner(generatePrivateKey()),
11});

:::tip[Choosing which mode to use] We currently offer two variants of Modular Account v2: default and 7702.

  • (Recommended) default provides you with the cheapest, most flexible and advanced Smart Account
  • 7702 if you are looking for 7702 support, learn about how to set up and take adavantage of our EIP-7702 compliant account here :::

Want to enable social login methods? Set up your Alchemy Signer.

Alternatively, you can bring a 3rd party signer as the owner of your new account.

Not sure what signer to use? Learn more.

Sending a user operation

Now that you have a client, you can send a User Operation. The first User Operation will also deploy the new Modular Account v2.

1import { createModularAccountV2Client } from "@account-kit/smart-contracts";
2import { LocalAccountSigner } from "@aa-sdk/core";
3import { sepolia, alchemy } from "@account-kit/infra";
4import { generatePrivateKey } from "viem/accounts";
5import { parseEther } from "viem";
6
7const accountClient = await createModularAccountV2Client({
8 chain: sepolia,
9 transport: alchemy({ apiKey: "your-api-key" }),
10 signer: LocalAccountSigner.privateKeyToAccountSigner(generatePrivateKey()),
11});
12
13const operation = await accountClient.sendUserOperation({
14 // simple UO sending no data or value to vitalik's address
15 uo: {
16 target: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", // The address to call in the UO
17 data: "0x", // The calldata to send in the UO
18 value: parseEther("0"), // The value to send in the UO
19 },
20});
21
22console.log(
23 "User operation sent! \nUO hash: ",
24 operation.hash,
25 "\nModular Account v2 Address: ",
26 operation.request.sender
27);