Skip to content

DelleonMcglone/dynamic-fee

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

How It Works

  1. Get Oracle Price — Fetch current price from Chainlink (ETH/USD, LINK/USD)
  2. Get Pool Price — Derive from current sqrtPriceX96
  3. Calculate Deviation|poolPrice - oraclePrice| / oraclePrice in basis points
  4. Classify Zone — TIGHT (0-1%), NORMAL (1-3%), ELEVATED (3-5%), HIGH (5-10%), EXTREME (>10%)
  5. Determine Direction — Does swap move price TOWARD or AWAY from oracle?
  6. 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%)

Architecture

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 Permissions

Hook Enabled Purpose
beforeSwap Yes Calculate and return dynamic fee with OVERRIDE_FEE_FLAG
afterSwap Yes Update zone tracking, emit ZoneTransition events

Build & Test

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

Key Transactions

Deployment

Action Transaction
Deploy tWETH 0x7e2f2c…
Deploy tUSDC 0x2d82d3…
Deploy tLINK 0x1d26e2…
Deploy ETH/USD Oracle 0x1e8200…
Deploy LINK/USD Oracle 0x208735…
Deploy DynamicFee Hook (CREATE2) 0x60d379…
Deploy LiquidityRouter 0x7e1a0c…
Deploy SwapRouter 0x333ab3…

Pool Creation & Liquidity

Action Transaction
Configure ETH/USDC pool 0xbca925…
Initialize ETH/USDC pool 0x7e700f…
Add liquidity ETH/USDC 0x9e0c48…
Configure LINK/USDC pool 0x933aaf…
Initialize LINK/USDC pool 0x2583c3…
Add liquidity LINK/USDC 0x8d928b…
Configure ETH/LINK pool 0x889e34…
Initialize ETH/LINK pool 0x4768c2…
Add liquidity ETH/LINK 0x0fec8a…

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…

Example Scenarios

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)

Network Details

  • Chain: Base Sepolia (Chain ID 84532)
  • PoolManager: 0x05E73354cFDd6745C338b50BcFDfA3Aa6fA03408
  • Deployer: 0xbaacDCFfA93B984C914014F83Ee28B68dF88DC87

License

MIT

About

Multi-signal dynamic fee hook that automatically adjusts swap fees based on volatility, volume, and pool state. Built as the enhancement layer of the Mantua.AI DeFi suite on Uniswap v4

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors