Getting started with Light Account
It is easy to get started with Light Account! We will show you how to create and send user operations for both LightAccount
and MultiOwnerLightAccount
using @alchemy/aa-alchemy
.
Install packages
npm
npm i @alchemy/aa-alchemy @alchemy/aa-core
Create a client and send a user operation
The code snippets below demonstrate how to use LightAccount
and MultiOwnerLightAccount
with Account Kit. They create the account and send a UserOperation
from it.
light-account.ts
import { createLightAccountAlchemyClient } from "@alchemy/aa-alchemy";
import { LocalAccountSigner, sepolia } from "@alchemy/aa-core";
import type { Hex } from "viem";
const chain = sepolia;
// The private key of your EOA that will be the signer of Light Account
const PRIVATE_KEY: Hex = "0xYourEOAPrivateKey";
const signer = LocalAccountSigner.privateKeyToAccountSigner(PRIVATE_KEY);
// Create a provider to send user operations from your smart account
const provider = await createLightAccountAlchemyClient({
// get your Alchemy API key at https://dashboard.alchemy.com
apiKey: "ALCHEMY_API_KEY",
chain,
signer,
version: "v2.0.0", // Required if not v1.1.0
});
// Fund your account address with ETH to send for the user operations
// (e.g. Get Sepolia ETH at https://sepoliafaucet.com)
console.log(provider.getAddress()); // Log the smart account address
// Send a user operation from your smart account
const { hash: uoHash } = await provider.sendUserOperation({
uo: {
target: "0xTargetAddress", // The desired target contract address
data: "0xCallData", // The desired call data
value: 0n, // (Optional) value to send the target contract address
},
});
console.log(uoHash); // Log the user operation hash
// Wait for the user operation to be mined
const txHash = await provider.waitForUserOperationTransaction({ hash: uoHash });
console.log(txHash); // Log the transaction hash