Getting started with Modular Account V2
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- minimum Typescript version of 5
- pin viem to 2.20.0 (
yarn add [email protected]
)
First, install the @account-kit/smart-contracts
package.
yarn
yarn add @account-kit/smart-contracts
yarn add @account-kit/infra
Creating a Modular Account V2 client
modular-account-v2.ts
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()),
});
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.
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
);