# Borrow Rate

Explanation of the Interest Rate charged to Traders

The GammaPool keeps an interest rate index charged to borrowers that depends on the utilization rate of the GammaPool's funds and the swap fees accrued. The interest rate calculation aims to pay the same yield LPs earn in the CFMM, excluding borrow fees, up until the point that there is more borrowed liquidity out of the CFMM (in the GammaPool) than in the CFMM. In addition the interest rate calculation is capped at a maximum rate of 250% APY.

The formula for the update of this index (accFeeIndex) is as follows

`accFeeIndex = accFeeIndex * min{1 + apy250, (1 + CFMM_Yield + BorrowRate)}`

The formula for calculating the BorrowRate is dependent on the utilization rate of the pool, while apy250 is the 250% APY equivalent yield for the time period.

## Utilization Rate

The utilization rate of the pool is calculated as follows

`utilizationRate = (liquidityInvariantBorrowed) / (liquidityInvarantBorrowed + liquidityInvariantInGammaPool)`

Liquidity invariant refers to the invariant used by the CFMM (e.g. geometric mean of reserve assets in UniswapV2).

## Log Derivative Rate Model

Therefore, the annual rate the pool charges to liquidity borrowers is as follows

`borrowRate = min{baseRate + factor * (utilizationRate ^ 2)/(1 - utilizationRate ^ 2), maxApy}`

The above formula is based on the first derivative of the logarithmic function. It has the feature of growing exponentially as the utilization rate of the pool increases.

**Parameters**:
*utilizationRate -* how much of the pool’s funds have been borrowed out
*factor -* parameter adjusts the sensitivity of the formula to the utilizationRate
*baseRate - *minimum/starting rate charged by the GammaPool
*maxApy -* maximum rate charged by the GammaPool

Before accruing the borrow rate, the rate is deannualized according to how much time has passed in terms of blocks since the last update.

`adjBorrowRate = (currentBlockNum - lastBlockNum) * borrowRate / blocksPerYear`

## CFMM Yield

The CFMM yield is tracked by checking the growth of the liquidity invariant in the GammaPool.

`CFMM_Yield = (cfmmInvariant1 / cfmmInvariant0) * (cfmmTotalSupply0 / cfmmTotalSupply1) - 1`

In the above formula the cfmmInvariant1 refers to the invariant in the CFMM (e.g. the geometric mean of the reserve assets) in the current update and cfmmInvariant0 refers to the invariant in the cfmm at time previous update.

cfmmTotalSupply1 refers to the total supply of CFMM LP tokens in the current update while cfmmTotalSupply0 is the the total supply of CFMM LP tokens in the previous update.

## Leveraged CFMM Yield

The liquidity borrowed out of GammaSwap can be greater than the liquidity in GammaSwap if there is additional liquidity in the underlying AMM. When that happens the GammaSwap platform is leveraged.

Note this is Liquidity not TVL. You can never borrow more liquidity from the pool than the TVL available. All positions in GammaSwap are always fully collateralized. This is to ensure accurate yields in the platform.

`Borrowed Liquidity > Liquidity in CFMM`

The effects of leverage can leverage the CFMM yield charged to liquidity borrowers in GammaSwap by the leverage factor.

`Leverage Factor = BorrowedLiquidity / LiquidityInCFMM`

If the leverage factor is substantially large it can have an unrealistic interest rate charged to the borrowers. Say there's only $1 of liquidity left in the AMM and $1 million borrowed in GammaSwap. $1 in fees in the AMM is 100% fee yield but borrowers should not be charged $1M for that.

## Deleveveraged CFMM Yield

To avoid a leveraged CFMM fee yield we deleverage the yield according to the following formula

`Deleveraged_CFMM_Yield = [(cfmmInvariant1 / cfmmInvariant0) * (cfmmTotalSupply0 / cfmmTotalSupply1) - 1] * (cfmmInvariant0 / borrowedInvariant)`

In the above formula borrowedInvariant refers to BorrowedLiquidity in the previous formula and cfmmInvariant refers to liquidity in the CFMM. Therefore the above formula is simply deleveraging the CFMM Yield by the “Leverage Factor”.

This deleveraging factor happens once the “Leverage Factor” becomes greater than 1.

### Rationale for Deleveraging CFMM Yield

The purpose of deleveraging is to ensure that the main component of yield in the GammaPool is from the borrowing of liquidity, not from trading fees in the platform, since LVR/Impermanent loss is a function of price volatility and time, not trading fees

Another reason for deleveraging the platform is as a countermeasure against attack vectors where most of the liquidity is borrowed out of the platform to overstate the CFMM yield.

## Final Short Liquidity Rate

### Final Short Liquidity Rate

The final rate charged to liquidity borrowers is therefore a combination of the CFMM yield accrued since the last update and the deannualized borrow rate of the GammaPool.

The cap on the borrow rate is the 250% APY deannualized for the time period since the last update.

`apy1000 = (currentBlockNum - lastBlockNum) * 250% / blocksPerYear`

Therefore the final borrow rate formula is

`GammaSwapBorrowRate = min{apy250, CFMM_Yield + adjBorrowRate}`

## Lending/LP Rate

From the formula above, based on the utilization rate we can calculate the lending rate, or yield earned by LPs in Gammaswap, as follows

`GammaSwapLendingRate = CFMM_Yield * (1 - utilizationRate) + utilizationRate * GammaSwapBorrowRate`

### Interest Rate Index

Since the GammaPool must charge all liquidity borrowers the same rate at any given time, it must keep track of an internal accrued interest rate index.

The interest rate index does not concern itself with the current yield in the CFMM, only with the yield charged to liquidity borrowers, therefore at every update of the GammaPool contract’s state, it calculates the last yield growth as follows

`lastFeeIndex = 1 + min{apy1000, CFMM_Yield + adjBorrowRate}`

The GammaPool then uses the calculation above to update the accrued interest rate index as follows

`accFeeIndex = accFeeIndex * lastFeeIndex`

The more often the GammaPool’s state is updated the more the interest rate approaches a continuous compounding interest rate.

### Loan Accrued Interest Rate Update

The accrued index is then used to update a liquidity loan as follows

`loanLiquidity = loanLiquidity * accFeeIndex / loanRateIndex loanRateIndex = accFeeIndex`

“loanRateLiquidity” is the debt in liquidity invariant terms of the liquidity loan. This function updates the loan’s liquidity debt by accrueing all interest since the last update of the loan, also taking into account the compounding nature of interest rate updates.

“loanRateIndex” is the accFeeIndex in the previous update or time the loan was created. After the update the loan’s rateIndex is updated to the current accFeeIndex to be ready for the next update.

## Fee Distribution

90% of the fees from the borrow APY are paid by the borrower directly to the LP - included in the Supply APY calculation. The protocol (DAO) earns the remaining 10%.

Last updated