delegation-framework icon indicating copy to clipboard operation
delegation-framework copied to clipboard

Caveats helper library

Open danfinlay opened this issue 1 year ago • 0 comments

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:

  1. createAllowedCalldataCaveat(address, uint256, bytes)
  2. createERC721TransferCaveat(address, address, uint256)
  3. createRedeemerCaveat(address, address[])
  4. createValueLteCaveat(address, uint256)
  5. createNativeAllowanceCaveat(address, uint256)
  6. createTimestampCaveat(address, uint128, uint128)
  7. createPasswordCaveat(address, uint256)
  8. createNonceCaveat(address, uint256)
  9. createIdCaveat(address, uint256)
  10. createNativeBalanceGteCaveat(address, address, uint256)
  11. createNativeTokenPaymentCaveat(address, address, uint256)
  12. createLimitedCallsCaveat(address, uint256)
  13. createAllowedMethodsCaveat(address, string[])
  14. createAllowedTargetsCaveat(address, address[])
  15. createArgsEqualityCheckCaveat(address, bytes)
  16. createBlockNumberCaveat(address, uint128, uint128)
  17. createDeployedEnforcerCaveat(address, address, bytes32, bytes)
  18. createERC20BalanceGteCaveat(address, address, uint256)
  19. 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.

danfinlay avatar Sep 25 '24 16:09 danfinlay