Skip to main content

Uni-V2 RAI/ETH Savior Math

The Uniswap V2 RAI/ETH savior allows users to deposit LP shares to protect their safe. Upon a liquidation attempt from a keeper, the savior will withdraw all RAI and ETH liquidity from Uniswap.

The saviour will then repay as much RAI debt as possible. If the Safe's collateralization ratio is still not at the target ratio that the Safe owner picked, the saviour will also add more ETH in the Safe. The keeper that triggered the saviour will then take a flat fee as reward for saving the Safe.

The savior does not guarantee that the safe will be saved. The user needs to make sure that they deposited enough LP tokens so the saviour can both reward the keeper that saves their position and bring the Safe's collateralization ratio to the target they picked.

Minimum Savior Balance Formula

In order to derive the formula for the minimum LP balance that can bring your Safe to a target CRatio, we use the following variables:

Liquidation price: $$P_{liq}$$

Redemption price: $$P_{RP}$$

Asset price: $$P{RAI}$$ , $$P{ETH}$$

Debt: $$D$$

Collateral: $$C$$

Accumulated rate: $$A_{cc}$$

CRatio: $$R{liq}$$ , $$R{targ}$$

Uniswap reserves: $$S{RAI}$$ , $$S{ETH}$$

We first need to use the liquidation price formula:

$$ P{liq} = \frac{DA{cc}R{liq}P{rp}}{C} $$

​From here, we can reorganize the formula to isolate the debt and collateral amount in the Safe. We also use the Target Rebalance CRatio $$R_{tar}$$ instead of the liquidation CRatio to know how much debt and collateral we need to save the Safe:

$$ \frac{C{sav}}{D{save}} = \frac{A{cc}R{tar}P{rp}}{P{liq}} $$

The savior will be withdrawing an amount of LP tokens to get $$D{lp}$$ RAI and $$C{LP}$$ ETH. Therefore, the resulting debt and collateral in safe after and adding the proceeds from the LP shares will be:

$$C{save} = C + C{lp}$$

$$D{save} = C - D{lp}$$

Which then gives us:

$$ \frac{C + C{lp}}{D - D{lp}} = \frac{A{cc}R{tar}P{rp}}{P{liq}} $$

We will reuse this formula later. We now need to model the Uniswap pool dynamics to calculate the amount $$D{lp}$$ and $$C{LP}$$ we would get from the savior LP shares at a given liquidation price. We start from the standard xy=k assumptions:

$$ \begin{cases}K{lp} = S{ETH}S{RAI}\ S{ETH}P{ETH}= S{RAI}P_{RAI}\end{cases} $$

$$ \iff K{LP} P{ETH} = {S{RAI}}^2 P{RAI} $$

$$ \iff S{RAI} = \sqrt{K{LP} \frac{P{ETH}}{P{RAI}}} $$

And similarly:

$$ \iff S{ETH} = \sqrt{K{LP} \frac{P{RAI}}{P{ETH}}} $$

We can now replace these results in the equation from the liquidation price:

$$ \frac{C + \sqrt{K{LP} \frac{P{RAI}}{P{ETH}}} }{D - \sqrt{K{LP} \frac{P{ETH}}{P{RAI}}} } = \frac{A{cc}R{tar}P{rp}}{P{liq}} $$

We simplify the equation using $$p = \frac{P{RAI}}{P{ETH}}$$ and $$j=\frac{A{cc}R{tar}P{rp}}{P{liq}}$$

$$ \frac{C + \sqrt{K{LP}p } }{D - \sqrt{K{LP} \frac{1}{p}} } = j $$

We need to solve the equation above for $$K_{LP}$$.

Using Wolfram Alpha:

{% embed url="https://www.wolframalpha.com/input?i=solve%5C%2840%29Divide%5Bc%2BSqrt%5Bk*p%5D%2Cd-Sqrt%5Bk*%5C%2840%29Divide%5B1%2Cp%5D%5C%2841%29%5D%5D%3Dj%5C%2844%29k%5C%2841%29&i2d=true" %}

​We get 2 solutions:

$$ \begin{cases} K{LP1}= \frac{p(C-Dj)^2}{(j-p)^2} \ K{LP2}= \frac{p(C-Dj)^2}{(j+p)^2} \end{cases} $$

Since we're interested in the minimum balance of LP tokens, we keep only the second solution that will give the lowest balance possible. Last, we need to take the square root of $$K_{LP}$$ ​to get the actual LP token balance:

$$ Bal_{LPmin}= \sqrt{\frac{p(C-Dj)^2}{(j+p)^2}}=\sqrt{p} \frac{|C-Dj|}{j+p} $$

Done! To that we have to add the flat $2000 keeper fee. Below is a calculation example:

{% embed url="https://docs.google.com/spreadsheets/d/1flg9LidXvxcAInw4-AtDCEFniMGoZg9fIgm-x36S3CA/edit#gid=0" %}

The spreadsheet is available here, make a copy to test your own parameters: https://docs.google.com/spreadsheets/d/1flg9LidXvxcAInw4-AtDCEFniMGoZg9fIgm-x36S3CA/edit#gid=0