# Lending

Diving deep into mechanics and calculations of a Lending transaction
Lending Transaction
Let Δx be the number of CCT ETH deposited into the pool.
Let Δy be the number of CCT USDC deposited into the pool
Let Δz be the number of Bond Tokens(BT) per second for the duration of the pool withdrawn from the pool.
Let d be the duration of the pool, thus dΔz is the total number of BT withdrawn from the pool.
Let k be the constant product invariant of the AMM

#### Here's the formula for a lending transaction:

$(x+y+Δx+Δy)(z-Δz)=k$

### Case 1 : If $K​

• #### only USDC exists in the pool. The contract will mint CCT USDC thus the above equation is simplified to:

$(y+Δy)(z-Δz)=k$
Let's say lender wants to lend USDC or ETH, lender needs Bond Tokens(BT). For this, the lender needs to buy BT by selling CCT tokens to the AMM.
Lender deposits: KΔy USDC
or
Lender deposits KΔy/S ETH
The final result for both case of depositing USDC or ETH:
Lender receives
$Δy(Principal)+dΔz(Interest)$
 BOND TOKENS(BT)
Explanation
In this case, if the lenders want to deposit USDC, the contract will directly mint Δy BT and KΔy CCT USDC, thus will require KΔy USDC deposited. Then the Δy CCT USDC is deposited into the pool to withdraw dΔz BT from the pool.
But if the lenders want to deposit ETH, the contract will first swap the ETH to USDC through Uniswap. Then mint Δy BT and KΔy CCT USDC, thus will require
$KΔy/S$
ETH deposited. Then the Δy CCT USDC is deposited into the pool to withdraw dΔz BT from the pool.
The end result is same in both cases. Lender receives Δy + dΔz BT
Here's a visualisation of the transaction:

### Case 2: If $K>S$​

• #### only ETH exists in the pool. The contract will mint CCT ETH thus the above equation is simplified to:

$(x+Δx)(z-Δz)=k$
Lender deposits Δx ETH
or
Lender deposits SΔx USDC
The final result for both case of depositing USD or ETH:
The lender receive the
$Δx(Principal)+dΔz(Interest)$
 BOND TOKENS(BT)
Explanation
In this case, if the lenders want to deposit ETH, the contract will directly mint Δx BT and Δx CCT ETH, thus will require Δx ETH deposited. Then the Δx CCT ETH is deposited into the pool to withdraw dΔz BT, from the pool.
But if the lenders want to deposit USDC, the contract will first swap the USDC to ETH through Uniswap. Then mint Δx BT and Δx CCT ETH, thus will require SΔx USDC deposited. Then the Δx CCT ETH is deposited into the pool to withdraw dΔz BT from the pool.
The end result is the same in both cases. Lender recieves: Δx + dΔz BT
Here's a visualisation of the transaction:

### Case 1: K<S

Alice would like to lend USDC in ETH/USDC Timeswap pool to earn interest with minimal risk. The pool has a transition/strike/safety price (K) of 800 USDC per ETH and suppose spot price (S) is 2000 USDC per ETH on a DEX. Suppose a Liquidity Provider(LP) creates the USDC-ETH pool (with 200 CCT USD and 20 BT) with the following parameters and maturity of 1 year (d=31557600 sec)
As per the AMM equation
(x+y)*z=k
x=0 y=200 z=20/d=20/31557600=0.000006337617563 k=0.000126752351256 (0+200)*0.000006337617563=0.000126752351256
Since S>K(x=0, 1 ETH > K USDC), only USDC exists in the pool. Let's say Alice wants to lend 1000 USDC. The AMM equation for a lending transaction would be:
(y+Δy)(z-Δz)=k
The contract do the following transactions:
=>
$KΔy = 1000$
=>
$Δy = 1000/800 =1.25$
(1000 CCT USDC minted)
Using the Timeswap mechanics,
=> Deposit 1000 USD to mint 1000 CCT USD and 1.25 BT. The minted 1000 CCT USDC (Δy=1.25) is deposited into the Timeswap pool to withdraw dΔz BT.
Following the constant product AMM equation
$(200+1.25)(0.000000633761756-Δz)=0.000126752351256$
Solving for Δz to get dΔz(Amount of Bond Tokens) as below:
$Δz=0.00000000393$
. Thus, dΔz = 0.1242 BT
$(31557600z ≈ 0.1242236025)$
is withdrawn from the Timeswap pool.
Total Bond tokens the lender has
$= 1.25(Principal)+0.1242(Interest)=1.3742$
1.3742 BT represents
$(1.3742/1.25) * 1000 = 1099.36$
USDC or
$1099.36/800 = 1.3742 ETH$
APR = (1099.36-1000)/1000 x 100% = 9.936%
$CDP = S(Δy + dΔz)/ KΔy$
=>
$2000*(1.25+ 0.1242236025) / (800 * 1.25) * 100% = 274.84%$
%
=>
$274.84%$
%
End Result
The lender has a total of 1.3742 Bond Tokens. If the spot price of ETH remains over 800 USD, the Bond amount represents a 1,099.36 USD return. It also represents 1.3742 ETH remuneration if the spot price goes below 800 USD per ETH.
The lender has a USDC annual percentage rate (APR) of 9.936% and an ETH collateral debt position (CDP) of 274.84%.
Suppose given the same example above, the lender wants to lend 0.5 ETH instead. The contract will do the following transactions:
=>
$0.5 ETH = KΔy USD$
=>
$800 * 1.25=1000 USD$
0.5 ETH will be swapped to 1,000 USDC through a DEX. Same steps as the above example when lender wants to deposit 1,000 USDC.
The end result is the same with a slight reframing:
=>
$((1.3742-0.5)/(0.5)) * 100 = 174.84$
% APR
The lender has ETH annual percentage rate (APR) of 174.84%, and USD collateral bond position of 109.936%

