# Comptroller

## Introduction

The Comptroller is the heart and soul of all of the risk-management related business logic within the protocol. It determines multiple things, including but not limited to:

* How much collateral a user is required to maintain
* Whether a user can be liquidated.
* How much a user can be liquidated for.
* What markets are supported.
* Mapping user balances to prices and their respective collateral factors.
* Quantifies risk based off assets that a user is using in the protocol.

The Comptroller is also implemented as an upgradeable proxy, and the proxy contract is called the Unitroller. The Unitroller acts as a proxy for all logic and delegate calls the contract methods of the Comptroller contract and state parameters are stored in the  Unitroller contract.

To properly call Comptroller functions, please use the Comptroller ABI on the Unitroller address.

## Market Functions

### Enter Markets <a href="#enter-markets" id="enter-markets"></a>

When a user supplies assets to the protocol, the `enterMarkets` function is called with the associated cToken address. By calling this function, the respective asset is marked as collateral and is able to be borrowed against.

### Exit Markets <a href="#enter-markets" id="enter-markets"></a>

A user can remove supplied assets from account health calculations by calling the `exitMarkets` function.

## Account Functions

### Get Assets In <a href="#enter-markets" id="enter-markets"></a>

In order to decide how much account liquidity a user has and which markets they have entered, the Comptroller calls the `getAssetsIn` function. All entered markets count towards the total liquidity an account has available.

### Get Account Liquidity <a href="#enter-markets" id="enter-markets"></a>

#### Overview

As stated in the Account Liquidity paragraph on the [Collateral](https://docs.ursa.finance/ursa/documentation/technical-overview/collateral) section, this value can be used to see the maximum USD value borrowable for a particular account before it reaches liquidation. Any users that have a negative Account Liquidity are subject to liquidation. In addition, a user will not be able to use the protocol to withdraw assets or borrow additional assets until their Account Liquidity is positive again.

#### Comptroller logic

After a user has entered a market and the comptroller knows what assets they are in, it will then compute the available liquidity in the account. The Comptroller looks at each market a user has entered into and multiplies their [Supply](https://docs.ursa.finance/ursa/documentation/technical-overview/supply) balance by the respective assets [Collateral Factor](https://docs.ursa.finance/ursa/documentation/contracts/broken-reference), sums it up and then subtracts the [Borrow](https://docs.ursa.finance/ursa/documentation/technical-overview/borrow) balances; this yields Account Liquidity.

Each time an asset is supplied, borrowed, withdrawn or repaid, an accounts overall liquidity will change accordingly.

* Supplying increases the Account Liquidity of an account.
* Borrowing reduces the Account Liquidity of an account.
* Withdrawing reduces Account Liquidity by refactoring the account's overall liquidity per the equation listed above.&#x20;
* Repaying increases Account Liquidity of an account by refactoring the account's overall liquidity per the equation listed above.

For additional more information, please see the Account Liquidity paragraph on the [Collateral](https://docs.ursa.finance/ursa/documentation/technical-overview/collateral) section.

### Collateral Factor <a href="#enter-markets" id="enter-markets"></a>

Within the protocol, a lToken's Collateral Factor can currently fall within a range of 15-85% (subject to change by governance or with the addition of new markets). As described in our [Interest Rate Models ](https://docs.ursa.finance/ursa/documentation/contracts/interest-rate-models)section, Ursa places assets into different buckets based on their level of risk.

Large, blue chip, liquid assets will have high Collateral Factors and will be able to be used as prime-collateral. Small, more niche, illiquid assets will have low Collateral Factors. Some assets may not have a Collateral Factor, which means they cannot be used as collateral and borrowed against.

For more information on Collateral Factors, please see the [Collateral](https://app.gitbook.com/o/QSfM36b0B19VqbqvsOQO/s/BGmCLfJmZgdkJvM2EN7T/) and [Collateral Factors](https://docs.ursa.finance/ursa/documentation/contracts/broken-reference) sections.

### Close Factor

As outlined in the Close Factor paragraph on the [Liquidation](https://docs.ursa.finance/ursa/documentation/technical-overview/liquidation) section, the protocol has a default Close Factor of 0.5 and this represents the maximum amount (50%) of a user's borrow balance that can be liquidated in a single transaction. If a user's account is still insolvent after liquidation, the account can be liquidated again until the account is back in good standing.

The Comptroller applies the `closeFactor` to a single borrowed asset, not the aggregated value of all of the assets a user has borrowed.

### Liquidation Incentive <a href="#liquidation-incentive" id="liquidation-incentive"></a>

This value is represented by the `liquidationIncentiveMantissa` parameter and acts as an incentive for liquidators to liquidate unhealthy accounts. The protocols default liquidation incentive for V0 is 8%.

For an example of how this breaks down and for more information on Liquidations, please see the [Liquidation](https://docs.ursa.finance/ursa/documentation/technical-overview/liquidation) section for additional information.

### Market Metadata <a href="#liquidation-incentive" id="liquidation-incentive"></a>

The Comptroller provides a useful function, `getAllMarkets` , that can be used to retrieve all of the currently supported lToken markets and their corresponding addresses.


---

# 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.ursa.finance/ursa/documentation/contracts/comptroller.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.
