Skip to content
Alchemy Logo

Modular Account V2 • Getting started

Getting started with Modular Account v2 is straightforward. Below, you create a new Modular Account v2 client to send transactions. Your MAv2 smart account deploys onchain when you send the first transaction from a unique owner.

Prerequisites

  • minimum Typescript version of 5

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.

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

Choosing which mode to use Two variants of Modular Account v2 are available: default and 7702.

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

Want to enable social login methods? Set up your authentication provider.

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

Not sure what authentication provider to use? Learn more.

Now that you have a client, you can send a transaction. The first transaction also deploys the new Modular Account v2.

import { createModularAccountV2Client } from "@account-kit/smart-contracts";
import { LocalAccountSigner } from "@aa-sdk/core";
import { sepolia, alchemy } from "@account-kit/infra";
import { generatePrivateKey } from "viem/accounts";
import { parseEther } from "viem";
 
const accountClient = await createModularAccountV2Client({
  chain: sepolia,
  transport: alchemy({ apiKey: "your-api-key" }),
  signer: LocalAccountSigner.privateKeyToAccountSigner(generatePrivateKey()),
});
 
const operation = await accountClient.sendUserOperation({
  // simple UO sending no data or value to vitalik's address
  uo: {
    target: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", // The address to call in the UO
    data: "0x", // The calldata to send in the UO
    value: parseEther("0"), // The value to send in the UO
  },
});
 
console.log(
  "User operation sent! \nUO hash: ",
  operation.hash,
  "\nModular Account v2 Address: ",
  operation.request.sender,
);
Was this page helpful?