|
| 1 | +--- |
| 2 | +description: Learn how to disable the delegation. |
| 3 | +sidebar_label: Disable a delegation |
| 4 | +toc_max_heading_level: 3 |
| 5 | +keywords: [delegation, disable, revoke] |
| 6 | +--- |
| 7 | + |
| 8 | +import Tabs from "@theme/Tabs"; |
| 9 | +import TabItem from "@theme/TabItem"; |
| 10 | + |
| 11 | +# Disable a delegation |
| 12 | + |
| 13 | +Delegations are created off-chain and can be stored anywhere, but you can disable a delegation on-chain using the |
| 14 | +toolkit. When a delegation is disabled, any attempt to redeem it will revert, effectively revoking the permissions |
| 15 | +that were previously granted. |
| 16 | + |
| 17 | +For example, if Alice has given permission to Bob to spend 10 USDC on her behalf, and after a week she wants to |
| 18 | +revoke that permission, Alice can disable the delegation she created for Bob. If Bob tries to redeem the disabled |
| 19 | +delegation, the transaction will revert, preventing him from spending Alice's USDC. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +- [Install and set up the Delegation Toolkit.](../../get-started/install.md) |
| 24 | +- [Create a delegator account.](execute-on-smart-accounts-behalf.md#3-create-a-delegator-account) |
| 25 | +- [Create a delegate account.](execute-on-smart-accounts-behalf.md#4-create-a-delegate-account) |
| 26 | + |
| 27 | + |
| 28 | +## Disable a delegation |
| 29 | + |
| 30 | +To disable a delegation, you can use the [`disableDelegation`](../../reference/delegation/index.md#disabledelegation) utility function from the |
| 31 | +toolkit to generate calldata. Once the calldata is prepared, you can send it to the |
| 32 | +Delegation Manager to disable the delegation. |
| 33 | + |
| 34 | +<Tabs> |
| 35 | +<TabItem value="example.ts"> |
| 36 | + |
| 37 | +```typescript |
| 38 | +import { DelegationManager } from '@metamask/delegation-toolkit/contracts'; |
| 39 | +import { environment, delegation, bundlerClient } from "./config.ts"; |
| 40 | + |
| 41 | +const disableDelegationData = DelegationManager.encode.disableDelegation({ |
| 42 | + delegation, |
| 43 | +}); |
| 44 | + |
| 45 | +// Appropriate fee per gas must be determined for the specific bundler being used. |
| 46 | +const maxFeePerGas = 1n; |
| 47 | +const maxPriorityFeePerGas = 1n; |
| 48 | + |
| 49 | +const userOperationHash = await bundlerClient.sendUserOperation({ |
| 50 | + account: delegatorAccount, |
| 51 | + calls: [ |
| 52 | + { |
| 53 | + to: environment.DelegationManager, |
| 54 | + data: disableDelegationData |
| 55 | + } |
| 56 | + ], |
| 57 | + maxFeePerGas, |
| 58 | + maxPriorityFeePerGas |
| 59 | +}); |
| 60 | +``` |
| 61 | + |
| 62 | +</TabItem> |
| 63 | +<TabItem value="config.ts"> |
| 64 | + |
| 65 | +```typescript |
| 66 | +import { sepolia as chain } from 'viem/chains' |
| 67 | +import { createPublicClient, http, parseEther } from 'viem' |
| 68 | +import { createBundlerClient } from 'viem/account-abstraction' |
| 69 | +import { getDeleGatorEnvironment, createDelegation } from '@metamask/delegation-toolkit' |
| 70 | + |
| 71 | +export const environment = getDeleGatorEnvironment(chain.id) |
| 72 | + |
| 73 | +const currentTime = Math.floor(Date.now() / 1000) |
| 74 | + |
| 75 | +export const delegation = createDelegation({ |
| 76 | + scope: { |
| 77 | + type: 'nativeTokenPeriodTransfer', |
| 78 | + periodAmount: parseEther('0.01'), |
| 79 | + periodDuration: 86400, |
| 80 | + startDate: currentTime, |
| 81 | + }, |
| 82 | + to: delegateAccount, |
| 83 | + from: delegatorAccount, |
| 84 | + environment: delegatorAccount.environment, |
| 85 | +}) |
| 86 | + |
| 87 | +const publicClient = createPublicClient({ |
| 88 | + chain, |
| 89 | + transport: http() |
| 90 | +}) |
| 91 | + |
| 92 | +export const bundlerClient = createBundlerClient({ |
| 93 | + client: publicClient, |
| 94 | + transport: http('https://api.pimlico.io/v2/11155111/rpc') |
| 95 | +}); |
| 96 | +``` |
| 97 | + |
| 98 | +</TabItem> |
| 99 | +</Tabs> |
0 commit comments