- Published on

# Summing series with flash loans

- Authors
- Name
- experience
- @trustlessexp

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

### Series method

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 $\approx 1.67 \times$ more than I started with, which is our leverage ratio $L$. What happened here is that since I minted $C_{0} / R$ worth of DAI, I was able to add that much worth of ETH to my initial collateral, ending up with $C_{0} + C_{0}/R$ what I started with, and with $C = 1.5$ and factoring by $C_{0}$, this gives us the result above. Now why not do it another time? I can take that $C_{0}/R$ 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 $\frac{C_{0}}{R}/R = C_{0}/R^{2}$. 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 $n$ 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 $n \rightarrow \infty$. This limit in the case of Maker where $R_{min} = 1.5$ 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 $C_{0}$ 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 $C_{L}$, we can write the above as the following equation:

At the end of the operation, we end up with a Maker vault that has $C_{L}$ ETH in it, having paid back the loan with the $C_{0}$ ETH we had in our wallet plus the ETH obtained with the generated DAI debt. Our leverage ratio is $C_{L}/C_{0}$, 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 $p_{m}$, 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 $B_{0}^{D}, \ B_{0}^{E}, \ C_{0}, \ R_{\mathrm{min}}$ and $p$ 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 $p$, the first round of minting DAI will give us $\displaystyle{\frac{C_{0}}{p R_{m}}}$ 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 $L = (C_{0} + C_{1})/C_{0}$.

If we do this $n$ steps, we get to the following problem:

Let $(C_{n})_{n \in \mathbb{N}}$ be defined by:

where $(B_{n}^{D})_{n \in \mathbb{N}}$ is given by:

and $(B_{n}^{E})_{n \in \mathbb{N}}$ by:

Now define $\displaystyle L_{n} = \frac{1}{C_{0}} \sum_{k = 0}^{n} C_{k}$ the leverage at step $n$. The maximum leverage we can get is $L = \displaystyle \lim_{n \rightarrow \infty} L_{n}$.

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 $C_{L}$, such that if we mint some DAI with it, we will be able to sell this DAI for $C_{1}$ ETH such that our original amount $C_{0}$ added to $C_{1}$ 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 $L = C_{L}/C_{0}$ 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 $R_{m}$ is stricly lower than 2, we get that $L$ is a solution to a quadratic equation and can be written as follows:

where $\displaystyle a = \frac{1}{pR_{m}}$, $\displaystyle b = B_{0}^{D} - \frac{C_{0}}{pR_{m}} - \frac{B_{0}^{E}}{pR_{m}}$ and $\displaystyle c = - C_{0}B_{0}^{D}$.

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.

### Bonus

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 $B_n^{D}$ and $B_n^{E}$ as a function of $L_n$. We get a recursive formula for $L_n$. All that's left to do is show that $L_n$ converges, and then take the limit of the equation as $n \rightarrow \infty$ and solve the quadratic obtained for $L$. 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!