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
Name | Type | Required | Description |
---|---|---|---|
client | Client | Yes | The Viem Client to interact with the caveat enforcer contracts and read their state. |
environment | DeleGatorEnvironment | Yes | Environment to resolve the smart contracts for the current chain. |
Example
- 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(),
})
getErc20PeriodTransferEnforcerAvailableAmount
Returns the available amount from the ERC-20 period transfer enforcer for the current period.
Parameters
Name | Type | Required | Description |
---|---|---|---|
delegation | Delegation | Yes | The delegation object for which you want to check the available amount. |
Example
- 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'
import { sepolia as chain } from 'viem/chains'
import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'
const environment = getDeleGatorEnvironment(chain.id)
export const delegation = createDelegation({
scope: {
type: 'erc20PeriodTransfer',
tokenAddress: '0xb4aE654Aca577781Ca1c5DE8FbE60c2F423f37da',
periodAmount: 1000000000000000000n,
periodDuration: 86400,
startDate: 1743763600,
},
to: 'DELEGATE_ADDRESS',
from: 'DELEGATOR_ADDRESS',
environment,
})
getErc20StreamingEnforcerAvailableAmount
Returns the available amount from the ERC-20 streaming enforcer.
Parameters
Name | Type | Required | Description |
---|---|---|---|
delegation | Delegation | Yes | The delegation object for which you want to check the available amount. |
Example
- example.ts
- config.ts
import { delegation } './config.ts'
// Returns the available amount
const { availableAmount } = await caveatEnforcerClient.getErc20StreamingEnforcerAvailableAmount({
delegation,
})
import { createDelegation } from '@metamask/delegation-toolkit'
import { sepolia as chain } from 'viem/chains'
import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'
const environment = getDeleGatorEnvironment(chain.id)
export const delegation = createDelegation({
scope: {
type: 'erc20Streaming',
tokenAddress: '0xc11F3a8E5C7D16b75c9E2F60d26f5321C6Af5E92',
amountPerSecond: 100n,
initialAmount: 1000000n,
maxAmount: 10000000n,
startTime: 1703980800,
},
to: 'DELEGATE_ADDRESS',
from: 'DELEGATOR_ADDRESS',
environment,
})
getNativeTokenPeriodTransferEnforcerAvailableAmount
Returns the available amount from the native token period enforcer for the current period.
Parameters
Name | Type | Required | Description |
---|---|---|---|
delegation | Delegation | Yes | The delegation object for which you want to check the available amount. |
Example
- example.ts
- config.ts
import { delegation } './config.ts'
// Returns the available amount for current period.
const { availableAmount } = await caveatEnforcerClient.getNativeTokenPeriodTransferEnforcerAvailableAmount({
delegation,
})
import { createDelegation } from '@metamask/delegation-toolkit'
import { sepolia as chain } from 'viem/chains'
import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'
const environment = getDeleGatorEnvironment(chain.id)
export const delegation = createDelegation({
scope: {
type: 'nativeTokenPeriodTransfer',
periodAmount: 1000000000000000000n,
periodDuration: 86400,
startDate: 1743763600,
},
to: 'DELEGATE_ADDRESS',
from: 'DELEGATOR_ADDRESS',
environment,
})
getNativeTokenStreamingEnforcerAvailableAmount
Returns the available amount from the native streaming enforcer.
Parameters
Name | Type | Required | Description |
---|---|---|---|
delegation | Delegation | Yes | The delegation object for which you want to check the available amount. |
Example
- example.ts
- config.ts
import { delegation } './config.ts'
// Returns the available amount
const { availableAmount } = await caveatEnforcerClient.getNativeTokenStreamingEnforcerAvailableAmount({
delegation,
})
import { createDelegation } from '@metamask/delegation-toolkit'
import { sepolia as chain } from 'viem/chains'
import { getDeleGatorEnvironment } from '@metamask/delegation-toolkit'
const environment = getDeleGatorEnvironment(chain.id)
export const delegation = createDelegation({
scope: {
type: "nativeTokenStreaming",
amountPerSecond: 100n,
initialAmount: 1000000n,
maxAmount: 10000000n,
startTime: 1703980800,
},
to: 'DELEGATE_ADDRESS',
from: 'DELEGATOR_ADDRESS',
environment,
})
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
Name | Type | Required | Description |
---|---|---|---|
delegation | Delegation | Yes | The delegation object with token index for which you want to check the available amount. |
Example
- example.ts
- config.ts
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,
})
import { createDelegation, getDeleGatorEnvironment, ROOT_AUTHORITY } from '@metamask/delegation-toolkit'
import { createCaveatBuilder } from '@metamask/delegation-toolkit/utils'
import { sepolia as chain } from 'viem/chains'
const environment = getDeleGatorEnvironment(chain.id)
const caveatBuilder = createCaveatBuilder(environment)
// Current time as start date.
// Since startDate is in seconds, we need to convert milliseconds to seconds.
const startDate = Math.floor(Date.now() / 1000);
const tokenConfigs = [
{
token: "0xb4aE654Aca577781Ca1c5DE8FbE60c2F423f37da",
// 1 token with 18 decimals.
periodAmount: 1000000000000000000n,
// 1 day in seconds.
periodDuration: 86400,
startDate
},
{
// For native token use zeroAddress
token: zeroAddress,
// 0.01 ETH in wei.
periodAmount: 10000000000000000n,
// 1 hour in seconds.
periodDuration: 3600,
startDate
}
]
const caveats = caveatBuilder.addCaveat('nativeTokenTransferAmount', 1000000n).addCaveat({
'multiTokenPeriod',
tokenConfigs
})
export const delegation: Delegation = {
delegate: 'DELEGATE_ADDRESS',
delegator: 'DELEGATOR_ADDRESS',
authority: ROOT_AUTHORITY,
caveats: caveats.build(),
salt: '0x',
};