Skip to main content

Delegation API reference

The following API methods are related to creating and managing delegations.

createCaveatBuilder

Builds an array of caveats.

Parameters

NameTypeRequiredDescription
environmentDeleGatorEnvironmentYesEnvironment to resolve the smart contracts for the current chain.
configCaveatBuilderConfigNoConfiguration for CaveatBuilder.

Example

import { createCaveatBuilder } from "@metamask/delegation-toolkit";
import { delegatorSmartAccount } from "./config.ts";

const caveats = createCaveatBuilder(delegatorSmartAccount.environment)

Allow empty caveats

To create an empty caveat collection, set the CaveatBuilderConfig.allowEmptyCaveats to true.

example.ts
import { createCaveatBuilder } from "@metamask/delegation-toolkit";
// The config.ts is the same as in the previous example.
import { delegatorSmartAccount } from "./config.ts";

const caveats = createCaveatBuilder(delegatorSmartAccount.environment, {
allowEmptyCaveats: true
})

createExecution

Creates an ExecutionStruct instance.

Parameters

NameTypeRequiredDescription
targetHexNoAddress of the contract or recipient that the call is directed to.
valuebigintNoValue of native tokens to send along with the call in wei.
callDataHexNoEncoded function data or payload to be executed on the target address.

Example

import { createExecution } from "@metamask/delegation-toolkit";

// Creates an ExecutionStruct to transfer 0.01 ETH to
// 0xe3C818389583fDD5cAC32f548140fE26BcEaE907 address.
const caveats = createExecution(
"0xe3C818389583fDD5cAC32f548140fE26BcEaE907",
// 0.01 ETH in wei
10000000000000000n,
"0x",
);

deployDeleGatorEnvironment

Deploys the Delegation Framework contracts to an EVM chain.

Parameters

NameTypeRequiredDescription
walletClientWalletClientYesViem Wallet Client to deploy the contracts.
publicClientPublicClientYesViem Public Client to interact with the given chain.
chainChainYesViem Chain where you wish to deploy the Delegation Framework contracts.
deployedContracts{ [contract: string]: Hex }NoAllows overriding specific contract addresses when calling the function. For example, if certain contracts have already been deployed on the target chain, their addresses can be provided directly to the function.

Example

import { deployDeleGatorEnvironment } from "@metamask/delegation-toolkit";
import { walletClient, publicClient } from "./config.ts";
import { lineaSepolia as chain } from "viem/chains";

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

Inject deployed contracts

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

example.ts
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
);

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

getDeleGatorEnvironment

Resolves the DeleGatorEnvironment for a chain.

Parameters

NameTypeRequiredDescription
chainIdnumberYesThe chain ID of the network for which the DeleGatorEnvironment should be resolved.
versionSupportedVersionNoSpecifies the version of the Delegation Framework contracts to use. If omitted, the latest supported version will be used by default.

Example

import { getDeleGatorEnvironment } from "@metamask/delegation-toolkit";
import { sepolia } from "viem/chains";

const environment = getDeleGatorEnvironment(sepolia.id)

overrideDeployedEnvironment

Overrides or adds the DeleGatorEnvironment for a chain and supported version.

Parameters

NameTypeRequiredDescription
chainIdnumberYesThe chain ID of the network for which the DeleGatorEnvironment should be overridden.
versionSupportedVersionYesThe version of the Delegation Framework contracts to override for the specified chain.
environmentDeleGatorEnvironmentYesThe environment containing contract addresses to override for the given chain and version.

Example

import { environment } from "./environment.ts";
import { getDeleGatorEnvironment } from "@metamask/delegation-toolkit";
import { sepolia } from "viem/chains";

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

redeemDelegation

Encodes calldata for redeeming delegations.

Parameters

NameTypeRequiredDescription
delegationsDelegation[][]YesA nested collection representing chains of delegations. Each inner collection contains a chain of delegations to be redeemed.
modesExecutionMode[]YesA collection specifying the execution mode for each corresponding delegation chain.
executionsExecutionStruct[][]YesA nested collection where each inner collection contains a list of ExecutionStruct objects associated with a specific delegation chain.

Example

This example assumes you have a delegation signed by the delegator.

import { 
createExecution,
DelegationFramework,
} from "@metamask/delegation-toolkit";

const execution = createExecution();
const data = DelegationFramework.encode.redeemDelegations({
delegations: [[ signedDelegation ]],
modes: [ SINGLE_DEFAULT_MODE ],
executions: [[ execution ]],
});

signDelegation

Signs the delegation and returns the delegation signature.

Parameters

NameTypeRequiredDescription
signerWalletClientYesViem Wallet Client to sign the delegation.
delegationOmit<Delegation, "signature">YesThe unsigned delegation object to sign.
chainIdnumberYesThe chain ID on which the delegation manager is deployed.
delegationManager0x${string}YesThe address of the Delegation Manager.
namestringNoThe name of the domain of the Delegation Manager. The default is DelegationManager.
versionstringNoThe version of the domain of the Delegation Manager. The default is 1.

Example

import { signDelegation } from "@metamask/delegation-toolkit";
import { walletClient, delegation, delegationManager } from "./config.ts";
import { sepolia } from "viem/chains";

const signature = signDelegation({
signer: walletClient,
delegation,
chainId: sepolia.id,
delegationManager,
})