image.png

image.png

image.png

<aside> 🦄 Protecting LPs from impermanent loss using dynamic fees based on volatility (calculated with Brevis) and swap size.

</aside>

<aside> 🔗 Links

Demo:

https://www.youtube.com/watch?v=ZU8osonTsqY

Description

In order book based exchanges liquidity providers are able to compensate for and deter trades that incur loss by increasing fees via bid/ask spreads. We have implemented a Uniswap V4 Hook that offers this protection to liquidity providers by dynamically adjusting fees so that higher fees are charged during periods of high volatility and when filling large swaps.

The on-chain calculation of realized volatility based on historical prices is computationally expensive (and infeasible due to gas costs). This, however, would be a perfect use-case for the Brevis co-processor. We have created a Brevis circuit for calculating realized volatility along with a corresponding ZK proof. The expensive volatility computation is now performed off-chain, and the results together with a ZK proof of the input price data and the calculation is then written on-chain. The Brevis service feeds updated verified volatility values into our hook contract via a callback function.

Our hook implements the beforeSwap function and returns an override fee which is calculated based on the latest volatility and the size of the current swap. We have various unit tests that demonstrate how the fee increased in relation to high volatility and large swap sizes.

Our inspiration for the project was taken from our TradeFi experience in seeing how markets react to volatility (we are a team of ex-TradFi quant traders and devs).

---Contact Info--- If you would like to get in touch please contact us via: Discord: superpie0596 Telegram: https://t.me/jamesw0x

Challenges

There were some connectivity issues with the Brevis service and some of the Brevis code example repos were out of date. However, the Brevis team was very helpful and responsive on Discord.

Also we had to refactor our hook to account for changes to the Uniswap v4-core repository, however Haardik helped us resolve these.

Contributions

There was a lot of overlap and paired programming with team members. The general work split is as follows:

Howell: research using Binance data into how order books price in volatility and order size. Research and implementation of fee calculation in Solidity.

Rayan: realized volatility Brevis circuit in Go using Brevis SDK. Service application for continuously calculating and publishing up-to-date volatilities using TypeScript.

Riccardo: development of testing of dynamic fee hook using Uniswap v4-core repo in Solidity.

James: development of hook callback function for receiving updated verified volatility data from Brevis in Solidity. Also some project management.