The Problem: Why Trampoline is Needed
Two critical vulnerabilities exist without the Hooks Trampoline:1. Privileged Context Exploitation
The settlement contract accumulates trading fees and holds funds. Without protection, malicious users could steal funds:2. Settlement Disruption
User hooks could disrupt settlements through:-
Gas Griefing: When settlement calls interactions, it forwards all remaining gas. If a hook reverts with an
INVALIDopcode, it consumes 63/64ths of total transaction gas, making settlements extremely expensive. - Cascading Failures: If a hook reverts without being caught, all other orders in the settlement batch would fail, effectively DoS-ing legitimate traders.
The Solution: Three-Layer Protection
1. Unprivileged Context
All hooks execute from theHooksTrampoline contract’s context, never the settlement contract. This isolates hooks from accessing settlement contract’s accumulated fees or privileged state.
msg.sender is the HooksTrampoline address—never the settlement contract.
Hook implementations can verify settlement execution by checking:
require(msg.sender == HOOKS_TRAMPOLINE_ADDRESS, "not a settlement");2. Gas Limits
Each hook specifies agasLimit capping maximum gas consumption, preventing 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.
3. Revert Tolerance
The trampoline explicitly allows hooks to revert without affecting the settlement, preventing a single failed hook from disrupting an entire batch of orders.success value is read (avoiding compiler warnings) but deliberately ignored.
Settlement Flow
Settlement Phases
Pre-hooks: Execute before the swap. 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 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 for gas estimation edge cases with certain node implementations:eth_estimateGas, preventing failures for transactions consuming less gas when reverting than when succeeding.
This mechanism activates when
forwardedGas < hook.gasLimit, ensuring solvers and users receive accurate gas estimates 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 execution within legitimate settlements
- Deterministic Execution: Hook execution order and gas limits are explicitly specified and enforced