|
| 1 | +--- |
| 2 | +description: Learn how to use ERC-7715 to request permisisons. |
| 3 | +sidebar_position: 5 |
| 4 | +sidebar_label: EIP-7715 quickstart |
| 5 | +--- |
| 6 | + |
| 7 | +# EIP-7715 quickstart |
| 8 | + |
| 9 | +This page demonstrates how to use [ERC-7715](https://eips.ethereum.org/EIPS/eip-7715) to request permissions |
| 10 | +from a wallet, and execute transactions on a user's behalf. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- [Install and set up the Delegation Toolkit.](install.md) |
| 15 | +- [Install MetaMask Flask 12.14.2 or later](/snaps/get-started/install-flask.md). |
| 16 | + |
| 17 | +## Steps |
| 18 | + |
| 19 | +### 1. Set up a Wallet Client |
| 20 | + |
| 21 | +Set up a [Viem Wallet Client](https://viem.sh/docs/clients/wallet) using Viem's `createWalletClient` function. This client will help you interact with MetaMask Flask. |
| 22 | + |
| 23 | +Then, extend the Wallet Client functionality |
| 24 | +using `erc7715ProviderActions`. These actions enable you to request ERC-7715 |
| 25 | +permissions from the user. |
| 26 | + |
| 27 | +```typescript |
| 28 | +import { createWalletClient, custom } from "viem"; |
| 29 | +import { erc7715ProviderActions } from "@metamask/delegation-toolkit/experimental"; |
| 30 | + |
| 31 | +const walletClient = createWalletClient({ |
| 32 | + transport: custom(window.ethereum), |
| 33 | +}).extend(erc7715ProviderActions()); |
| 34 | +``` |
| 35 | + |
| 36 | +### 2. Set up a Public Client |
| 37 | + |
| 38 | +Set up a [Viem Public Client](https://viem.sh/docs/clients/public) using Viem's `createPublicClient` function. |
| 39 | +This client will help you query the account state and interact with blockchain networks. |
| 40 | + |
| 41 | +```typescript |
| 42 | +import { createPublicClient, http } from "viem"; |
| 43 | +import { sepolia as chain } from "viem/chains"; |
| 44 | + |
| 45 | +const publicClient = createPublicClient({ |
| 46 | + chain, |
| 47 | + transport: http(), |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +### 3. Set up a session account |
| 52 | + |
| 53 | +Set up a session account which can either be a smart account or an externally owned |
| 54 | +account (EOA) to request ERC-7715 permissions. This account is responsible |
| 55 | +for executing transactions on behalf of the user. |
| 56 | + |
| 57 | +This example uses [MetaMask Smart Accounts](../concepts/smart-accounts.md) as a session account. |
| 58 | + |
| 59 | +```typescript |
| 60 | +import { privateKeyToAccount } from "viem/accounts"; |
| 61 | +import { |
| 62 | + toMetaMaskSmartAccount, |
| 63 | + Implementation |
| 64 | +} from "@metamask/delegation-toolkit"; |
| 65 | + |
| 66 | +const privateKey = "0x..."; |
| 67 | +const account = privateKeyToAccount(privateKey); |
| 68 | + |
| 69 | +const sessionAccount = await toMetaMaskSmartAccount({ |
| 70 | + client: publicClient, |
| 71 | + implementation: Implementation.Hybrid, |
| 72 | + deployParams: [account.address, [], [], []], |
| 73 | + deploySalt: "0x", |
| 74 | + signatory: { account }, |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +### 4. Request ERC-7715 permissions |
| 79 | + |
| 80 | +Request ERC-7715 permissions from the user. Currently, only the |
| 81 | +`native-token-stream` permission type is supported, which allows the dapp to stream |
| 82 | +native tokens from the user's wallet. |
| 83 | + |
| 84 | +```typescript |
| 85 | +const expiry = Math.floor(Date.now() / 1000 + 604_800); // 1 week from now. |
| 86 | +const currentTime = Math.floor(Date.now() / 1000); // now |
| 87 | + |
| 88 | +const grantedPermissions = await walletClient.grantPermissions([{ |
| 89 | + chainId: chain.id, |
| 90 | + expiry, |
| 91 | + signer: { |
| 92 | + type: "account", |
| 93 | + data: { |
| 94 | + address: sessionAccount.address, |
| 95 | + }, |
| 96 | + }, |
| 97 | + permission: { |
| 98 | + type: "native-token-stream", |
| 99 | + data: { |
| 100 | + initialAmount: 1n, // 1 wei |
| 101 | + amountPerSecond: 1n, // 1 wei per second |
| 102 | + maxAmount: 10n, // 10 wei |
| 103 | + startTime: currentTime, |
| 104 | + justification: "Payment for a week long subscription", |
| 105 | + }, |
| 106 | + }, |
| 107 | +}]); |
| 108 | +``` |
| 109 | + |
| 110 | +### 5. Set up a Bundler Client |
| 111 | + |
| 112 | +Set up a [Viem Bundler Client](https://viem.sh/account-abstraction/clients/bundler) |
| 113 | +using Viem's `createBundlerClient` function. This lets you use the bundler service |
| 114 | +to estimate gas for user operations and submit transactions to the network. |
| 115 | + |
| 116 | +Then, extend the Bundler Client |
| 117 | +functionality using `erc7710BundlerActions`. These actions enable you to redeem ERC-7715 permissions, and execute transactions on a user's behalf. |
| 118 | + |
| 119 | +```typescript |
| 120 | +import { createBundlerClient } from "viem/account-abstraction"; |
| 121 | +import { erc7710BundlerActions } from "@metamask/delegation-toolkit/experimental"; |
| 122 | + |
| 123 | +const bundlerClient = createBundlerClient({ |
| 124 | + client: publicClient, |
| 125 | + transport: http("https://your-bundler-rpc.com"), |
| 126 | + // Allows you to use the same Bundler Client as paymaster. |
| 127 | + paymaster: true |
| 128 | +}).extend(erc7710BundlerActions()); |
| 129 | +``` |
| 130 | + |
| 131 | +### 6. Redeem ERC-7715 permissions |
| 132 | + |
| 133 | +The session account can now [redeem the delegation](../how-to/redeem-delegation.md). The redeem transaction is sent to the `DelegationManager` contract, which validates the delegation and executes actions on the user's behalf. |
| 134 | + |
| 135 | +To redeem the permissions, you can use the `sendUserOperationWithDelegation` bundler client action. |
| 136 | + |
| 137 | +```typescript |
| 138 | +// These properties must be extracted from the permission response. |
| 139 | +const permissionsContext = grantedPermissions[0].context; |
| 140 | +const delegationManager = grantedPermissions[0].signerMeta.delegationManager; |
| 141 | +const accountMetadata = grantedPermissions[0].accountMeta; |
| 142 | + |
| 143 | +// Calls without permissionsContext and delegationManager will be executed |
| 144 | +// as a normal user operation. |
| 145 | +const userOperationHash = await bundlerClient.sendUserOperationWithDelegation({ |
| 146 | + publicClient, |
| 147 | + account: sessionAccount, |
| 148 | + calls: [ |
| 149 | + { |
| 150 | + to: sessionAccount.address, |
| 151 | + data: "0x", |
| 152 | + value: 1n, |
| 153 | + permissionsContext, |
| 154 | + delegationManager, |
| 155 | + }, |
| 156 | + ], |
| 157 | + // Appropriate values must be used for fee-per-gas. |
| 158 | + maxFeePerGas: 1n, |
| 159 | + maxPriorityFeePerGas: 1n |
| 160 | + accountMetadata, |
| 161 | +}); |
| 162 | +``` |
0 commit comments