# Interest Rate Model

### Overview

The Bank contract allows for the use of custom Interest Rate Models (IRMs) to calculate borrow rates for different markets. This feature enables flexible and dynamic interest rate calculations tailored to specific market conditions.

### InterestRateModel Interface

Custom IRMs must implement the `IInterestRateModel` interface:

```solidity
solidityCopyinterface IInterestRateModel {
    function isIrm() external view returns (bool);
    function borrowRate(MarketConfigs memory marketConfigs, Market memory market) external returns (uint256);
    function borrowRateView(MarketConfigs memory marketConfigs, Market memory market) external view returns (uint256);
}
```

#### Key Functions

1. `isIrm()`: Validates that the contract is a legitimate IRM.
2. `borrowRate()`: Calculates and returns the current borrow rate. This function may modify the contract state.
3. `borrowRateView()`: Similar to `borrowRate()`, but doesn't modify the contract state.

### Creating a Custom IRM

To create a custom IRM:

1. Implement the `IInterestRateModel` interface.
2. Define your interest rate calculation logic in the `borrowRate()` and `borrowRateView()` functions.
3. Ensure `isIrm()` returns `true`.

Example:

```solidity
solidityCopycontract MyCustomIRM is IInterestRateModel {
    using WadMath for uint128;

    function isIrm() external pure returns (bool) {
        return true;
    }

    function borrowRateView(MarketConfigs memory, Market memory market) public pure returns (uint256) {
        if (market.totalSupplyAssets == 0) return 0;
        uint256 utilization = market.totalBorrowAssets.divWadDown(market.totalSupplyAssets);
        return utilization / 365 days;
    }

    function borrowRate(MarketConfigs memory marketConfigs, Market memory market) external pure returns (uint256) {
        return borrowRateView(marketConfigs, market);
    }
}
```

### Registering and Using a Custom IRM

1. Deploy your custom IRM contract.
2. The DAO must register the IRM with the Bank contract using the `registerIrm()` function:

   ```solidity
   solidityCopyfunction registerIrm(address irm, bool isIrm) external onlyOwner;
   ```
3. Once registered, you can use the IRM when creating a new market:

   ```solidity
   solidityCopyfunction createMarket(MarketConfigs memory marketConfigs) external payable returns (uint256);
   ```

   Include the address of your registered IRM in the `marketConfigs.irm` field.

### Important Notes

* Only IRMs registered by the DAO can be used in market creation.
* Ensure your IRM calculations are accurate and gas-efficient.
* Test your IRM thoroughly before deployment to avoid potential issues in live markets.
* The Bank contract will call the IRM's `borrowRate()` function during interest accrual, so make sure it handles all possible market states correctly.

By following these guidelines, you can create and integrate custom Interest Rate Models to suit various market strategies and conditions within the Bank ecosystem.


---

# Agent Instructions: 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:

```
GET https://docs.untitledbank.co/developer-docs/interest-rate-model.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
