# Collateral

Collateral is security that must be deposited into a smart contract to secure the loan. Collateral can only be retrieved from the contract if the loan is repaid or liquidated.

On Lenfi, collateral can be any native Cardano asset or ADA itself. However, each pool has a determined collateral that can be used for the loans.&#x20;

For example:

<table><thead><tr><th width="152"></th><th width="183">Loan asset</th><th>Accepted collateral</th></tr></thead><tbody><tr><td>Pool 1</td><td>ADA</td><td>LENFI</td></tr><tr><td>Pool 2</td><td>ADA</td><td>MIN</td></tr><tr><td>Pool 3</td><td>LENFI</td><td>ADA</td></tr><tr><td>Pool 4</td><td>LENFI</td><td>MIN</td></tr></tbody></table>

Each collateral token has a protocol-defined `Liquidation threshold` which is factored in the Health Factor calculation. A higher liquidation threshold means more collateral must be locked in order to retrieve the loan.

### Collateral tokens

These are currently accepted collateral tokens.

<table><thead><tr><th width="117">Token</th><th width="234">Liquidation threshold (LT)</th><th>Initial LT </th></tr></thead><tbody><tr><td>ADA</td><td>130%</td><td>150%</td></tr><tr><td>LENFI</td><td>200%</td><td>220%</td></tr><tr><td>MIN</td><td>170%</td><td>190%</td></tr><tr><td>WMT</td><td>200%</td><td>220%</td></tr><tr><td>LQ</td><td>200%</td><td>220%</td></tr><tr><td>WRT</td><td>200%</td><td>220%</td></tr><tr><td>COPI</td><td>200%</td><td>220%</td></tr><tr><td>DJED</td><td>200%</td><td>220%</td></tr><tr><td>SHEN</td><td>200%</td><td>220%</td></tr><tr><td>SNEK</td><td>200%</td><td>220%</td></tr><tr><td>IUSD</td><td>170%</td><td>190%</td></tr><tr><td>IAG</td><td>200%</td><td>220%</td></tr><tr><td>HUNT</td><td>200%</td><td>220%</td></tr><tr><td>FLDT</td><td>200%</td><td>220%</td></tr><tr><td>OPTIM</td><td>200%</td><td>220%</td></tr><tr><td>INDY</td><td>200%</td><td>220%</td></tr><tr><td>FACT</td><td>200%</td><td>220%</td></tr></tbody></table>

The liquidation threshold is used when calculating the Health Factor for already active loans. While initial LT is used when a new loan is being created.

### Value calculation

Collateral value is calculated using constant product formula with 0.03% fee.&#x20;

````
```rust
pub fn token_b_received_from_seling_token_a(
  sell_amount: Rational, // Amount of collateral assets
  token_a_amount: Int, // Amount of collateral liquidity available on DEX'es
  token_b_amount: Int, // Amount of ADA liquidity available on DEX'es
) -> Int {
  let sell_amount_int = sell_amount |> rational.truncate()

  let nominator = sell_amount_int * 997 * token_b_amount
  let denominator = token_a_amount * 1000 + 997 * sell_amount_int
  let asset_return = nominator / denominator
  asset_return
}
```
````

This method allows Lenfi to efficiently operate in low liquidity markets.

### Health Factor

The Health Factor (HF) represents the health of the loan. A borrower can only take out a loan if the HF is greater than 1. If the health factor falls below 1, the loan can be liquidated. In the event of liquidation, the liquidator repays the borrower's loan in exchange for part or all of the collateral, depending on what is needed to compensate for the repayment.

#### Health Factor - new loan:

$$Health Factor = (Collateral Value / (Loan Value) / Initial LiquidationThreshold$$

#### Health Factor - loan is active

$$Health Factor = (Collateral Value / (LoanValue + InterestValue)) / Liquidation threshold$$

***

**Example 1: New Loan with Healthy Factor**

* Collateral Value: 200 ADA
* Loan Amount: 100 ADA&#x20;
* Initial Collateral Ratio: 1.5

Health Factor = (200 / 100) / 1.5 = 1.33

The Health Factor is greater than 1, so the loan can be taken out safely.

**Example 2: Active Loan with Risky Factor**

* Collateral Value: 200 ADA
* Loan Amount + Interest: 155 ADA
* Liquidation threshold: 1.3

Health Factor = (200 / 155) / 1.3 = 0.992

The Health Factor has fallen below 1, meaning the loan can be liquidated.

{% hint style="info" %}
Below functions consumed collateralValue and loanValue calculated using [price feeds](https://lenfi.gitbook.io/docs/liquidity-pools/broken-reference).
{% endhint %}

```rust
/// Calculate health from debt, collateral, threshold
pub fn calculate_health_factor(
  debt: Int,
  collateral: Int,
  threshold: Int,
) -> Option<Rational> {
  collateral * 1000000000000
    |> rational.new(threshold * debt)
}

/// Health factor >= 100%
pub fn check_is_overcollaterized(
  value_in_debt: Int,
  collateral_value: Int,
  liquidation_threshold: Int,
) -> Bool {
  // Check if loan is overcollaterized. Meaning 'value' in debt is large enought compared to collateral factor.
  let over_collateralized: Bool =
    when
      calculate_health_factor(
        value_in_debt,
        collateral_value,
        liquidation_threshold,
      )
    is {
      Some(rat) -> Some(rational.compare(rat, 1000000 |> rational.from_int))
      None -> None
    } == Some(Greater)
  over_collateralized
}

/// Health factor < 100%
pub fn check_is_undercollaterized(
  value_in_debt: Int,
  collateral_value: Int,
  liquidation_threshold: Int,
) {
  let under_collateralized: Bool =
    (
      calculate_health_factor(
        value_in_debt,
        collateral_value,
        liquidation_threshold,
      )
        |> option.map(rational.compare(_, 1000000 |> rational.from_int))
    ) == Some(Less)
  under_collateralized
}
```
