Skip to main content

Disable a delegation

Delegations are created off-chain and can be stored anywhere, but you can disable a delegation on-chain using the toolkit. When a delegation is disabled, any attempt to redeem it will revert, effectively revoking the permissions that were previously granted.

For example, if Alice has given permission to Bob to spend 10 USDC on her behalf, and after a week she wants to revoke that permission, Alice can disable the delegation she created for Bob. If Bob tries to redeem the disabled delegation, the transaction will revert, preventing him from spending Alice's USDC.

Prerequisites

Disable a delegation

To disable a delegation, you can use the disableDelegation utility function from the toolkit to generate calldata. Once the calldata is prepared, you can send it to the Delegation Manager to disable the delegation.

import { DelegationManager } from '@metamask/delegation-toolkit/contracts';
import { environment, delegation, bundlerClient } from "./config.ts";

const disableDelegationData = DelegationManager.encode.disableDelegation({
delegation,
});

// Appropriate fee per gas must be determined for the specific bundler being used.
const maxFeePerGas = 1n;
const maxPriorityFeePerGas = 1n;

const userOperationHash = await bundlerClient.sendUserOperation({
account: delegatorAccount,
calls: [
{
to: environment.DelegationManager,
data: disableDelegationData
}
],
maxFeePerGas,
maxPriorityFeePerGas
});