-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Delegation Toolkit] ERC-7715 quickstart #2153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
--- | ||
description: Learn how to use ERC-7715 to request permisisons. | ||
sidebar_position: 5 | ||
sidebar_label: EIP-7715 quickstart | ||
--- | ||
|
||
# EIP-7715 quickstart | ||
|
||
This page demonstrates how to use [ERC-7715](https://eips.ethereum.org/EIPS/eip-7715) to request permissions | ||
from a wallet, and execute transactions on a user's behalf. | ||
|
||
## Prerequisites | ||
|
||
- [Install and set up the Delegation Toolkit.](install.md) | ||
- [Install MetaMask Flask 12.14.2 or later](/snaps/get-started/install-flask.md). | ||
|
||
## Steps | ||
|
||
### 1. Set up a Wallet Client | ||
|
||
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. | ||
|
||
Then, extend the Wallet Client functionality | ||
using `erc7715ProviderActions`. These actions enable you to request ERC-7715 | ||
permissions from the user. | ||
|
||
```typescript | ||
import { createWalletClient, custom } from "viem"; | ||
import { erc7715ProviderActions } from "@metamask/delegation-toolkit/experimental"; | ||
|
||
const walletClient = createWalletClient({ | ||
transport: custom(window.ethereum), | ||
}).extend(erc7715ProviderActions()); | ||
``` | ||
|
||
### 2. Set up a Public Client | ||
|
||
Set up a [Viem Public Client](https://viem.sh/docs/clients/public) using Viem's `createPublicClient` function. | ||
This client will help you query the account state and interact with blockchain networks. | ||
|
||
```typescript | ||
import { createPublicClient, http } from "viem"; | ||
import { sepolia as chain } from "viem/chains"; | ||
|
||
const publicClient = createPublicClient({ | ||
chain, | ||
transport: http(), | ||
}); | ||
``` | ||
|
||
### 3. Set up a session account | ||
|
||
Set up a session account which can either be a smart account or an externally owned | ||
account (EOA) to request ERC-7715 permissions. This account is responsible | ||
for executing transactions on behalf of the user. | ||
|
||
This example uses [MetaMask Smart Accounts](../concepts/smart-accounts.md) as a session account. | ||
|
||
```typescript | ||
import { privateKeyToAccount } from "viem/accounts"; | ||
import { | ||
toMetaMaskSmartAccount, | ||
Implementation | ||
} from "@metamask/delegation-toolkit"; | ||
|
||
const privateKey = "0x..."; | ||
const account = privateKeyToAccount(privateKey); | ||
|
||
const sessionAccount = await toMetaMaskSmartAccount({ | ||
client: publicClient, | ||
implementation: Implementation.Hybrid, | ||
deployParams: [account.address, [], [], []], | ||
deploySalt: "0x", | ||
signatory: { account }, | ||
}); | ||
``` | ||
|
||
### 4. Request ERC-7715 permissions | ||
|
||
Request ERC-7715 permissions from the user. Currently, only the | ||
`native-token-stream` permission type is supported, which allows the dapp to stream | ||
native tokens from the user's wallet. | ||
|
||
```typescript | ||
const expiry = Math.floor(Date.now() / 1000 + 604_800); // 1 week from now. | ||
const currentTime = Math.floor(Date.now() / 1000); // now | ||
|
||
const grantedPermissions = await walletClient.grantPermissions([{ | ||
chainId: chain.id, | ||
expiry, | ||
signer: { | ||
type: "account", | ||
data: { | ||
address: sessionAccount.address, | ||
}, | ||
}, | ||
permission: { | ||
type: "native-token-stream", | ||
data: { | ||
initialAmount: 1n, // 1 wei | ||
amountPerSecond: 1n, // 1 wei per second | ||
maxAmount: 10n, // 10 wei | ||
startTime: currentTime, | ||
justification: "Payment for a week long subscription", | ||
}, | ||
}, | ||
}]); | ||
``` | ||
|
||
### 5. Set up a Bundler Client | ||
|
||
Set up a [Viem Bundler Client](https://viem.sh/account-abstraction/clients/bundler) | ||
using Viem's `createBundlerClient` function. This lets you use the bundler service | ||
to estimate gas for user operations and submit transactions to the network. | ||
|
||
Then, extend the Bundler Client | ||
functionality using `erc7710BundlerActions`. These actions enable you to redeem ERC-7715 permissions, and execute transactions on a user's behalf. | ||
|
||
```typescript | ||
import { createBundlerClient } from "viem/account-abstraction"; | ||
import { erc7710BundlerActions } from "@metamask/delegation-toolkit/experimental"; | ||
|
||
const bundlerClient = createBundlerClient({ | ||
client: publicClient, | ||
transport: http("https://your-bundler-rpc.com"), | ||
// Allows you to use the same Bundler Client as paymaster. | ||
paymaster: true | ||
}).extend(erc7710BundlerActions()); | ||
``` | ||
|
||
### 6. Redeem ERC-7715 permissions | ||
|
||
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. | ||
|
||
To redeem the permissions, you can use the `sendUserOperationWithDelegation` bundler client action. | ||
|
||
```typescript | ||
// These properties must be extracted from the permission response. | ||
const permissionsContext = grantedPermissions[0].context; | ||
const delegationManager = grantedPermissions[0].signerMeta.delegationManager; | ||
const accountMetadata = grantedPermissions[0].accountMeta; | ||
|
||
// Calls without permissionsContext and delegationManager will be executed | ||
// as a normal user operation. | ||
const userOperationHash = await bundlerClient.sendUserOperationWithDelegation({ | ||
publicClient, | ||
account: sessionAccount, | ||
calls: [ | ||
{ | ||
to: sessionAccount.address, | ||
data: "0x", | ||
value: 1n, | ||
permissionsContext, | ||
delegationManager, | ||
}, | ||
], | ||
// Appropriate values must be used for fee-per-gas. | ||
maxFeePerGas: 1n, | ||
maxPriorityFeePerGas: 1n | ||
accountMetadata, | ||
}); | ||
``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that this page has some overlap with ERC-7715: Request Permissions and ERC-7710: Redeem delegations. Would it make sense to consolidate this content or refer to those experimental guides somewhere on this page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but we need to improve ERC-7715, since with the addition of new permissions, each will have its own dedicated section. This way, the page content won’t be overlapping.