Skip to main content

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

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.

import { environment, publicClient as client } from './config.ts'
import { createCaveatEnforcerClient } from '@metamask/delegation-toolkit'

const caveatEnforcerClient = createCaveatEnforcerClient({
environment,
client,
})

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.

import { delegation } './config.ts'

// Returns the available amount for current period.
const { availableAmount } = await caveatEnforcerClient.getErc20PeriodTransferEnforcerAvailableAmount({
delegation,
})

Next steps

See the Caveat Enforcer Client reference for the full list of available methods.