Skip to content

Commit 394811c

Browse files
[Delegtion Toolkit] Improve ERC-7715 guide (#2406)
* improve 7715 guide * resolve cursor comments * edits --------- Co-authored-by: Alexandra Carrillo <alexandra.carrillo@consensys.net> Co-authored-by: Alexandra Carrillo <12214231+alexandratran@users.noreply.github.com>
1 parent cc82e67 commit 394811c

File tree

3 files changed

+92
-12
lines changed

3 files changed

+92
-12
lines changed

delegation-toolkit/guides/erc7715/execute-on-metamask-users-behalf.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,50 @@ const sessionAccount = privateKeyToAccount("0x...");
9595
</TabItem>
9696
</Tabs>
9797

98-
### 4. Request ERC-7715 permissions
98+
### 4. Check the EOA account code
99+
100+
Currently, ERC-7715 does not support automatically upgrading a user's account to a [MetaMask smart account](../../concepts/smart-accounts.md). Therefore, you must
101+
ensure that the user is upgraded to a smart account before requesting ERC-7715 permissions.
102+
103+
If the user has not yet been upgraded, you can handle the upgrade [programmatically](/wallet/how-to/send-transactions/send-batch-transactions/#about-atomic-batch-transactions) or ask the
104+
user to [switch to a smart account manually](https://support.metamask.io/configure/accounts/switch-to-or-revert-from-a-smart-account/#how-to-switch-to-a-metamask-smart-account).
105+
106+
:::info Why is a Smart Account upgrade is required?
107+
MetaMask's ERC-7715 implementation requires the user to be upgraded to a MetaMask
108+
Smart Account because, under the hood, you're requesting a signature for an [ERC-7710 delegation](../../concepts/delegation/index.md).
109+
ERC-7710 delegation is one of the core features supported only by MetaMask Smart Accounts.
110+
:::
111+
112+
```typescript
113+
import { getDeleGatorEnvironment } from "@metamask/delegation-toolkit";
114+
import { sepolia as chain } from "viem/chains";
115+
116+
const addresses = await walletClient.requestAddresses();
117+
const address = addresses[0];
118+
119+
// Get the EOA account code
120+
const code = await publicClient.getCode({
121+
address,
122+
});
123+
124+
if (code) {
125+
// The address to which EOA has delegated. According to EIP-7702, 0xef0100 || address
126+
// represents the delegation.
127+
//
128+
// You need to remove the first 8 characters (0xef0100) to get the delegator address.
129+
const delegatorAddress = `0x${code.substring(8)}`;
130+
131+
const statelessDelegatorAddress = getDeleGatorEnvironment(chain.id)
132+
.implementations
133+
.EIP7702StatelessDeleGatorImpl;
134+
135+
// If account is not upgraded to MetaMask smart account, you can
136+
// either upgrade programmatically or ask the user to switch to a smart account manually.
137+
const isAccountUpgraded = delegatorAddress.toLowerCase() === statelessDelegatorAddress.toLowerCase();
138+
}
139+
```
140+
141+
### 5. Request ERC-7715 permissions
99142

100143
Request ERC-7715 permissions from the user. In this example, you'll request an
101144
[ERC-20 periodic permission](use-permissions/erc20-token.md#erc-20-periodic-permission) using the Wallet Client's
@@ -137,7 +180,7 @@ const grantedPermissions = await walletClient.requestExecutionPermissions([{
137180
}]);
138181
```
139182

140-
### 5. Set up a Viem client
183+
### 6. Set up a Viem client
141184

142185
Set up a Viem client depending on your session account type.
143186

@@ -184,7 +227,7 @@ const sessionAccountWalletClient = createWalletClient({
184227
</Tabs>
185228

186229

187-
### 6. Redeem ERC-7715 permissions
230+
### 7. Redeem ERC-7715 permissions
188231

189232
The session account can now redeem the permissions. The redeem transaction is sent to the `DelegationManager` contract, which validates the delegation and executes actions on the user's behalf.
190233

gator_versioned_docs/version-0.13.0/guides/erc7715/execute-on-metamask-users-behalf.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,50 @@ const sessionAccount = privateKeyToAccount("0x...");
9595
</TabItem>
9696
</Tabs>
9797

98-
### 4. Request ERC-7715 permissions
98+
### 4. Check the EOA account code
99+
100+
Currently, ERC-7715 does not support automatically upgrading a user's account to a [MetaMask smart account](../../concepts/smart-accounts.md). Therefore, you must
101+
ensure that the user is upgraded to a smart account before requesting ERC-7715 permissions.
102+
103+
If the user has not yet been upgraded, you can handle the upgrade [programmatically](/wallet/how-to/send-transactions/send-batch-transactions/#about-atomic-batch-transactions) or ask the
104+
user to [switch to a smart account manually](https://support.metamask.io/configure/accounts/switch-to-or-revert-from-a-smart-account/#how-to-switch-to-a-metamask-smart-account).
105+
106+
:::info Why is a Smart Account upgrade is required?
107+
MetaMask's ERC-7715 implementation requires the user to be upgraded to a MetaMask
108+
Smart Account because, under the hood, you're requesting a signature for an [ERC-7710 delegation](../../concepts/delegation/index.md).
109+
ERC-7710 delegation is one of the core features supported only by MetaMask Smart Accounts.
110+
:::
111+
112+
```typescript
113+
import { getDeleGatorEnvironment } from "@metamask/delegation-toolkit";
114+
import { sepolia as chain } from "viem/chains";
115+
116+
const addresses = await walletClient.requestAddresses();
117+
const address = addresses[0];
118+
119+
// Get the EOA account code
120+
const code = await publicClient.getCode({
121+
address,
122+
});
123+
124+
if (code) {
125+
// The address to which EOA has delegated. According to EIP-7702, 0xef0100 || address
126+
// represents the delegation.
127+
//
128+
// You need to remove the first 8 characters (0xef0100) to get the delegator address.
129+
const delegatorAddress = `0x${code.substring(8)}`;
130+
131+
const statelessDelegatorAddress = getDeleGatorEnvironment(chain.id)
132+
.implementations
133+
.EIP7702StatelessDeleGatorImpl;
134+
135+
// If account is not upgraded to MetaMask smart account, you can
136+
// either upgrade programmatically or ask the user to switch to a smart account manually.
137+
const isAccountUpgraded = delegatorAddress.toLowerCase() === statelessDelegatorAddress.toLowerCase();
138+
}
139+
```
140+
141+
### 5. Request ERC-7715 permissions
99142

100143
Request ERC-7715 permissions from the user. In this example, you'll request an
101144
[ERC-20 periodic permission](use-permissions/erc20-token.md#erc-20-periodic-permission) using the Wallet Client's
@@ -137,7 +180,7 @@ const grantedPermissions = await walletClient.requestExecutionPermissions([{
137180
}]);
138181
```
139182

140-
### 5. Set up a Viem client
183+
### 6. Set up a Viem client
141184

142185
Set up a Viem client depending on your session account type.
143186

@@ -184,7 +227,7 @@ const sessionAccountWalletClient = createWalletClient({
184227
</Tabs>
185228

186229

187-
### 6. Redeem ERC-7715 permissions
230+
### 7. Redeem ERC-7715 permissions
188231

189232
The session account can now redeem the permissions. The redeem transaction is sent to the `DelegationManager` contract, which validates the delegation and executes actions on the user's behalf.
190233

package-lock.json

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)