Dynamic LP fees based on swap size
This hook demonstrates beforeSwap with fee override. Optimized for micro-swaps with fees from 0.01% → 0.30%.
Fee Calculator
Your Swap Size
Fee Tiers
Test the dynamic fee
Swap through the pool and see the fee change based on your swap size.
Connect your wallet to swap
The beforeSwap fee override
Hook Intercepts
When a swap is initiated, the PoolManager calls our beforeSwap function with the swap parameters.
Calculate Fee
We look at the swap size and determine which fee tier applies: 0.01%, 0.05%, 0.10%, or 0.30%.
Override Fee
We return the fee with OVERRIDE_FEE_FLAG, telling the PoolManager to use our calculated fee.
Understanding Dynamic Fees
DYNAMIC_FEE_FLAG
Pools that want hooks to override fees must be created with DYNAMIC_FEE_FLAG as the fee parameter. This signals to the PoolManager that fee overrides are allowed.
OVERRIDE_FEE_FLAG
When returning from beforeSwap, we OR the fee with OVERRIDE_FEE_FLAG to tell the PoolManager to use our fee instead of the pool's base fee.
beforeSwap Permissions
This hook only needs beforeSwap: true. We don't modify swap amounts (no delta return), just the fee charged.
Use Cases
Dynamic fees enable sophisticated pricing: volume-based fees, volatility-adjusted fees, time-based fees, or MEV-aware fees.