owned this note
owned this note
Published
Linked with GitHub
## Hyperdrive: How it works
There are 6 actions possible in Hyperdrive: add/remove liquidity, and open/close long/short.
### Accounting for LP tokens
| Balance | Adding | Removing |
| ---------- |:--------------------------------------:|:-----------------------------------------------:|
| $\Delta l$ | $\Delta l = \frac{\Delta z}{z}\cdot l$ | $\Delta l = \frac{\Delta z}{z - b_z} \cdot {l}$ |
| Trader | $l_t = l_t + \Delta l$ | $l_t = l_t - \Delta l$ |
| Global | $l = l + \Delta l$ | $l = l - \Delta l$ |
### Accounting for Shares and Bonds
As users buy and sell bonds, we track the impact on the pool's reserves:
- As users purchase bonds, bonds flow out of the pool in exchange for shares ($-\Delta y$, $+\Delta z$)
- As users sell bonds, bonds flow into the pool in exchange for shares ($+\Delta y$, $-\Delta z$)
When purchasing a bond, it's minted on demand, backed 1:1 by reserves tracked by the `share buffer` ($b_z = b_z + \frac{\Delta y}{c}$). LPs can't withdraw this amount, as it would leave bonds unbacked.
#### Safu By Design: A User's Margin Account
> ##### Can't Rekt Us If We Hold Your Funds
By design, a user's margin account will always be able to cover the worst case scenario. That is, they need enough base in their margin account to buy back the bonds owed, no matter the price. How do we ensure that?
As the worst outcome for a short is at `price=1.0` they have to back their short position by 1:1 of base assets, or $\frac{\Delta y}{c}$ of shares. Since they received $\Delta x$ shares as `proceeds from sale`, they only need to top up their margin account by $\frac{\Delta y}{c} - \Delta z$. **need to be able to talk about changing price over time, p1 and p2**
At initiation, `user margin = proceeds from sale + max loss`
Which is equivalent to
#### Open Short (Sell)
Get $\Delta z$ shares as `proceeds from sale` shorting $\Delta y$ bonds, which are placed in the user's margin account along with `max loss` shares.
On close, this closing cost is subtracted from the margin account and any remaining amount can be withdrawn
| User's margin |
| :------------------------------------------------------------------------------------: |
| $\text{proceeds from sale}(\Delta z) + \text{max loss}(\frac{\Delta y}{c} - \Delta z)$ |
| $\Delta z + \frac{\Delta y}{c} - \Delta z$ |
| $\frac{\Delta y}{c}$
#### Close Short (Buy)
Get $\Delta y$ bonds for $\Delta z$ shares, pay out $\Delta z$ from margin, withdraw what's left
- same relationship as in `Open Long` above, but from the perspective of a known $\Delta y$
- $\Delta z = \mu^{-1} \cdot (\frac{\mu}{c} \cdot (k - (y + s - \Delta y)^{1 - \tau(d)}))^{\frac{1}{1 - \tau(d)}} - z$
- The accounts receivable will be decreased by $\Delta z$.
## Appendix 2: Formulas
#### Swap Math
calculates how much a user gets from transacting a swap with the pool
##### User buys bonds: Open Long
Get $\Delta y$ bonds for $\Delta z$ shares
$$\Delta y = y+s - (k - \frac{c}{\mu} \cdot (\mu \cdot (z + \Delta z))^{1-\tau(d_b)})^{\frac{1}{1-\tau(d_b)}}$$
##### User sells bonds: Close Long and Open Short
Get $\Delta z$ shares for $\Delta y$ bonds:
$$\Delta z = z - \mu^{-1} \cdot (\frac{\mu}{c} \cdot (k - (y + s + \Delta y)^{1-\tau(d)}))^{\frac{1}{1-\tau(d)}}$$
#### LP Math
calculates the number of tokens representing an LP's position after they add or withdraw liquidity.
## Appendix 2: Deriving Formulas
### LPs
how does withdrawing liquidity work? from `Hyperdrive AMM Math`:
$$
\Delta x = c \cdot \Delta z = c \cdot (z - b_z) \cdot \frac{\Delta l}{l}
$$
$$
c \cdot \Delta z \cdot {l} = c \cdot (z - b_z) \cdot \Delta l
$$
$$
\Delta z \cdot {l} = (z - b_z) \cdot \Delta l
$$
$$
\frac{\Delta z}{z - b_z} \cdot {l} = \Delta l
$$