# Borrowing

Diving deep into mechanics and calculations of a borrowing transaction
Borrowing Transaction
For users to get CCT tokens; Collateral claim ETH(CCT ETH) or Collateral claim USD(CCT USD) from depositing ETH and/or USD, the contract does the following:
Let Δx be the number of CCT ETH withdrawn from the pool.
Let Δy be the number of CCT USD divided by K withdrawn from the pool.
Let Δz be the number of Bond tokens(BT) per second for the duration of the pool deposited into the pool.
Let d be the duration of the pool, thus dΔz is the total number of Bond tokens(BT) deposited into the pool.
Let k be the constant product invariant of the AMM

#### Here's the AMM formula for a borrowing transaction:

$(x+y-Δx-Δy)(z+Δz)=k$
A small portion of Borrower's collateral is used to mint and buy CCT tokens, remaining majority of collateral is used to fund the borrow amount by utilising the CCT tokens.
To get CCT ETH/CCT USD from this pool, Bond tokens(BT) are required to be deposited into the pool. The contract will first use the ETH and/or USD deposited by the borrower to mint equivalent Bond(dΔz) and CCT ETH/CCT USD. Then deposit the Bond tokens (dΔz) into the pool.
The CCT ETH or CCT USD minted corresponds to the borrower's collateral. But due to the rebalance mechanism, only either CCT ETH or CCT USD will be in the pool. If the Collateral claim token in the pool is different from borrower's collateral, then it will be transformed to the appropriate token.

### Case 1 : If $K​

• #### only USDC exists in the pool. The AMM equation for borrowing txn is simplified to:

$(y-Δy)(z+Δz)=k$
Let's say the borrower wants to borrow USDC by depositing ETH as collateral, they need CCT ETH. The borrower would buy CCT USDC and convert it to CCT ETH in this case.
Borrower deposits: (Δy+dΔz) ETH
Borrower receives: KΔy USD(amount borrowed) and (Δy+dΔz) CCT ETH (collateral claim)
Explanation
The contract first mints dΔz Bond Tokens and dΔz CCT ETH, thus requires dΔz ETH from the borrower. Then the dΔz BT is deposited in the pool to withdraw KΔy CCT USD (since there is no CCT ETH in the pool). Finally, the contract transforms the KΔy CCT USD to Δy CCT ETH, by depositing remaining collateral Δy ETH and withdrawing KΔy USD to be given to the borrower. In total, the borrower deposits (Δy+dΔz) ETH as collateral; the borrower receives KΔy USD as amount borrowed and (Δy+dΔz) CCT ETH.
Here's a visualisation of the transaction:
In the same case, say the borrower wants to borrow ETH using USDC collateral, they need CCT USD. Borrower would withdraw CCT USD by depositing Bond tokens. Uniswap will also be used to fund the transaction.
Borrower deposits: K(Δy+dΔz) USD
Borrower receives: KΔy/S ETH (amount borrowed) and K(Δy+dΔz) CCT USD (collateral claim)
Explanation
The contract first mints dΔz Bond Tokens and KdΔz CCT USD, thus requiring KdΔz USD from the borrower. Then the dΔz BT is deposited in the pool to withdraw KΔy CCT USD. Finally, the contract uses Uniswap to swap remaining collateral KΔy USD to KΔy/S ETH which becomes borrowed principal. In total, the borrower deposits K(Δy+dΔz) USD as collateral; the borrower receives KΔy/S ETH and K(Δy+dΔz) CCT USD as collateral claim.
Here's a visualisation of the transaction:

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

• #### only ETH exists in the pool. The AMM equation for borrowing txn is simplified to:

