Skip to content

Commit e68e3c6

Browse files
Add custom caveat enforcer tutorial (#2253)
* Add custom caveat enforcer tutorial * address reviewer comments * fix links * address reviewer comments
1 parent cc78af4 commit e68e3c6

File tree

5 files changed

+53
-31
lines changed

5 files changed

+53
-31
lines changed

delegation-toolkit/concepts/delegation/caveat-enforcers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ When designing delegations with caveats, consider these best practices:
152152

153153
## Available caveat enforcers
154154

155-
The Delegation Toolkit provides [many out-of-the-box caveat enforcers](../../reference/caveats.md)
155+
The Delegation Toolkit provides [out-of-the-box caveat enforcers](../../reference/caveats.md)
156156
for common restriction patterns, including:
157157

158158
- Limiting target addresses and methods.
159159
- Setting time or block number constraints.
160160
- Restricting token transfers and approvals.
161161
- Limiting execution frequency.
162162

163-
For more complex scenarios, you can also [create custom caveat enforcers](../../guides/delegation/create-custom-caveat-enforcer.md) by implementing the `ICaveatEnforcer` interface.
163+
For other restriction patterns, you can also [create custom caveat enforcers](../../tutorials/create-custom-caveat-enforcer.md) by implementing the `ICaveatEnforcer` interface.
164164

165165
## Attenuating authority with redelegations
166166

delegation-toolkit/get-started/install.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This page provides instructions to install and set up the MetaMask Delegation To
1717
- Install [Yarn](https://yarnpkg.com/),
1818
[npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), or another package manager.
1919
- If you plan to use any smart contracts (for example, to
20-
[create a custom caveat enforcer](../guides/delegation/create-custom-caveat-enforcer.md)),
20+
[create a custom caveat enforcer](../tutorials/create-custom-caveat-enforcer.md)),
2121
install [Foundry](https://book.getfoundry.sh/getting-started/installation).
2222

2323
## Steps
@@ -33,7 +33,7 @@ npm install @metamask/delegation-toolkit
3333
### 2. (Optional) Install the contracts
3434

3535
If you plan to extend the Delegation Framework smart contracts (for example, to
36-
[create a custom caveat enforcer](../guides/delegation/create-custom-caveat-enforcer.md)), install
36+
[create a custom caveat enforcer](../tutorials/create-custom-caveat-enforcer.md)), install
3737
the contract package using Foundry's command-line tool, Forge:
3838

3939
```bash

delegation-toolkit/guides/delegation/restrict-delegation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,5 @@ const delegation = createDelegation({
105105
});
106106
```
107107

108-
For more granular or custom control, you can also [create custom caveat enforcers](create-custom-caveat-enforcer.md)
108+
For more specific or custom control, you can also [create custom caveat enforcers](../../tutorials/create-custom-caveat-enforcer.md)
109109
and add them to the caveat builder.

delegation-toolkit/guides/delegation/create-custom-caveat-enforcer.md renamed to delegation-toolkit/tutorials/create-custom-caveat-enforcer.md

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
11
---
2-
description: Learn how to create, deploy, and apply a custom caveat enforcer
3-
sidebar_position: 3
2+
description: Follow this tutorial to create, deploy, and apply a custom caveat enforcer for a delegation.
3+
sidebar_position: 2
44
---
55

66
import Tabs from "@theme/Tabs";
77
import TabItem from "@theme/TabItem";
88

99
# Create a custom caveat enforcer
1010

11-
When [restricting a delegation](restrict-delegation.md), the MetaMask Delegation Toolkit provides some [out-of-the-box caveat enforcers](../../reference/caveats.md)
12-
that cover common use cases.
13-
For more granular or custom control, you can follow the instructions on this page to create custom caveat enforcers from scratch.
11+
This tutorial walks you through creating a custom [caveat enforcer](../concepts/delegation/caveat-enforcers.md) and applying it to a [delegation](../concepts/delegation/index.md).
12+
13+
The MetaMask Delegation Toolkit includes [out-of-the-box caveat enforcers](../reference/caveats.md) that define rules and restrictions for common use cases.
14+
For more specific control or other use cases, you can create custom caveat enforcers.
15+
In this tutorial, you'll create and apply a caveat enforcer that only allows a delegation to be redeemed after a specific timestamp.
1416

1517
## Prerequisites
1618

17-
- [Install and set up the Delegation Toolkit.](../../get-started/install.md)
18-
- [Configure the Delegation Toolkit.](../configure.md)
19+
- [Install and set up the Delegation Toolkit](../get-started/install.md) in your project.
20+
- [Configure the Delegation Toolkit.](../guides/configure.md)
21+
- [Install Foundry and Forge.](https://getfoundry.sh/introduction/installation)
22+
- Get an [Infura API key](/developer-tools/dashboard/get-started/create-api) from the MetaMask Developer dashboard.
23+
- Have a MetaMask account with some Sepolia ETH to deploy your contract.
24+
:::note
25+
You can use the [MetaMask faucet](/developer-tools/faucet) to get Sepolia ETH.
26+
:::
1927

2028
## Steps
2129

2230
### 1. Create the caveat enforcer
2331

24-
Create a contract that extends the
25-
[`ICaveatEnforcer.sol`](https://github.com/MetaMask/delegation-framework/blob/main/src/interfaces/ICaveatEnforcer.sol)
26-
interface.
32+
At the root of your project, create a `contracts` directory.
33+
In that directory, create a new contract named `AfterTimestampEnforcer.sol`.
2734

28-
For example, the following is a simple caveat enforcer that only allows a delegation to be redeemed after a specific timestamp.
35+
Add the following code to `AfterTimestampEnforcer.sol`, which creates a caveat enforcer that extends the
36+
[`ICaveatEnforcer.sol`](https://github.com/MetaMask/delegation-framework/blob/main/src/interfaces/ICaveatEnforcer.sol)
37+
interface and only allows a delegation to be redeemed after a specific timestamp:
2938

3039
```solidity title="AfterTimestampEnforcer.sol"
3140
// SPDX-License-Identifier: MIT
@@ -66,23 +75,28 @@ contract AfterTimestampEnforcer is CaveatEnforcer {
6675

6776
### 2. Deploy the caveat enforcer
6877

69-
Deploy your custom caveat enforcer to obtain its contract address.
70-
For example, you can [deploy your smart contract using Forge](https://book.getfoundry.sh/forge/deploying).
78+
Deploy your custom caveat enforcer using [Forge](https://book.getfoundry.sh/forge/deploying) to obtain its contract address.
79+
Replace `<YOUR-API-KEY>` with your Infura API key, and `<YOUR-PRIVATE-KEY>` with the private key of your MetaMask account:
7180

72-
### 3. Apply the caveat enforcer
81+
```bash
82+
forge create src/AfterTimestampEnforcer.sol:AfterTimestampEnforcer \
83+
--rpc-url https://sepolia.infura.io/v3/<YOUR-API-KEY> \
84+
--private-key <YOUR-PRIVATE-KEY> \
85+
--broadcast
86+
```
7387

74-
When creating a delegation, add the `Caveat` for the custom caveat to the `CaveatBuilder`.
75-
Learn more about [applying caveats to a delegation](restrict-delegation.md).
88+
The Forge CLI will display the address of the deployed caveat enforcer.
7689

77-
The following example uses the custom `AfterTimestampEnforcer.sol` caveat enforcer to create a delegation granting
78-
an allowance of 1,000,000 wei that can only be spent after one hour from when the delegation is created.
90+
### 3. Apply the caveat enforcer
91+
92+
Specify the address of the deployed `AfterTimestampEnforcer.sol` contract, add it to the caveat builder, and create a delegation.
93+
Learn more about [applying caveats to a delegation](../guides/delegation/restrict-delegation.md).
7994

80-
:::warning Important
81-
Depending on the use case, it may be necessary to add additional caveats to restrict the delegation further.
82-
:::
95+
The following code snippet uses the custom caveat enforcer to create a delegation granting
96+
a 1,000,000 wei allowance that becomes spendable one hour after it is created:
8397

8498
<Tabs>
85-
<TabItem value="example.ts">
99+
<TabItem value="delegation.ts">
86100

87101
```typescript
88102
import {
@@ -94,7 +108,7 @@ import { delegatorSmartAccount } from "./config.ts";
94108

95109
const environment = delegatorSmartAccount.environment;
96110

97-
// Replace this with the address where the AfterTimestampEnforcer.sol contract is deployed.
111+
// Replace this with the address of the deployed AfterTimestampEnforcer.sol contract.
98112
const afterTimestampEnforcer = "0x22Ae4c4919C3aB4B5FC309713Bf707569B74876F";
99113

100114
const caveatBuilder = createCaveatBuilder(environment);
@@ -115,7 +129,6 @@ const delegation = createDelegation({
115129
});
116130
```
117131

118-
119132
</TabItem>
120133

121134
<TabItem value="config.ts">
@@ -147,3 +160,8 @@ export const delegatorSmartAccount = await toMetaMaskSmartAccount({
147160

148161
</TabItem>
149162
</Tabs>
163+
164+
You've successfully created, deployed, and applied a custom caveat enforcer!
165+
166+
For production use cases, you might need to add additional caveats to restrict the delegation further.
167+
Learn more about [caveat enforcers](../concepts/delegation/caveat-enforcers.md).

vercel.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,8 +735,8 @@
735735
"destination": "/delegation-toolkit/development/guides/delegation/execute-on-smart-accounts-behalf/"
736736
},
737737
{
738-
"source": "/delegation-toolkit/development/how-to/create-delegation/:path*/",
739-
"destination": "/delegation-toolkit/development/guides/delegation/:path*/"
738+
"source": "/delegation-toolkit/development/how-to/create-delegation/restrict-delegation/",
739+
"destination": "/delegation-toolkit/development/guides/delegation/restrict-delegation/"
740740
},
741741
{
742742
"source": "/delegation-toolkit/development/how-to/redeem-delegation/",
@@ -755,6 +755,10 @@
755755
"destination": "/delegation-toolkit/development/get-started/use-the-cli/"
756756
},
757757
{
758+
"source": "/delegation-toolkit/development/how-to/create-delegation/create-custom-caveat-enforcer/",
759+
"destination": "/delegation-toolkit/development/tutorials/create-custom-caveat-enforcer/"
760+
},
761+
{
758762
"source": "/delegation-toolkit/development/concepts/caveat-enforcers/",
759763
"destination": "/delegation-toolkit/development/concepts/delegation/caveat-enforcers/"
760764
},

0 commit comments

Comments
 (0)