# Liquidity Provision

Diving deep into the mechanics and calculations of Liquidity provision.
Liquidity Providers are counterparties to both Lenders and Borrowers. They can create pools with any parameters of their choice or add liquidity to the existing pools. They add both Collateral Claim tokens (CCT) and Bond tokens(BT) in the pool with a proportion that doesn't change the Interest Rate, thus maintaining the current state of the pool. Usually, the proportion of Total CCT (x+y) and BT(dz) in the pool is not the same because of varying demand on Lend/Borrow side, therefore LPs will have an excess of either CCT or BT tokens in their balance. Even if the interest rate stays the same, the amount of shorts(dz) to be deposited by Liquidity Providers decreases with time as 'd' decreases.
Liquidity Providers take some divergent loss whenever the ratio of (x+y) and z (different from Amount of BT) changes compared to when they added Liquidity, this divergent loss is similar to Uniswap V2 LPs. To minimize it, LPs should add/create liquidity at a fair market rate.

### Incentives for LPs

Incentives for Liquidity Providers by adding liquidity will be the transaction fees paid by both lenders and borrowers. Transaction fees are earned as Bond Tokens(BT) withdrawn (from Lenders) and Collateral Claim tokens (CCT) withdrawn(by Borrowers) in the pool.

### Liquidity Provision Transaction

Consider a USDC/ETH pool following the three variable AMM
$(x+y)*z=k$
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 deposited into the pool. Let d be the duration of the pool, thus d(Δz) is the total number of BT deposited into the pool.
The formula for Liquidity Provision transaction:
$(x + y + Δx + Δy)(z + Δz) = k+Δk$
Let I be the marginal interest rate of the pool
$I = (z + Δz)/(x + y + Δx + Δy)$
While the ratio of (x+y)*z=k does not change with time, the amount of Bond tokens (dz) in the pool decrease as time passes and the duration is closer to the maturity of the pool. This is because z amount of BT are given to LPs every second.
Liquidity Providers receive ERC 1155 liquidity tokens (LT) l as a receipt of liquidity position. It represents their share of the liquidity pool.
Here's how the amount of liquidity tokens received is calculated:
$l=[(Δx+Δy)Δz]^1/2$

## Deep Dive into Liquidity Provision Transaction:

### 1. Creation of Liquidity Pool

Suppose a Liquidity Provider wants to create USDC/ETH pool with 10% APR (I), 1000 USDC per ETH strike/transition (K), and 1 Year maturity (d) . Here are some ratios and parameters we would need to create the pool:
$ETH:USDC = 1:1000$
$I=z/(x+y)=10/100=(10/100)/d$
where I is the annual marginal interest rate=10% annually= 10/100 annually
Marginal interest rate per second = (10/100)/d per second where, d is the duration of the pool, therefore,
$dz/(x+y)=1/10$
$dz:(x+y)=1:10$
where dz is the amount of Bond Tokens and (x+y) is the amount of CCT tokens
$BT:CCT = 1:10$
$d=31557600$
(seconds in 1 year which is the pool maturity in our example)
Depending on the state of the pool, LPs add either CCT ETH(when K>S) or CCT USDC(when S>K). Liquidity Providers can add USDC/ETH or a proportion of both, Timeswap will utilize a DEX to make transactions efficient. Let's look at both the cases.
NOTE: Liquidity Providers can add any amount of tokens by maintaining Marginal Interest 'I' or
$BT:CCT$
ratio. Following examples have their values for simplicity purposes.
Case 1: If spot price (S) of ETH<1000 USDC (K) , say 900 USDC per ETH, then the pool parameters will look like:
$x*z=k, y=0$
(Create pool using CCT ETH since S<K)
Initial
$x= 10$
​ Initial
$y=0$
Initial
$z=1/31557600, (dz=1) ; duration of the pool (d) is 1 yr = 31557600 sec$
​Initial
$k = 0.000000316880878$
$(x+y)*z=k$
$(10+0)*(1/31557600)=0.000000316880878$
$I=(1/31557600)/(10+0)$
Now, the above parameters indicate that Liquidity Provider needs 10 CCT ETH and 1 BT In order to create the pool.(They can also add less tokens but the ratio of CCT ETH : BT  needs to be the same).
Liquidity Provider can use 10 ETH to mint 10 CCT ETH and 10 BT. Or Liquidity Provider can use 9000 USDC to create the pool. In this case, 9000 USDC is first swapped for 10 ETH using a DEX which is then used to mint 10 CCT ETH and 10 BT.
After creating the pool, Liquidity Provider is left with excess of 9 Bond Tokens.
Liquidity tokens
$l$
$l=[(Δx+Δy)Δz]^1/2$
$l=[(10+0)*(1/31557600)]^1/2$
$l=0.0005629217335$
Case 2: If spot price (S) of ETH> 1000 USDC, say 1100 USDC per ETH, then the pool parameters will look like:
$y*z=k, x=0$
(Create pool using CCT USDC since S>K)
Initial x=0 Initial y=10 Initial z= 1/31557600 , (dz=1) ; duration of the pool (d) is 1 yr = 31557600 sec Initial k= 0.000000316880878
$(x+y)*z=k$
$(0+10)*(1/31557600)=0.000000316880878$
$I=(1/31557600)/(0+10)$
Now, the above parameters indicate that Liquidity Provider needs 10000 CCT USDC(Since y= 10000 CCT USDC/K = 10,000/1,000) and 1 BT to create the pool.
Liquidity Provider can use 10000 USDC to mint 10000 CCT USDC and 10 BT. Or Liquidity Provider can use 9.091 ETH to create the pool. In this case, 9.091 ETH is first swapped for 10,000 USDC using a DEX and then used to mint 10,000 CCT USDC and 10 BT.
After creating the pool, Liquidity Provider is left with excess of 9 Bond tokens.(Representing a lend position of 9,000 USDC)
Liquidity tokens
$l$
$l=[(Δx+Δy)Δz]^1/2$
$l=[(0+10)*(1/31557600)]^1/2$
$l=0.0005629217335$

