Hook Execution Flow
Every swap triggers two hook calls:beforeSwap runs after oracle validation but before vault execution, and afterSwap runs after swap completion with actual output amounts.
Hooks can modify execution parameters, reject swaps by reverting, or record execution data for monitoring. All hook operations execute within the same transaction as the swap itself—either everything succeeds or everything reverts.
Before Swap Hook
ThebeforeSwap function receives swap details and oracle price, returning adjusted execution price and expected output amount. Use this for after-hours spread application, dynamic fee adjustment, or custom pricing logic.
afterSwap. This enables verification that actual execution matched predicted outcomes.
After Swap Hook
TheafterSwap function receives actual output amounts after vault execution, enabling post-execution validation and monitoring. Compare actual outputs against expected amounts to detect excessive slippage or pricing anomalies.
Slippage Validation Logic
Slippage Validation Logic
The hook calculates realized slippage by comparing expected versus actual output:Unlike
beforeSwap, this function cannot revert the swap since tokens already transferred. Instead, emit events for monitoring systems to detect unusual execution patterns.After-Hours Spreads
Apply additional spreads when trading occurs outside traditional market hours (14:00-21:00 UTC for US markets). This compensates liquidity providers for increased risk when external price discovery mechanisms are inactive.| Time Period | Market Status | Spread Applied |
|---|---|---|
| 14:00-21:00 UTC, Mon-Fri | Market Open | 0 bps (oracle price) |
| 21:00-14:00 UTC, Mon-Fri | After Hours | 25-100 bps configurable |
| Saturday-Sunday | Weekend | 50-150 bps configurable |
| Exchange Holidays | Closed | 50-150 bps configurable |
setAfterHoursSpread function with values in basis points. Higher spreads during low-liquidity periods protect providers from adverse selection while maintaining trading availability.
Pool-Specific Configuration
Configure different slippage thresholds and pricing rules per pool to accommodate varying liquidity profiles and volatility characteristics.Configure maximum acceptable slippage for specific pool, overriding default threshold
Apply custom pricing logic to individual pools (e.g., different after-hours spreads)
Exclude specific pools from hook execution, falling back to standard oracle pricing
Execution Context Storage
Hooks maintain execution state in memory betweenbeforeSwap and afterSwap calls, storing expected outcomes for later comparison.
Pool identifier linking pre-swap and post-swap contexts
Asset identifier (ISIN) for price feed lookup
Oracle price at execution time, before any spread adjustments
Calculated output amount based on oracle price and pool state
Actual received amount after Balancer vault execution
Calculated deviation between expected and actual output in basis points
Custom Hook Development
Deploy your own hooks implementing theIPricingHookV3 interface for specialized pricing logic. Common use cases include:
- Dynamic fee adjustment based on volatility or time of day
- Circuit breakers that halt trading during extreme price movements
- MEV protection through price impact limits or randomized execution
- Compliance checks validating additional rules beyond token-level restrictions
- Analytics recording for trade surveillance and market monitoring
setPricingHook function. This routes through timelock governance to prevent immediate changes that could disrupt active pools.