Skip to main content

Configure the Smart Accounts Kit

The Smart Accounts KitSmart Accounts Kit Toolkit for creating, managing, and interacting with MetaMask Smart Accounts, delegations, and Advanced Permissions. is highly configurable, providing support for custom bundlersBundler A service that collects user operations from smart accounts, packages them, and submits them to the network. and paymastersPaymaster A service that pays for user operations on behalf of a smart account.. You can also configure the toolkit environment to interact with the Delegation FrameworkDelegation Framework A set of audited smart contracts that handle smart account creation, the delegation lifecycle, and caveat enforcement..

Prerequisites

Install and set up the Smart Accounts Kit.

Configure the bundler

The toolkit uses Viem's Account Abstraction API to configure custom bundlersBundler A service that collects user operations from smart accounts, packages them, and submits them to the network. and paymastersPaymaster A service that pays for user operations on behalf of a smart account.. This provides a robust and flexible foundation for creating and managing MetaMask Smart Accounts. See Viem's account abstraction documentation for more information on the API's features, methods, and best practices.

To use the bundler and paymaster clients with the toolkit, create instances of these clients and configure them as follows:

import {
createPaymasterClient,
createBundlerClient,
} from "viem/account-abstraction";
import { http } from "viem";
import { sepolia as chain } from "viem/chains";

// Replace these URLs with your actual bundler and paymaster endpoints.
const bundlerUrl = "https://your-bundler-url.com";
const paymasterUrl = "https://your-paymaster-url.com";

// The paymaster is optional.
const paymasterClient = createPaymasterClient({
transport: http(paymasterUrl),
});

const bundlerClient = createBundlerClient({
transport: http(bundlerUrl),
paymaster: paymasterClient,
chain,
});

Replace the bundler and paymaster URLs with your bundler and paymaster endpoints. For example, you can use endpoints from Pimlico, Infura, or ZeroDev.

note

Providing a paymaster is optional when configuring your bundler client. However, if you choose not to use a paymaster, the smart account must have enough funds to pay gas fees.

(Optional) Configure the toolkit environment

The toolkit environment (SmartAccountsEnvironment) defines the contract addresses necessary for interacting with the Delegation Framework on a specific network. It serves several key purposes:

  • It provides a centralized configuration for all the contract addresses required by the Delegation Framework.
  • It enables easy switching between different networks (for example, Mainnet and testnet) or custom deployments.
  • It ensures consistency across different parts of the application that interact with the Delegation Framework.

Resolve the environment

When you create a MetaMask smart accountMetaMask smart account A smart contract account that supports programmable behavior, delegated permissions, flexible signing options, and other advanced features., the toolkit automatically resolves the environment based on the version it requires and the chain configured. If no environment is found for the specified chain, it throws an error.

import { SmartAccountsEnvironment } from "@metamask/smart-accounts-kit";
import { delegatorSmartAccount } from "./config.ts";

const environment: SmartAccountsEnvironment = delegatorSmartAccount.environment;
note

See the changelog of the toolkit version you are using (in the left sidebar) for supported chains.

Alternatively, you can use the getSmartAccountsEnvironment function to resolve the environment. This function is especially useful if your delegatorDelegator account The account that creates and signs a delegation to grant limited authority to another account. is not a smart account when creating a redelegationRedelegation A delegation that passes on a subset of authority granted by a previous delegation..

import { 
getSmartAccountsEnvironment,
SmartAccountsEnvironment,
} from "@metamask/smart-accounts-kit";
import { sepolia } from "viem/chains";

// Resolves the SmartAccountsEnvironment for Sepolia
const environment: SmartAccountsEnvironment = getSmartAccountsEnvironment(sepolia.id);

Deploy a custom environment

You can deploy the contracts using any method, but the toolkit provides a convenient deploySmartAccountsEnvironment function. This function simplifies deploying the Delegation FrameworkDelegation Framework A set of audited smart contracts that handle smart account creation, the delegation lifecycle, and caveat enforcement. contracts to your desired EVM chain.

This function requires a Viem Public Client, Wallet Client, and Chain to deploy the contracts and resolve the SmartAccountsEnvironment.

Your wallet must have a sufficient native token balance to deploy the contracts.

import { walletClient, publicClient } from "./config.ts";
import { sepolia as chain } from "viem/chains";
import { deploySmartAccountsEnvironment } from "@metamask/smart-accounts-kit/utils";

const environment = await deploySmartAccountsEnvironment(
walletClient,
publicClient,
chain
);

You can also override specific contracts when calling deploySmartAccountsEnvironment. For example, if you've already deployed the EntryPoint contract on the target chain, you can pass the contract address to the function.

// The config.ts is the same as in the previous example.
import { walletClient, publicClient } from "./config.ts";
import { sepolia as chain } from "viem/chains";
import { deploySmartAccountsEnvironment } from "@metamask/smart-accounts-kit/utils";

const environment = await deploySmartAccountsEnvironment(
walletClient,
publicClient,
chain,
+ {
+ EntryPoint: "0x0000000071727De22E5E9d8BAf0edAc6f37da032"
+ }
);

Once the contracts are deployed, you can use them to override the environment.

Override the environment

To override the environment, the toolkit provides an overrideDeployedEnvironment function to resolve SmartAccountsEnvironment with specified contracts for the given chain and contract version.

// The config.ts is the same as in the previous example.
import { walletClient, publicClient } from "./config.ts";
import { sepolia as chain } from "viem/chains";
import { SmartAccountsEnvironment } from "@metamask/smart-accounts-kit";
import {
overrideDeployedEnvironment,
deploySmartAccountsEnvironment
} from "@metamask/smart-accounts-kit";

const environment: SmartAccountsEnvironment = await deploySmartAccountsEnvironment(
walletClient,
publicClient,
chain
);

overrideDeployedEnvironment(
chain.id,
"1.3.0",
environment,
);

If you've already deployed the contracts using a different method, you can create a SmartAccountsEnvironment instance with the required contract addresses, and pass it to the function.

- import { walletClient, publicClient } from "./config.ts";
- import { sepolia as chain } from "viem/chains";
import { SmartAccountsEnvironment } from "@metamask/smart-accounts-kit";
import {
overrideDeployedEnvironment,
- deploySmartAccountsEnvironment
} from "@metamask/smart-accounts-kit";

- const environment: SmartAccountsEnvironment = await deploySmartAccountsEnvironment(
- walletClient,
- publicClient,
- chain
- );

+ const environment: SmartAccountsEnvironment = {
+ SimpleFactory: "0x124..",
+ // ...
+ implementations: {
+ // ...
+ },
+ };

overrideDeployedEnvironment(
chain.id,
"1.3.0",
environment
);
note

Make sure to specify the Delegation FrameworkDelegation Framework A set of audited smart contracts that handle smart account creation, the delegation lifecycle, and caveat enforcement. version required by the toolkit. See the changelog of the toolkit version you are using (in the left sidebar) for its required Framework version.