### 2. Adding Liquidity in the pool we created.

#### Now, Suppose Alice wants to Add Liquidity in the above pool. Let's look at both the cases.

Case 1: If spot price of ETH<1000 USDC, say 900 USDC per ETH, then the transaction parameters will look like:
Δx=1 Δy=0 Δz=0.1/31557600 dΔz=0.1
$(x + y + Δx + Δy)*(z + Δz) = k+Δk$
Before:
$(10+0)*(1/31557600)=0.000000316880878$
After:
$(10+0+1+0)*(1/31557600+0.1/31557600)=0.000000316880878 +0.000000066544985$
$I = (z + Δz)/(x + y + Δx + Δy)$
Before:
$I=(1/31557600)/(10+0)$
After:
$I=(1/31557600+0.1/31557600)/(10+0+1+0)$
Above parameters indicate that Alice adds 1 CCT ETH and 0.1 BT in the pool. Alice can deposit 1 ETH to mint 1 CCT ETH and 1 BT. Alice holds a balance of 0.9 BT after adding liquidity. Similarly, Alice can add 900 USDC in the pool wherein USDC is first swapped for 1 ETH using a DEX and the above functions are performed.
Liquidity tokens
$l$
$l=[(Δx+Δy)Δz]^1/2$
$l=[(1+0)*(0.1/31557600)]^1/2$
$l=0.00005629217335$
Case 2: If spot price of ETH> 1000 USDC, say 1100 USDC per ETH, then the transaction parameters will look like:
Δx=0 Δy=1 Δz=0.1/31557600 dΔz=0.1
$(x + y + Δx + Δy)*(z + Δz) = k+Δk$
Before:
$(0+10)*(1/31557600)=0.000000316880878$
After:
$(0+10+0+1)*(1/31557600+0.1/31557600)=0.000000316880878 +0.000000066544985$
$I = (z + Δz)/(x + y + Δx + Δy)$
Before:
$I=(1/31557600)/(0+10)$
After:
$I=(1/31557600+0.1/31557600)/(0+10+0+1)$
Above parameters indicate that Alice adds 1000 CCT USDC(Since Δy= CCT USDC/K= 1000 CCT USDC/1000) and 0.1 BT in the pool. Alice can deposit 1000 USDC to mint 1 CCT USDC and 1 BT. Alice has a balance of 0.9 BT after adding liquidity. Similarly, Alice can deposit 0.9091 ETH which is first swapped for 1000 USDC using a DEX and then above functions are performed.
Liquidity tokens
$l$
$l=[(Δx+Δy)Δz]^1/2$
$l=[(0+1)*(0.1/31557600)]^1/2$
$l=0.00005629217335$
In both the cases, Alice will receive
$z$
BT every second for her duration of Liquidity Addition.
$z$
can change with change in Interest Rate.

## Liquidity Removal

Liquidity providers in Timeswap V2 can burn their Liquidity tokens
$l$
to remove their liquidity from the pool anytime. This is a significant improvement from Timeswap V1 where liquidity providers need to wait till maturity to withdraw their liquidity.
Keeping the above parameters in mind, here's how a liquidity removal Transaction will look like:
$(x + y - Δx - Δy)(z - Δz) = k-Δk$
$I = (z - Δz)/(x + y - Δx - Δy)$
Where, Liquidity tokens
$l$
$l=[(Δx+Δy)Δz]^1/2$
Value of (Δx+Δy) and Δz is calculated in a way that the proportion of CCT and Bond tokens in the pool does not change. In other words, liquidity removal should not affect the Interest rate of the pool.

### Deep Dive into Liquidity Removal Transaction:

Let's say Alice wants to withdraw liquidity from the pool.
In both the above cases, Alice holds
$l=0.00005629217335$
$l=0.00005629217335=[(Δx+Δy)Δz]^1/2$
$(Δx+Δy)Δz=0.000000003168809$
Alice added liquidity when I = (x+y)/z= 10%, let's say the interest rate was the same when she removed liquidity. Irrespective of weather S>K or K>S, this is how the AMM would look like:
Before:
$(11)*(1.1/31557600)=0.000000383425863$
After:
$(11-1)*(1.1/31557600-0.1/31557600)=0.000000383425863-0.000000003168809$
Alice receives 1000 CCT USDC or 1 CCT ETH + 0.1 BT tokens. She also has a Balance of 0.9 Bond tokens. Therefore, Alice holds a total of 1000 CCT USDC or 1 CCT ETH + 1 Bond tokens.
She can now burn both the tokens to withdraw 1 ETH or 1000 USDC. Alice also receives fees from lenders and borrowers in addition to her initial capital.
Here, if the Interest rate were to be different from 10%, say 20% or 5%, then she would've received CCT and Bond tokens in a different proportion, hence facing Divergence Loss.