Skip to content

Commit 602f3b4

Browse files
Merge branch 'main' into feat/rules
2 parents d3385f5 + 55fa999 commit 602f3b4

3 files changed

Lines changed: 193 additions & 7 deletions

File tree

smart-accounts-kit/guides/advanced-permissions/use-permissions/erc20-token.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,77 @@ import TabItem from "@theme/TabItem";
99
# Use ERC-20 token permissions
1010

1111
[Advanced Permissions (ERC-7715)](../../../concepts/advanced-permissions.md) 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.
12+
permissions for ERC-20 token transfers with periodic, fixed allowance, or streaming conditions, depending on your use case.
1313

1414
## Prerequisites
1515

1616
- [Install and set up the Smart Accounts Kit.](../../../get-started/install.md)
1717
- [Configure the Smart Accounts Kit.](../../configure-toolkit.md)
1818
- [Create a session account.](../execute-on-metamask-users-behalf.md#3-set-up-a-session-account)
1919

20+
## ERC-20 allowance permission
21+
22+
This permission type ensures a fixed ERC-20 token allowance.
23+
It allows transfers up to a maximum total amount and doesn't reset by period.
24+
25+
For example, a user signs an ERC-7715 permission that lets your dapp spend up to 50 USDC in total.
26+
After the dapp transfers 50 USDC, no additional transfers are allowed under this permission.
27+
28+
See the [ERC-20 allowance permission API reference](../../../reference/advanced-permissions/permissions.md#erc-20-allowance-permission) for more information.
29+
30+
<Tabs>
31+
<TabItem value="example.ts">
32+
33+
```typescript
34+
import { sepolia as chain } from 'viem/chains'
35+
import { parseUnits } from 'viem'
36+
import { walletClient } from './client.ts'
37+
38+
// Since current time is in seconds, convert milliseconds to seconds.
39+
const currentTime = Math.floor(Date.now() / 1000)
40+
// 1 week from now.
41+
const expiry = currentTime + 604800
42+
43+
// USDC address on Ethereum Sepolia.
44+
const tokenAddress = '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238'
45+
46+
const grantedPermissions = await walletClient.requestExecutionPermissions([
47+
{
48+
chainId: chain.id,
49+
expiry,
50+
// The requested permissions will be granted to the
51+
// session account.
52+
to: sessionAccount.address,
53+
permission: {
54+
type: 'erc20-token-allowance',
55+
data: {
56+
tokenAddress,
57+
// 50 USDC in WEI format. Since USDC has 6 decimals, 50 * 10^6.
58+
allowanceAmount: parseUnits('50', 6),
59+
startTime: currentTime,
60+
justification: 'Permission to transfer up to 50 USDC in total',
61+
},
62+
isAdjustmentAllowed: true,
63+
},
64+
},
65+
])
66+
```
67+
68+
</TabItem>
69+
<TabItem value="client.ts">
70+
71+
```typescript
72+
import { createWalletClient, custom } from 'viem'
73+
import { erc7715ProviderActions } from '@metamask/smart-accounts-kit/actions'
74+
75+
export const walletClient = createWalletClient({
76+
transport: custom(window.ethereum),
77+
}).extend(erc7715ProviderActions())
78+
```
79+
80+
</TabItem>
81+
</Tabs>
82+
2083
## ERC-20 periodic permission
2184

2285
This permission type ensures a per-period limit for ERC-20 token transfers. At the start of each new period, the allowance resets.
@@ -46,7 +109,7 @@ const grantedPermissions = await walletClient.requestExecutionPermissions([
46109
{
47110
chainId: chain.id,
48111
expiry,
49-
// The requested permissions will granted to the
112+
// The requested permissions will be granted to the
50113
// session account.
51114
to: sessionAccount.address,
52115
permission: {

smart-accounts-kit/guides/advanced-permissions/use-permissions/native-token.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,73 @@ import TabItem from "@theme/TabItem";
1818
# Use native token permissions
1919

2020
[Advanced Permissions (ERC-7715)](../../../concepts/advanced-permissions.md) supports native token permission types that allow you to request fine-grained
21-
permissions for native token transfers with time-based (periodic) or streaming conditions, depending on your use case.
21+
permissions for native token transfers with periodic, fixed-allowance, or streaming conditions, depending on your use case.
2222

2323
## Prerequisites
2424

2525
- [Install and set up the Smart Accounts Kit.](../../../get-started/install.md)
2626
- [Configure the Smart Accounts Kit.](../../configure-toolkit.md)
2727
- [Create a session account.](../execute-on-metamask-users-behalf.md#3-set-up-a-session-account)
2828

29+
## Native token allowance permission
30+
31+
This permission type ensures a fixed native token allowance.
32+
It allows transfers up to a maximum total amount and doesn't reset by period.
33+
34+
For example, a user signs an ERC-7715 permission that lets your dapp spend up to 0.05 ETH in total.
35+
After the dapp transfers 0.05 ETH, no additional transfers are allowed under this permission.
36+
37+
See the [native token allowance permission API reference](../../../reference/advanced-permissions/permissions.md#native-token-allowance-permission) for more information.
38+
39+
<Tabs>
40+
<TabItem value="example.ts">
41+
42+
```typescript
43+
import { sepolia as chain } from 'viem/chains'
44+
import { parseEther } from 'viem'
45+
import { walletClient } from './client.ts'
46+
47+
// Since current time is in seconds, convert milliseconds to seconds.
48+
const currentTime = Math.floor(Date.now() / 1000)
49+
// 1 week from now.
50+
const expiry = currentTime + 604800
51+
52+
const grantedPermissions = await walletClient.requestExecutionPermissions([
53+
{
54+
chainId: chain.id,
55+
expiry,
56+
// The requested permissions will be granted to the
57+
// session account.
58+
to: sessionAccount.address,
59+
permission: {
60+
type: 'native-token-allowance',
61+
data: {
62+
// 0.05 ETH in wei format.
63+
allowanceAmount: parseEther('0.05'),
64+
startTime: currentTime,
65+
justification: 'Permission to transfer up to 0.05 ETH in total',
66+
},
67+
isAdjustmentAllowed: true,
68+
},
69+
},
70+
])
71+
```
72+
73+
</TabItem>
74+
<TabItem value="client.ts">
75+
76+
```typescript
77+
import { createWalletClient, custom } from 'viem'
78+
import { erc7715ProviderActions } from '@metamask/smart-accounts-kit/actions'
79+
80+
export const walletClient = createWalletClient({
81+
transport: custom(window.ethereum),
82+
}).extend(erc7715ProviderActions())
83+
```
84+
85+
</TabItem>
86+
</Tabs>
87+
2988
## Native token periodic permission
3089

3190
This permission type ensures a per-period limit for native token transfers. At the start of each new period, the allowance resets.
@@ -52,7 +111,7 @@ const grantedPermissions = await walletClient.requestExecutionPermissions([
52111
{
53112
chainId: chain.id,
54113
expiry,
55-
// The requested permissions will granted to the
114+
// The requested permissions will be granted to the
56115
// session account.
57116
to: sessionAccount.address,
58117
permission: {
@@ -114,7 +173,7 @@ const grantedPermissions = await walletClient.requestExecutionPermissions([
114173
{
115174
chainId: chain.id,
116175
expiry,
117-
// The requested permissions will granted to the
176+
// The requested permissions will be granted to the
118177
// session account.
119178
to: sessionAccount.address,
120179
permission: {

smart-accounts-kit/reference/advanced-permissions/permissions.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,40 @@ Learn [how to use Advanced Permissions types](../../guides/advanced-permissions/
1111

1212
## ERC-20 token permissions
1313

14+
### ERC-20 allowance permission
15+
16+
Ensures a fixed ERC-20 token allowance.
17+
Transfers are allowed until the total transferred amount reaches the allowance amount.
18+
19+
#### Parameters
20+
21+
| Name | Type | Required | Description |
22+
| ----------------- | --------- | -------- | ---------------------------------------------------------------------- |
23+
| `tokenAddress` | `Address` | Yes | The ERC-20 token contract address. |
24+
| `allowanceAmount` | `bigint` | Yes | The maximum total amount of tokens that can be transferred. |
25+
| `startTime` | `number` | No | The start timestamp in seconds. The default is the current time. |
26+
| `justification` | `string` | No | A human-readable explanation of why the permission is being requested. |
27+
28+
#### Example
29+
30+
```typescript
31+
import { parseUnits } from 'viem'
32+
33+
const currentTime = Math.floor(Date.now() / 1000)
34+
const expiry = currentTime + 604800
35+
36+
const permission = {
37+
type: 'erc20-token-allowance',
38+
data: {
39+
tokenAddress: '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238',
40+
allowanceAmount: parseUnits('50', 6),
41+
startTime: currentTime,
42+
justification: 'Permission to transfer up to 50 USDC in total',
43+
},
44+
isAdjustmentAllowed: true,
45+
}
46+
```
47+
1448
### ERC-20 periodic permission
1549

1650
Ensures a per-period limit for ERC-20 token transfers.
@@ -87,6 +121,38 @@ const permission = {
87121

88122
## Native token permissions
89123

124+
### Native token allowance permission
125+
126+
Ensures a fixed native token allowance.
127+
Transfers are allowed until the total transferred amount reaches the allowance amount.
128+
129+
#### Parameters
130+
131+
| Name | Type | Required | Description |
132+
| ----------------- | -------- | -------- | ---------------------------------------------------------------------- |
133+
| `allowanceAmount` | `bigint` | Yes | The maximum total amount of tokens that can be transferred. |
134+
| `startTime` | `number` | No | The start timestamp in seconds. The default is the current time. |
135+
| `justification` | `string` | No | A human-readable explanation of why the permission is being requested. |
136+
137+
#### Example
138+
139+
```typescript
140+
import { parseEther } from 'viem'
141+
142+
const currentTime = Math.floor(Date.now() / 1000)
143+
const expiry = currentTime + 604800
144+
145+
const permission = {
146+
type: 'native-token-allowance',
147+
data: {
148+
allowanceAmount: parseEther('0.05'),
149+
startTime: currentTime,
150+
justification: 'Permission to transfer up to 0.05 ETH in total',
151+
},
152+
isAdjustmentAllowed: true,
153+
}
154+
```
155+
90156
### Native token periodic permission
91157

92158
Ensures a per-period limit for native token transfers.
@@ -140,9 +206,7 @@ At the start, a specified initial amount is released, after which tokens accrue
140206
#### Example
141207

142208
```typescript
143-
import { sepolia as chain } from 'viem/chains'
144209
import { parseEther } from 'viem'
145-
import { walletClient } from './client.ts'
146210

147211
const currentTime = Math.floor(Date.now() / 1000)
148212
const expiry = currentTime + 604800

0 commit comments

Comments
 (0)