# BorrowLiquidation Logics

{% hint style="info" %}
In code, many base PRECISION are used to avoid, arithmetic, division or modulo by zero errors.
{% endhint %}

### ETH price ratio&#x20;

{% code title="borrowLiquidation.sol" %}

```solidity
uint128 ratio = BorrowLib.calculateEthPriceRatio(
        depositDetail.ethPriceAtDeposit,
        _currentEthPrice
    );
```

{% endcode %}

{% code title="BorrowLib.sol" %}

```solidity
function calculateEthPriceRatio(
        uint128 depositEthPrice, 
        uint128 currentEthPrice
    ) public pure returns(uint128){
        return (currentEthPrice * 10000)/depositEthPrice;
    }
```

{% endcode %}

It will returns the ratio of current ETH price to the deposit ETH price.

Functions which are using the above logic

* [Borrowing Withdraw.](/autonomint/blockchain-docs/core-contracts/borrowing.md#withdraw)
* [Borrow Liquidation.](/autonomint/blockchain-docs/core-contracts/borrowliquidation.md#liquidateborrowposition)

### Borrower Debt

{% code title="borrowLiquidation.sol" %}

```solidity
uint256 borrowerDebt = (
    (depositDetail.normalizedAmount * _lastCumulativeRate)/BorrowLib.RATE_PRECISION
    );
```

{% endcode %}

to calculate the borrower's debt we are using this formula. Last cumulative rate we will get from [calculateCumulativeRate ](/autonomint/blockchain-docs/core-contracts/borrowing.md#calculatecumulativerate)method.

BorrowLib.RATE\_PRECISION is base precision used, since last cumulative rate already have this precision we are dividing it here.

Functions which are using the above logic

* [Borrowing Withdraw.](/autonomint/blockchain-docs/core-contracts/borrowing.md#withdraw)
* [Borrow Liquidation.](/autonomint/blockchain-docs/core-contracts/borrowliquidation.md#liquidateborrowposition)

### Return to ABOND

{% code title="borrowLiquidation.sol" %}

```solidity
uint128 returnToAbond = BorrowLib.calculateReturnToAbond(
            depositDetail.depositedAmount,
            depositDetail.ethPriceAtDeposit, 
            returnToTreasury
);
```

{% endcode %}

{% code title="BorrowLib.sol" %}

```solidity
function calculateReturnToAbond(
        uint128 depositedAmount,
        uint128 depositEthPrice,
        uint128 returnToTreasury
    ) public pure returns(uint128){
        return (
        ((((depositedAmount * depositEthPrice)/USDA_PRECISION)/100) - returnToTreasury) * 10)/100;
    }
```

{% endcode %}

here return to treasury is the borrower's debt. USDA\_PRECISION is used for decimals, since USDa has 6 decimals only.

Functions which are using the above logic

* [Borrow Liquidation.](/autonomint/blockchain-docs/core-contracts/borrowliquidation.md#liquidateborrowposition)

### Liquidation Amount to get from other chain

{% code title="borrowLiquidation.sol" fullWidth="false" %}

```solidity
 uint256 liqAmountToGetFromOtherChain = BorrowLib.getLiquidationAmountProportions(
            liquidationAmountNeeded,
            cds.totalCdsDepositedAmount(),
            cds.omniChainCDSTotalCdsDepositedAmount(),
            cds.totalAvailableLiquidationAmount(),
            cds.omniChainCDSTotalAvailableLiquidationAmount()
        );
```

{% endcode %}

{% code title="BorrowLib.sol" fullWidth="false" %}

```solidity
function getLiquidationAmountProportions(
        uint256 _liqAmount,
        uint256 _totalCdsDepositedAmount,
        uint256 _totalGlobalCdsDepositedAmount,
        uint256 _totalAvailableLiqAmount,
        uint256 _totalGlobalAvailableLiqAmountAmount
    ) public pure returns (uint256){

        uint256 otherChainCDSAmount = _totalGlobalCdsDepositedAmount - _totalCdsDepositedAmount;

        uint256 totalAvailableLiqAmountInOtherChain = _totalGlobalAvailableLiqAmountAmount - _totalAvailableLiqAmount;

        uint256 share = (otherChainCDSAmount * LIQ_AMOUNT_PRECISION)/_totalGlobalCdsDepositedAmount;
        uint256 liqAmountToGet = (_liqAmount * share)/LIQ_AMOUNT_PRECISION;
        uint256 liqAmountRemaining = _liqAmount - liqAmountToGet;

        if(totalAvailableLiqAmountInOtherChain == 0){
            liqAmountToGet = 0;
        }else{
            if(totalAvailableLiqAmountInOtherChain < liqAmountToGet) {
                liqAmountToGet = totalAvailableLiqAmountInOtherChain;
            }else{
                if(totalAvailableLiqAmountInOtherChain > liqAmountToGet && _totalAvailableLiqAmount < liqAmountRemaining){
                    liqAmountToGet += liqAmountRemaining - _totalAvailableLiqAmount;
                }else{
                    liqAmountToGet = liqAmountToGet;
                }
            }
        }
        return liqAmountToGet;
    }
```

{% endcode %}

First calculate the each chain CDS amount proportions by dividing them with global CDS amount. Based on the proportions, calculate the liquidation amount to get from each chain.

Let say, if one chain don't have any CDS amount, then take all liquidation amount from other chain. If one chain don't have enough liquidation amount to get, then get available CDS amount from that chain and get remaining from other chain. If both have enough CDS amount, then get the amount based on proportion.

Functions which are using the above logic

* [Borrow Liquidation.](/autonomint/blockchain-docs/core-contracts/borrowliquidation.md#liquidateborrowposition)

### CDS profits for Destination Chain

{% code title="borrowLiquidation.sol" %}

```solidity
uint128 cdsProfitsForOtherChain = BorrowLib.getCdsProfitsProportions(
            liquidationAmountNeeded,
            uint128(liqAmountToGetFromOtherChain),
            cdsProfits
            );
```

{% endcode %}

{% code title="BorrowLib.sol" %}

```solidity
function getCdsProfitsProportions(
        uint128 _liqAmount,
        uint128 _liqAmountToGetFromOtherChain,
        uint128 _cdsProfits
    ) public pure returns (uint128){

        uint128 share = (_liqAmountToGetFromOtherChain * LIQ_AMOUNT_PRECISION)/_liqAmount;
        uint128 cdsProfitsForOtherChain = (_cdsProfits * share)/LIQ_AMOUNT_PRECISION;

        return cdsProfitsForOtherChain;
    }
```

{% endcode %}

<table><thead><tr><th width="263">Param Name</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>_liqAmount</td><td>uint128</td><td>Total Liquidation amount needed.</td></tr><tr><td>_liqAmountToGetFromOtherChain</td><td>uint128</td><td>Proportion of Liquidation amount to get from destination chain in total liquidation amount.</td></tr><tr><td>_cdsProfits</td><td>uint128</td><td>Total CDS profits from liquidation.</td></tr></tbody></table>

Calculate the proportion of CDS amount contributed by each chain. Based on the proportions divide the CDS profits for each chain.

Functions which are using the above logic

* [Borrow Liquidation.](/autonomint/blockchain-docs/core-contracts/borrowliquidation.md#liquidateborrowposition)


---

# 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.nondollar.life/autonomint/blockchain-docs/core-logics/borrowliquidation-logics.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.
