Delegation API reference
The following API methods are related to creating and managing delegations.
createCaveatBuilder
Builds an array of caveats.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
environment | SmartAccountsEnvironment | Yes | Environment to resolve the smart contracts for the current chain. |
config | CaveatBuilderConfig | No | Configuration for CoreCaveatBuilder. |
Example
import { createCaveatBuilder } from "@metamask/smart-accounts-kit/utils";
import { getSmartAccountsEnvironment } from "@metamask/smart-accounts-kit";
import { sepolia } from "viem/chains";
const environment = getSmartAccountsEnvironment(sepolia.id);
const caveatBuilder = createCaveatBuilder(environment);
Allow empty caveats
To create an empty caveat collection, set the CaveatBuilderConfig.allowInsecureUnrestrictedDelegation to true.
example.ts
import { createCaveatBuilder } from "@metamask/smart-accounts-kit/utils";
import { getSmartAccountsEnvironment } from "@metamask/smart-accounts-kit";
import { sepolia } from "viem/chains";
const environment = getSmartAccountsEnvironment(sepolia.id);
const caveatBuilder = createCaveatBuilder(environment, {
allowInsecureUnrestrictedDelegation: true,
});
createDelegation
Creates a delegation with a specific delegate.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
from | Hex | Yes | The address that is granting the delegation. |
to | Hex | Yes | The address to which the delegation is being granted. |
scope | ScopeConfig | Yes | The scope of the delegation that defines the initial authority. |
environment | SmartAccountsEnvironment | Yes | The environment used by the toolkit to define contract addresses for interacting with the Delegation Framework contracts. |
caveats | Caveats | No | Caveats that further refine the authority granted by the scope. |
parentDelegation | Delegation | Hex | No | The parent delegation or its corresponding hex to create a delegation chain. |
salt | Hex | No | The salt for generating the delegation hash. This helps prevent hash collisions when creating identical delegations. |
Example
import { createDelegation, getSmartAccountsEnvironment } from "@metamask/smart-accounts-kit";
import { sepolia } from "viem/chains";
import { parseEther } from "viem";
const delegation = createDelegation({
// Address that is granting the delegation
from: "0x7E48cA6b7fe6F3d57fdd0448B03b839958416fC1",
// Address to which the delegation is being granted
to: "0x2B2dBd1D5fbeB77C4613B66e9F35dBfE12cB0488",
// Alternatively you can use environment property of MetaMask smart account.
environment: getSmartAccountsEnvironment(sepolia.id),
scope: {
type: "nativeTokenTransferAmount",
// 0.001 ETH in wei format.
maxAmount: parseEther("0.001"),
},
});
createOpenDelegation
Creates an open delegation that can be redeemed by any delegate.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
from | Hex | Yes | The address that is granting the delegation. |
scope | ScopeConfig | Yes | The scope of the delegation that defines the initial authority. |
environment | SmartAccountsEnvironment | Yes | The environment used by the toolkit to define contract addresses for interacting with the Delegation Framework contracts. |
caveats | Caveats | No | Caveats that further refine the authority granted by the scope. |
parentDelegation | Delegation | Hex | No | The parent delegation or its corresponding hex to create a delegation chain. |
salt | Hex | No | The salt for generating the delegation hash. This helps prevent hash collisions when creating identical delegations. |
Example
import { createOpenDelegation, getSmartAccountsEnvironment } from "@metamask/smart-accounts-kit";
import { sepolia } from "viem/chains";
import { parseEther } from "viem";
const delegation = createOpenDelegation({
// Address that is granting the delegation
from: "0x7E48cA6b7fe6F3d57fdd0448B03b839958416fC1",
// Alternatively you can use environment property of MetaMask smart account.
environment: getSmartAccountsEnvironment(sepolia.id),
scope: {
type: "nativeTokenTransferAmount",
// 0.001 ETH in wei format.
maxAmount: parseEther("0.001"),
},
});
createExecution
Creates an ExecutionStruct instance.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
target | Address | No | Address of the contract or recipient that the call is directed to. |
value | bigint | No | Value of native tokens to send along with the call in wei. |
callData | Hex | No | Encoded function data or payload to be executed on the target address. |
Example
import { createExecution } from "@metamask/smart-accounts-kit";
import { parseEther } from "viem";
// Creates an ExecutionStruct to transfer 0.01 ETH to
// 0xe3C818389583fDD5cAC32f548140fE26BcEaE907 address.
const execution = createExecution({
target: "0xe3C818389583fDD5cAC32f548140fE26BcEaE907",
// 0.01 ETH in wei
value: parseEther("0.01"),
callData: "0x",
});
decodeDelegations
Decodes an ABI-encoded hex string to an array of delegations.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
encoded | Hex | Yes | The ABI encoded hex string to decode. |