Uniswap V4 Hook Demo

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

0 ETH0.00010 ETH0.01 ETH
Current Fee
0.10%
Small Tier (1000 bips)

Fee Tiers

Micro0.30%
< 0.0001 ETH
Small← Current0.10%
≥ 0.0001 ETH
Medium0.05%
≥ 0.001 ETH
Large0.01%
≥ 0.005 ETH

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

01

Hook Intercepts

When a swap is initiated, the PoolManager calls our beforeSwap function with the swap parameters.

02

Calculate Fee

We look at the swap size and determine which fee tier applies: 0.01%, 0.05%, 0.10%, or 0.30%.

03

Override Fee

We return the fee with OVERRIDE_FEE_FLAG, telling the PoolManager to use our calculated fee.

DynamicFee.sol
/// @notice Calculate fee based on swap size
function _calculateFee(uint256 size) internal pure {
if (size < 0.0001 ether) return 100; // 0.01%
if (size < 0.001 ether) return 500; // 0.05%
if (size < 0.005 ether) return 1000; // 0.10%
return 3000; // 0.30%
}
/// @notice Return fee with override flag
return (selector, ZERO_DELTA, fee | OVERRIDE_FEE_FLAG);

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.

fee: LPFeeLibrary.DYNAMIC_FEE_FLAG

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.

fee | LPFeeLibrary.OVERRIDE_FEE_FLAG

beforeSwap Permissions

This hook only needs beforeSwap: true. We don't modify swap amounts (no delta return), just the fee charged.

✓ beforeSwap: true
✗ beforeSwapReturnDelta: false

Use Cases

Dynamic fees enable sophisticated pricing: volume-based fees, volatility-adjusted fees, time-based fees, or MEV-aware fees.

• Larger trades → Higher fees
• High volatility → Higher fees
• Low liquidity → Higher fees