> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mystockline.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# InterestRateModel

> InterestRateModel is a pure library that turns pool utilization into a per-second borrow rate.

InterestRateModel is a pure library that turns pool utilization into a per-second borrow rate. It implements a kinked curve: a gentle slope up to the kink and a steep slope above it, so full utilization becomes expensive and liquidity returns to the pool. Stockline calls it during every accrual with the book's IrmParams from RiskRegistry. The library has no external functions.

`InterestRateModel` lets you:

* Compute the per-second borrow rate for a given utilization and book
* Round interest up so debt never accrues slower than the curve
* Reject utilization above 100% or an invalid kink

`InterestRateModel` is a Solidity library. It is linked into the contracts that use it and has no address of its own.

The source code is [`src/core/InterestRateModel.sol`](https://github.com/stockline-xyz/contracts/blob/main/src/core/InterestRateModel.sol) on GitHub. The ABI is [`/abis/InterestRateModel.json`](/abis/InterestRateModel.json).

## Pure functions

### rate

```solidity theme={"system"}
function rate(
    uint256 utilization,
    IrmParams memory params
) internal pure returns (uint256)
```

Returns the per-second borrow rate for a utilization and a book's curve. Below the kink the rate is `baseRate + slopeBelowKink * u / kink`. Above it the rate is `baseRate + slopeBelowKink + slopeAboveKink * (u - kink) / (1 - kink)`. Reverts with UtilizationAboveWad above 1e18 and InvalidKink unless 0 \< kink \< 1e18.

<Note>
  Internal function. Divisions round up against the borrower. Parameters are already per-second; the library does not convert from APY.
</Note>

**Input parameters**

| Name        | Type        | Description                                                                                                  |
| ----------- | ----------- | ------------------------------------------------------------------------------------------------------------ |
| utilization | `uint256`   | Borrowed divided by supplied, in WAD (1e18 = 100%).                                                          |
| params      | `IrmParams` | IrmParams struct: `baseRate`, `slopeBelowKink`, `slopeAboveKink` (per-second WAD), `kink` (utilization WAD). |

**Return values**

| Type      | Description                                                                     |
| --------- | ------------------------------------------------------------------------------- |
| `uint256` | Per-second borrow rate in WAD. Multiply by 31,536,000 for a simple annual rate. |

### \_mulDivUp

```solidity theme={"system"}
function _mulDivUp(
    uint256 a,
    uint256 b,
    uint256 d
) private pure returns (uint256)
```

<Note>
  ceil(a \* b / d). Borrow interest rounds against the borrower.
</Note>

**Input parameters**

| Name | Type      | Description |
| ---- | --------- | ----------- |
| a    | `uint256` |             |
| b    | `uint256` |             |
| d    | `uint256` |             |

**Return values**

| Type      | Description |
| --------- | ----------- |
| `uint256` |             |

## Errors

### UtilizationAboveWad

```solidity theme={"system"}
error UtilizationAboveWad(uint256 utilization)
```

The utilization argument is above 1e18.

### InvalidKink

```solidity theme={"system"}
error InvalidKink(uint64 kink)
```

The kink is 0 or 1e18 or more.
