DynamicFee Hook — Nezlobin's Directional Fee Framework
A Uniswap v4 hook that protects LPs from toxic arbitrage by charging asymmetric fees based on whether swaps move the pool price toward or away from the Chainlink oracle price.
Get Oracle Price — Fetch current price from Chainlink (ETH/USD, LINK/USD)
Get Pool Price — Derive from current sqrtPriceX96
Calculate Deviation — |poolPrice - oraclePrice| / oraclePrice in basis points
Classify Zone — TIGHT (0-1%), NORMAL (1-3%), ELEVATED (3-5%), HIGH (5-10%), EXTREME (>10%)
Determine Direction — Does swap move price TOWARD or AWAY from oracle?
Apply Asymmetric Fee — Lower fee for stabilizing trades, higher for arbitrage
Fee Matrix (basis points)
Zone
Toward Oracle
Away from Oracle
TIGHT (0-1%)
5
10
NORMAL (1-3%)
10
30
ELEVATED (3-5%)
20
50
HIGH (5-10%)
30
100
EXTREME (>10%)
50
200
Max fee cap: 200 bps (2%)
src/
├── DynamicFee.sol # Main hook — beforeSwap returns dynamic fee
├── base/
│ └── BaseHook.sol # Minimal base hook with permission validation
└── libraries/
├── OracleManager.sol # Chainlink integration (staleness checks, decimal normalization)
├── DeviationMonitor.sol # Deviation calculation and zone classification
└── FeeCalculator.sol # Fee matrix lookup
Hook
Enabled
Purpose
beforeSwap
Yes
Calculate and return dynamic fee with OVERRIDE_FEE_FLAG
afterSwap
Yes
Update zone tracking, emit ZoneTransition events
forge install
forge build
forge test -vvv
forge coverage
Deployed Contracts (Base Sepolia)
Contract
Address
BaseScan
DynamicFee Hook
0x25F98678a92Af6aCC54cE3cE687762aCA316C0C0
view
tWETH (Mock)
0x99cbB0a7B93000304CD1f345f3619aA2e2a04178
view
tUSDC (Mock)
0xD7a18736e6E76091069398A6b7ed30b696938704
view
tLINK (Mock)
0x3aD21bfc40d1C4232599ee61412976980f55463e
view
ETH/USD Oracle (Mock)
0xa4f7aa1Facd8056D27047adD81Cb92dFAA2a5C71
view
LINK/USD Oracle (Mock)
0x09C3483Cb112e39754b367C14A94E8F883194554
view
PoolModifyLiquidityTest
0x07D9e709ad91465Cd790486b86dddA24D94b2904
view
PoolSwapTest
0x779469D958cdc0ac7e00B4933E2C7b1974bc5Fd3
view
Uniswap v4 PoolManager
0x05E73354cFDd6745C338b50BcFDfA3Aa6fA03408
view
Pool Creation & Liquidity
Test Swaps (9 total — 3 per pool)
Pool
Swap
Transaction
ETH/USDC
Small (1 tWETH → tUSDC)
0x46e46b…
ETH/USDC
Medium (10 tWETH → tUSDC)
0xe8a492…
ETH/USDC
Reverse (5 tUSDC → tWETH)
0x273014…
LINK/USDC
Small (1 tLINK → tUSDC)
0xe67d75…
LINK/USDC
Medium (10 tLINK → tUSDC)
0xdde162…
LINK/USDC
Reverse (5 tUSDC → tLINK)
0x0c6910…
ETH/LINK
Small (1 tWETH → tLINK)
0x3436f0…
ETH/LINK
Medium (10 tWETH → tLINK)
0x9dfdfcef…
ETH/LINK
Reverse (5 tLINK → tWETH)
0x075155…
Arbitrage Against Oracle (High Fee)
Oracle: ETH = $3000, Pool: ETH = $2900 (underpriced)
Swap: Buy ETH (moves price away from oracle)
Fee: 100+ bps (HIGH zone, AWAY direction)
Stabilizing Trade (Low Fee)
Oracle: ETH = $3000, Pool: ETH = $3100 (overpriced)
Swap: Sell ETH (moves price toward oracle)
Fee: 10-30 bps (incentivizing rebalance)
Chain : Base Sepolia (Chain ID 84532)
PoolManager : 0x05E73354cFDd6745C338b50BcFDfA3Aa6fA03408
Deployer : 0xbaacDCFfA93B984C914014F83Ee28B68dF88DC87
MIT