Skip to content

Commit 5689fb2

Browse files
AyushBherwani1998AyushBherwani1998alexandratran
authored
Improve tutorial
* improve tutorial * apply reviewer suggestion Co-authored-by: Alexandra Carrillo <[email protected]> --------- Co-authored-by: AyushBherwani1998 <“[email protected]”> Co-authored-by: Alexandra Carrillo <[email protected]>
1 parent 6a0d2a8 commit 5689fb2

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/pages/tutorials/create-custom-caveat-enforcer.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ npm install @metamask/delegation-toolkit
4242
### 2. Create the caveat enforcer
4343

4444
At the root of your project, create a `contracts` directory.
45-
In that directory, create a new contract named `AfterTimestampEnforcer.sol`.
45+
Inside that directory, create a new contract named `AfterTimestampEnforcer.sol`.
4646

47-
Add the following code to `AfterTimestampEnforcer.sol`, which creates a caveat enforcer that extends the
48-
[`ICaveatEnforcer.sol`](https://github.com/MetaMask/delegation-framework/blob/main/src/interfaces/ICaveatEnforcer.sol)
49-
interface and only allows a delegation to be redeemed after a specific timestamp:
47+
Add the following code to `AfterTimestampEnforcer.sol`. This contract implements a caveat enforcer that
48+
extends the `ICaveatEnforcer.sol` interface and ensures that a delegation can only be redeemed after
49+
a specific timestamp.
50+
51+
This contract overrides the `beforeHook` function, which is responsible for enforcing
52+
conditions before a delegation's execution during the redemption process. In this example, it verifies that
53+
the current block timestamp is later than the defined allowed timestamp.
5054

5155
```solidity title="AfterTimestampEnforcer.sol"
5256
// SPDX-License-Identifier: MIT
@@ -77,7 +81,10 @@ contract AfterTimestampEnforcer is CaveatEnforcer {
7781
// This function MUST revert if the conditions are not met.
7882
// Get the current timestamp
7983
uint256 timestamp = block.timestamp;
80-
84+
85+
// Convert the encoded `terms` into a uint256 timestamp.
86+
// Casting to bytes32 ensures the data is exactly 32 bytes, matching
87+
// the size of a uint256.
8188
uint256 validAfter = uint256(bytes32(_terms));
8289
8390
require(timestamp > validAfter, "AfterTimestampEnforcer:cannot-redeem-too-early");
@@ -104,15 +111,15 @@ The Forge CLI will display the address of the deployed caveat enforcer.
104111
Specify the address of the deployed `AfterTimestampEnforcer.sol` contract, add it to the [caveat builder](/delegation-toolkit/reference/delegation/#createcaveatbuilder), and create a delegation.
105112

106113
The following code snippet uses the custom caveat enforcer to create a delegation granting
107-
a 1,000,000 wei allowance that becomes spendable one hour after it is created:
114+
a 0.01 ETH allowance that becomes spendable one hour after it is created:
108115

109116
<Tabs>
110117
<TabItem value="delegation.ts">
111118

112119
```typescript
113120
import { createDelegation, ROOT_AUTHORITY } from '@metamask/delegation-toolkit'
114121
import { createCaveatBuilder } from '@metamask/delegation-toolkit/utils'
115-
import { toHex } from 'viem'
122+
import { toHex, parseEther } from 'viem'
116123
import { delegatorSmartAccount } from './config.ts'
117124

118125
const environment = delegatorSmartAccount.environment
@@ -122,11 +129,15 @@ const afterTimestampEnforcer = '0x22Ae4c4919C3aB4B5FC309713Bf707569B74876F'
122129

123130
const caveatBuilder = createCaveatBuilder(environment)
124131

125-
const tenAM = 10 * 60 * 60 // 10:00 AM as seconds since midnight.
132+
// Since block.timestamp is in seconds, convert milliseconds to seconds.
133+
const currentTime = Math.floor(Date.now() / 1000)
134+
135+
// Add an hour to the currentTime
136+
const validTimestamp = currentTime + 3600
126137

127-
const caveats = caveatBuilder.addCaveat('nativeTokenTransferAmount', 1000000n).addCaveat({
138+
const caveats = caveatBuilder.addCaveat('nativeTokenTransferAmount', parseEther('0.01')).addCaveat({
128139
enforcer: afterTimestampEnforcer,
129-
terms: toHex(tenAM),
140+
terms: toHex(validTimestamp),
130141
})
131142

132143
const delegation: Delegation = {

0 commit comments

Comments
 (0)