This repo provides a Uniswap v4 hook that applies a dynamic pool fee based on the deviation between the implied pool price and a reference rate.
POC FOR UNISWAP HOOK INCUBATOR; DEFINITELY NOT PROD-READY AND ALMOST GUARANTEED TO CONTAIN BUGS
There are many proposed approaches to handling MEV. While some solutions propose MEV as a "problem" to stop, this takes the opposite approach of treating MEV as profit to be shared amongst liquidity providers.
To share MEV, this hook:
- is invoked during beforeSwap
- calculates the approx expected
finalPoolPricerice post-swap (using a custom, read-only fork of PoolManager.swap() calculations logic) - queries the
referenceRatecall associated with the pool - calculates the percent deviation of
finalPoolPricefromreferenceRate(e.g., 5%) - applies a dynamic fee that increases with percent deviation
- the reference rate is hardcoded to be a ChainlinkAggregatorV3 interface. It will work with any contract adhering to that interface.
- improvement: handle arbitrary rate calldata
- uses a binary fee that is either "base" or "elevated"
- improvement: add a dynamic fee curve
- uses the same fee and deviation tolerance for all pools
- improvement: per-pool fees and deviation tolerances
- current assumption: final price deviation is assumed to represent MEV
- improvement: account for direction, size, and initial tick of swap
- general improvement: optimize for gas by caching pool and reference rate precisions
$ forge build$ forge test