Overview
Hooks Trampoline is a critical security component in CoW Protocol that enables traders to execute custom Ethereum calls atomically within their settlement transactions—without compromising the protocol’s security or stability. Hooks are a CoW Protocol feature that allows traders to specify custom Ethereum calls as part of their order. These calls execute atomically in the same transaction as the trade, enabling powerful use cases like conditional orders, automated portfolio rebalancing, and multi-step DeFi interactions. However, executing arbitrary user code within a settlement transaction introduces significant risks. The Hooks Trampoline contract acts as a protective intermediary that isolates user hooks from the privileged settlement contract context while ensuring that poorly configured hooks cannot disrupt settlements.The Problem: Why Trampoline is Needed
Without the Hooks Trampoline, executing user hooks directly from the settlement contract would create two critical vulnerabilities:1. Privileged Context Exploitation
The CoW Protocol settlement contract accumulates trading fees and holds funds. If user hooks executed from this privileged context, malicious users could trivially steal funds by specifying hooks like:2. Settlement Disruption
User hooks could disrupt or sabotage entire settlements in two ways: Gas Griefing: When the settlement contract calls interactions, it forwards all remaining gas (gasleft()). If a hook reverts with an INVALID opcode, it consumes 63/64ths of the total transaction gas, making settlements extremely expensive for no reason.
Cascading Failures: If a hook reverts and the revert is not caught, all other orders in the settlement batch would fail to execute, effectively DoS-ing legitimate traders.
The Solution: Three-Layer Protection
The Hooks Trampoline implements three fundamental protections to address these vulnerabilities:1. Unprivileged Context
All hooks execute from theHooksTrampoline contract’s context, not the settlement contract. This means hooks have no access to the settlement contract’s accumulated fees or privileged state.
msg.sender is the HooksTrampoline address—never the settlement contract. This architectural separation ensures complete isolation of privileges.
Hook implementations can verify they’re being called during a settlement by checking:
2. Gas Limits
Each hook specifies agasLimit that caps the maximum gas consumption. This prevents INVALID opcodes or gas-intensive operations from consuming excessive gas.
The gas limit calculation accounts for the EVM’s 63/64 forwarding rule. When a contract makes a call, it automatically reserves 1/64th of remaining gas for post-call operations. The trampoline checks that sufficient gas exists before attempting the hook call.
3. Revert Tolerance
The trampoline explicitly allows hooks to revert without affecting the settlement. This is critical for preventing a single failed hook from disrupting an entire batch of orders.success value is read (to avoid compiler warnings) but deliberately ignored. Whether a hook succeeds or reverts, the settlement continues executing.
Settlement Flow
The following diagram illustrates how hooks integrate into the CoW Protocol settlement process:Settlement Phases
Pre-hooks: Execute before the swap. These are ideal for:- Token approvals
- Position setup
- Conditional checks
- Pre-trade state modifications
- Staking received tokens
- Claiming rewards
- Triggering follow-up actions
- State cleanup
Both pre-hooks and post-hooks execute atomically within the same transaction. If the settlement reverts for any reason (e.g., slippage protection), all hook effects are also reverted.
Edge Case: Gas Estimation
The trampoline includes a special mechanism to handle gas estimation edge cases with certain node implementations:eth_estimateGas, preventing estimation failures for transactions that consume less gas when reverting than when succeeding.
This mechanism is called when
forwardedGas < hook.gasLimit, ensuring that solvers and users receive accurate gas estimates even when hooks cannot execute due to insufficient gas.Security Guarantees
The Hooks Trampoline provides the following security guarantees:- Privilege Isolation: Hooks never execute with settlement contract privileges
- Gas Protection: No single hook can consume unlimited gas
- Fault Isolation: Hook failures cannot disrupt settlements or affect other orders
- Settlement Verification: Hooks can cryptographically verify they’re executing within a legitimate settlement
- Deterministic Execution: Hook execution order and gas limits are explicitly specified and enforced
