> For the complete documentation index, see [llms.txt](https://lenfi.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lenfi.gitbook.io/docs/liquidity-pools/interest-rate.md).

# Interest rate

The loan interest rate is an Annual Percentage Rate (APR) paid by the borrower for the period of the loan.

The `getInterestRates` function sets the interest rate for borrowing money, making it cheaper when there's a lot of money available and more expensive when funds are running low.

Interest rates are calculated for the borrowed amount (`loanAmount`). Where taking a loan of 1,000 ADA will have a lower interest rate than taking a loan of 100,000 ADA (if taken from the same pool).

The loan interest rate is fixed for every loan and does not change for as long as the loan exists.

### getInterestRates()

***

#### Parameters

* `loanAmount`: The amount to be loaned.
* `lentOut`: The total amount already loaned out by the system.
* `balance`: The total amount of funds in the system.

***

#### Constants

Constants are protocol-set values to ensure a fluent lending/borrowing experience.

* `optimalUtilizationBN`: Optimal rate at which the system should be loaning out funds.
* `baseInterestRateBN`: The minimum interest rate to be charged.
* `rslope1BN` and `rslope2BN`: Rates at which interest increases.

```
const optimalUtilizationRate = 45%
const baseInterestRate = 3%
const rslope1BN = 7.5%
const rslope2BN = 300%
```

***

#### Utilization Rate

* **Formula**: `(loanAmount + lentOut) / (balance + lentOut)`
* **Purpose**: It measures how much of the available balance is being loaned out.

***

#### Interest Rate Calculation

1. **If Utilization Rate ≤ Optimal Utilization Rate**
   * **Formula**: `baseInterestRateBN + (utilizationRateBN * rslope1BN)`
2. **If Utilization Rate > Optimal Utilization Rate**
   * **Formula**: A more complicated formula involving both `rslope1BN` and `rslope2BN` is used to calculate the interest rate.

***

```typescript
export function getInterestRates(
  interestParams: InterestParams,
  loanAmount: bigint,
  lentOut: bigint,
  balance: bigint,
): bigint {

  const optimalUtilizationBN = new BigNumber(Number(interestParams.optimalUtilization))
  const baseInterestRateBN = new BigNumber(Number(BigInt(interestParams.baseInterestRate) * 1000000n))
  const rslope1BN = new BigNumber(Number(interestParams.rslope1))
  const rslope2BN = new BigNumber(Number(interestParams.rslope2))
  const oneMillionBN = new BigNumber(1000000)
  const loanAmountBN = new BigNumber(Number(loanAmount))
  const lentOutBN = new BigNumber(Number(lentOut))
  const balanceBN = new BigNumber(Number(balance))

  const utilizationRateBN = new BigNumber(
    lentOutBN.plus(loanAmountBN).multipliedBy(oneMillionBN).dividedBy(balanceBN.plus(lentOutBN)),
  )

  if (utilizationRateBN.lte(optimalUtilizationBN)) {
    const utilizationCharge = utilizationRateBN.multipliedBy(rslope1BN)

    const interestRate = BigInt(Math.floor(baseInterestRateBN.plus(utilizationCharge).dividedBy(1000000).toNumber()))

    return interestRate
  } else {
    const lowCharge = rslope1BN.multipliedBy(optimalUtilizationBN)
    const highCharge = utilizationRateBN.minus(optimalUtilizationBN).multipliedBy(rslope2BN)

    return BigInt(Math.floor(Number(baseInterestRateBN.plus(lowCharge).plus(highCharge).dividedBy(1000000).toNumber())))
  }
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://lenfi.gitbook.io/docs/liquidity-pools/interest-rate.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
