Check the delegation state
When using spending limit delegation scopes or relevant caveat enforcers, you might need to check the remaining transferrable amount in a delegation. For example, if a delegation allows a user to spend 10 USDC per week and they have already spent 10 - n USDC in the current period, you can determine how much of the allowance is still available for transfer.
Use the CaveatEnforcerClient
to check the available balances for specific scopes or caveats.
Prerequisites
- Install and set up the Delegation Toolkit.
- Create a delegator account.
- Create a delegate account.
- Create a delegation with an ERC-20 periodic scope.
Create a CaveatEnforcerClient
To check the delegation state, create a CaveatEnforcerClient
.
This client allows you to interact with the caveat enforcers of the delegation, and read the required state.
- example.ts
- config.ts
import { environment, publicClient as client } from './config.ts'
import { createCaveatEnforcerClient } from '@metamask/delegation-toolkit'
const caveatEnforcerClient = createCaveatEnforcerClient({
environment,
client,
})
import { sepolia as chain } from 'viem/chains'
import { createPublicClient, http } from 'viem'
import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'
export const environment = getDeleGatorEnvironment(chain.id)
export const publicClient = createPublicClient({
chain,
transport: http(),
})
Read the caveat enforcer state
This example uses the getErc20PeriodTransferEnforcerAvailableAmount
method to read the state and retrieve the remaining amount for the current transfer period.
- example.ts
- config.ts
import { delegation } './config.ts'
// Returns the available amount for current period.
const { availableAmount } = await caveatEnforcerClient.getErc20PeriodTransferEnforcerAvailableAmount({
delegation,
})
import { createDelegation } from '@metamask/delegation-toolkit'
export const delegation = createDelegation({
scope: {
type: 'erc20PeriodTransfer',
tokenAddress: '0xb4aE654Aca577781Ca1c5DE8FbE60c2F423f37da',
periodAmount: 1000000000000000000n,
periodDuration: 86400,
startDate: 1743763600,
},
to: delegateAccount,
from: delegatorAccount,
environment: delegatorAccount.environment,
})
Next steps
See the Caveat Enforcer Client reference for the full list of available methods.