### Case 2: K>S

Alice would like to lend USDC in ETH/USDC Timeswap pool to earn interest with minimal risk. The pool has a transition/strike/safety price (K) of 800 USDC per ETH and suppose spot price (S) is 600 USDC per ETH on a DEX. A lender wishes to lend 1.25 ETH, therefore he invokes the lend function, the contract to perform the following actions:
Since K>S(y=0, K USDC > 1 ETH), only ETH exists in the pool. Therefore, the AMM equation for the lending transaction would look like:
(x+Δx)(z-Δz)=k
x=200 z=20/d=20/31557600=0.000006337617563 k=0.000126752351256
(200+Δx)(0.000006337617563-Δz)=0.000126752351256
Alice wants to lend 1.25 ETH, therefore: Δx=1.25 (Minting 1.25 CCT ETH by depositing 1.25 ETH)
(200+1.25)(0.000006337617563-Δz)=0.000126752351256 Solving for Δz to get dΔz(Amount of BT removed), Δz=0.00000000393 dΔz=0.1242 (31557600(z) ≈ 0.1242236025)
When lender deposits 1.25 ETH, 1.25 Bond tokens are also minted. Both 1.25 BT minted and 0.1242 Bond tokens from the pool goes to the lender.
The end result is as follows:
The lender has a total of: 1.3742 Bond tokens = 1.25(Principal) + 0.1242(Interest)
The Bond amount represents 1,099.36 USDC collateral coverage, if the spot price rise above 800 USDC per ETH. It also represents 1.3742 ETH return, if the spot price stays below 800 USDC per ETH. The lender has ETH annual percentage rate (APR) of 9.936%, and USD collateral debt position of 146.58%.
Suppose given the same example above, the lender wants to lend 750 USDC instead. The contract will do the following transactions:
750 USDC will be swapped to 1.25 ETH through a DEX. Same steps as the above example when the lender wants to deposit 1.25 ETH.
End Result
The lender has USDC annual percentage rate (APR) of 46.58%, and ETH collateral bond position of 109.936%

### Summarising:

For the case S>K Suppose that Alice wants to earn a safe yield on her 1,000 USD in a timeframe of 1 year. She chooses a USD/ETH pool with a safety price (strike price K) of 400 USD per ETH and maturity of 1 year. The current spot price S is 2000 USD per ETH, thus she deems a safety price of 400 USD per ETH safe enough, as the possibility of the spot price going below 400 USD per ETH in 1 year is low. She deposits 1,000 USD into the protocol and receives an ERC20 token representing returns of 1,098.76 USD and collateral insurance of 2.7469 ETH.
Suppose after 1 year, the spot price is 1,800 USD per ETH, which is above the safety price of 400 USD per ETH. Her ERC20 token, representing the lending position, gives her 1,098.76 USD.
For the case K>S Alternatively, suppose after 1 year, the spot price falls to 300 USD per ETH, which is below the safety price. Her ERC20 token gives her 2.7469 ETH.
Withdrawing the lending position before maturity
Suppose instead of waiting for 1 year, in 6 months, Alice wants to withdraw from her lending position earlier than the 1 year maturity. She deposits Claim tokens back into the protocol, and she receives 1,045 USD from the pool. As one can see, she has received USD amount smaller than 1,098.76 USD. This is due to withdrawing earlier.