MetaMask Smart Accounts quickstart
You can get started quickly with MetaMask Smart Accounts by creating your first smart accountMetaMask smart account A smart contract account that supports programmable behavior, delegated permissions, flexible signing options, and other advanced features. and sending a user operationUser operation A signed instruction package that tells a smart account what executions to perform..
Prerequisites
Steps
1. Install the Smart Accounts Kit
Install the Smart Accounts Kit:
- npm
- Yarn
- pnpm
- Bun
npm install @metamask/smart-accounts-kit
yarn add @metamask/smart-accounts-kit
pnpm add @metamask/smart-accounts-kit
bun add @metamask/smart-accounts-kit
2. Set up a Public Client
Set up a Public ClientPublic Client An interface used to fetch blockchain state, read from smart contracts, and monitor transactions. using Viem's createPublicClient function.
This client will let the smart account query the signer's account state and interact with the blockchain network.
import { createPublicClient, http } from "viem";
import { sepolia as chain } from "viem/chains";
const publicClient = createPublicClient({
chain,
transport: http(),
});
3. Set up a Bundler Client
Set up a Bundler ClientBundler A service that collects user operations from smart accounts, packages them, and submits them to the network. using Viem's createBundlerClient function.
This lets you use the bundler service to estimate gas for user operationsUser operation A signed instruction package that tells a smart account what executions to perform. and submit transactions to the network.
import { createBundlerClient } from "viem/account-abstraction";
const bundlerClient = createBundlerClient({
client: publicClient,
transport: http("https://your-bundler-rpc.com"),
});
4. Create a MetaMask smart account
Create a MetaMask smart accountMetaMask smart account A smart contract account that supports programmable behavior, delegated permissions, flexible signing options, and other advanced features. to send the first user operationUser operation A signed instruction package that tells a smart account what executions to perform..
This example configures a Hybrid smart accountHybrid smart account A smart account implementation that supports both an EOA owner and passkey signers., which is a flexible smart account implementation that supports both an EOAEOA A private-key-controlled account with no built-in programmable execution logic. owner and any number of passkeyPasskey A cryptographic key that can be used to sign transactions instead of a private key. (WebAuthn) signers:
import { Implementation, toMetaMaskSmartAccount } from "@metamask/smart-accounts-kit";
import { privateKeyToAccount } from "viem/accounts";
const account = privateKeyToAccount("0x...");
const smartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Hybrid,
deployParams: [account.address, [], [], []],
deploySalt: "0x",
signer: { account },
});
See Create a MetaMask smart account to learn how to configure different smart account types.
5. Send a user operation
Send a user operationUser operation A signed instruction package that tells a smart account what executions to perform. using Viem's sendUserOperation method.
The smart account will remain counterfactual until the first user operation. If the smart account is not deployed, it will be automatically deployed upon the sending first user operation.
import { parseEther } from "viem";
// Appropriate fee per gas must be determined for the specific bundler being used.
const maxFeePerGas = 1n;
const maxPriorityFeePerGas = 1n;
const userOperationHash = await bundlerClient.sendUserOperation({
account: smartAccount,
calls: [
{
to: "0x1234567890123456789012345678901234567890",
value: parseEther("1"),
},
],
maxFeePerGas,
maxPriorityFeePerGas,
});
See Send a user operation to learn how to estimate fee per gas, and wait for the transaction receipt.
Next steps
- To grant specific permissions to other accounts from your smart account, create a delegation.
- This quickstart example uses a Hybrid smart account. You can also configure other smart account types.
- To upgrade an EOA to a smart account, see the EIP-7702 quickstart.
- To quickly bootstrap a MetaMask Smart Accounts project, use the CLI.