Configure the Delegation Toolkit
The MetaMask Delegation toolkit is highly configurable, providing support for custom bundlers and paymasters. You can also configure the toolkit environment to interact with the Delegation Framework.
Prerequisites
Install and set up the Delegation Toolkit.
Configure the bundler
The toolkit uses Viem's Account Abstraction API to configure custom bundlers and paymasters. 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.
Providing a paymaster is optional when configuring your bundler client. However, if you choose not to use a paymaster, the smart contract account must have enough funds to pay gas fees.
(Optional) Configure the toolkit environment
The toolkit environment (DeleGatorEnvironment
) 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 account, 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.
- example.ts
- config.ts
import { DeleGatorEnvironment } from "@metamask/delegation-toolkit";
import { delegatorSmartAccount } from "./config.ts";
const environment: DeleGatorEnvironment = delegatorSmartAccount.environment;
import {
Implementation,
toMetaMaskSmartAccount,
} from "@metamask/delegation-toolkit";
import { privateKeyToAccount } from "viem/accounts";
import { createPublicClient, http } from "viem";
import { sepolia as chain } from "viem/chains";
const publicClient = createPublicClient({
chain,
transport: http(),
});
const delegatorAccount = privateKeyToAccount("0x...");
const delegatorSmartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Hybrid,
deployParams: [delegatorAccount.address, [], [], []],
deploySalt: "0x",
signer: { account: delegatorAccount },
});
export delegatorSmartAccount;
See the changelog of the toolkit version you are using (in the left sidebar) for supported chains.
Alternatively, you can use the getDelegatorEnvironment
function to resolve the environment.
This function is especially useful if your delegator is not a smart account when
creating a redelegation.
import {
getDeleGatorEnvironment,
DeleGatorEnvironment,
} from "@metamask/delegation-toolkit";
// Resolves the DeleGatorEnvironment for Sepolia
const environment: DeleGatorEnvironment = getDelegatorEnvironment(11155111);
Deploy a custom environment
You can deploy the contracts using any method, but the toolkit provides a convenient deployDelegatorEnvironment
function. This function simplifies deploying the Delegation Framework contracts to your desired EVM chain.
This function requires a Viem Public Client, Wallet Client, and Chain
to deploy the contracts and resolve the DeleGatorEnvironment
.
Your wallet must have a sufficient native token balance to deploy the contracts.
- example.ts
- config.ts
import { walletClient, publicClient } from "./config.ts";
import { sepolia as chain } from "viem/chains";
import { deployDeleGatorEnvironment } from "@metamask/delegation-toolkit/utils";
const environment = await deployDeleGatorEnvironment(
walletClient,
publicClient,
chain
);
import { privateKeyToAccount } from "viem/accounts";
import { sepolia as chain } from "viem/chains";
import { http, createWalletClient, createPublicClient } from "viem";
// Your deployer wallet private key.
const privateKey = "0x123..";
const account = privateKeyToAccount(privateKey);
export const walletClient = createWalletClient({
account,
chain,
transport: http()
});
export const publicClient = createPublicClient({
transport: http(),
chain,
});
You can also override specific contracts when calling deployDelegatorEnvironment
.
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 { deployDeleGatorEnvironment } from "@metamask/delegation-toolkit/utils";
const environment = await deployDeleGatorEnvironment(
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
DeleGatorEnvironment
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 { DeleGatorEnvironment } from "@metamask/delegation-toolkit";
import {
overrideDeployedEnvironment,
deployDeleGatorEnvironment
} from "@metamask/delegation-toolkit/utils";
const environment: DeleGatorEnvironment = await deployDeleGatorEnvironment(
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 DelegatorEnvironment
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 { DeleGatorEnvironment } from "@metamask/delegation-toolkit";
import {
overrideDeployedEnvironment,
- deployDeleGatorEnvironment
} from "@metamask/delegation-toolkit/utils";
- const environment: DeleGatorEnvironment = await deployDeleGatorEnvironment(
- walletClient,
- publicClient,
- chain
- );
+ const environment: DeleGatorEnvironment = {
+ SimpleFactory: "0x124..",
+ // ...
+ implementations: {
+ // ...
+ },
+ };
overrideDeployedEnvironment(
chain.id,
"1.3.0",
environment
);
Make sure to specify the Delegation Framework version required by the toolkit. See the changelog of the toolkit version you are using (in the left sidebar) for its required Framework version.