Skip to content

Commit d33f82b

Browse files
AyushBherwani1998Ayush Bherwanialexandratran
authored
Document 7715 permissions (#2360)
* add 7715 permissions * edits * edit comments * change streaming description to "per second" * fix typo --------- Co-authored-by: Ayush Bherwani <“[email protected]”> Co-authored-by: Alexandra Carrillo <[email protected]>
1 parent 8dc5a9b commit d33f82b

File tree

3 files changed

+307
-7
lines changed

3 files changed

+307
-7
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
description: Learn how to use the ERC-20 token permissions with ERC-7715.
3+
keywords: [permissions, spending limit, restrict, 7715, erc-7715, erc20-permissions]
4+
---
5+
6+
import Tabs from "@theme/Tabs";
7+
import TabItem from "@theme/TabItem";
8+
9+
# Use ERC-20 token permissions
10+
11+
[ERC-7715](https://eips.ethereum.org/EIPS/eip-7715) supports ERC-20 token permission types that allow you to request fine-grained
12+
permissions for ERC-20 token transfers with time-based (periodic) or streaming conditions, depending on your use case.
13+
14+
## Prerequisites
15+
16+
- [Install and set up the Delegation Toolkit.](../../../get-started/install.md)
17+
- [Configure the Delegation Toolkit.](../../configure-toolkit.md)
18+
- [Create a session account.](../execute-on-metamask-user-behalf.md#3-set-up-a-session-account)
19+
20+
## ERC-20 periodic permission
21+
22+
This permission type ensures a per-period limit for ERC-20 token transfers. At the start of each new period, the allowance resets.
23+
24+
For example, a user signs an ERC-7715 permission that lets a dapp spend up to 10 USDC on their behalf each day. The dapp can transfer a total of
25+
10 USDC per day; the limit resets at the beginning of the next day.
26+
27+
<Tabs>
28+
<TabItem value="example.ts">
29+
30+
```typescript
31+
import { sepolia as chain } from "viem/chains";
32+
import { parseUnits } from "viem";
33+
import { walletClient } from "./client.ts"
34+
35+
// Since current time is in seconds, convert milliseconds to seconds.
36+
const currentTime = Math.floor(Date.now() / 1000);
37+
// 1 week from now.
38+
const expiry = currentTime + 604800;
39+
40+
// USDC address on Ethereum Sepolia.
41+
const tokenAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
42+
43+
const grantedPermissions = await walletClient.requestExecutionPermissions([{
44+
chainId: chain.id,
45+
expiry,
46+
signer: {
47+
type: "account",
48+
data: {
49+
// Session account created as a prerequisite.
50+
address: sessionAccountAddress,
51+
},
52+
},
53+
permission: {
54+
type: "erc20-token-periodic",
55+
data: {
56+
tokenAddress,
57+
// 10 USDC in WEI format. Since USDC has 6 decimals, 10 * 10^6.
58+
periodAmount: parseUnits("10", 6),
59+
// 1 day in seconds.
60+
periodDuration: 86400,
61+
justification?: "Permission to transfer 1 USDC every day",
62+
},
63+
},
64+
}]);
65+
```
66+
67+
</TabItem>
68+
<TabItem value="client.ts">
69+
70+
```typescript
71+
import { createWalletClient, custom } from "viem";
72+
import { erc7715ProviderActions } from "@metamask/delegation-toolkit/experimental";
73+
74+
export const walletClient = createWalletClient({
75+
transport: custom(window.ethereum),
76+
}).extend(erc7715ProviderActions());
77+
```
78+
79+
</TabItem>
80+
</Tabs>
81+
82+
## ERC-20 stream permission
83+
84+
This permission type ensures a linear streaming transfer limit for ERC-20 tokens. Token transfers are blocked until the
85+
defined start timestamp. At the start, a specified initial amount is released, after which tokens accrue linearly at the
86+
configured rate, up to the maximum allowed amount.
87+
88+
For example, a user signs an ERC-7715 permission that allows a dapp to spend 0.1 USDC per second, starting with an initial amount
89+
of 1 USDC, up to a maximum of 2 USDC.
90+
91+
<Tabs>
92+
<TabItem value="example.ts">
93+
94+
```typescript
95+
import { sepolia as chain } from "viem/chains";
96+
import { parseUnits } from "viem";
97+
import { walletClient } from "./client.ts"
98+
99+
// Since current time is in seconds, convert milliseconds to seconds.
100+
const currentTime = Math.floor(Date.now() / 1000);
101+
// 1 week from now.
102+
const expiry = currentTime + 604800;
103+
104+
// USDC address on Ethereum Sepolia.
105+
const tokenAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
106+
107+
const grantedPermissions = await walletClient.requestExecutionPermissions([{
108+
chainId: chain.id,
109+
expiry,
110+
signer: {
111+
type: "account",
112+
data: {
113+
// Session account created as a prerequisite.
114+
address: sessionAccountAddress,
115+
},
116+
},
117+
permission: {
118+
type: "erc20-token-stream",
119+
data: {
120+
tokenAddress,
121+
// 0.1 USDC in WEI format. Since USDC has 6 decimals, 0.1 * 10^6.
122+
amountPerSecond: parseUnits("0.1", 6),
123+
// 1 USDC in WEI format. Since USDC has 6 decimals, 1 * 10^6.
124+
initialAmount: parseUnits("1", 6),
125+
// 2 USDC in WEI format. Since USDC has 6 decimals, 2 * 10^6.
126+
maxAmount: parseUnits("2", 6),
127+
// 1 hour in seconds.
128+
duration: 3600,
129+
startTime: currentTime,
130+
justification: "Permission to use 0.1 USDC per second",
131+
},
132+
},
133+
}]);
134+
```
135+
136+
</TabItem>
137+
<TabItem value="client.ts">
138+
139+
```typescript
140+
import { createWalletClient, custom } from "viem";
141+
import { erc7715ProviderActions } from "@metamask/delegation-toolkit/experimental";
142+
143+
export const walletClient = createWalletClient({
144+
transport: custom(window.ethereum),
145+
}).extend(erc7715ProviderActions());
146+
```
147+
148+
</TabItem>
149+
</Tabs>
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
description: Learn how to use the native token permissions with ERC-7715.
3+
keywords: [permissions, spending limit, restrict, 7715, erc-7715, native-token-permissions]
4+
---
5+
6+
import Tabs from "@theme/Tabs";
7+
import TabItem from "@theme/TabItem";
8+
9+
# Use native token permissions
10+
11+
[ERC-7715](https://eips.ethereum.org/EIPS/eip-7715) supports native token permission types that allow you to request fine-grained
12+
permissions for native token transfers with time-based (periodic) or streaming conditions, depending on your use case.
13+
14+
## Prerequisites
15+
16+
- [Install and set up the Delegation Toolkit.](../../../get-started/install.md)
17+
- [Configure the Delegation Toolkit.](../../configure-toolkit.md)
18+
- [Create a session account.](../execute-on-metamask-user-behalf.md#3-set-up-a-session-account)
19+
20+
## Native token periodic permission
21+
22+
This permission type ensures a per-period limit for native token transfers. At the start of each new period, the allowance resets.
23+
24+
For example, a user signs an ERC-7715 permission that lets a dapp spend up to 0.001 ETH on their behalf each day. The dapp can transfer a total of
25+
0.001 USDC per day; the limit resets at the beginning of the next day.
26+
27+
<Tabs>
28+
<TabItem value="example.ts">
29+
30+
```typescript
31+
import { sepolia as chain } from "viem/chains";
32+
import { parseEther } from "viem";
33+
import { walletClient } from "./client.ts"
34+
35+
// Since current time is in seconds, convert milliseconds to seconds.
36+
const currentTime = Math.floor(Date.now() / 1000);
37+
// 1 week from now.
38+
const expiry = currentTime + 604800;
39+
40+
const grantedPermissions = await walletClient.requestExecutionPermissions([{
41+
chainId: chain.id,
42+
expiry,
43+
signer: {
44+
type: "account",
45+
data: {
46+
// Session account created as a prerequisite.
47+
address: sessionAccountAddress,
48+
},
49+
},
50+
permission: {
51+
type: "native-token-periodic",
52+
data: {
53+
// 0.001 ETH in wei format.
54+
periodAmount: parseEther("0.001"),
55+
// 1 hour in seconds.
56+
periodDuration: 86400,
57+
startTime: currentTime,
58+
justification: "Permission to use 0.001 ETH every day",
59+
},
60+
},
61+
}]);
62+
```
63+
64+
</TabItem>
65+
<TabItem value="client.ts">
66+
67+
```typescript
68+
import { createWalletClient, custom } from "viem";
69+
import { erc7715ProviderActions } from "@metamask/delegation-toolkit/experimental";
70+
71+
export const walletClient = createWalletClient({
72+
transport: custom(window.ethereum),
73+
}).extend(erc7715ProviderActions());
74+
```
75+
76+
</TabItem>
77+
</Tabs>
78+
79+
## Native token stream permission
80+
81+
This permission type ensures a linear streaming transfer limit for native tokens. Token transfers are blocked until the
82+
defined start timestamp. At the start, a specified initial amount is released, after which tokens accrue linearly at the
83+
configured rate, up to the maximum allowed amount.
84+
85+
For example, a user signs an ERC-7715 permission that allows a dapp to spend 0.0001 ETH per second, starting with an initial amount
86+
of 0.1 ETH, up to a maximum of 1 ETH.
87+
88+
<Tabs>
89+
<TabItem value="example.ts">
90+
91+
```typescript
92+
import { sepolia as chain } from "viem/chains";
93+
import { parseEther } from "viem";
94+
import { walletClient } from "./client.ts"
95+
96+
// Since current time is in seconds, convert milliseconds to seconds.
97+
const currentTime = Math.floor(Date.now() / 1000);
98+
// 1 week from now.
99+
const expiry = currentTime + 604800;
100+
101+
const grantedPermissions = await walletClient.requestExecutionPermissions([{
102+
chainId: chain.id,
103+
expiry,
104+
signer: {
105+
type: "account",
106+
data: {
107+
// Session account created as a prerequisite.
108+
address: sessionAccountAddress,
109+
},
110+
},
111+
permission: {
112+
type: "native-token-stream",
113+
data: {
114+
// 0.0001 ETH in wei format.
115+
amountPerSecond: parseEther("0.0001"),
116+
// 0.1 ETH in wei format.
117+
initialAmount: parseEther("0.1"),
118+
// 1 ETH in wei format.
119+
maxAmount: parseEther("1"),
120+
// 1 hour in seconds.
121+
duration: 3600,
122+
startTime: currentTime,
123+
justification: "Permission to use 0.0001 ETH per second",
124+
},
125+
},
126+
}]);
127+
```
128+
129+
</TabItem>
130+
<TabItem value="client.ts">
131+
132+
```typescript
133+
import { createWalletClient, custom } from "viem";
134+
import { erc7715ProviderActions } from "@metamask/delegation-toolkit/experimental";
135+
136+
export const walletClient = createWalletClient({
137+
transport: custom(window.ethereum),
138+
}).extend(erc7715ProviderActions());
139+
```
140+
141+
</TabItem>
142+
</Tabs>

gator-sidebar.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const sidebar = {
1818
{
1919
type: 'category',
2020
label: 'Smart account quickstart',
21-
collapsed: false,
21+
collapsed: true,
2222
link: { type: "doc", id: "get-started/smart-account-quickstart/index" },
2323
items: [
2424
'get-started/smart-account-quickstart/eip7702',
@@ -37,7 +37,7 @@ const sidebar = {
3737
{
3838
type: 'category',
3939
label: 'Delegation',
40-
collapsed: false,
40+
collapsed: true,
4141
link: { type: "doc", id: "concepts/delegation/index" },
4242
items: [
4343
'concepts/delegation/caveat-enforcers',
@@ -54,7 +54,7 @@ const sidebar = {
5454
{
5555
type: 'category',
5656
label: 'MetaMask Smart Accounts',
57-
collapsed: false,
57+
collapsed: true,
5858
items: [
5959
'guides/smart-accounts/create-smart-account',
6060
'guides/smart-accounts/deploy-smart-account',
@@ -66,13 +66,13 @@ const sidebar = {
6666
{
6767
type: 'category',
6868
label: 'Delegation',
69-
collapsed: false,
69+
collapsed: true,
7070
items: [
7171
'guides/delegation/execute-on-smart-accounts-behalf',
7272
{
7373
type: 'category',
7474
label: 'Use delegation scopes',
75-
collapsed: false,
75+
collapsed: true,
7676
link: { type: "doc", id: "guides/delegation/use-delegation-scopes/index" },
7777
items: [
7878
'guides/delegation/use-delegation-scopes/spending-limit',
@@ -87,9 +87,18 @@ const sidebar = {
8787
{
8888
type: 'category',
8989
label: 'ERC-7715',
90-
collapsed: false,
90+
collapsed: true,
9191
items: [
9292
'guides/erc7715/execute-on-metamask-user-behalf',
93+
{
94+
type: 'category',
95+
label: 'Use permissions',
96+
collapsed: true,
97+
items: [
98+
'guides/erc7715/use-permissions/erc20-token',
99+
'guides/erc7715/use-permissions/native-token',
100+
],
101+
},
93102
],
94103
},
95104
],
@@ -139,7 +148,7 @@ const sidebar = {
139148
{
140149
type: 'category',
141150
label: 'Delegation',
142-
collapsed: false,
151+
collapsed: true,
143152
link: { type: "doc", id: "reference/delegation/index" },
144153
items: [
145154
'reference/delegation/delegation-scopes',

0 commit comments

Comments
 (0)