$(x-Δx)(z+Δz)=k$
Suppose the borrower wants to borrow USDC by depositing ETH as collateral, borrower needs CCT ETH. The borrower would withdraw CCT ETH by depositing Bond tokens. Uniswap will also be used to fund the transaction.
Borrower deposits: (Δx+dΔz) ETH
Borrower receives: SΔx USD (amount borrowed) and (Δx+dΔz) CCT ETH (collateral claim)
Here's a visualisation of the transaction:
Explanation
The contract first mints dΔz Bond and dΔz CC ETH, thus requiring dΔz ETH from the borrower. Then the dΔz Bond is deposited in the pool to withdraw Δx CC ETH. Finally, the contract uses Uniswap to swap remaining collateral Δx ETH to SΔx USD which goes to the borrower. In total, the borrower deposits (Δx+dΔz) ETH as collateral; the borrower receives SΔx USD as amount borrowed and (Δx+dΔz) CC ETH.
In the same case, suppose the borrower wants to borrow ETH using USD as collateral, they need CCT USD. Borrower would withdraw CCT ETH and convert it to CCT USD.
Borrower deposits: K(Δx+dΔz) USD.
Borrower receives: Δx ETH (amount borrowed) and K(Δx+dΔz) CC USD (collateral claim).
Here's a infographic of the transaction:
Explanation
The contract first mints dΔz Bond Tokens and KdΔz CCT USD, thus requiring KdΔz USD from the borrower. Then the dΔz BT is deposited in the pool to withdraw Δx CCT ETH. Finally, the contract transforms the Δx CCT ETH to KΔx CCT USD by depositing remaining collateral KΔx USD and withdrawing Δx ETH to be given to the borrower. In total, the borrower deposits K(Δx+dΔz) USD as collateral; the borrower receives Δx ETH as amount borrowed and K(Δx+dΔz) CCT USD.

### Case 1: K<S

Supposed S>K, there are 160,000 CC USD and 20 Bond in a pool that matures in 1 year
x=0 y=200 z=20/31557600 since 1 year has 31557600 seconds K= 800 USD per ETH S= 2000 USD per ETH
AMM would look like:
$(x+y)*z=k$
$(0+200)*(20/31557600)=0.000126752351256$
A borrower wants to borrow 1,000 USD with ETH as collateral, the contract will do the following transactions:
Δy=1.25 (1000 CCT USD)
Solving for Δz
$(y-Δy)(z+Δz)=k$
$(200-1.25)*(20/31557600 + 0.1242/31557600)= 0.000126752351256$
Therefore, Δz=0.00000000393 (0.1242 BT i.e 31557600Δz ≈ 0.1242236025)
Borrower deposits 0.1242 ETH to mint 0.1242 CCT ETH and 0.1242 BT, to which the 0.1242 Bond is deposited in the pool. Again deposits 1.25 ETH to transform 1,000 CCT USD to 1.25 CCT ETH, thus withdrawing 1000 USD which is the borrowed principal.
End Result for the above example
The borrower has a total of 1.3742 CCT ETH. The CCT ETH amount represents 1,099.36 USDC debt. It also represents 1.3742 ETH collateral locked. When the spot price stays above 800 USD per ETH, then the borrower will pay the debt to withdraw the collateral locked. When the spot price falls below the safety price, then the borrower will default. The borrower has USDC annual percentage rate (APR) of 9.936%, and ETH collateral debt position (CDP) of 274.84%.
Suppose given the same example as above, but instead, a borrower wants to borrow 1.25 ETH using USD as collateral, the contract will do the following transactions:
Δy=1.25
$(y-Δy)(z+Δz)=k$
$(200-1.25)*(20/31557600 + 0.1242/31557600)= 0.000126752351256$
Δz=0.00000000393 (0.1242 Bond Tokens i.e 31557600z ≈ 0.1242236025)
Borrower deposits 99.44 USD to mint 0.1242 CCT USD and 0.1242 Bond tokens, to which the 0.1242 BT is deposited in the pool. Swap 2,500 USD through Uniswap to get 1.25 ETH, which goes to the borrower.
End Result for the above example
The borrower has a total of 1,099.36 CCT USDC. The CCT USDC amount represents 1,099.36 USDC collateral. It also represents 1.3742 ETH debt. When the spot price falls below 800 USD per ETH, then the borrower will pay the debt to withdraw the collateral locked. When the spot price stays above the safety price, then the borrower will default. The borrower has ETH annual percentage rate (APR) of 174.84%, and USDC collateral debt position (CDP) of 109.936%.
There are other cases as well, but they all follow very similar processes as above cases.