Comment on page

# Formulas

## Borrow Formulas

#### interestPerYear to interestPerSecond

$i_\text{second} = (i_{year})^\frac{1}{31622400}$
with both
$i_{year}$
and
$i_{second}$
interest accrual factors (and not interest rates) and where it is assumed that one full year consists of 31622400 seconds (366 days with 86400 seconds each).
Find a sample calculation here.

#### interestPerSecond to interestPerYear

$i_\text{year} = (i_{second})^{31622400}$
with both
$i_{year}$
and
$i_{second}$
interest accrual factors (and not interest rates) and where it is assumed that one full year consists of 31622400 seconds (366 days with 86400 seconds each).

#### interestPerSecond to interestToMaturity

$i_{\text{maturity}} = \begin{cases} (i_{\text{second}})^{T-t} & \text{if } t < T \\ 1e^{18} &\text{else} \end{cases}$
where
$t$
is the current block.timestamp and
$T$
is the collateral asset’s maturityboth expressed in seconds.

#### normalDebt to debt

$d = \frac{d_n*\text{rate}}{1e^{18}}$

#### debt to normalDebt

$d_n = \begin{cases} \frac{d*1e^{18}}{\text{rate}} &\text{if}\quad \text{rate}>0 \\ \infty &\text{else} \end{cases}$
where it is assumed that
$rate \geq 1e^{18}$
.
The following correction has to be performed on the resulting amount
$d_n$
to avoid rounding errors due to precision loss:
$d_n = \begin{cases} d_n+1 &\text{if } \frac{d_n*\text{rate}}{1e^{18}} < d \\ d_n &\text{else } \end{cases}$

#### normalDebt to debtAtMaturity

$d_m = d_n\frac{\text{rate} + i_{\text{maturity}} - 1^{18}}{1^{18}}$

#### collateralizationRatio

$r = \begin{cases} \frac{p*c}{d} &\text{if}\quad d>0 \\ \infty &\text{else} \end{cases}$

#### Max.debt for a given collateralizationRatio and collateral amount

$d_\text{max} = \begin{cases} \frac{p*c}{r} &\text{if}\quad r>0 \\ \infty &\text{else} \end{cases}$

#### Min. collateral for a given collateralizationRatio and debt amount

$c_\text{min} = \begin{cases} \frac{r*d}{p} &\text{if}\quad p>0 \\ \infty &\text{else} \end{cases}$

## Leverage Formulas

#### Min. collateralizationRatio for a levered deposit

$r_{\text{min}}=\frac{\frac{p*x_{\text{f→u}}}{1e^{18}}x_{\text{u→c}}}{1e^{18}}$
where
$x_{f→u}$
and
$x_{u→c}$
are the FIAT/underlying and underlying/collateral exchange rates including price impact and slippage.

#### Max. collateralizationRatio for a levered deposit

$r_{\text{max}}=\begin{cases} \frac{p*(c + \frac{x_{\text{u→c}}u}{1e^{18}})}{d} &\text{if } d>0 \\ \infty &\text{else} \end{cases}$
where
$u$
is the deposited underlier amount and
$x_{uc}$
is the underlying/collateral exchange rate including price impact and slippage.

#### flashloan amount for a levered deposit

$f=\frac{p*(c + \frac{x_{\text{u→c}}u}{1e^{18}}) - rd}{1e^{18}r - \frac{px_{\text{f→u}}}{1e^{18}}x_{\text{u→c}}}$
where
$u$
is the deposited underlier amount and
$x_{f→u}$
and
$x_{u→c}$
are the FIAT/underlying and underlying/collateral exchange rates including price impact and slippage.

#### Min. collateralizationRatio for a levered withdrawal

$r_{\text{min}} = \begin{cases} \frac{p(c - \Delta c)}{d} &\text{if}\quad \Delta c < c \text{ AND } d>0 \\ \infty &\text{else} \end{cases}$
where
$c$
and
$d$
are the position collateral and debt, and
$\Delta c$
is the withdrawn collateral amount.

#### Max. collateralizationRatio for a levered withdrawal

$r_{\text{max}} = \begin{cases} \frac{p(c-\Delta c)}{d - \Delta c x_{c\rightarrow u}x_{u\rightarrow f}} &\text{if}\quad \Delta c < c \text{ AND } d>0 \\ \infty &\text{else} \end{cases}$
where
$c$
is the position collateral and
$\Delta c$
the withdrawn collateral amount.

#### flashloan amount for a levered withdrawal

$f = \begin{cases} d-\frac{p(c - \Delta c)}{r} &\text{if}\quad c>\Delta c \\ d &\text{if else}\quad c=\Delta c \\ \text{revert} &\text{else}\end{cases}$
where
$c$
and
$d$
are the position collateral and debt, and
$\Delta c$
is the withdrawn collateral amount. It is thus assumed that
$\Delta c \leq c$
and
$d>0$
as the computation would otherwise yield an invalid result.

#### Estimated underlier for a levered withdrawal

$w = \frac{(\Delta c - \frac{\frac{f*1e^{18}}{x_{u→f}}1e^{18}}{x_{c→u}})x_{c→u}}{1e^{18}}$
where
$\Delta c$
is the withdrawn collateral amount,
$f$
is the flashloan amount used for the withdrawal, and
$x_{u→f}$
and
$x_{c→u}$
are the underlying/FIAT and collateral/underlying exchange rates including price impact and slippage.
Note that for a collateral asset beyond its maturity the formula remains intact with the difference that the input underlying/collateral exchange rate is fixed, i.e.
$x_{u→c} = 1e^{18}$
.

#### profitAtMaturity for a levered deposit

$g = w - u$
where
$w$
is the estimated underlier amount withdrawn at maturity (i.e. with an input of
$x_{cu} = 1e^{18}$
and
$\Delta c = c$
) and
$u$
is the deposited underlier amount. It is further assumed that the collateral token can be redeemed for underlier tokens at a rate of
$x_{cu}=1e^{18}$
.

#### yieldToMaturity for a levered deposit

$y_{\text{maturity}} = \begin{cases} \frac{(u + g)1e^{18}}{u} - 1e^{18} &\text{if}\quad u>0 \\ \infty &\text{else} \end{cases}$
where
$u$
is the deposited underlier amount and
$g$
is the estimated profitAtMaturity.

#### yieldToMaturity to annualYield

$y_{\text{year}} = \begin{cases} (1e^{18}+y_{\text{maturity}})^{\frac{31622400}{T-t}}-1e^{18} & \text{if } t < T \\ 0 &\text{else} \end{cases}$

#### Minimal amountOut for a max slippagePercentage
$a_s = \frac{a(1e^{18}-s_\text{percentage})}{1e^{18}}$
$a$
is the estimated amountOut without accounting for slippage.