Hooks
Introduction
Hooks are a powerful feature in Euler that allows vault governors to customize and control vault operations. By implementing a hook configuration, governors can specify a hook target (a smart contract) and define which vault operations should trigger the hook. This system provides a flexible way to add custom logic and restrictions to vault operations without modifying the core vault code.
Understanding Hooks
At their core, hooks are external contracts that can intercept and potentially modify or block vault operations. When a hook is triggered during an operation, the vault calls the hook target contract before completing the transaction. If the hook fails, the vault operation also fails, providing a way to enforce additional rules and restrictions.
Key Features
Customizable Operations: Hooks can be configured to affect various vault operations, including deposits, withdrawals, borrows, and repays. This allows for fine-grained control over how users interact with the vault.
Flexible Implementation: Hook targets can implement any custom logic, making them suitable for a wide range of use cases, from simple restrictions to complex business rules.
Security Layer: Hooks provide an additional security layer by allowing governors to implement custom checks and validations before operations are executed.
Common Use Cases
Pause Guardian: A hook can be used to implement a pause mechanism, allowing governors to temporarily disable specific vault functions in response to market conditions or potential exploits.
Permissioned/RWA Vaults: Hooks can control who can deposit or borrow, which is particularly useful for compliance requirements or undercollateralized lending scenarios.
Flash Loan Fees: While flash loans are free by default, hooks can be used to implement fees on flash loan operations.
Utilization Caps: Hooks can prevent a vault's utilization from exceeding a set threshold, helping to manage risk and maintain protocol stability.
Minimum Debt Sizes: To prevent the creation of positions that are too small to liquidate profitably, hooks can block transactions that would create dust positions.
Collateral Limits: Hooks can restrict the number of collateral assets an account can enable, helping to manage risk and complexity.
Implementation Details
When a hooked operation is called on a vault, the vault forwards the exact same calldata to the hook target contract, with the called address attached as trailing data. This means that hook targets must implement the same interface as vaults for the operations they want to hook into. This design allows hook targets to process the same parameters and make decisions based on the same information that the vault would use.
For example, if a user calls deposit(100)
on a vault, and the deposit operation is hooked, the hook target will receive the same deposit(100)
calldata, plus the address of the account authenticated. This ensures that hook targets have all the necessary information to make informed decisions and implement additional features.
For more detailed information about the implementation and examples, please refer to the EVK Periphery repository.