Dynamic fees with
sigmoid S-curve
for LPs
A Uniswap v4 hook that adjusts swap fees in real-time using a sigmoid curve model — protecting liquidity providers from MEV arbitrage and minimizing impermanent loss on the USDC/WETH pool.
How it protects
liquidity providers
The hook intercepts every swap and applies a dynamic fee based on the real-time price discrepancy between the pool and the global market.
Price delta detection
On every beforeSwap call, the hook compares the pool's sqrtPriceX96 with the custom low-latency oracle price (CryptoCompare via Chainlink Functions). The absolute percentage difference Δp is computed using 64.64 fixed-point arithmetic.
beforeSwap → getSlot0 → computeDeltaSigmoid fee calculation
The price delta is fed into the sigmoid S-curve function using ABDK Math 64x64 library. The result scales smoothly from the base fee to a maximum of 2× — slow growth for small deltas, exponential for large ones.
fee(Δp) = c₀ + c₁ / (1 + e^(c₂·(c₃−Δp)))Arbitrage direction check
The hook detects which direction would be profitable for an arbitrageur. If pool price > oracle price, selling WETH is the arb direction — those swaps get the higher fee. Swaps against the arb direction get a reduced fee to incentivize rebalancing.
zeroForOne XOR (delta < 0) → penaltyOnce per block (gas-efficient)
The oracle price delta and fee are recalculated only on the first swap of each block. Subsequent swaps in the same block reuse the cached value. Effective MEV arbitrage only happens at the top of the block due to searcher competition, so the impact is negligible.
if (block.number > lastBlockNumber) { update }3-layer oracle fallback
If the custom oracle becomes stale (> 5 minutes without update), the hook automatically falls back to the Chainlink ETH/USD price feed. If that too times out, the base fee is applied directly. LPs are always protected.
custom → chainlink → baseFeeNo deterrence of informed traders
The sigmoid curve ensures excessive fees only apply on large, obvious discrepancies. Regular traders and informed traders (who would avoid high-fee pools) are unaffected. Only MEV bots exploiting inter-block arbitrage pay the penalty.
fee ≤ 2× baseFee alwaysThe sigmoid function
Mathematical expression of the dynamic fee model, implemented in Solidity using ABDK Math 64x64 fixed-point arithmetic.
Price delta between pool (post-swap) and the Binance/CryptoCompare price at the estimated timestamp
Baseline offset of the curve in 64.64 fixed-point format
Maximum amplitude of the sigmoid — caps the delta multiplier at 1
Slope factor controlling how fast the fee grows with the price delta
Inflection point at 1% price difference — where the curve accelerates
Ready to deploy
on mainnet
All contracts are written manually with Foundry. The web interface connects directly to the deployed contracts via wagmi + viem.