# Lending under different scenario

What happens underneath the hood when a lend function is called?
Lenders mint Bond tokens(BT) which provide a claim to his principal plus interest amount
In total lenders receive -> BT minted(Principal) + BT withdrawn(Yield)

### The flow of a lend transaction:

1. 1.
Whenever a lending or borrowing tx is initiated equal amount of bond and collateral claim tokens are minted by the smart contract.
2. 2.
Bond tokens are transferred to the lenders which gives an equivalent claim to the principal locked.
3. 3.
Collateral claim tokens of that asset is minted whichever has The corresponding collateral claim tokens are then transferred to the AMM pool and accordingly interest and collateral value to be locked is calculated via AMM.
For every lending tx with lending value of Δx or Δy this is how AMM values changes-
$(x+y+Δx+Δy)(z-Δz)=L^2$
NOTE: Due to rebalancing it is expected that either of x or y will exist in the pool, whichever has a lower value.

## Lending Dynamics under different scenarios:

Now let's find out how interaction with AMM happens and how the interest and collateral values are calculated for every lending transaction.

#### At the core, lenders mint Bond tokens(BT) and CCT while depositing funds in Timeswap. The ultimate result of lending is withdrawing Bond tokens by adding CCT to the pool.

Like we mentioned in the previous section, there's only one asset(USDC or ETH) in the pool at a given time. Timeswap still allows lending of both the pool tokens, where a DEX is utilised to lend the token which did not exist in the pool.

#### With that out of the way, let's dive into the specifics with both the cases below:

1. 1.
Lending a Token which is in the pool:
A lender has to mint BT and CCT by locking whichever asset (USDC/ETH) is overcollateralised is lent into the pool.
Consider Strike Price (K)=1000 USDC per ETH If Spot price (S) of ETH < 1000 USDC (S<K), lending 1 ETH against 1000 USDC is overcollateralised, hence only CCT ETH exists in the pool. If Spot price (S) of ETH > 1000 USDC (S>K), Lending 1000 USDC against 1 ETH is overcollateralised, hence only CCT USDC exists in the pool.
• Say USDC is lent into ETH-USDC pool, thereby CCT USDC & BT is minted by locking USDC in a smart contract.
• Since lenders want to hold Bond tokens to earn yields, CCT USDC will be swapped for BT using the Timeswap V2 AMM.
• In total, a lender holds BT minted & BT withdrawn representing their lend position.
To get a better sense, K < S is in other words depicting CCT ETH to be more valued than CCT USDC.
Since there will be no CCT ETH thereby x = 0; Δx = 0, making the AMM:
$(y+Δy)(z-Δz)=L^2$
where , dΔz is BT withdrawn by depositing CCT USDC to the pool. All in all, lender receives Δy + dΔz the BT while opening lend position in ETH-USDC pool.
2. Lending a Token which is not in the pool:
A lender has to mint BT and CCT by locking whichever asset (USDC/ETH) is overcollateralised is lent into the pool.
To make sure transactions are overcollateraised, if user tries to lend the other token, then it is first swapped to the appropriate pool token using a DEX and same above procedure is followed.
For example: If a user tries to lend USDC when the pool contains CCT ETH, then USDC will first be swapped for ETH which will then be locked to mint CCT ETH.
In the above infographic, since K > S, CCT USDC > CCT ETH. Hence, USDC would be swapped with ETH using a DEX, y = 0; Δy = 0, making the AMM:
$(x+Δx)(z-Δz)=L^2$
where dΔz is the total BT withdrawn by depositing Long ETH to the pool. All in all, lenders hold Δx +dΔz Bond tokens while lending USDC in USDC-ETH pool.