Skip to content

Commit d0e365d

Browse files
AyushBherwani1998alexandratranAyushBherwani1998
authored
Update creating delegation references for v0.13
* Minor DTK fixes (#2277) * Minor DTK fixes * Apply suggestions from code review Co-authored-by: Ayush Bherwani <ayush.bherwani1998@gmail.com> --------- Co-authored-by: Ayush Bherwani <ayush.bherwani1998@gmail.com> * Fix broken links (#2278) * update execute on smart accounts behalf guide * add parameters in delegation scopes * update reference docs * update guide: execute-on-smart-accounts-behalf * Apply suggestions from code review Co-authored-by: Alexandra Carrillo <12214231+alexandratran@users.noreply.github.com> --------- Co-authored-by: Alexandra Carrillo <12214231+alexandratran@users.noreply.github.com> Co-authored-by: AyushBherwani1998 <“ayush.bherwani1998@gmail.com”>
1 parent e897d01 commit d0e365d

File tree

7 files changed

+232
-43
lines changed

7 files changed

+232
-43
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ export const delegateWalletClient = createWalletClient({
122122
Create a [root delegation](../../concepts/delegation/index.md#delegation-types) from Alice to Bob.
123123
With a root delegation, Alice is delegating her own authority away, as opposed to *redelegating* permissions she received from a previous delegation.
124124

125-
Use the toolkit's [`createDelegation`](../../reference/api/delegation.md#createdelegation) method to create a root delegation.
126-
This example passes an empty `caveats` array, which means Bob can perform any action on Alice's behalf. We recommend [restricting the delegation](restrict-delegation.md) by adding caveat enforcers.
127-
For example, Alice can delegate the ability to spend her USDC to Bob, limiting the amount to 100 USDC.
125+
Use the toolkit's [`createDelegation`](../../reference/api/delegation.md#createdelegation) method to create a root delegation. When creating
126+
delegation, you need to configure the scope of the delegation to define the initial authority.
127+
128+
This example uses the [`erc20TransferAmount`](./use-delegation-scopes/spending-limit#erc-20-transfer-scope) scope, allowing Alice to delegate to Bob the ability to spend her USDC, with a
129+
specified limit on the total amount.
128130

129131
:::warning Important
130132

@@ -135,10 +137,18 @@ Before creating a delegation, ensure that the delegator account (in this example
135137
```typescript
136138
import { createDelegation } from "@metamask/delegation-toolkit"
137139

140+
// USDC address on Ethereum Sepolia.
141+
const tokenAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
142+
138143
const delegation = createDelegation({
139144
to: delegateSmartAccount.address, // This example uses a delegate smart account
140145
from: delegatorSmartAccount.address,
141-
caveats: [], // Empty caveats array - we recommend adding appropriate restrictions.
146+
environment: delegatorSmartAccount.environment
147+
scope: {
148+
type: "erc20TransferAmount",
149+
tokenAddress,
150+
maxAmount: 10000000n,
151+
},
142152
})
143153
```
144154

@@ -226,3 +236,8 @@ const transactionHash = await delegateWalletClient.sendTransaction({
226236

227237
</TabItem>
228238
</Tabs>
239+
240+
## Next steps
241+
242+
- See [how to configure different scopes](use-delegation-scopes/index.md) to define the initial authority of a delegation.
243+
- See [how to further refine the authority of a delegation](use-delegation-scopes/refine-scope.md) using caveat enforcers.

delegation-toolkit/guides/delegation/use-delegation-scopes/function-call.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const delegation = createDelegation({
3838
},
3939
to: delegateAccount,
4040
from: delegatorAccount,
41+
environment: delegatorAccount.environment,
4142
});
4243
```
4344

delegation-toolkit/guides/delegation/use-delegation-scopes/ownership-transfer.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const delegation = createDelegation({
3232
},
3333
to: delegateAccount,
3434
from: delegatorAccount,
35+
environment: delegatorAccount.environment,
3536
});
3637
```
3738

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const delegation = createDelegation({
3939
},
4040
to: delegateAccount,
4141
from: delegatorAccount,
42+
environment: delegatorAccount.environment,
4243
});
4344
```
4445

@@ -67,6 +68,7 @@ const delegation = createDelegation({
6768
},
6869
to: delegateAccount,
6970
from: delegatorAccount,
71+
environment: delegatorAccount.environment,
7072
});
7173
```
7274

@@ -92,6 +94,7 @@ const delegation = createDelegation({
9294
},
9395
to: delegateAccount,
9496
from: delegatorAccount,
97+
environment: delegatorAccount.environment,
9598
});
9699
```
97100

@@ -113,6 +116,7 @@ const delegation = createDelegation({
113116
},
114117
to: delegateAccount,
115118
from: delegatorAccount,
119+
environment: delegatorAccount.environment,
116120
});
117121
```
118122

@@ -140,6 +144,7 @@ const delegation = createDelegation({
140144
},
141145
to: delegateAccount,
142146
from: delegatorAccount,
147+
environment: delegatorAccount.environment,
143148
});
144149
```
145150

@@ -167,6 +172,7 @@ const delegation = createDelegation({
167172
},
168173
to: delegateAccount,
169174
from: delegatorAccount,
175+
environment: delegatorAccount.environment,
170176
});
171177
```
172178

@@ -187,10 +193,12 @@ import { createDelegation } from "@metamask/delegation-toolkit";
187193
const delegation = createDelegation({
188194
scope: {
189195
type: "nativeTokenTransferAmount",
190-
maxAmount: 1000000n,
196+
// 0.001 ETH in wei format.
197+
maxAmount: 1000000000000000n,
191198
},
192199
to: delegateAccount,
193200
from: delegatorAccount,
201+
environment: delegatorAccount.environment,
194202
});
195203
```
196204

delegation-toolkit/reference/api/delegation.md

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,30 @@ Creates a delegation with a specific delegate.
9090
| ---- | ---- | -------- | ----------- |
9191
| `from` | `Hex` | Yes | The address that is granting the delegation. |
9292
| `to` | `Hex` | Yes | The address to which the delegation is being granted. |
93-
| `caveats` | `Caveats` | Yes | Caveats to restrict the authority being granted. |
93+
| `scope` | `ScopeConfig` | Yes | The scope of the delegation that defines the initial authority. |
94+
| `environment` | `DeleGatorEnvironment` | Yes | The environment used by the toolkit to define contract addresses for interacting with the Delegation Framework contracts. |
95+
| `caveats` | `Caveats` | No | Caveats that further refine the authority granted by the `scope`. |
9496
| `parentDelegation` | `Delegation \| Hex` | No | The parent delegation or its corresponding hex to create a delegation chain. |
9597
| `salt` | `Hex` | No | The salt for generating the delegation hash. This helps prevent hash collisions when creating identical delegations. |
9698

9799
### Example
98100

99101
```typescript
100-
import { createDelegation } from "@metamask/delegation-toolkit";
102+
import { createDelegation, getDelegatorEnvironment } from "@metamask/delegation-toolkit";
103+
import { sepolia } from "viem/chains";
101104

102105
const delegation = createDelegation({
103106
// Address that is granting the delegation
104107
from: "0x7E48cA6b7fe6F3d57fdd0448B03b839958416fC1",
105108
// Address to which the delegation is being granted
106109
to: "0x2B2dBd1D5fbeB77C4613B66e9F35dBfE12cB0488",
107-
// Empty caveats array - we recommend adding appropriate restrictions
108-
caveats: [],
110+
// Alternatively you can use environment property of MetaMask smart account.
111+
environment: getDelegatorEnvironment(sepolia.id);
112+
scope: {
113+
type: "nativeTokenTransferAmount",
114+
// 0.001 ETH in wei format.
115+
maxAmount: 1000000000000000n,
116+
},
109117
});
110118
```
111119

@@ -118,21 +126,29 @@ Creates an open delegation that can be redeemed by any delegate.
118126
| Name | Type | Required | Description |
119127
| ---- | ---- | -------- | ----------- |
120128
| `from` | `Hex` | Yes | The address that is granting the delegation. |
121-
| `caveats` | `Caveats` | Yes | Caveats to restrict the authority being granted. |
129+
| `scope` | `ScopeConfig` | Yes | The scope of the delegation that defines the initial authority. |
130+
| `environment` | `DeleGatorEnvironment` | Yes | The environment used by the toolkit to define contract addresses for interacting with the Delegation Framework contracts. |
131+
| `caveats` | `Caveats` | No | Caveats that further refine the authority granted by the `scope`. |
122132
| `parentDelegation` | `Delegation \| Hex` | No | The parent delegation or its corresponding hex to create a delegation chain. |
123133
| `salt` | `Hex` | No | The salt for generating the delegation hash. This helps prevent hash collisions when creating identical delegations. |
124134

125135

126136
### Example
127137

128138
```typescript
129-
import { createOpenDelegation } from "@metamask/delegation-toolkit";
139+
import { createOpenDelegation, getDelegatorEnvironment } from "@metamask/delegation-toolkit";
140+
import { sepolia } from "viem/chains";
130141

131142
const delegation = createOpenDelegation({
132143
// Address that is granting the delegation
133144
from: "0x7E48cA6b7fe6F3d57fdd0448B03b839958416fC1",
134-
// Empty caveats array - we recommend adding appropriate restrictions
135-
caveats: [],
145+
// Alternatively you can use environment property of MetaMask smart account.
146+
environment: getDelegatorEnvironment(sepolia.id);
147+
scope: {
148+
type: "nativeTokenTransferAmount",
149+
// 0.001 ETH in wei format.
150+
maxAmount: 1000000000000000n,
151+
},
136152
});
137153
```
138154

@@ -278,12 +294,17 @@ const disableDelegationData = DelegationManager.encode.disableDelegation({
278294

279295
```ts
280296
import { createDelegation } from "@metamask/delegation-toolkit";
297+
import { sepolia } from "viem/chains";
281298

282299
export const delegation = createDelegation({
283300
from: "0x7E48cA6b7fe6F3d57fdd0448B03b839958416fC1",
284301
to: "0x2B2dBd1D5fbeB77C4613B66e9F35dBfE12cB0488",
285-
// Empty caveats array - we recommend adding appropriate restrictions
286-
caveats: [],
302+
environment: getDelegatorEnvironment(sepolia.id);
303+
scope: {
304+
type: "nativeTokenTransferAmount",
305+
// 0.001 ETH in wei format.
306+
maxAmount: 1000000000000000n,
307+
},
287308
});
288309
```
289310

@@ -432,9 +453,8 @@ import { createWalletClient } from "viem";
432453
import { privateKeyToAccount } from "viem/accounts";
433454
import { sepolia } from "viem/chains";
434455

435-
export const delegationManager = getDeleGatorEnvironment(
436-
sepolia.id
437-
).DelegationManager;
456+
const environment = getDelegatorEnvironment(sepolia.id);
457+
export const delegationManager = environment.DelegationManager;
438458

439459
const account = privateKeyToAccount(delegateWallet as `0x${string}`);
440460

@@ -451,8 +471,12 @@ const delegate = "0x2FcB88EC2359fA635566E66415D31dD381CF5585";
451471
export const delegation = createDelegation({
452472
to: delegate,
453473
from: account.address,
454-
// Empty caveats array - we recommend adding appropriate restrictions.
455-
caveats: [],
474+
environment,
475+
scope: {
476+
type: "nativeTokenTransferAmount",
477+
// 0.001 ETH in wei format.
478+
maxAmount: 1000000000000000n,
479+
},
456480
});
457481
```
458482

delegation-toolkit/reference/api/smart-account.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Signs the delegation and returns the delegation signature.
288288
<TabItem value ="example.ts">
289289

290290
```ts
291-
import { createDelegation } from "@metamask/delegation-toolkit";
291+
import { createDelegation, getDelegatorEnvironment } from "@metamask/delegation-toolkit";
292292
import { delegatorSmartAccount } from "./config.ts";
293293

294294
// The address to which the delegation is granted. It can be an EOA address, or
@@ -298,8 +298,12 @@ const delegate = "0x2FcB88EC2359fA635566E66415D31dD381CF5585";
298298
const delegation = createDelegation({
299299
to: delegate,
300300
from: account.address,
301-
// Empty caveats array - we recommend adding appropriate restrictions.
302-
caveats: [],
301+
environment: delegatorSmartAccount.environment,
302+
scope: {
303+
type: "nativeTokenTransferAmount",
304+
// 0.001 ETH in wei format.
305+
maxAmount: 1000000000000000n,
306+
},
303307
});
304308

305309
const signature = delegatorSmartAccount.signDelegation({ delegation });

0 commit comments

Comments
 (0)