Delegator environment
The DeleGatorEnvironment
object is a component of the MetaMask Delegation Toolkit that defines the contract addresses necessary for interacting with the Delegation Framework on a specific network.
The delegator environment 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 delegator environment
When you create a MetaMaskSmartAccount
instance, the Delegation 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 { lineaSepolia 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",
signatory: { account: delegatorAccount },
});
export delegatorSmartAccount;
See the changelog of the toolkit version you are using 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 contract account when
creating a redelegation.
import {
getDeleGatorEnvironment,
DeleGatorEnvironment,
} from "@metamask/delegation-toolkit";
// Resolves the DeleGatorEnvironment for Linea Sepolia
const environment: DeleGatorEnvironment = getDelegatorEnvironment(59141);
Deploy custom delegator 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 sufficient native token balance to deploy the contracts.
- example.ts
- config.ts
import { walletClient, publicClient } from "./config.ts";
import { lineaSepolia as chain } from "viem/chains";
const environment = await deployDeleGatorEnvironment(
walletClient,
publicClient,
chain
);
import { privateKeyToAccount } from "viem/accounts";
import { lineaSepolia 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 { lineaSepolia as chain } from "viem/chains";
const environment = await deployDeleGatorEnvironment(
walletClient,
publicClient,
chain,
+ {
+ EntryPoint: "0x0000000071727De22E5E9d8BAf0edAc6f37da032"
+ }
);
Once the contracts are deployed, you can use them to override the delegator environment.
Override delegator environment
To override the delegator environment, the toolkit provides a 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 { lineaSepolia as chain } from "viem/chains";
import {
DeleGatorEnvironment,
overrideDeployedEnvironment,
deployDeleGatorEnvironment
} from "@metamask/delegation-toolkit";
const environment: DeleGatorEnvironment = await deployDeleGatorEnvironment(
walletClient,
publicClient,
chain
);
const delegatorEnvironment: DeleGatorEnvironment = overrideDeployedEnvironment(
chainId,
"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 { lineaSepolia as chain } from "viem/chains";
import {
DeleGatorEnvironment,
overrideDeployedEnvironment,
- deployDeleGatorEnvironment
} from "@metamask/delegation-toolkit";
- const environment: DeleGatorEnvironment = await deployDeleGatorEnvironment(
- walletClient,
- publicClient,
- chain
- );
+ const evniroment: DeleGatorEnvironment = {
+ SimpleFactory: "0x124..",
+ // ...
+ implementations: {
+ // ...
+ },
+ };
const delegatorEnvironment: DeleGatorEnvironment = overrideDeployedEnvironment(
chainId,
"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 for its required Framework version.