Borrowing

Borrowing transaction
Let
xx
be the decrease in the
XX
pool. This decrease is due to the borrower withdrawing
xx
assets from the pool.
yy
be the increase in the
YY
pool.
zz
be the increase in the
ZZ
pool.
The value of
xx
,
yy
, and
zz
is calculated from maintaining constant
K.K.
(Xx)×(Y+y)×(Z+z)=K(X-x)\times(Y+y)\times(Z+z)=K
So as
yy
increases,
zz
has to decrease ; when
zz
increases,
yy
has to decrease.
The maximum of
yy
is when
z=0z=0
:
(Xx)×(Y+ymax)×(Z+0)=K(X-x)\times(Y+y_{max})\times(Z+0)=K
The maximum of
zz
is when
y=0:y=0:
(Xx)×(Y+0)×(Z+zmax)=K(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
The borrower receives
xx
assets from the pool.
The Debt that a borrower will have to repay before maturity is
x+dyx+dy
with a constraint that there is a minimum interest debt to be paid by borrowers such that
y>=ymax16y>=\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
zmax z_{max}
when
z=0z=0
is chosen so that enough collateral will always cover the amount borrowed
xx
. Similarly the debt has a minimum value when
y=ymax/16y={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,000X = 10,000
Y=0.0000475Y=0.0000475
Z=4.16Z = 4.16
K=1.979K=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:
(100001000)×(0.0000475+y)×(4.16+z)=1.979(10000-1000)\times(0.0000475+y)\times(4.16+z)=1.979
Solving for
ymaxy_{max}
,
(100001000)×(0.0000475+ymax)×(4.16+0)=1.979(10000-1000) \times(0.0000475+y_{max}) \times(4.16+0) =1.979
ymax=0.0000053y_{max}=0.0000053
Solving for
zmaxz_{max}
,
(100001000)×(0.0000475+0)×(4.16+zmax)=1.979(10000-1000)\times(0.0000475+0)\times(4.16+z_{max}) =1.979
zmax=0.469z_{max}=0.469
Let
d=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
(ymax16)×dyearx(\frac{y_{max}}{16})\times \frac{d_{year}}{x}
to
ymax×dyearxy_{max}\times \frac{d_{year}}{x}
(where
dyeard_{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×0.131556926=0.00000316=y=1000\times \frac{0.1}{31556926} =0.00000316
Debt to be repaid by Bob=
x+dy=1000+2592000×0.00000316=1008.2x+dy=1000+2592000\times 0.00000316=1008.2
Solving for
zz
,
(100001000)×(0.0000475+0.00000316)×(4.16+z)=1.979(10000-1000)\times(0.0000475+0.00000316)\times(4.16+z)=1.979
z=0.18z=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.