Skip to content

Commit 3975e1e

Browse files
committed
Add custom caveat enforcer tutorial
1 parent 2696e2b commit 3975e1e

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"label": "Tutorials",
3+
"position": 5
4+
}

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

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

@@ -8,24 +8,31 @@ 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/caveat-enforcers.md) and applying it to a [delegation](../concepts/delegation.md).
12+
13+
The MetaMask Delegation Toolkit provides some [out-of-the-box caveat enforcers](../reference/caveats.md) that define rules and restrictions for common use cases.
14+
For more granular or custom control, you can create custom caveat enforcers from scratch.
15+
This tutorial walks you through creating and applying 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)
19+
- [Install and set up the Delegation Toolkit](../../get-started/install.md) in your project.
1820
- [Configure the Delegation Toolkit.](../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
32+
In your project's `src` directory, create a contract that extends the
2533
[`ICaveatEnforcer.sol`](https://github.com/MetaMask/delegation-framework/blob/main/src/interfaces/ICaveatEnforcer.sol)
2634
interface.
27-
28-
For example, the following is a simple caveat enforcer that only allows a delegation to be redeemed after a specific timestamp.
35+
The following `AfterTimestampEnforcer.sol` caveat enforcer only allows a delegation to be redeemed after a specific timestamp:
2936

3037
```solidity title="AfterTimestampEnforcer.sol"
3138
// SPDX-License-Identifier: MIT
@@ -66,23 +73,28 @@ contract AfterTimestampEnforcer is CaveatEnforcer {
6673

6774
### 2. Deploy the caveat enforcer
6875

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).
76+
Deploy your custom caveat enforcer using [Forge](https://book.getfoundry.sh/forge/deploying) to obtain its contract address.
77+
Replace `<YOUR-API-KEY>` with your Infura API key, and `<YOUR-PRIVATE-KEY>` with the private key of your MetaMask account:
7178

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

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).
86+
The Forge CLI will display the address of the deployed caveat enforcer.
7687

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.
88+
### 3. Apply the caveat enforcer
7989

80-
:::warning Important
81-
Depending on the use case, it may be necessary to add additional caveats to restrict the delegation further.
82-
:::
90+
Specify the address where your `AfterTimestampEnforcer.sol` contract is deployed, add it to the caveat builder, and create a delegation.
91+
Learn more about [applying caveats to a delegation](../guides/create-delegation/restrict-delegation.md).
92+
93+
The following code snippet uses the custom caveat enforcer to create a delegation granting
94+
an allowance of 1,000,000 wei that can only be spent after one hour from when the delegation is created:
8395

8496
<Tabs>
85-
<TabItem value="example.ts">
97+
<TabItem value="delegation.ts">
8698

8799
```typescript
88100
import {
@@ -94,7 +106,7 @@ import { delegatorSmartAccount } from "./config.ts";
94106

95107
const environment = delegatorSmartAccount.environment;
96108

97-
// Replace this with the address where the AfterTimestampEnforcer.sol contract is deployed.
109+
// Replace this with the address where your AfterTimestampEnforcer.sol contract is deployed.
98110
const afterTimestampEnforcer = "0x22Ae4c4919C3aB4B5FC309713Bf707569B74876F";
99111

100112
const caveatBuilder = createCaveatBuilder(environment);
@@ -115,7 +127,6 @@ const delegation = createDelegation({
115127
});
116128
```
117129

118-
119130
</TabItem>
120131

121132
<TabItem value="config.ts">
@@ -147,3 +158,8 @@ export const delegatorSmartAccount = await toMetaMaskSmartAccount({
147158

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

vercel.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,10 @@
754754
"source": "/delegation-toolkit/development/get-started/cli-quickstart/",
755755
"destination": "/delegation-toolkit/development/get-started/use-the-cli/"
756756
},
757+
{
758+
"source": "/delegation-toolkit/development/guides/create-delegation/create-custom-caveat-enforcer/",
759+
"destination": "/delegation-toolkit/development/tutorials/create-custom-caveat-enforcer/"
760+
},
757761
{
758762
"source": "/developer-tools/faucet/sepolia/",
759763
"destination": "/developer-tools/faucet/"

0 commit comments

Comments
 (0)