Skip to content

Commit fede0be

Browse files
AyushBherwani1998AyushBherwani1998
andauthored
improve code snippets (#2384)
Co-authored-by: AyushBherwani1998 <“[email protected]”>
1 parent 3bdab36 commit fede0be

File tree

10 files changed

+154
-63
lines changed

10 files changed

+154
-63
lines changed

delegation-toolkit/guides/delegation/check-delegation-state.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,18 @@ const { availableAmount } = await caveatEnforcerClient.getErc20PeriodTransferEnf
8282
8383
```typescript
8484
import { createDelegation } from '@metamask/delegation-toolkit'
85+
import { parseUnits } from 'viem'
86+
87+
// startDate should be in seconds.
88+
const startDate = Math.floor(Date.now() / 1000);
8589

8690
export const delegation = createDelegation({
8791
scope: {
8892
type: 'erc20PeriodTransfer',
8993
tokenAddress: '0xb4aE654Aca577781Ca1c5DE8FbE60c2F423f37da',
90-
periodAmount: 1000000000000000000n,
94+
periodAmount: parseUnits('10', 6),
9195
periodDuration: 86400,
92-
startDate: 1743763600,
96+
startDate,
9397
},
9498
to: delegateAccount,
9599
from: delegatorAccount,

delegation-toolkit/guides/delegation/execute-on-smart-accounts-behalf.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ Before creating a delegation, ensure that the delegator account (in this example
137137

138138
```typescript
139139
import { createDelegation } from "@metamask/delegation-toolkit"
140+
import { parseUnits } from "viem"
140141

141142
// USDC address on Ethereum Sepolia.
142-
const tokenAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
143+
const tokenAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"
143144

144145
const delegation = createDelegation({
145146
to: delegateSmartAccount.address, // This example uses a delegate smart account
@@ -148,7 +149,8 @@ const delegation = createDelegation({
148149
scope: {
149150
type: "erc20TransferAmount",
150151
tokenAddress,
151-
maxAmount: 10000000n,
152+
// 10 USDC
153+
maxAmount: parseUnits("10", 6),
152154
},
153155
})
154156
```
@@ -175,7 +177,7 @@ Bob can now redeem the delegation. The redeem transaction is sent to the `Delega
175177
To prepare the calldata for the redeem transaction, use the [`redeemDelegations`](../../reference/delegation/index.md#redeemdelegations) method from `DelegationManager`.
176178
Since Bob is redeeming a single delegation chain, use the [`SingleDefault`](../../concepts/delegation/index.md#execution-modes) execution mode.
177179

178-
Bob can redeem the delegation by submitting a user operation if his account is a smart account, or a regular transaction if his account is an EOA:
180+
Bob can redeem the delegation by submitting a user operation if his account is a smart account, or a regular transaction if his account is an EOA. In this example, Bob transfers 1 USDC from Alice’s account to his own.
179181

180182
<Tabs>
181183
<TabItem value="Redeem with a smart account">
@@ -184,10 +186,14 @@ Bob can redeem the delegation by submitting a user operation if his account is a
184186
import { createExecution, ExecutionMode } from "@metamask/delegation-toolkit"
185187
import { DelegationManager } from "@metamask/delegation-toolkit/contracts"
186188
import { zeroAddress } from "viem"
189+
import { callData } from "./config.ts"
187190

188191
const delegations = [signedDelegation]
189192

190-
const executions = createExecution({ target: zeroAddress })
193+
// USDC address on Ethereum Sepolia.
194+
const tokenAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"
195+
196+
const executions = createExecution({ target: tokenAddress, callData })
191197

192198
const redeemDelegationCalldata = DelegationManager.encode.redeemDelegations({
193199
delegations: [delegations],
@@ -215,10 +221,14 @@ const userOperationHash = await bundlerClient.sendUserOperation({
215221
import { createExecution, getDeleGatorEnvironment, ExecutionMode } from "@metamask/delegation-toolkit"
216222
import { DelegationManager } from "@metamask/delegation-toolkit/contracts"
217223
import { zeroAddress } from "viem"
224+
import { callData } from "./config.ts"
218225

219226
const delegations = [signedDelegation]
220227

221-
const executions = createExecution({ target: zeroAddress })
228+
// USDC address on Ethereum Sepolia.
229+
const tokenAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"
230+
231+
const executions = createExecution({ target: tokenAddress, callData })
222232

223233
const redeemDelegationCalldata = DelegationManager.encode.redeemDelegations({
224234
delegations: [delegations],
@@ -233,6 +243,21 @@ const transactionHash = await delegateWalletClient.sendTransaction({
233243
})
234244
```
235245

246+
</TabItem>
247+
248+
<TabItem value="config.ts">
249+
250+
```typescript
251+
import { encodeFunctionData, erc20Abi, parseUnits } from "viem"
252+
253+
// calldata to transfer 1 USDC to delegate address.
254+
export const callData = encodeFunctionData({
255+
abi: erc20Abi,
256+
args: [ delegateSmartAccount.address, parseUnits("1", 6) ],
257+
functionName: 'transfer',
258+
})
259+
```
260+
236261
</TabItem>
237262
</Tabs>
238263

delegation-toolkit/guides/delegation/use-delegation-scopes/spending-limit.md

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,19 @@ Internally, this scope uses the [`erc20PeriodTransfer`](../../../reference/deleg
2929

3030
```typescript
3131
import { createDelegation } from "@metamask/delegation-toolkit";
32+
import { parseUnits } from "viem";
33+
34+
// startDate should be in seconds.
35+
const startDate = Math.floor(Date.now() / 1000);
3236

3337
const delegation = createDelegation({
3438
scope: {
3539
type: "erc20PeriodTransfer",
3640
tokenAddress: "0xb4aE654Aca577781Ca1c5DE8FbE60c2F423f37da",
37-
periodAmount: 1000000000000000000n,
41+
// USDC has 6 decimal places.
42+
periodAmount: parseUnits("10", 6),
3843
periodDuration: 86400,
39-
startDate: 1743763600,
44+
startDate,
4045
},
4146
to: delegateAccount,
4247
from: delegatorAccount,
@@ -57,15 +62,20 @@ Internally, this scope uses the [`erc20Streaming`](../../../reference/delegation
5762

5863
```typescript
5964
import { createDelegation } from "@metamask/delegation-toolkit";
65+
import { parseUnits } from "viem";
66+
67+
// startTime should be in seconds.
68+
const startTime = Math.floor(Date.now() / 1000);
6069

6170
const delegation = createDelegation({
6271
scope: {
6372
type: "erc20Streaming",
6473
tokenAddress: "0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92",
65-
amountPerSecond: 100n,
66-
initialAmount: 1000000n,
67-
maxAmount: 10000000n,
68-
startTime: 1703980800,
74+
// USDC has 6 decimal places.
75+
amountPerSecond: parseUnits("0.1", 6),
76+
initialAmount: parseUnits("10", 6),
77+
maxAmount: parseUnits("100", 6),
78+
startTime,
6979
},
7080
to: delegateAccount,
7181
from: delegatorAccount,
@@ -86,12 +96,14 @@ Internally, this scope uses the [`erc20TransferAmount`](../../../reference/deleg
8696

8797
```typescript
8898
import { createDelegation } from "@metamask/delegation-toolkit";
99+
import { parseUnits } from "viem";
89100

90101
const delegation = createDelegation({
91102
scope: {
92103
type: "erc20TransferAmount",
93104
tokenAddress: "0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92",
94-
maxAmount: 10000n,
105+
// USDC has 6 decimal places.
106+
maxAmount: parseUnits("10", 6),
95107
},
96108
to: delegateAccount,
97109
from: delegatorAccount,
@@ -135,13 +147,17 @@ Internally, this scope uses the [`exactCalldata`](../../../reference/delegation/
135147

136148
```typescript
137149
import { createDelegation } from "@metamask/delegation-toolkit";
150+
import { parseEther } from "viem";
151+
152+
// startDate should be in seconds.
153+
const startDate = Math.floor(Date.now() / 1000);
138154

139155
const delegation = createDelegation({
140156
scope: {
141157
type: "nativeTokenPeriodTransfer",
142-
periodAmount: 1000000000000000000n,
158+
periodAmount: parseEther("0.01"),
143159
periodDuration: 86400,
144-
startDate: 1743763600,
160+
startDate,
145161
},
146162
to: delegateAccount,
147163
from: delegatorAccount,
@@ -162,14 +178,18 @@ Internally, this scope uses the [`exactCalldata`](../../../reference/delegation/
162178

163179
```typescript
164180
import { createDelegation } from "@metamask/delegation-toolkit";
181+
import { parseEther } from "viem";
182+
183+
// startTime should be in seconds.
184+
const startTime = Math.floor(Date.now() / 1000);
165185

166186
const delegation = createDelegation({
167187
scope: {
168188
type: "nativeTokenStreaming",
169-
amountPerSecond: 100n,
170-
initialAmount: 1000000n,
171-
maxAmount: 10000000n,
172-
startTime: 1703980800,
189+
amountPerSecond: parseEther("0.001"),
190+
initialAmount: parseEther("0.01"),
191+
maxAmount: parseEther("0.1"),
192+
startTime,
173193
},
174194
to: delegateAccount,
175195
from: delegatorAccount,
@@ -190,12 +210,12 @@ Internally, this scope uses the [`exactCalldata`](../../../reference/delegation/
190210

191211
```typescript
192212
import { createDelegation } from "@metamask/delegation-toolkit";
213+
import { parseEther } from "viem";
193214

194215
const delegation = createDelegation({
195216
scope: {
196217
type: "nativeTokenTransferAmount",
197-
// 0.001 ETH in wei format.
198-
maxAmount: 1000000000000000n,
218+
maxAmount: parseEther("0.001"),
199219
},
200220
to: delegateAccount,
201221
from: delegatorAccount,

delegation-toolkit/guides/erc7715/execute-on-metamask-users-behalf.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,11 @@ const transactionHash = await sessionAccountWalletClient.sendTransactionWithDele
252252
<TabItem value="config.ts">
253253

254254
```typescript
255-
import { encodeFunctionData, erc20Abi } from "viem";
255+
import { encodeFunctionData, erc20Abi, parseUnits } from "viem";
256256

257257
export const calldata = encodeFunctionData({
258258
abi: erc20Abi,
259-
args: [ sessionAccount.address, 1000000n ],
259+
args: [ sessionAccount.address, parseUnits("1", 6) ],
260260
functionName: 'transfer',
261261
});
262262
```

delegation-toolkit/guides/smart-accounts/deploy-smart-account.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const userOperationHash = await bundlerClient.sendUserOperation({
3838
calls: [
3939
{
4040
to: "0x1234567890123456789012345678901234567890",
41-
value: parseEther("1")
41+
value: parseEther("0.001"),
4242
}
4343
],
4444
maxFeePerGas,

delegation-toolkit/guides/smart-accounts/send-gasless-transaction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const userOperationHash = await bundlerClient.sendUserOperation({
3939
calls: [
4040
{
4141
to: "0x1234567890123456789012345678901234567890",
42-
value: parseEther("1")
42+
value: parseEther("0.001")
4343
}
4444
],
4545
maxFeePerGas,

delegation-toolkit/guides/smart-accounts/send-user-operation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const userOperationHash = await bundlerClient.sendUserOperation({
5252
calls: [
5353
{
5454
to: "0x1234567890123456789012345678901234567890",
55-
value: parseEther("1")
55+
value: parseEther("0.001")
5656
}
5757
],
5858
maxFeePerGas,

delegation-toolkit/reference/delegation/caveat-enforcer-client.md

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,16 @@ import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'
9292

9393
const environment = getDeleGatorEnvironment(chain.id)
9494

95+
// Since current time is in seconds, we need to convert milliseconds to seconds.
96+
const startDate = Math.floor(Date.now() / 1000)
97+
9598
export const delegation = createDelegation({
9699
scope: {
97100
type: 'erc20PeriodTransfer',
98101
tokenAddress: '0xb4aE654Aca577781Ca1c5DE8FbE60c2F423f37da',
99-
periodAmount: 1000000000000000000n,
102+
periodAmount: parseUnits('10', 6),
100103
periodDuration: 86400,
101-
startDate: 1743763600,
104+
startDate,
102105
},
103106
to: 'DELEGATE_ADDRESS',
104107
from: 'DELEGATOR_ADDRESS',
@@ -140,17 +143,21 @@ const { availableAmount } = await caveatEnforcerClient.getErc20StreamingEnforcer
140143
import { createDelegation } from '@metamask/delegation-toolkit'
141144
import { sepolia as chain } from 'viem/chains'
142145
import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'
146+
import { parseUnits } from 'viem'
143147

144148
const environment = getDeleGatorEnvironment(chain.id)
145149

150+
// Since current time is in seconds, we need to convert milliseconds to seconds.
151+
const startTime = Math.floor(Date.now() / 1000)
152+
146153
export const delegation = createDelegation({
147154
scope: {
148155
type: 'erc20Streaming',
149156
tokenAddress: '0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92',
150-
amountPerSecond: 100n,
151-
initialAmount: 1000000n,
152-
maxAmount: 10000000n,
153-
startTime: 1703980800,
157+
amountPerSecond: parseUnits('0.1', 6),
158+
initialAmount: parseUnits('1', 6),
159+
maxAmount: parseUnits('10', 6),
160+
startTime,
154161
},
155162
to: 'DELEGATE_ADDRESS',
156163
from: 'DELEGATOR_ADDRESS',
@@ -191,16 +198,20 @@ const { availableAmount } = await caveatEnforcerClient.getNativeTokenPeriodTrans
191198
```typescript
192199
import { createDelegation } from '@metamask/delegation-toolkit'
193200
import { sepolia as chain } from 'viem/chains'
201+
import { parseEther } from 'viem'
194202
import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'
195203

196204
const environment = getDeleGatorEnvironment(chain.id)
197205

206+
// Since current time is in seconds, we need to convert milliseconds to seconds.
207+
const startDate = Math.floor(Date.now() / 1000)
208+
198209
export const delegation = createDelegation({
199210
scope: {
200211
type: 'nativeTokenPeriodTransfer',
201-
periodAmount: 1000000000000000000n,
212+
periodAmount: parseEther('0.01', 6),
202213
periodDuration: 86400,
203-
startDate: 1743763600,
214+
startDate,
204215
},
205216
to: 'DELEGATE_ADDRESS',
206217
from: 'DELEGATOR_ADDRESS',
@@ -245,13 +256,16 @@ import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'
245256

246257
const environment = getDeleGatorEnvironment(chain.id)
247258

259+
// Since current time is in seconds, we need to convert milliseconds to seconds.
260+
const startTime = Math.floor(Date.now() / 1000)
261+
248262
export const delegation = createDelegation({
249263
scope: {
250264
type: "nativeTokenStreaming",
251-
amountPerSecond: 100n,
252-
initialAmount: 1000000n,
253-
maxAmount: 10000000n,
254-
startTime: 1703980800,
265+
amountPerSecond: parseEther('0.001'),
266+
initialAmount: parseEther('0.01'),
267+
maxAmount: parseEther('0.1'),
268+
startTime,
255269
},
256270
to: 'DELEGATE_ADDRESS',
257271
from: 'DELEGATOR_ADDRESS',
@@ -300,6 +314,7 @@ const { availableAmount } = await caveatEnforcerClient.getMultiTokenPeriodEnforc
300314
import { createDelegation, getDeleGatorEnvironment, ROOT_AUTHORITY } from '@metamask/delegation-toolkit'
301315
import { createCaveatBuilder } from '@metamask/delegation-toolkit/utils'
302316
import { sepolia as chain } from 'viem/chains'
317+
import { parseUnits, parseEther } from 'viem'
303318

304319
const environment = getDeleGatorEnvironment(chain.id)
305320
const caveatBuilder = createCaveatBuilder(environment)
@@ -311,8 +326,8 @@ const startDate = Math.floor(Date.now() / 1000);
311326
const tokenConfigs = [
312327
{
313328
token: "0xb4aE654Aca577781Ca1c5DE8FbE60c2F423f37da",
314-
// 1 token with 18 decimals.
315-
periodAmount: 1000000000000000000n,
329+
// 1 token with 6 decimals.
330+
periodAmount: parseUnits('1', 6),
316331
// 1 day in seconds.
317332
periodDuration: 86400,
318333
startDate
@@ -321,14 +336,14 @@ const tokenConfigs = [
321336
// For native token use zeroAddress
322337
token: zeroAddress,
323338
// 0.01 ETH in wei.
324-
periodAmount: 10000000000000000n,
339+
periodAmount: parseEther('0.01'),
325340
// 1 hour in seconds.
326341
periodDuration: 3600,
327342
startDate
328343
}
329344
]
330345

331-
const caveats = caveatBuilder.addCaveat('nativeTokenTransferAmount', 1000000n).addCaveat({
346+
const caveats = caveatBuilder.addCaveat({
332347
'multiTokenPeriod',
333348
tokenConfigs
334349
})

0 commit comments

Comments
 (0)