HooksTrampoline contract provides a secure execution environment for user-specified hooks during CoW Protocol settlements. It ensures hooks are executed in an unprivileged context and prevents hook failures from blocking settlements.
Contract Overview
Constructor
Initializes the trampoline with the CoW Protocol settlement contract address.Parameters
Example
State Variables
settlement
Returns the address of the authorized settlement contract. Only this address can call the
execute() function.
Functions
execute
Executes an array of user-specified hooks during a CoW Protocol settlement.Parameters
Behavior
- Access Control: Can only be called by the settlement contract (enforced by
onlySettlementmodifier) - Gas Management: Each hook is called with its specified gas limit using the formula
forwardedGas = gasleft() * 63 / 64 - Revert Handling: Hook failures do NOT cause the entire transaction to revert, protecting the settlement
- Sequential Execution: Hooks execute in array order
- Gas Limit Verification: If insufficient gas remains for a hook’s gas limit, the function reverts by wasting all remaining gas
Gas Limit Notes
The trampoline attempts to forward exactly the specified gas limit to each hook, but may forward slightly less due to gas overhead between measurement and execution.
Example
Modifiers
onlySettlement
msg.sender is the CoW Protocol settlement contract. Methods with this modifier are guaranteed to only be called as part of a CoW Protocol settlement.
Behavior
- Checks if
msg.sender == settlement - Reverts with
NotASettlement()if the check fails - Allows function execution to proceed if the check passes
Example
Security Features
Unprivileged Execution
Hooks execute in the trampoline’s context, not the settlement contract’s context. This prevents hooks from accessing privileged settlement contract state or functionality.Revert Isolation
Hook reverts are caught and ignored, preventing a single malicious or badly configured hook from blocking an entire settlement batch.Gas Limit Enforcement
Each hook has a specified gas limit, preventing runaway execution from consuming excessive gas or causing out-of-gas errors for subsequent operations.Related
- Hook Struct — Structure specification for hooks
- Errors — Error types and handling
- Usage Guide — Guide to implementing hooks