Caveats helper library
Caveats Library: Simplified Caveat Creation for Tests
This PR introduces the Caveats library, designed to streamline the process of creating caveats for testing purposes. The library provides a set of easy-to-use functions that generate caveat structures with minimal input, prioritizing readability over gas efficiency.
Key Features
- Simplified caveat creation for various enforcer types
- Consistent interface across different caveat types
- Improved readability in test code
- Encapsulation of complex abi encoding and packing
Library Interface
The Caveats library exposes the following functions, each returning a Caveat struct:
-
createAllowedCalldataCaveat(address, uint256, bytes) -
createERC721TransferCaveat(address, address, uint256) -
createRedeemerCaveat(address, address[]) -
createValueLteCaveat(address, uint256) -
createNativeAllowanceCaveat(address, uint256) -
createTimestampCaveat(address, uint128, uint128) -
createPasswordCaveat(address, uint256) -
createNonceCaveat(address, uint256) -
createIdCaveat(address, uint256) -
createNativeBalanceGteCaveat(address, address, uint256) -
createNativeTokenPaymentCaveat(address, address, uint256) -
createLimitedCallsCaveat(address, uint256) -
createAllowedMethodsCaveat(address, string[]) -
createAllowedTargetsCaveat(address, address[]) -
createArgsEqualityCheckCaveat(address, bytes) -
createBlockNumberCaveat(address, uint128, uint128) -
createDeployedEnforcerCaveat(address, address, bytes32, bytes) -
createERC20BalanceGteCaveat(address, address, uint256) -
createERC20TransferAmountCaveat(address, address, uint256)
Each function takes the enforcer address as its first parameter, followed by caveat-specific parameters. The library handles the necessary abi encoding and packing internally, returning a properly formatted Caveat struct.
Usage Example
Caveat memory timestampCaveat = Caveats.createTimestampCaveat(
address(timestampEnforcer),
uint128(block.timestamp),
uint128(block.timestamp + 1 days)
);
This library significantly simplifies caveat creation in tests, improving code readability and reducing the potential for errors when manually constructing caveat structures.