There's nothing better than when math meets DeFi. This post is about something that might very well be of no practical use whatsoever, but it is something I came across which I found interesting, and I'm wondering if there's any deeper consequence I'm missing.
This starts from the question: what is the maximum leverage one can get using on-chain lending protocols like Maker? The answer to this question is a function of the minimum collateral ration, the amount of liquidity available for the asset pair considered, and the current price of the asset pair. It is possible to answer it exactly in a couple of theoretical cases which we will explore below. We will see that there are two methods to calculate this maximum leverage, one including basic arithmetic, and the other involving infinite series. By equating the two, we can find closed form solutions to series that would otherwise not seem to have an analytical answer.
Oh no, someone appears to have hacked the Aave website!
Maximum leverage assuming infinite liquidity
To understand the underlying principle, let's first use the simplifying assumption of infinite liquidity. In particular, in the case of Maker, we assume that the ETH/DAI pair has a constant spot price. The series method in this case is already discussed in a blog post by Ian Macalinao, but I will repeat the argument below for the sake of completeness we will briefly over it again here.
The leverage ratio is the ratio of the value of the ETH collateral currently in the leveraged position and the amount of ETH the user actually started with. If the ETH/DAI price is currently 1000, and I use 15 ETH as initial collateral, I can generate 10K DAI at the minimum collateral ratio of 1.5 for Maker. I then sell these DAI for ETH to acquire 10 more ETH, assuming the trade incurs zero slippage. I now have 25 ETH, or more than I started with, which is our leverage ratio . What happened here is that since I minted worth of DAI, I was able to add that much worth of ETH to my initial collateral, ending up with what I started with, and with and factoring by , this gives us the result above. Now why not do it another time? I can take that new ETH and generate more DAI which I will then swap for ETH again. The amount of ETH I'm getting this new round is . Assuming no gas fees, we can keep this going for an infinite amount of steps. The total amount of collateral we'll have at step is:
Which corresponds to a leverage:
As it turns out, this is a well known geometric series. The additional collateral (and thus the additional leverage) that we're getting gets smaller at each step, and we tend to a maximum leverage factor by taking the limit as . This limit in the case of Maker where is:
Flash loans to the rescue!
Now even though this limit calculation is a standard result, it still seems pretty cumbersome to have to sum a series to get the answer and there is a much simpler way to proceed.
The idea comes from a practical implementation used by DeFi Saver to open leveraged vaults. They don't recycle the generated DAI a large number of steps to get close to the desired collateral. Rather, they flash loan the maximum possible amount of funds to buy collateral, such that the initial amount in the wallet of the user plus the generated DAI obtained with the funds will be enough to pay back the loan within the same transaction. Assuming that we are flash loaning ETH directly as a simplification, and calling the flash loaned amount , we can write the above as the following equation:
At the end of the operation, we end up with a Maker vault that has ETH in it, having paid back the loan with the ETH we had in our wallet plus the ETH obtained with the generated DAI debt. Our leverage ratio is , which can be easily obtained from the equation above:
The maximum is reached with the minimum collateral ratio, and fortunately we find the exact same result as with the series method.
While this seems trivial, I find it very surprising that we have been able to find the sum of a series without actually summing it. The power of this method will be made clear below when we remove the infinite liquidity assumption.
Maximum leverage with finite liquidity: constant product market
In this section, we make things a bit more complicated. We will assume that the DAI obtained from the ETH is always minted at the same price which we can call the minting price , but that we are taking on leverage by selling in a single constant product ETH/DAI market like Uniswap v2 with finite liquidity. What this means is that for example, if the minting price and the market price are both equal to 1000, 15 ETH put as collateral can give me a maximum of 15K DAI. If I sell these for ETH in the pool, I will obtain a bit less than 10 ETH due to slippage and the final market price will be lower than 1000, but whatever I obtain, minting DAI with it will still give me an amount calculated with the initial price of 1000.
Let and designate the initial balance of DAI and ETH in the pool, the initial amount of ETH collateral we have in our wallet, the minimum collateraliztion ratio of the protocol, and the minting price in ETH per DAI respectively.
With our definition of , the first round of minting DAI will give us DAI to work with. Drawing from the Balancer whitepaper, we can calculate that the amount of ETH we can get by swapping this in the pool is given by:
After this, each balance is updated to reflect the amount swapped in and out. We index these new balances to reflect which step they correspond to:
At this point our leverage is .
If we do this steps, we get to the following problem:
Let be defined by:
where is given by:
Now define the leverage at step . The maximum leverage we can get is .
At a glance it looks like there is no way to sum this series using any traditional method: it's ugly as hell, involves coupled sequences defined by recursion, and we're stuck with no way to compute an analytical answer to the problem of maximum leverage under our assumptions... or are we?
Flash loans again
Even with our assumptions, we can still use the same reasoning as before by using a flash loan. What if we borrow the maximum amount of ETH , such that if we mint some DAI with it, we will be able to sell this DAI for ETH such that our original amount added to is enough to pay back the loan?
Using the result from the Balancer whitepaper again, the amount of ETH we can obtain with the minted DAI is:
And we to be able to pay back the flash loan we need to verify the equation:
Once we have written this condition, we know that the amount of leverage we will have at the end of this operation is so we can just solve for that in the equation above and get our answer for the maximum leverage.
Skipping a bunch of boring arithmetic, and using the simplifying assumption that the minimum collateral ratio is stricly lower than 2, we get that is a solution to a quadratic equation and can be written as follows:
where , and .
So we have obtained an answer for the maximum possible leverage we can obtain under these assumptions. But since the maximum leverage we can get is always the same, then this answer must be the same as with the series method, so what we just wrote is actually the limit of that insane series we thought we would never be able to get a closed form solution for! (I checked numerically that this is correct)
Now is this of any practical use? I don't think so. But I find it amusing that we could get closed form solution in this manner, so I wanted to share this result in case someone finds any deeper mathematical implication that could help solve some previously unsolved problems.
This idea was essentially just an excuse to use a DeFi primitive to do math. For the specific example used however, it turns out that while ugly looking, the series is perfectly summable by hand. Hint: express and as a function of . We get a recursive formula for . All that's left to do is show that converges, and then take the limit of the equation as and solve the quadratic obtained for . This is left as an exercise to the reader. However the reasoning used in this article is applicable to any constant function market maker. Perhaps there are some choices of market makers that would lead to a series that can't actually be summed by hand, in which case we would have a nice math idea derived from flash loans!