# Borrowing

Borrowing transaction
Let
$x$
be the decrease in the
$X$
pool. This decrease is due to the borrower withdrawing
$x$
assets from the pool.
$y$
be the increase in the
$Y$
pool.
$z$
be the increase in the
$Z$
pool.
The value of
$x$
,
$y$
, and
$z$
is calculated from maintaining constant
$K.$
$(X-x)\times(Y+y)\times(Z+z)=K$
So as
$y$
increases,
$z$
has to decrease ; when
$z$
increases,
$y$
has to decrease.
The maximum of
$y$
is when
$z=0$
:
$(X-x)\times(Y+y_{max})\times(Z+0)=K$
The maximum of
$z$
is when
$y=0:$
$(X-x)\times(Y+0)\times(Z+z_{max})=K$
Let d be time duration from the time of user transaction to maturity time of pool in seconds
$x$
assets from the pool.
The Debt that a borrower will have to repay before maturity is
$x+dy$
with a constraint that there is a minimum interest debt to be paid by borrowers such that
$y>=\frac{y_{max}}{16}$
The Collateral (w) that a borrower will have to lock is z_{max}+zd/2^25
For the borrowers, the more collateral they lock, the lower interest they have to pay. This reflects the fact that the higher the collateral locked, the less is the probability for the borrower to default at the maturity date. Thus, borrowers have the flexibility to decide their opportunity cost vs cost of loan profile at the time of borrowing assets from the pool.
The amount of collateral the borrower has to stake has a minimum value of
$z_{max}$
when
$z=0$
is chosen so that enough collateral will always cover the amount borrowed
$x$
. Similarly the debt has a minimum value when
$y={y_{max}}/{16}$
such that there is always a minimum interest payable by a borrower

#### Deep-dive into a Borrowing transaction

Bob wants to borrow DAI using his collateral ETH. Suppose a LP initiates the DAI-ETH pool with similar parameters as mentioned in the previous lending transaction such that:
$X = 10,000$
$Y=0.0000475$
$Z = 4.16$
$K=1.979$
Now Bob wants to borrow 1000 DAI by depositing collateral in the form of ETH. So as per the AMM equation for a borrowing transaction:
$(10000-1000)\times(0.0000475+y)\times(4.16+z)=1.979$
Solving for
$y_{max}$
,
$(10000-1000) \times(0.0000475+y_{max}) \times(4.16+0) =1.979$
$y_{max}=0.0000053$
Solving for
$z_{max}$
,
$(10000-1000)\times(0.0000475+0)\times(4.16+z_{max}) =1.979$
$z_{max}=0.469$
Let
$d=$
2592000 (duration of 30 days from the time of tx in seconds)
The annual interest rate that can be selected by Alice will have a range of
$(\frac{y_{max}}{16})\times \frac{d_{year}}{x}$
to
$y_{max}\times \frac{d_{year}}{x}$
(where
$d_{year}$
is no. of sec in a year = 31556926). In this case, Bob can select APR from 1% to 16%
Assuming Bob chose to pay annual interest of 10%
$=>$
Interest value per second
$=y=1000\times \frac{0.1}{31556926} =0.00000316$
Debt to be repaid by Bob=
$x+dy=1000+2592000\times 0.00000316=1008.2$
Solving for
$z$
,
$(10000-1000)\times(0.0000475+0.00000316)\times(4.16+z)=1.979$
$z=0.18$
$=>$
Collateral to be locked z_{max}+zd/2^25 = 0.469+0.18*2592000/33554432 = 0.4829 ETH
Bob now has the optionality to pay back his debt of 1008.2 DAI before maturity in order to unlock his locked collateral of 0.4829 ETH. In case Bob decides to default, his collateral will be automatically forfeited at maturity
Summarising:
Bob borrowed 1000 DAI from the pool before 1 month of pool expiry by depositing 0.4829 ETH as collateral. When Bob repays 1008.2 DAI before maturity of the pool, he will receive his collateral back. If Bob defaults on the borrowing, his locked collateral of 0.4829 ETH will be proportionately distributed to the lenders based on the Insurance tokens lenders hold.