๐งฎMathematics
A summary of the math involved in building Blend
The information provided by Aloe Labs, Inc. (โwe,โ โusโ or โourโ) on docs.aloe.capital (the โSiteโ) is for general informational purposes only. All information on the Site is provided in good faith, however we make no representation or warranty of any kind, express or implied, regarding the accuracy, adequacy, validity, reliability, availability or completeness of any information on the Site.
Under no circumstance shall we have any liability to you for any loss or damage of any kind incurred as a result of the use of the site or reliance on any information provided on the site. Your use of the site and your reliance on any information on the site is solely at your own risk.
Portion of Funds in Uniswap
Aloe Blend is designed to mimic the payoff curve of Uniswap V2, which means matching its liquidity density from . But what exactly is that density?
To keep things simple, let's look at the no-fee scenario for a Uniswap V2 pool containing assets X and Y. At price we have the constant product formula . Imagine someone trades units of Y for units of X. We now have and a new price , where and . Isolating and , we get the following:
Key Insight: In this context, the liquidity between and is either or , depending on which price is considered "current." If , then liquidity is units of Y. If , then liquidity is units of X.
These results can be massaged to match equations 6.29 and 6.30 in the Uniswap V3 Whitepaper with the slight modification that . That's all it takes to mimic V2 on V3! This is all the background we need to implement the basics of Aloe Blend.
Each Blend Vault has some inventory, , but only part of that inventory is deposited to Uniswap V3: . Since we want the vault to behave like Uniswap V2, we set and . The choice of and is arbitrary as long as , but to make the math work out nicely we use . Combining these expressions with the previous equations yields the following:
In code form:
These equations allow Blend to compute the portion of vault inventory that should be deposited to Uniswap V3. The symmetry and 1.0001 exponentials are quite convenient here, as they reduce on-chain computation and allow existing TickMath library functions to be reused.
Position Width
To use the equations above and figure out what percentage of funds to deposit to Uniswap, Blend must first choose how wide its position will be. This is tough because it has to optimize over the following criteria:
Maximizing silo deposits to earn more yield (smaller Uniswap position is better)
Keeping the Uniswap position in-range (larger Uniswap position is better)
Keeping rebalances as infrequent as possible so that maintenance fees paid to bots are kept to a minimum (larger Uniswap position is better)
These trade-offs are made even more complicated by the fact that Blend should work for all sorts of trading pairs, and stable-stable pairs behave very differently from others. Blend addresses this by measuring implied volatility on-chain and scaling position width accordingly.
Implied Volatility
Implied volatility (IV) differs from historical volatility (HV) in that it's forward-looking rather than backward-looking. In general, high IV values mean that the market thinks a given price is going to swing around a lot. This is perfect information for Blend, because if it observes high IV, it can preemptively increase its Uniswap position width to keep it in-range.
Surprisingly, computing IV on-chain is not only possible, but also more precise and gas-efficient than computing HV. Guillaume Lambert was the first to discover that you can estimate IV from Uniswap V3 data. What follows is a summary of Guillaume's results, plus some modifications to make everything run in the EVM.
This equation operates on data from a single Uniswap pool and determines the implied volatility between the two pool assets. is the fee tier, daily volume is self-explanatory, and tick liquidity is the amount of liquidity available at the current tick. Since IV is a dimensionless quantity, daily volume and tick liquidity must be denominated in the same asset.
There are a few problems here:
isn't as simple as it seems. Uniswap governance can excise a protocol fee on one or both assets independently.
There's no direct way to get daily volume on-chain. It must be estimated from daily LP fee revenue.
With this in mind, let's rework the equation such that it's actually implementable:
There are a few approximations here. First, we've replaced with the geometric mean of and . This may or may not be the "correct" mean to use, but it helps the math work out cleanly. We're also assuming that when in reality it's where and are vectors containing one element for each trade.
The only other tricky thing is making sure that these calculations don't overflow. You can find most of the code here and the oracle documentation here.
The Aloe Labs team did a brief analysis of the accuracy of this volume approximation and found that it was good enough for Blend. Prices were modeled with GBM, and simulations were run for both uniformly-distributed and normally-distributed trade sizes.
The approximation error seems to be proportional to with negligible dependence on GBM's . For reasonable daily price movements (%) the error is just 1%.
Mapping IV to Ticks
The last step is to map IV values to position widths, measured in ticks. To encompass 95% of trading activity, Blend's Uniswap position should cover 2 standard deviations of price movement: . Unfortunately, this would require an uneven number of ticks above the current price () and below the current price (). To make other math simpler, the larger value was chosen.
In code form:
Rebalancing
Since 95% of trading activity should stay in-range of Blend's Uniswap positions, most rebalances can be done by just shuffling liquidity between Uniswap and the silos. No swaps or limit orders necessary.
If/when a primary Uniswap position does slide out of range (or when interest income piles up in one silo faster than the other), Blend is able to place limit orders to get back to a 50/50 inventory ratio. These limit orders are carefully placed to avoid locking in impermanent loss.
One question that arises is how large a limit order should be such that, when it's pushed through, the vault's inventory ratio will be exactly 50/50. As long as one really can create a limit order (a sufficiently thin range order on Uniswap V3), the answer is simple:
This approximation is good enough for Blend, but we've provided more exact formulas here.
Last updated