Published on

Summing series with flash loans

Authors

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.

flash_loans.png

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 1.67×\approx 1.67 \times more than I started with, which is our leverage ratio LL. What happened here is that since I minted C0/RC_{0} / R worth of DAI, I was able to add that much worth of ETH to my initial collateral, ending up with C0+C0/RC_{0} + C_{0}/R what I started with, and with C=1.5C = 1.5 and factoring by C0C_{0}, this gives us the result above. Now why not do it another time? I can take that C0/RC_{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 C0R/R=C0/R2\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 nn is:

C=C0+C0/R+C0/R2+...+C0/RnC = C_{0} + C_{0}/R + C_{0}/R^{2} + ... + C_{0}/R^{n}

Which corresponds to a leverage:

C/C0=k=0n1RnC/C_{0} = \sum_{k = 0}^{n} \frac{1}{R^{n}}

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 nn \rightarrow \infty. This limit in the case of Maker where Rmin=1.5R_{min} = 1.5 is:

Lmax=111Rmin=RminRmin1=3L_{\mathrm{max}} = \frac{1}{1 - \frac{1}{R_{\mathrm{min}}}} = \frac{R_{\mathrm{min}}}{R_{\mathrm{min}} -1} = 3

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 C0C_{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 CLC_{L}, we can write the above as the following equation:

C0+CL/R=CLC_{0} + C_{L}/R = C_{L}

At the end of the operation, we end up with a Maker vault that has CLC_{L} ETH in it, having paid back the loan with the C0C_{0} ETH we had in our wallet plus the ETH obtained with the generated DAI debt. Our leverage ratio is CL/C0C_{L}/C_{0}, which can be easily obtained from the equation above:

CL/C0=L=RR1C_{L}/C_{0} = L = \frac{R}{R - 1}

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 pmp_{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 B0D, B0E, C0, RminB_{0}^{D}, \ B_{0}^{E}, \ C_{0}, \ R_{\mathrm{min}} and pp 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 pp, the first round of minting DAI will give us C0pRm\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:

C1=B0E(1B0DB0D+C0pRm)C_{1} = B_{0}^{E} \left( 1 - \frac{B_{0}^{D}}{B_{0}^{D} + \frac{C_{0}}{p R_{m}}} \right)

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:

B1D=B0D+C0pRmB_{1}^{D} = B_{0}^{D} + \frac{C_{0}}{p R_{m}}
B1E=B0EC1B_{1}^{E} = B_{0}^{E} - C_{1}

At this point our leverage is L=(C0+C1)/C0L = (C_{0} + C_{1})/C_{0}.

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

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

nN, Cn=Bn1E(1Bn1DBn1D+Cn1pRm)\forall n \in \mathbb{N}^{*}, \ C_{n} = B_{n-1}^{E} \left( 1 - \frac{B_{n-1}^{D}}{B_{n-1}^{D} + \frac{C_{n-1}}{p R_{m}}} \right)

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

nN, n>2, BnD=B0D+1pRmk=0n1Ck\forall n \in \mathbb{N}, \ n > 2, \ B_{n}^{D} = B_{0}^{D} + \frac{1}{p R_{m}} \sum_{k = 0}^{n-1} C_{k}

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

nN, n>2, BnE=B0Ek=0nCk\forall n \in \mathbb{N}, \ n > 2, \ B_{n}^{E} = B_{0}^{E} - \sum_{k = 0}^{n} C_{k}

Now define Ln=1C0k=0nCk\displaystyle L_{n} = \frac{1}{C_{0}} \sum_{k = 0}^{n} C_{k} the leverage at step nn. The maximum leverage we can get is L=limnLnL = \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 CLC_{L}, such that if we mint some DAI with it, we will be able to sell this DAI for C1C_{1} ETH such that our original amount C0C_{0} added to C1C_{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:

C1=B0E(1B0DB0D+CLpRm)C_{1} = B_{0}^{E} \left( 1 - \frac{B_{0}^{D}}{B_{0}^{D} + \frac{C_{L}}{p R_{m}}} \right)

And we to be able to pay back the flash loan we need to verify the equation:

C0+C1=CLC_{0} + C_{1} = C_{L}

Once we have written this condition, we know that the amount of leverage we will have at the end of this operation is L=CL/C0L = 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 RmR_{m} is stricly lower than 2, we get that LL is a solution to a quadratic equation and can be written as follows:

L={bΔ2a if b<0b+Δ2a if b>0\displaystyle L =\left\{ \begin{array}{ll} \frac{-b - \sqrt{\Delta}}{2a} \ \mathrm{if} \ b < 0\\ \frac{-b + \sqrt{\Delta}}{2a} \ \mathrm{if} \ b > 0\\ \end{array} \right.

where a=1pRm\displaystyle a = \frac{1}{pR_{m}}, b=B0DC0pRmB0EpRm\displaystyle b = B_{0}^{D} - \frac{C_{0}}{pR_{m}} - \frac{B_{0}^{E}}{pR_{m}} and c=C0B0D\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 BnDB_n^{D} and BnEB_n^{E} as a function of LnL_n. We get a recursive formula for LnL_n. All that's left to do is show that LnL_n converges, and then take the limit of the equation as nn \rightarrow \infty and solve the quadratic obtained for LL. 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!