Skip to main content

Caveat Enforcer Client

The following API methods are related to CaveatEnforcerClient used to check the delegation state.

createCaveatEnforcerClient

Create a Viem Client extended with caveat enforcer actions. This client allows you to interact with the caveat enforcers of the delegation, and read the required state.

Parameters

NameTypeRequiredDescription
clientClientYesThe Viem Client to interact with the caveat enforcer contracts and read their state.
environmentDeleGatorEnvironmentYesEnvironment to resolve the smart contracts for the current chain.

Example

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

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

getErc20PeriodTransferEnforcerAvailableAmount

Returns the available amount from the ERC-20 period transfer enforcer for the current period.

Parameters

NameTypeRequiredDescription
delegationDelegationYesThe delegation object for which you want to check the available amount.

Example

import { delegation } './config.ts'

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

getErc20StreamingEnforcerAvailableAmount

Returns the available amount from the ERC-20 streaming enforcer.

Parameters

NameTypeRequiredDescription
delegationDelegationYesThe delegation object for which you want to check the available amount.

Example

import { delegation } './config.ts'

// Returns the available amount
const { availableAmount } = await caveatEnforcerClient.getErc20StreamingEnforcerAvailableAmount({
delegation,
})

getNativeTokenPeriodTransferEnforcerAvailableAmount

Returns the available amount from the native token period enforcer for the current period.

Parameters

NameTypeRequiredDescription
delegationDelegationYesThe delegation object for which you want to check the available amount.

Example

import { delegation } './config.ts'

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

getNativeTokenStreamingEnforcerAvailableAmount

Returns the available amount from the native streaming enforcer.

Parameters

NameTypeRequiredDescription
delegationDelegationYesThe delegation object for which you want to check the available amount.

Example

import { delegation } './config.ts'

// Returns the available amount
const { availableAmount } = await caveatEnforcerClient.getNativeTokenStreamingEnforcerAvailableAmount({
delegation,
})

getMultiTokenPeriodEnforcerAvailableAmount

Returns the available amount from the multi token period transfer enforcer for the current period. You'll need to encode the args for the token index you want to check the available amount.

Parameters

NameTypeRequiredDescription
delegationDelegationYesThe delegation object with token index for which you want to check the available amount.

Example

import { delegation } './config.ts'

// Encode the args for the multiTokenPeriod enforcer.
const args = encodePacked(['uint256'], [BigInt(0)]);

// Ensure the index is correct when working with multiple enforcers.
delegation.caveats[0].args = args

// Returns the available amount for the first token in the list.
const { availableAmount } = await caveatEnforcerClient.getMultiTokenPeriodEnforcerAvailableAmount({
delegation,
})