Protecting AMM Liquidity with On-Chain ML Models

Matthew Wang
Valence
Published in
7 min readSep 25, 2023

Introduction

Here at Vanna Labs we’re big fans of permissionless and decentralized systems, which is why we’re also big fans of decentralized finance (DeFi). Protocols like automated market-makers (AMMs) allow anyone, not just sophisticated traders and market-makers, to effortlessly provide liquidity to the markets and earn fees with the simple click of a button. However, the unfortunate reality of things is that with existing AMM designs, the majority of liquidity providers often bleed money through impermanent loss and arbitrage: here’s an article based on this research paper that present data that shows that fees earned by liquidity providers (LP) are often outweighed by impermanent loss.

Research on 17 pools on Uniswap V3 show that impermanent loss outweighs fees made in the majority of pools (Source: https://arxiv.org/abs/2111.09192?ref=hackernoon.com)

That’s why we are building the Vanna Blockchain, a blockchain that supports native, secure, and scalable on-chain inference to allow for decentralized applications (dApps) to seamlessly leverage sophisticated compute to build intelligent features to solve difficult problems like impermanent loss. And that’s exactly what we’ve done today, by building VannaSwap: a proof-of-concept AMM that uses on-chain machine learning models to tackle the impermanent loss problem head-on.

Impermanent Loss & Arbitrage

Before we move on to the mechanisms of VannaSwap, it’s important to understand the major drivers of impermanent loss in automated market-making. Impermanent loss (IL) is a risk that LP funds are exposed to when the ratio of the tokens in the liquidity pool changes over time due to traders repricing the ratio of tokens in the pool to match the ratio of the changing asset prices. If you’re not familiar with impermanent loss this article helps breaks it down. Simply put, risk is elevated when the markets are volatile because there are more opportunities for arbitrageurs to rebalance the ratio of tokens in the pool to match the volatile asset prices which imposes impermanent loss on the liquidity providers.

This research paper published by a16z provides an even more precise definition to quantify counterparty risk by arbitrageurs with liquidity provision, that is: loss-versus-rebalance or LVR. LVR is a continuous time model that aims to measure the aggregate value that arbitrageurs instantaneously extract from LP pools trade-by-trade. This article breaks it down in more detail.

So what are the main solutions to this IL and LVR epidemic that plagues AMMs? A major initiative that is being explored by many AMMs to address LVR loss is the idea of dynamic fees. When traders trade against an AMM pool, the ‘fee’ is the cut that the pool takes and compensates liquidity providers with, which is how LPs make money. Currently, that fee is often a static percentage fee, but dynamic fees that change depending on market conditions can actually be used to protect liquidity in times of volatility. As we’ll explain in more detail below, higher fees can increase LP compensation or be used to disincentivize arbitrage against the pool during periods of elevated volatility.

The Power of Dynamic Fees

TradFi market-makers often have sophisticated algorithms that compute and determine the “optimal” amount of spread/fees to quote on orderbook exchanges. The reason why quoting the correct spreads or fees in market-making is extremely important is explained in the image below:

As one can see from above, quoting the right fees in market-making is crucial to improving profitability: it’s a precarious balancing game between quoting more fees to compensate for elevated risk during volatile markets and quoting lower fees to attract trading volume during calm markets.

However, if you look at AMM DEXs, they simply do not have the ability to run sophisticated models in the protocol due to limitations in compute for traditional PoS blockchains (see our previous article on said limitations). That’s why here at Vanna Labs we are so excited to be building the Vanna Blockchain, where the secure and seamless on-chain inference could allow use-cases like an intelligent AMM that leverages the power of on-chain machine learning to quote dynamic fees.

Enter: VannaSwap

In order to showcase how on-chain machine learning inference on Vanna can benefit DeFi, we decided to create VannaSwap. VannaSwap, similar to many other AMMs, uses the constant-product formula for market-making. However, we also upload a machine learning model onto the Vanna Blockchain that we seamlessly inference from the solidity smart contract to compute the optimal fee we should charge to trade against pools on VannaSwap.

The question now becomes, how do we create a good model that is able to quote optimal spreads in an AMM setting similar to how sophisticated market-makers in TradFi quote on centralized exchanges? We decided to train a regression model that regresses different rolling windows of price volatility as features on the spreads that market makers quote on actual centralized exchanges.

First, we pull historical orderbook data from Kraken, and feature engineer 1-minute, 2-minute, and 4-minute window volatilites as features to train a lasso-regularized linear regression model. Like aforementioned, the dependent variable we use for the model is the top-of-the-book spread quoted by market-makers on Kraken. The reason we decided to use l1 lasso regularization was to create a sparser model to address feature multicollinearity between different rolling windows of price volatility. After we train and test the model, we convert it to ONNX format and upload it the Vanna Blockchain where it can be inferenced with it’s CID.

VannaSwap ML

After developing the core features of the AMM, we decided to run a historical on-chain simulation (11/09/2022–11/12/2022) of our AMM to see the dynamic fee model in action. We replay historical prices for the AMM pool and model traders as purely rational participants that mathematically optimize their trades to maximize arbitrage profit, the simulation is also run with the assumption of 0 hedging slippage and gas costs.

In the image below, one can observe that when ETH/USDC (green line) crashed and volatility was high, the fees computed by the regression model shot up. This intuitively makes sense because the regression model is aimed to emulate market-maker behavior, and in times of volatility it’s common for market makers to widen their spreads on the orderbook.

Red: AMM Dynamic Fee, Green: ETH/USDC Price

In order to concretely quantify the performance benefit brought about by the dynamic fee model, we also proceed to run the simulation for a standard constant-product market maker (CPMM) with static fees equal to the median fee quoted by the dynamic model. This allows us to benchmark the relative performance of the dynamic fee model against a control group. The results can be seen in the image below that graphs the difference between the LP balance (red) and the exploitable LVR at Δt (blue) of the dynamic fee AMM model - the static fee AMM model.

From the graph above, one can observe that when the price is more volatile the difference (dynamic - static) between the exploitable LVR indicated by the blue bars is more negative, signaling that the dynamic fee model lowers the exploitable LVR relative to the static fee model. The results of that are immediately reflected in the difference in LP balance between the dynamic fee versus static fee model, as the difference climbs more in times of higher volatility.

From the results of the historical simulation, we can see that empirically ML-driven dynamic fees for AMMs can actually protect LP liquidity and result in less net loss for LPs. This is quite exciting as LP risk/loss has been one of the biggest pain points in DeFi in recent years.

Finally, we build a sweet-looking front-end that enhances the UI/UX of the VannaSwap experience. See for yourself below:

Conclusion

From VannaSwap, one can observe that sophisticated compute can potentially bring about great benefits to the dApp space, and can revolutionize DeFi the same way it revolutionized TradFi trading.

Here at Vanna Labs we’re trying to build infrastructure that would allow easy and seamless deployment of reliable and trustless AI/ML empowered dApps similar to VannaSwap. We have so many more interesting ideas on our list: AI-generated NFTs, ML risk engines on lending protocols, models that optimize utilization rates between lending pools, AI-driven yield farming strategies…etc.

Stay tuned to our medium page, our website, and our twitter for further updates that are coming very soon. I hope you’re as excited as we are.

Twitter: https://twitter.com/0xVannaLabs

Website: https://www.vannalabs.ai/

--

--