# Borrows

> The information provided by Aloe Labs, Inc. (“we,” “us” or “our”) on docs.aloe.capital (the “Site”) is for general informational purposes only. All information on the Site is provided in good faith, however we make no representation or warranty of any kind, express or implied, regarding the accuracy, adequacy, validity, reliability, availability or completeness of any information on the Site.
>
> Under no circumstance shall we have any liability to you for any loss or damage of any kind incurred as a result of the use of the site or reliance on any information provided on the site. Your use of the site and your reliance on any information on the site is solely at your own risk.

## Accounting

In Aloe II, the `Lender` is responsible for tracking borrows. This means two things:

* recording the amount each address has borrowed
* accounting for the growth of these borrows as interest accrues

The interest compounds continuously, so borrow growth should be modeled with the exponential function:

{% hint style="info" %}
$$\text{totalBorrows}_{t=T}=\text{totalBorrows}_{t=0} \cdot e^{rT}$$
{% endhint %}

Since there are no borrows at deployment time, and different users request borrows at different times, $$\text{totalBorrows}_{t=0}$$ is purely hypothetical. When a user takes on some amount of $$\text{newBorrows}$$, we must update the hypothetical value:

{% hint style="info" %}
$$\text{totalBorrows}_{t=T} + \text{newBorrows}=(\text{totalBorrows}_{t=0} + x) \cdot e^{rT}$$

$$\text{totalBorrows}_{t=0} \cdot e^{rT} + \text{newBorrows}=\text{totalBorrows}_{t=0} \cdot e^{rT} + xe^{rT}$$

$$\text{newBorrows}=xe^{rT}$$

$$x=\frac{\text{newBorrows}}{e^{rT}}$$
{% endhint %}

So when a user calls `borrow`, we add $$x$$ to borrows-at-deployment-time, and when a user calls `repay`, we do the opposite: subtract from borrows-at-deployment-time. This addition and subtraction applies _both_ to the user's `borrows`, and to the pool's total `borrowBase`.

We'd be done if `r` were constant, but it's not－the interest rate changes with time, as a function of utilization. Instead of an elegant exponential, we get a piecewise function:

{% hint style="info" %}
$$\text{totalBorrows}_{t}= \begin{cases}  \text{totalBorrows}_{t=0} \cdot e^{r_0 t} & t \leq T_0 \\ \text{totalBorrows}_{t=0} \cdot e^{r_0 T_0} e^{r_1 (t - T_0)} & T_0 \lt t \leq T_1 \\ \text{totalBorrows}_{t=0} \cdot e^{r_0 T_0} e^{r_1 (T_1 - T_0)} e^{r_2 (t - T_1)} & T_1 \lt t \leq T_2 \\ ... \end{cases}$$
{% endhint %}

The product of this chain of exponentials is the `borrowIndex`. For precision, it starts at $$10^{12}$$ (instead of 1) and it gets updated in the first call to the pool in a given block. Subsequent calls in the same block do nothing because `block.timestamp` is the same. The update looks like:

`borrowIndex *= e ** (currentRate * secondsSinceLastUpdate)`

_In practice we do change-of-base and implement `(1 + yieldPerSecond) ** T`, but the concept is the same._

As for the coefficient, $$\text{totalBorrows}_{t=0}$$, we scale that up by $$2^{32}$$ and call it `borrowBase`. Individual users have their own personal `borrowBase`s stored in the `borrows` mapping (we use the expression for $$x$$ from earlier, but scale up by $$2^{32}$$ and divide by `borrowIndex` instead of the plain exponential).

<table><thead><tr><th width="198">Value</th><th>Expression</th></tr></thead><tbody><tr><td>Total Borrows</td><td><code>borrowBase * borrowIndex / (2**32 * 10**12)</code></td></tr><tr><td>User's Borrows*</td><td><code>borrows[user] * borrowIndex / (2**32 * 10**12)</code></td></tr></tbody></table>

_\*In the code, we subtract one from `borrows[user]` because the first unit is used for whitelisting; it's not real debt._

## Interest Rate

The interest rate is a function of utilization, defined in `RateModel`. The default `RateModel` is a rational function.

{% embed url="https://www.desmos.com/calculator/tkuyru3pfr" %}
The <mark style="color:blue;">blue</mark> line shows the borrow APY, and the black line shows the supply APY (assuming a 12.5% reserve factor).
{% endembed %}

The supply rate (earned by lenders) is always less than the borrow rate (paid by borrowers). Given utilization $$U$$ and reserve factor $$f$$, they are related like so:

$$\text{rate}_{supply}=(1 - \frac{1}{f}) \cdot U \cdot \text{rate}_{borrow}$$
