---
# System prepended metadata

title: Hyperdrive Flat + Curve

---

# Hyperdrive Flat + Curve

Backgroud reading materials:
- [Yield Space](https://yield.is/Yield.pdf)
- [Modified Yield Space](https://hackmd.io/lRZ4mgdrRgOpxZQXqKYlFw)
- [Element V1 Construction Paper](https://paper.element.fi/)
- [Element V1 Presentation](https://hackmd.io/@vJTdwcwQSByvMaZGfTX_oQ/BkyM9mQD_)



## Sandwich Attack 

This vulnerability can be executed by monitoring the mempool until a large redemption is found. The attacker can then sandwich the redemption between an open/close short in order to profit. The vulnerability is demonstrated with the following trades: 

1) alice purchase bonds (red) 
2) bob opens a short (orange) 
3) alice redeems her matured bonds (blue). 
4) bob closes his short (green).


![](https://i.imgur.com/FZVC4vX.png)

The end result of the sandwich attack results in a loss of $\frac{.761}{5}\approx15.22\%$ of the base reserves.

## Virtual Reserves

Notice how paying 2 base results in only ~1.5 bonds? This essentially means that the user is receiving negative interest on a fixed rate purchase. Not good.

![](https://i.imgur.com/bPTXYfE.png)

Let's see how small of an input it takes for this curve to result in positive interest.


![](https://i.imgur.com/YZkqEdm.png)


:::success
🔗 [Link](https://www.desmos.com/calculator/hveo99fxct) to Desmos 🔗
:::

It looks like the largest purchase the curve can handle before resulting in negative interest rates is $\frac{.24}{5}\approx4.8\%$ of the base reserves. Adding back the virtual reserves, as described in the [YieldSpace](https://yield.is/YieldSpace.pdf) paper, allows for positive interest rates when the base reserves > bond reserves. 

<iframe src="https://www.desmos.com/calculator/mvyqrt4unp?embed" width="750" height="500" style="border: 1px solid #ccc" frameborder=0></iframe>


However, at a 5% APR, there is little liquidity available in the bond reserves to support trades of significant size. The curve can now only support trades up to $\frac{.12}{5}\approx2.4\%$ of the base reserves.




## Time Stretch

The TimeStretch parameterization, as described in the [Element Construction Paper](https://paper.element.fi/#b-convergent-curve-parameter-configuration), allows for much larger trades up to $\frac{4.29}{5}\approx85.8\%$ of the reserves.

<iframe src="https://www.desmos.com/calculator/vvsc9pcwxp?embed" width="750" height="500" style="border: 1px solid #ccc" frameborder=0></iframe>

Unfortunately, the sandwich attack still results in a loss of $\frac{.44}{5}\approx8.8\%$ of the base reserves. However, if we reduce the trade size to match that of the initial example used to describe the sandwich attack we can see that the effectiveness of the attack has been reduced to $\frac{.13}{5}\approx2.6\%$ ( compared to 15.22% ) of the base reserves.

<iframe src="https://www.desmos.com/calculator/taibu4noie?embed" width="750" height="500" style="border: 1px solid #ccc" frameborder=0></iframe>

That being said, the attack still works so we must fix the root cause of the exploit.

## |+(

To understand the fix, it is important to understand why the sandwich attack works.  Let's review the steps of the attack:

1) alice purchase bonds (red) 
2) bob opens a short (orange) 
3) alice redeems her matured bonds (blue). 
4) bob closes his short (green).

Alice's bond purchase and bob's open short happens on the curve configured for t=1. When Alice redeems her bonds at maturity, she does this on a new curve configured with t=0. This causes a major imbalance of reserves that can now be used to drain the pool of base reserves when Bob closes his short on a new curve configured for t=1.  The figure below illustrates how this works.

![](https://i.imgur.com/juQyzQk.png)

The fix is simple. We only allow trades on a single curve.  We use the YieldSpace curve with the modifications mentioned above configured for t = 1 to price longs and shorts.  If the user redeems after the bond matures, the pool credits the user the full fixed interest, but the bonds are never added back to the pool because they have been "used up". If the user sells halfway through the term, then the user is credited with 1/2 of matured bonds and sells the rest on the yieldspace curve configured for t = 1. 

The figure below shows the effectiveness of this model.

![](https://i.imgur.com/uEjHbax.png)

You can see that the pool loses ~0.12 base reserves with ~0.1139 going to the user with the long position. Keep in mind that this model does not yet account for interest accrued so this loss would only happen if the yield source drops to 0% APY. The remaining loss can be attributed to the short position is accounts for $\frac{0.00727}{5}\approx0.1454\%$ of the base reserves. This loss can be easily mitigated by adding fees to the model.


:::success
🔗 [Link](https://www.desmos.com/calculator/pxxve291tz) to Desmos 🔗
:::

## Conclusion 

One important thing to note about this model is how slowly the bond reserves will be replenished. This will result in low APRs (and high spot prices), but this will provide a great opportunity for shorts to come in, collect variable interest and profit by providing short term bond liquidity.  When shorts and longs are balanced we should see:

- LPs make money purely on variable rates + fees as none of the LP funds are wasted on fixed/variable rate spread. In a balanced scenario, LPs liquidity will primarily be unused, but is useful for providing the temporary liquidity needed for trades before longs/shorts balance.
- Shorts make money from betting on the fixed/variable rate spread and collecting the variable interest on the full position.
- Longs just make money on fixed interest. When the shorts and longs are balanced there should be a steady upward pressure on fixed rates as the variable rate is collected by LPs. This will make longs attractive.

This *should* create a situation where it is profitable to keep the pool's fixed rate close to the variable rate. A bot that provides bond liquidity through shorts could be extremely profitable.


## Appedix

### Open Long

Calculate $\Delta y_{long}$ bonds that can be purchased with $\Delta x_{long}$ base assets using the following invariant:

$(x+\Delta x_{long})^{\left(1-\frac{t}{t_{stretch}}\right)\ }+\left(y+t_{supply} - \Delta y_{long}\right)^{\left(1-\frac{t}{t_{stretch}}\right)}=k$

where $t$ is the term length in days. Solving for $\Delta y_{long}$:

$\Delta y_{long}=y+t_{supply} - (k - \left(x+\Delta x_{long}\right)^{\left(1-\frac{t}{t_{stretch}}\right)})^\frac{1}{1-\frac{t}{t_{stretch}}}$

### Close Long

Calculate the number of matured bonds:

$\Delta y_{flat}=\frac{\Delta t}{t}\cdot \Delta y_{long}$

Since the $\Delta y_{mature}$ bonds are redeemable 1:1 for the base asset we can say that:

$\Delta y_{flat} = \Delta x_{flat}$

The remaining bonds are still open can be calculated as follows:

$\Delta y_{curve}=\Delta y_{long} - \Delta y_{flat}$

The sale price of the remaining $\Delta y_{curve}$ bonds can be calculated using the following invariant:

$(x-\Delta x_{curve})^{\left(1-\frac{t}{t_{stretch}}\right)\ }+\left(y+t_{supply} + \Delta y_{curve}\right)^{\left(1-\frac{t}{t_{stretch}}\right)}=k$

solving for $\Delta x_{curve}$:

$\Delta x_{curve}=x - (k - \left(y+t_{supply} + \Delta y_{curve} \right)^{\left(1-\frac{t}{t_{stretch}}\right)})^\frac{1}{1-\frac{t}{t_{stretch}}}$

The total profit from the long can be calculated as follows:

$\Delta x_{profit} = \Delta x_{long} - (\Delta x_{flat} + \Delta x_{curve})$

### Open Short

Calculate $\Delta x_{short}$ base assets needed to short $\Delta y_{short}$ bonds using the following invariant:

$(x-\Delta x_{short})^{\left(1-\frac{t}{t_{stretch}}\right)\ }+\left(y+t_{supply} + \Delta y_{short}\right)^{\left(1-\frac{t}{t_{stretch}}\right)}=k$

solving for $\Delta x_{short}$:

$\Delta x_{short}=x - (k - \left(y+t_{supply} + \Delta y_{short} \right)^{\left(1-\frac{t}{t_{stretch}}\right)})^\frac{1}{1-\frac{t}{t_{stretch}}}$

The max loss for the short is:

$\Delta x_{maxloss}=\Delta y_{short}-\Delta x_{short}$

> Note: The short position could just supply the max loss as collateral for the trade


### Close Short

Calculate how many base assets are needed to close the short of $\Delta y_{short}$ bonds by first calculating the number of matured bonds:

$\Delta y_{flat}=\frac{\Delta t}{t}\cdot \Delta y_{short}$

Since the $\Delta y_{mature}$ bonds are redeemable 1:1 for the base asset we can say that:

$\Delta y_{flat} = \Delta x_{flat}$

The remaining bonds are still open can be calculated as follows:

$\Delta y_{curve}=\Delta y_{short{}} - \Delta y_{flat}$

Next, determine how many base assets are needed to close the remaining $\Delta y_{curve}$ bonds using the following invariant:

$(x+\Delta x_{curve})^{\left(1-\frac{t}{t_{stretch}}\right)\ }+\left(y+t_{supply} - \Delta y_{curve}\right)^{\left(1-\frac{t}{t_{stretch}}\right)}=k$

solving for $\Delta x_{curve}$:

$\Delta x_{curve}=(k - \left(y+t_{supply} - \Delta y_{curve} \right)^{\left(1-\frac{t}{t_{stretch}}\right)})^\frac{1}{1-\frac{t}{t_{stretch}}}-x$

The profit from the short can be calculated as follows:

$\Delta x_{profit} = \Delta x_{short} + \Delta x_{short}(1 + r_{variable}\cdot\frac{\Delta t}{t}) - (\Delta x_{flat} + \Delta x_{curve})$

> Note: The short position earns variable interest on the face value of the bond

## Further Work

### Finalize fee model

### Add variable interest to the invariant 

### Investigate Longs making Shorts whole.  With the current accounting system, longs can't counterbalance the effect of shorts because they can't buy the bonds created by shorts.

### Investigate Profitability of LPs

### Investigate the model where bond reserves are backed by base and LPs sell whatever fraction of reserves are in bonds back to the pool in order to exit.

### Consider modifying time-stretch to target 50-50 reserves instead of target a ratio of reserves equal to the spot price

### Investigate possible use of a dynamic time stretch to ensure the reserves remain balanced regardless of the APR. This is potentially easier to do now that time is fixed at t=1.