Skip to content

Commit 3a231de

Browse files
alexandratranAyushBherwani1998AyushBherwani1998
authored
Clarify delegation scope parameters (#2365)
* Clarify delegation scope parameters * fixes * clarify defaults * address feedback * [Ext] Improve delegation scope guide (#2380) * improve the function call scope guide * Apply suggestions from code review --------- Co-authored-by: AyushBherwani1998 <“[email protected]”> Co-authored-by: Alexandra Carrillo <[email protected]> --------- Co-authored-by: Ayush Bherwani <[email protected]> Co-authored-by: AyushBherwani1998 <“[email protected]”>
1 parent 5689fb2 commit 3a231de

File tree

5 files changed

+146
-46
lines changed

5 files changed

+146
-46
lines changed

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

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ description: Learn how to use the function call scope for a delegation.
33
keywords: [delegation scope, function call, restrict, delegation]
44
---
55

6+
import Tabs from "@theme/Tabs";
7+
import TabItem from "@theme/TabItem";
8+
69
# Use the function call scope
710

811
The function call scope defines the specific methods, contract addresses, and calldata that are allowed for the delegation.
912
For example, Alice delegates to Bob the ability to call the `approve` function on the USDC contract, with the approval amount set to `0`.
1013

11-
Internally, this scope uses the [`allowedTargets`](../../../reference/delegation/caveats.md#allowedtargets) and [`allowedMethods`](../../../reference/delegation/caveats.md#allowedmethods) caveat enforcers, and
12-
optionally uses the [`allowedCalldata`](../../../reference/delegation/caveats.md#allowedcalldata) or [`exactCalldata`](../../../reference/delegation/caveats.md#exactcalldata) caveat enforcers when those parameters are specified.
13-
1414
## Prerequisites
1515

1616
- [Install and set up the Delegation Toolkit.](../../../get-started/install.md)
@@ -20,10 +20,13 @@ optionally uses the [`allowedCalldata`](../../../reference/delegation/caveats.md
2020

2121
## Function call scope
2222

23-
This scope requires `targets` and `selectors` as mandatory parameters for the configuration.
24-
You can specify the allowed methods in `selectors` and the permitted contract addresses in `targets`.
23+
This scope requires `targets`, which specifies the permitted contract addresses, and `selectors`, which specifies the allowed methods.
24+
25+
Internally, this scope uses the [`allowedTargets`](../../../reference/delegation/caveats.md#allowedtargets) and [`allowedMethods`](../../../reference/delegation/caveats.md#allowedmethods) caveat enforcers, and
26+
optionally uses the [`allowedCalldata`](../../../reference/delegation/caveats.md#allowedcalldata) or [`exactCalldata`](../../../reference/delegation/caveats.md#exactcalldata) caveat enforcers when those parameters are specified.
27+
See the [function call scope reference](../../../reference/delegation/delegation-scopes.md#function-call-scope) for more details.
2528

26-
The following example sets the delegation scope to allow the delegate to call the `approve` function on the USDC token contract.
29+
The following example sets the delegation scope to allow the delegate to call the `approve` function on the USDC token contract:
2730

2831
```typescript
2932
import { createDelegation } from "@metamask/delegation-toolkit";
@@ -35,7 +38,85 @@ const delegation = createDelegation({
3538
scope: {
3639
type: "functionCall",
3740
targets: [USDC_ADDRESS],
38-
selectors: ["approve(address, uint256)"]
41+
selectors: ["approve(address, uint256)"],
42+
},
43+
to: delegateAccount,
44+
from: delegatorAccount,
45+
environment: delegatorAccount.environment,
46+
});
47+
```
48+
49+
### Define allowed calldata
50+
51+
You can further restrict the scope by defining the `allowedCalldata`. For example, you can set
52+
`allowedCalldata` so the delegate is only permitted to call the `approve` function on the
53+
USDC token contract with an allowance value of `0`. This effectively limits the delegate to
54+
revoking ERC-20 approvals.
55+
56+
:::important Usage
57+
The `allowedCalldata` doesn't support multiple selectors. Each entry in the
58+
list represents a portion of calldata corresponding to the same function signature.
59+
60+
You can include or exclude specific parameters to precisely define what parts of the calldata are valid.
61+
:::
62+
63+
```typescript
64+
import { createDelegation } from "@metamask/delegation-toolkit";
65+
import { encodeAbiParameters, erc20Abi } from "viem";
66+
67+
// USDC address on Sepolia.
68+
const USDC_ADDRESS = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
69+
70+
const delegation = createDelegation({
71+
scope: {
72+
type: "functionCall",
73+
targets: [USDC_ADDRESS],
74+
selectors: ["approve(address, uint256)"],
75+
allowedCalldata: [
76+
{
77+
// Limits the allowance amount to be 0.
78+
value: encodeAbiParameters(
79+
[{ name: 'amount', type: 'uint256' }],
80+
[0n],
81+
),
82+
// The first 4 bytes are for selector, and next 32 bytes
83+
// are for spender address.
84+
startIndex: 36,
85+
},
86+
]
87+
},
88+
to: delegateAccount,
89+
from: delegatorAccount,
90+
environment: delegatorAccount.environment,
91+
});
92+
```
93+
94+
### Define exact calldata
95+
96+
You can define the `exactCalldata` instead of the `allowedCalldata`. For example, you can
97+
set `exactCalldata` so the delegate is permitted to call only the `approve` function on the USDC token
98+
contract, with a specific spender address and an allowance value of 0. This effectively limits the delegate to
99+
revoking ERC-20 approvals for a specific spender.
100+
101+
```typescript
102+
import { createDelegation } from "@metamask/delegation-toolkit";
103+
import { encodeFunctionData, erc20Abi } from "viem";
104+
105+
// USDC address on Sepolia.
106+
const USDC_ADDRESS = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
107+
108+
const delegation = createDelegation({
109+
scope: {
110+
type: "functionCall",
111+
targets: [USDC_ADDRESS],
112+
selectors: ["approve(address, uint256)"],
113+
exactCalldata: {
114+
calldata: encodeFunctionData({
115+
abi: erc20Abi,
116+
args: ["0x0227628f3F023bb0B980b67D528571c95c6DaC1c", 0n],
117+
functionName: 'approve',
118+
})
119+
}
39120
},
40121
to: delegateAccount,
41122
from: delegatorAccount,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ keywords: [delegation scope, ownership transfer, restrict, delegation]
88
The ownership transfer scope restricts a delegation to ownership transfer calls only.
99
For example, Alice has deployed a smart contract, and she delegates to Bob the ability to transfer ownership of that contract.
1010

11-
Internally, this scope uses the [`ownershipTransfer`](../../../reference/delegation/caveats.md#ownershiptransfer) caveat enforcer.
12-
1311
## Prerequisites
1412

1513
- [Install and set up the Delegation Toolkit.](../../../get-started/install.md)
@@ -21,6 +19,9 @@ Internally, this scope uses the [`ownershipTransfer`](../../../reference/delegat
2119

2220
This scope requires a `contractAddress`, which represents the address of the deployed contract.
2321

22+
Internally, this scope uses the [`ownershipTransfer`](../../../reference/delegation/caveats.md#ownershiptransfer) caveat enforcer.
23+
See the [ownership transfer scope reference](../../../reference/delegation/delegation-scopes.md#ownership-transfer-scope) for more details.
24+
2425
```typescript
2526
import { createDelegation } from "@metamask/delegation-toolkit";
2627

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ You can set transfer limits with or without time-based (periodic) or streaming c
1818
## ERC-20 periodic scope
1919

2020
This scope ensures a per-period limit for ERC-20 token transfers.
21-
You set the amount and the time window.
21+
You set the amount, period, and start data.
2222
At the start of each new period, the allowance resets.
2323
For example, Alice creates a delegation that lets Bob spend up to 10 USDC on her behalf each day.
2424
Bob can transfer a total of 10 USDC per day; the limit resets at the beginning of the next day.
2525

2626
When this scope is applied, the toolkit automatically disallows native token transfers (sets the native token transfer limit to `0`).
2727

2828
Internally, this scope uses the [`erc20PeriodTransfer`](../../../reference/delegation/caveats.md#erc20periodtransfer) and [`valueLte`](../../../reference/delegation/caveats.md#valuelte) caveat enforcers.
29+
See the [ERC-20 periodic scope reference](../../../reference/delegation/delegation-scopes.md#erc-20-periodic-scope) for more details.
2930

3031
```typescript
3132
import { createDelegation } from "@metamask/delegation-toolkit";
@@ -59,6 +60,7 @@ For example, Alice creates a delegation that allows Bob to spend 0.1 USDC per se
5960
When this scope is applied, the toolkit automatically disallows native token transfers (sets the native token transfer limit to `0`).
6061

6162
Internally, this scope uses the [`erc20Streaming`](../../../reference/delegation/caveats.md#erc20streaming) and [`valueLte`](../../../reference/delegation/caveats.md#valuelte) caveat enforcers.
63+
See the [ERC-20 streaming scope reference](../../../reference/delegation/delegation-scopes.md#erc-20-streaming-scope) for more details.
6264

6365
```typescript
6466
import { createDelegation } from "@metamask/delegation-toolkit";
@@ -86,13 +88,14 @@ const delegation = createDelegation({
8688
## ERC-20 transfer scope
8789

8890
This scope ensures that ERC-20 token transfers are limited to a predefined maximum amount.
89-
This scope is useful for setting simple, fixed transfer limits without any time based or streaming conditions.
91+
This scope is useful for setting simple, fixed transfer limits without any time-based or streaming conditions.
9092
For example, Alice creates a delegation that allows Bob to spend up to 10 USDC without any conditions.
9193
Bob may use the 10 USDC in a single transaction or make multiple transactions, as long as the total does not exceed 10 USDC.
9294

9395
When this scope is applied, the toolkit automatically disallows native token transfers (sets the native token transfer limit to `0`).
9496

9597
Internally, this scope uses the [`erc20TransferAmount`](../../../reference/delegation/caveats.md#erc20transferamount) and [`valueLte`](../../../reference/delegation/caveats.md#valuelte) caveat enforcers.
98+
See the [ERC-20 transfer scope reference](../../../reference/delegation/delegation-scopes.md#erc-20-transfer-scope) for more details.
9699

97100
```typescript
98101
import { createDelegation } from "@metamask/delegation-toolkit";
@@ -117,6 +120,7 @@ This scope limits the delegation to ERC-721 token transfers only.
117120
For example, Alice creates a delegation that allows Bob to transfer an NFT she owns on her behalf.
118121

119122
Internally, this scope uses the [`erc721Transfer`](../../../reference/delegation/caveats.md#erc721transfer) caveat enforcer.
123+
See the [ERC-721 scope reference](../../../reference/delegation/delegation-scopes.md#erc-721-scope) for more details.
120124

121125
```typescript
122126
import { createDelegation } from "@metamask/delegation-toolkit";
@@ -136,14 +140,18 @@ const delegation = createDelegation({
136140
## Native token periodic scope
137141

138142
This scope ensures a per-period limit for native token transfers.
139-
You set the amount and the time window.
143+
You set the amount, period, and start date.
140144
At the start of each new period, the allowance resets.
141145
For example, Alice creates a delegation that lets Bob spend up to 0.01 ETH on her behalf each day.
142146
Bob can transfer a total of 0.01 ETH per day; the limit resets at the beginning of the next day.
143147

144-
When this scope is applied, the toolkit disallows ERC-20 and ERC-721 token transfers by default, setting the allowed `calldata` to `0x`.
148+
When this scope is applied, the toolkit disallows ERC-20 and ERC-721 token transfers by default (sets `exactCalldata` to `0x`).
149+
You can optionally configure `exactCalldata` to restrict transactions to a specific operation, or configure
150+
`allowedCalldata` to allow transactions that match certain patterns or ranges.
145151

146-
Internally, this scope uses the [`exactCalldata`](../../../reference/delegation/caveats.md#exactcalldata) and [`nativeTokenPeriodTransfer`](../../../reference/delegation/caveats.md#nativetokenperiodtransfer) caveat enforcers.
152+
Internally, this scope uses the [`nativeTokenPeriodTransfer`](../../../reference/delegation/caveats.md#nativetokenperiodtransfer) caveat enforcer, and
153+
optionally uses the [`allowedCalldata`](../../../reference/delegation/caveats.md#allowedcalldata) or [`exactCalldata`](../../../reference/delegation/caveats.md#exactcalldata) caveat enforcers when those parameters are specified.
154+
See the [native token periodic scope reference](../../../reference/delegation/delegation-scopes.md#native-token-periodic-scope) for more details.
147155

148156
```typescript
149157
import { createDelegation } from "@metamask/delegation-toolkit";
@@ -172,9 +180,13 @@ Token transfers are blocked until the defined start timestamp.
172180
At the start, a specified initial amount is released, after which tokens accrue linearly at the configured rate, up to the maximum allowed amount.
173181
For example, Alice creates delegation that allows Bob to spend 0.001 ETH per second, starting with an initial amount of 0.01 ETH, up to a maximum of 0.1 ETH.
174182

175-
When this scope is applied, the toolkit disallows ERC-20 and ERC-721 token transfers by default, setting the allowed `calldata` to `0x`.
183+
When this scope is applied, the toolkit disallows ERC-20 and ERC-721 token transfers by default (sets `exactCalldata` to `0x`).
184+
You can optionally configure `exactCalldata` to restrict transactions to a specific operation, or configure
185+
`allowedCalldata` to allow transactions that match certain patterns or ranges.
176186

177-
Internally, this scope uses the [`exactCalldata`](../../../reference/delegation/caveats.md#exactcalldata) and [`nativeTokenStreaming`](../../../reference/delegation/caveats.md#nativetokenstreaming) caveat enforcers.
187+
Internally, this scope uses the [`nativeTokenStreaming`](../../../reference/delegation/caveats.md#nativetokenstreaming) caveat enforcer, and
188+
optionally uses the [`allowedCalldata`](../../../reference/delegation/caveats.md#allowedcalldata) or [`exactCalldata`](../../../reference/delegation/caveats.md#exactcalldata) caveat enforcers when those parameters are specified.
189+
See the [native token streaming scope reference](../../../reference/delegation/delegation-scopes.md#native-token-streaming-scope) for more details.
178190

179191
```typescript
180192
import { createDelegation } from "@metamask/delegation-toolkit";
@@ -200,13 +212,17 @@ const delegation = createDelegation({
200212
## Native token transfer scope
201213

202214
This scope ensures that native token transfers are limited to a predefined maximum amount.
203-
This scope is useful for setting simple, fixed transfer limits without any time based or streaming conditions.
215+
This scope is useful for setting simple, fixed transfer limits without any time-based or streaming conditions.
204216
For example, Alice creates a delegation that allows Bob to spend up to 0.1 ETH without any conditions.
205217
Bob may use the 0.1 ETH in a single transaction or make multiple transactions, as long as the total does not exceed 0.1 ETH.
206218

207-
When this scope is applied, the toolkit disallows ERC-20 and ERC-721 token transfers by default, setting the allowed `calldata` to `0x`.
219+
When this scope is applied, the toolkit disallows ERC-20 and ERC-721 token transfers by default (sets `exactCalldata` to `0x`).
220+
You can optionally configure `exactCalldata` to restrict transactions to a specific operation, or configure
221+
`allowedCalldata` to allow transactions that match certain patterns or ranges.
208222

209-
Internally, this scope uses the [`exactCalldata`](../../../reference/delegation/caveats.md#exactcalldata) and [`nativeTokenTransferAmount`](../../../reference/delegation/caveats.md#nativetokentransferamount) caveat enforcers.
223+
Internally, this scope uses the [`nativeTokenTransferAmount`](../../../reference/delegation/caveats.md#nativetokentransferamount) caveat enforcer, and
224+
optionally uses the [`allowedCalldata`](../../../reference/delegation/caveats.md#allowedcalldata) or [`exactCalldata`](../../../reference/delegation/caveats.md#exactcalldata) caveat enforcers when those parameters are specified.
225+
See the [native token transfer scope reference](../../../reference/delegation/delegation-scopes.md#native-token-transfer-scope) for more details.
210226

211227
```typescript
212228
import { createDelegation } from "@metamask/delegation-toolkit";

delegation-toolkit/reference/delegation/caveats.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ Caveat enforcer contract: [`AllowedCalldataEnforcer.sol`](https://github.com/Met
2323

2424
| Name | Type | Required | Description |
2525
| ------------ | -------- | -------- | -------------------------------------------------------------------------------------------------------------------- |
26-
| `startIndex` | `number` | Yes | The index in the `calldata` byte array (including the 4-byte method selector) where the expected `calldata` starts. |
27-
| `value` | `Hex` | Yes | The expected `calldata` that must match at the specified index. |
26+
| `startIndex` | `number` | Yes | The index in the calldata byte array (including the 4-byte method selector) where the expected calldata starts. |
27+
| `value` | `Hex` | Yes | The expected calldata that must match at the specified index. |
2828

2929
### Example
3030

@@ -400,7 +400,7 @@ Caveat enforcer contract: [`ExactCalldataEnforcer.sol`](https://github.com/MetaM
400400

401401
| Name | Type | Required | Description |
402402
| ----------------- | -------------------------------- | -------- | ----------------------------------------------------- |
403-
| `calldata` | `Hex` | Yes | The `calldata` that the delegate is allowed to call. |
403+
| `calldata` | `Hex` | Yes | The calldata that the delegate is allowed to call. |
404404

405405
### Example
406406

0 commit comments

Comments
 (0)