@@ -42,11 +42,15 @@ npm install @metamask/delegation-toolkit
4242### 2. Create the caveat enforcer
4343
4444At 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.
104111Specify the address of the deployed ` AfterTimestampEnforcer.sol ` contract, add it to the [ caveat builder] ( /delegation-toolkit/reference/delegation/#createcaveatbuilder ) , and create a delegation.
105112
106113The 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
113120import { createDelegation , ROOT_AUTHORITY } from ' @metamask/delegation-toolkit'
114121import { createCaveatBuilder } from ' @metamask/delegation-toolkit/utils'
115- import { toHex } from ' viem'
122+ import { toHex , parseEther } from ' viem'
116123import { delegatorSmartAccount } from ' ./config.ts'
117124
118125const environment = delegatorSmartAccount .environment
@@ -122,11 +129,15 @@ const afterTimestampEnforcer = '0x22Ae4c4919C3aB4B5FC309713Bf707569B74876F'
122129
123130const 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' , 1000000 n ).addCaveat ({
138+ const caveats = caveatBuilder .addCaveat (' nativeTokenTransferAmount' , parseEther ( ' 0.01 ' ) ).addCaveat ({
128139 enforcer: afterTimestampEnforcer ,
129- terms: toHex (tenAM ),
140+ terms: toHex (validTimestamp ),
130141})
131142
132143const delegation: Delegation = {
0 commit comments