|
| 1 | +--- |
| 2 | +description: Learn how to create a redelegation. |
| 3 | +sidebar_label: Create a redelegation |
| 4 | +toc_max_heading_level: 3 |
| 5 | +keywords: [delegation, state, caveat enforcer, delegation scope, redelegation] |
| 6 | +--- |
| 7 | + |
| 8 | +import Tabs from "@theme/Tabs"; |
| 9 | +import TabItem from "@theme/TabItem"; |
| 10 | + |
| 11 | +# Create a redelegation |
| 12 | + |
| 13 | +Redelegation is a core feature that sets delegations apart from other permission sharing frameworks. |
| 14 | +It allows a delegate to create a delegation chain, passing on the same or reduced level of authority |
| 15 | +from the root delegator. |
| 16 | + |
| 17 | +For example, if Alice grants Bob permission to spend 10 USDC on her behalf, Bob can further grant Carol |
| 18 | +permission to spend up to 5 USDC on Alice's behalf-that is, Bob can redelegate. This creates a delegation |
| 19 | +chain where the root permissions are reshared with additional parties. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +- [Install and set up the Smart Accounts Kit](../../get-started/install.md). |
| 24 | +- [Learn how to create a delegation](execute-on-smart-accounts-behalf.md). |
| 25 | + |
| 26 | +## Create a delegation |
| 27 | + |
| 28 | +Create a [root delegation](../../concepts/delegation/index.md#delegation-types) from Alice to Bob. |
| 29 | + |
| 30 | +This example uses the [`erc20TransferAmount`](use-delegation-scopes/spending-limit.md#erc-20-transfer-scope) scope, allowing |
| 31 | +Alice to delegate to Bob the ability to spend 10 USDC on her behalf. |
| 32 | + |
| 33 | +<Tabs> |
| 34 | +<TabItem value="delegation.ts"> |
| 35 | + |
| 36 | +```typescript |
| 37 | +import { aliceSmartAccount, bobSmartAccount } from "./config.ts"; |
| 38 | +import { createDelegation } from '@metamask/smart-accounts-kit' |
| 39 | +import { parseUnits } from 'viem' |
| 40 | + |
| 41 | +const delegation = createDelegation({ |
| 42 | + scope: { |
| 43 | + type: "erc20TransferAmount", |
| 44 | + tokenAddress: "0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92", |
| 45 | + // USDC has 6 decimal places. |
| 46 | + maxAmount: parseUnits("10", 6), |
| 47 | + }, |
| 48 | + to: bobSmartAccount.address, |
| 49 | + from: aliceSmartAccount.address, |
| 50 | + environment: aliceSmartAccount.environment, |
| 51 | +}) |
| 52 | + |
| 53 | +const signedDelegation = aliceSmartAccount.signDelegation({ delegation }) |
| 54 | +``` |
| 55 | + |
| 56 | +</TabItem> |
| 57 | +<TabItem value="config.ts"> |
| 58 | + |
| 59 | +```typescript |
| 60 | +import { Implementation, toMetaMaskSmartAccount } from "@metamask/smart-accounts-kit" |
| 61 | +import { privateKeyToAccount } from "viem/accounts" |
| 62 | +import { createPublicClient, http } from "viem" |
| 63 | +import { sepolia as chain } from "viem/chains" |
| 64 | + |
| 65 | +const publicClient = createPublicClient({ |
| 66 | + chain, |
| 67 | + transport: http(), |
| 68 | +}) |
| 69 | + |
| 70 | +const aliceAccount = privateKeyToAccount("0x...") |
| 71 | +const bobAccount = privateKeyToAccount("0x...") |
| 72 | + |
| 73 | +export const aliceSmartAccount = await toMetaMaskSmartAccount({ |
| 74 | + client: publicClient, |
| 75 | + implementation: Implementation.Hybrid, |
| 76 | + deployParams: [aliceAccount.address, [], [], []], |
| 77 | + deploySalt: "0x", |
| 78 | + signer: { account: aliceAccount }, |
| 79 | +}) |
| 80 | + |
| 81 | +export const bobSmartAccount = await toMetaMaskSmartAccount({ |
| 82 | + client: publicClient, |
| 83 | + implementation: Implementation.Hybrid, |
| 84 | + deployParams: [bobAccount.address, [], [], []], |
| 85 | + deploySalt: "0x", |
| 86 | + signer: { account: bobAccount }, |
| 87 | +}) |
| 88 | +``` |
| 89 | + |
| 90 | +</TabItem> |
| 91 | +</Tabs> |
| 92 | + |
| 93 | +## Create a redelegation |
| 94 | + |
| 95 | +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. |
| 96 | + |
| 97 | +To create a redelegation, provide the signed delegation as the `parentDelegation` argument when calling [createDelegation](../../reference/delegation/index.md#createdelegation). |
| 98 | +This example uses the [`erc20TransferAmount`](use-delegation-scopes/spending-limit.md#erc-20-transfer-scope) scope, allowing |
| 99 | +Bob to delegate to Carol the ability to spend 5 USDC on Alice's behalf. |
| 100 | + |
| 101 | +<Tabs> |
| 102 | +<TabItem value="redelegation.ts"> |
| 103 | + |
| 104 | +```typescript |
| 105 | +import { bobSmartAccount, carolSmartAccount } from "./config.ts" |
| 106 | +import { createDelegation } from '@metamask/smart-accounts-kit' |
| 107 | +import { parseUnits } from 'viem' |
| 108 | + |
| 109 | +const redelegation = createDelegation({ |
| 110 | + scope: { |
| 111 | + type: "erc20TransferAmount", |
| 112 | + tokenAddress: "0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92", |
| 113 | + // USDC has 6 decimal places. |
| 114 | + maxAmount: parseUnits("5", 6), |
| 115 | + }, |
| 116 | + to: carolSmartAccount.address, |
| 117 | + from: bobSmartAccount.address, |
| 118 | + // Signed root delegation from previous step. |
| 119 | + parentDelegation: signedDelegation, |
| 120 | + environment: bobSmartAccount.environment, |
| 121 | +}) |
| 122 | + |
| 123 | +const signedRedelegation = bobSmartAccount.signDelegation({ delegation: redelegation }) |
| 124 | +``` |
| 125 | + |
| 126 | +</TabItem> |
| 127 | +<TabItem value="config.ts"> |
| 128 | + |
| 129 | +```typescript |
| 130 | +// Update the existing config to create a smart account for Carol. |
| 131 | + |
| 132 | +const carolAccount = privateKeyToAccount("0x...") |
| 133 | + |
| 134 | +export const carolSmartAccount = await toMetaMaskSmartAccount({ |
| 135 | + client: publicClient, |
| 136 | + implementation: Implementation.Hybrid, |
| 137 | + deployParams: [carolAccount.address, [], [], []], |
| 138 | + deploySalt: "0x", |
| 139 | + signer: { account: carolAccount }, |
| 140 | +}) |
| 141 | +``` |
| 142 | + |
| 143 | +</TabItem> |
| 144 | +</Tabs> |
| 145 | + |
| 146 | +## Next steps |
| 147 | + |
| 148 | +- See [how to disable a delegation](disable-delegation.md) to revoke permissions. |
0 commit comments