Skip to main content
Hooks are lightweight, permissionless contracts which perform a specific action (bridge, swap, lend, etc.). They are designed to be composable and can be chained together to create complex transaction flows which can achieve any multi-chain strategy. Hook chains can be fulfilled by any ERC-7579 smart account with the SuperValidator and SuperExecutor modules installed.

Key Concepts

Hooks implement transient storage during the execution of a transaction to temporarily hold state changes. This allows for efficient inter-hook communication that avoids the high gas costs associated with permanent storage writes. If any hook in a chain fails, the entire transaction is reverted, ensuring atomicity.

Hook Lifecycle

The execution of hooks is a three-step sequence bundled in the hook’s build() function: preExecuteexecution(s)postExecute
  1. Build - Frontend selects the addresses and calldata of the hook(s) in a chain.
  2. Bundle - SuperBundler splits hooks per chain and wraps each into an ERC-4337 UserOp, storing a Merkle tree.
  3. Validate - Smart Account delegates to SuperValidator onchain to perform Merkle proof verification.
  4. Execute - SuperExecutor iterates through hooks, performs each action and updates SuperLedger.

Hook Classifications

Hooks are classified based on HookType and HookSubType. HookType must be defined as NONACCOUNTING, INFLOW, or OUTFLOW depending on the hook’s expected interaction with the accounting system. HookSubTypes designate which action category the hook implements.
  • NONACCOUNTING - Utility hooks that do not update the accounting system. These hooks include HookSubTypes like SWAP, STAKE, BRIDGE.
  • INFLOW - Hooks that process additions to positions and typically encode some sort of deposit action. They include a HookSubType corresponding to the underlying vault being deposited into like ERC4626, ERC7540, etc.
  • OUTFLOW - Hooks that process reductions to positions and typically encode some sort of redeem action. They include similar HookSubType to INFLOW hooks.

Quick Start

Requirements
  • Foundry ≥ v1.3.0
  • Solidity 0.8.30
  • pnpm
  • yarn
Repo Setup Clone the public repo and its submodules:
Install dependencies:
Copy the environment file:
Build & Testing Compile the contracts:
Run tests:

Anatomy of a Hook

  1. Every hook must inherit BaseHook to obtain core hook functionality, along with any specific interfaces your hook may require from ISuperHook.
  2. Each hook contract begins with natspec detailing the layout of the hook data, this ensures that SuperBundler is able to encode the data required for hook execution.
    1. The natspec of the data structure must be placed after the line saying @dev data has the following structure and the natspec must follow the format of @notice DataType Name Offset.
    2. An example of a hook with a simple encoding structure can be found here. This is a more complex example of encoding natspec.
  3. The constructor sets the HookType and HookSubtype as well as any immutable state variables such as the target contract address to call during execution. For a breakdown of different HookType options see Hook Classifications, and the HookSubTypes are found in this library. If your hook will perform an action type that is not yet in the library, add it there as bytes32 public constant CATEGORY_NAME.
  4. It is necessary to decode and validate the encoded hook data used by the hook. It is recommended to create some internal decoder and validation helpers. The decoding must align with the data locations denoted in the natspec as that is the order in which data will be encoded. There are two libraries you can use during the decoding process:
    1. BytesLib - This necessary library can be found in the src/vendor directory, you need only import it to gain access to its functionality.
    2. HookDataDecoder - This library lives in the src/libraries directory. It must be imported and the statement using HookDataDecoder for bytes must be placed at the top of the hook contract.
  5. The _buildHookExecutions() function takes decoded hook data and returns an array of Executions.
    1. There are 3 parameters for this function:
      1. prevHook - The address of the prior hook in a chain, address(0) if this hook is unchained or the first in a chain. Note: If you intend for this hook to be after another hook in a chain bool usePrevHookAmount must be added as a parameter to be encoded in the hook data structure natspec, and ISuperHookResult is required.
      2. account - The smart account address if needed for the Execution payload.
      3. data - The bytes hook data (encoded in the structure defined by the natspec)
    2. The payload of an Execution is as follows:
      1. Target - The address to call (contract or another module)
      2. Value - Native ETH amount to forward as msg.value
      3. Calldata - The encoded function selector to be called and any arguments

Hook Contract Outline

Key fields inherited from BaseHook: See the full base contract for inline docs here.

Writing your First Hook: Approve-and-Deposit ERC-4626

We’ll re-create the example at src/hooks/vaults/4626/ApproveAndDeposit4626VaultHook.sol (GitHub). Approve the vault, deposit amount into it, and emit the number of shares received so that downstream hooks can compose on it.

Hook Data Layout

Implementation Highlights

To test the newly created hook follow the steps outlined in the Quick Start section above.