-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[Smart Accounts Kit] Add redelegation guide #2554
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
59e8454
add redelegation guide
AyushBherwani1998 886a1dc
Merge branch 'main' into feat/redelegations
AyushBherwani1998 4787e68
minor fixes
AyushBherwani1998 3ad58a9
minor fix
AyushBherwani1998 560ffa7
improve guide
AyushBherwani1998 3802fb8
Update smart-accounts-kit/guides/delegation/create-redelegation.md
m4sterbunny 53dcf8d
Update smart-accounts-kit/guides/delegation/create-redelegation.md
m4sterbunny 80afd49
Update smart-accounts-kit/guides/delegation/create-redelegation.md
m4sterbunny 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
148 changes: 148 additions & 0 deletions
148
smart-accounts-kit/guides/delegation/create-redelegation.md
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,148 @@ | ||
| --- | ||
| description: Learn how to create a redelegation. | ||
| sidebar_label: Create a redelegation | ||
| toc_max_heading_level: 3 | ||
| keywords: [delegation, state, caveat enforcer, delegation scope, redelegation] | ||
| --- | ||
|
|
||
| import Tabs from "@theme/Tabs"; | ||
| import TabItem from "@theme/TabItem"; | ||
|
|
||
| # Create a redelegation | ||
|
|
||
| Redelegation is a core feature that sets delegations apart from other permission sharing frameworks. | ||
| It allows a delegate to create a delegation chain, passing on the same or reduced level of authority | ||
| from the root delegator. | ||
|
|
||
| For example, if Alice grants Bob permission to spend 10 USDC on her behalf, Bob can further grant Carol | ||
| permission to spend up to 5 USDC on Alice's behalf-that is, Bob can redelegate. This creates a delegation | ||
| chain where the root permissions are reshared with additional parties. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - [Install and set up the Smart Accounts Kit](../../get-started/install.md). | ||
| - [Learn how to create a delegation](execute-on-smart-accounts-behalf.md). | ||
|
|
||
| ## Create a delegation | ||
|
|
||
| Create a [root delegation](../../concepts/delegation/index.md#delegation-types) from Alice to Bob. | ||
|
|
||
| This example uses the [`erc20TransferAmount`](use-delegation-scopes/spending-limit.md#erc-20-transfer-scope) scope, allowing | ||
| Alice to delegate to Bob the ability to spend 10 USDC on her behalf. | ||
|
|
||
| <Tabs> | ||
| <TabItem value="delegation.ts"> | ||
|
|
||
| ```typescript | ||
| import { aliceSmartAccount, bobSmartAccount } from "./config.ts"; | ||
| import { createDelegation } from '@metamask/smart-accounts-kit' | ||
| import { parseUnits } from 'viem' | ||
|
|
||
| const delegation = createDelegation({ | ||
| scope: { | ||
| type: "erc20TransferAmount", | ||
| tokenAddress: "0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92", | ||
| // USDC has 6 decimal places. | ||
| maxAmount: parseUnits("10", 6), | ||
| }, | ||
| to: bobSmartAccount.address, | ||
| from: aliceSmartAccount.address, | ||
| environment: aliceSmartAccount.environment, | ||
| }) | ||
|
|
||
| const signedDelegation = aliceSmartAccount.signDelegation({ delegation }) | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="config.ts"> | ||
|
|
||
| ```typescript | ||
| import { Implementation, toMetaMaskSmartAccount } from "@metamask/smart-accounts-kit" | ||
| import { privateKeyToAccount } from "viem/accounts" | ||
| import { createPublicClient, http } from "viem" | ||
| import { sepolia as chain } from "viem/chains" | ||
|
|
||
| const publicClient = createPublicClient({ | ||
| chain, | ||
| transport: http(), | ||
| }) | ||
|
|
||
| const aliceAccount = privateKeyToAccount("0x...") | ||
| const bobAccount = privateKeyToAccount("0x...") | ||
|
|
||
| export const aliceSmartAccount = await toMetaMaskSmartAccount({ | ||
| client: publicClient, | ||
| implementation: Implementation.Hybrid, | ||
| deployParams: [aliceAccount.address, [], [], []], | ||
| deploySalt: "0x", | ||
| signer: { account: aliceAccount }, | ||
| }) | ||
|
|
||
| export const bobSmartAccount = await toMetaMaskSmartAccount({ | ||
| client: publicClient, | ||
| implementation: Implementation.Hybrid, | ||
| deployParams: [bobAccount.address, [], [], []], | ||
| deploySalt: "0x", | ||
| signer: { account: bobAccount }, | ||
| }) | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| </Tabs> | ||
|
|
||
| ## Create a redelegation | ||
|
|
||
| Create a [redelegation](../../concepts/delegation/index.md#delegation-types) from Bob to Carol. When creating a redelegation, you can only narrow the scope of the original authority, not expand it. | ||
|
|
||
| To create a redelegation, provide the signed delegation as the `parentDelegation` argument when calling [createDelegation](../../reference/delegation/index.md#createdelegation). | ||
| This example uses the [`erc20TransferAmount`](use-delegation-scopes/spending-limit.md#erc-20-transfer-scope) scope, allowing | ||
m4sterbunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Bob to delegate to Carol the ability to spend 5 USDC on Alice's behalf. | ||
|
|
||
| <Tabs> | ||
| <TabItem value="redelegation.ts"> | ||
|
|
||
| ```typescript | ||
| import { bobSmartAccount, carolSmartAccount } from "./config.ts" | ||
| import { createDelegation } from '@metamask/smart-accounts-kit' | ||
| import { parseUnits } from 'viem' | ||
|
|
||
| const redelegation = createDelegation({ | ||
| scope: { | ||
| type: "erc20TransferAmount", | ||
| tokenAddress: "0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92", | ||
| // USDC has 6 decimal places. | ||
| maxAmount: parseUnits("5", 6), | ||
| }, | ||
| to: carolSmartAccount.address, | ||
| from: bobSmartAccount.address, | ||
| // Signed root delegation from previous step. | ||
| parentDelegation: signedDelegation, | ||
| environment: bobSmartAccount.environment, | ||
| }) | ||
|
|
||
| const signedRedelegation = bobSmartAccount.signDelegation({ delegation: redelegation }) | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| <TabItem value="config.ts"> | ||
|
|
||
| ```typescript | ||
| // Update the existing config to create a smart account for Carol. | ||
|
|
||
| const carolAccount = privateKeyToAccount("0x...") | ||
|
|
||
| export const carolSmartAccount = await toMetaMaskSmartAccount({ | ||
| client: publicClient, | ||
| implementation: Implementation.Hybrid, | ||
| deployParams: [carolAccount.address, [], [], []], | ||
| deploySalt: "0x", | ||
| signer: { account: carolAccount }, | ||
| }) | ||
| ``` | ||
|
|
||
| </TabItem> | ||
| </Tabs> | ||
|
|
||
| ## Next steps | ||
|
|
||
| - See [how to disable a delegation](disable-delegation.md) to revoke permissions. | ||
m4sterbunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.