This example demonstrates how to initialize a Modular Account v2 (MAv2), set up a bundler client with gas estimation and optional paymaster sponsorship, and send a user operation on the Sepolia network.
Note: This package is a low-level interface for interacting with Smart Accounts via viem. Most users should prefer @alchemy/wallet-apis for higher-level functionality.
import { createPublicClient } from "viem";
import {
createBundlerClient,
createPaymasterClient,
} from "viem/account-abstraction";
import { sepolia } from "viem/chains";
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
import { alchemyTransport } from "@alchemy/common";
import { estimateFeesPerGas } from "@alchemy/aa-infra";
import { toModularAccountV2 } from "@alchemy/smart-accounts";
const transport = alchemyTransport({ apiKey: "YOUR_API_KEY" });
// 1. Create a MAv2 smart account
const account = await toModularAccountV2({
client: createPublicClient({ chain: sepolia, transport }),
owner: privateKeyToAccount(generatePrivateKey()),
});
// 2. Create a bundler client with the account
const bundlerClient = createBundlerClient({
account,
chain: sepolia,
transport,
userOperation: {
estimateFeesPerGas,
},
// Optional: sponsor gas with a paymaster
paymaster: createPaymasterClient({ transport }),
paymasterContext: { policyId: "YOUR_POLICY_ID" },
});
// 3. Send a user operation
const hash = await bundlerClient.sendUserOperation({
calls: [{ to: "0x...", value: 0n, data: "0x" }],
});
const receipt = await bundlerClient.waitForUserOperationReceipt({ hash });