|
| 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 | +The [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 types ensures a per-period limit for ERC-20 token transfers. At the start of each new period, the allowance resets. |
| 23 | + |
| 24 | +For example, user signs a ERC-7715 permission that lets dapp spend up to 10 USDC on their behalf each day. 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, we need to 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 | + // Check the prerequisites for session account. |
| 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, user signs a ERC-7715 permission that allows dapp to spend 0.1 USDC every hour, 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, we need to 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 | + // Check the prerequisites for session account. |
| 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 every hour", |
| 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> |
0 commit comments