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.
- example.ts
- config.ts
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
});
import { sepolia as chain } from 'viem/chains'
import { createPublicClient, http, parseEther } from 'viem'
import { createBundlerClient } from 'viem/account-abstraction'
import { getDeleGatorEnvironment, createDelegation } from '@metamask/delegation-toolkit'
export const environment = getDeleGatorEnvironment(chain.id)
const currentTime = Math.floor(Date.now() / 1000)
export const delegation = createDelegation({
scope: {
type: 'nativeTokenPeriodTransfer',
periodAmount: parseEther('0.01'),
periodDuration: 86400,
startDate: currentTime,
},
to: delegateAccount,
from: delegatorAccount,
environment: delegatorAccount.environment,
})
const publicClient = createPublicClient({
chain,
transport: http()
})
export const bundlerClient = createBundlerClient({
client: publicClient,
transport: http('https://api.pimlico.io/v2/11155111/rpc')
});