# Borrowing Logics

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

### USDa to mint

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

```solidity
function _transferToken(
        address _borrower,
        uint256 amount,
        uint128 _ethPrice,
        uint256 optionFees
)
```

{% endcode %}

| Param Name | Param Type | Description                           |
| ---------- | ---------- | ------------------------------------- |
| \_borrower | address    | Address of the borrower.              |
| amount     | uint256    | Deposited ETH amount.                 |
| \_ethPrice | uint128    | Current ETH price.                    |
| optionFees | uint256    | Options fees to deduct from borrower. |

```solidity
uint256 tokensToLend = BorrowLib.tokensToLend(amount, _ethPrice, LTV);
bool minted = usda.mint(_borrower, (tokensToLend - optionFees));
```

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

```solidity
function tokensToLend(
        uint256 depositedAmont, 
        uint128 ethPrice, 
        uint8 LTV
    ) public pure returns(uint256){
        uint256 tokens = (depositedAmont * ethPrice * LTV) / (USDA_PRECISION * RATIO_PRECISION);
        return tokens;
}
```

{% endcode %}

Calculate the USDa to lend, by using LTV ratio. Then deduct the options fees, mint the remaining to borrower and options fees to treasury.

Functions which are using the above logic

* [Borrowing Deposit.](/autonomint/blockchain-docs/core-contracts/borrowing.md#deposittokens)

{% hint style="info" %}
LTV is global variable in Borrowing contract.
{% endhint %}

### ABOND to mint

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

```solidity
function _mintAbondToken(
        address _toAddress, 
        uint64 _index, 
        uint256 _amount
)
```

{% endcode %}

| Param Name  | Type    | Description                                   |
| ----------- | ------- | --------------------------------------------- |
| \_toAddress | address | Address of the borrower.                      |
| \_index     | uint64  | For which position index needs to mint ABOND. |
| \_amount    | uint256 | ETH backed.                                   |

```solidity
uint128 amount = BorrowLib.abondToMint(_amount,bondRatio);
```

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

```solidity
function abondToMint(
        uint256 _amount, 
        uint64 _bondRatio
) public pure returns(uint128 amount){
        amount = (uint128(_amount) * USDA_PRECISION)/_bondRatio;
}
```

{% endcode %}

Calculate the ABOND to mint to user during withdraw based on bondRatio and mint it to borrower.

Functions which are using the above logic

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

### Normalized amount calculation

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

```solidity
uint256 normalizedAmount = BorrowLib.calculateNormAmount(tokensToLend,lastCumulativeRate);
```

{% endcode %}

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

```solidity
function calculateNormAmount(
        uint256 amount,
        uint256 cumulativeRate
) public pure returns(uint256){
        return (amount * RATE_PRECISION)/cumulativeRate;
}
```

{% endcode %}

Calculate the normalized amount by dividing the total borrowed amount with cumulative rate. Cumulative rate should get from [calculateCumulativeRate](/autonomint/blockchain-docs/core-contracts/borrowing.md#calculatecumulativerate) method.

Functions which are using the above logic

* [Borrowing Deposit.](/autonomint/blockchain-docs/core-contracts/borrowing.md#deposittokens)

### ABOND USDa Pool Calculation

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

```solidity
uint256 discountedETH = BorrowLib.calculateDiscountedETH(
    depositDetail.depositedAmount,
    _ethPrice
    );
```

{% endcode %}

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

```solidity
function calculateDiscountedETH(
        uint256 amount,
        uint128 ethPrice
    ) public pure returns(uint256){
        return ((((80*calculateHalfValue(amount))/100)*ethPrice)/100)/USDA_PRECISION;
}

function calculateHalfValue(uint256 amount) public pure returns(uint128){
        return uint128((amount * 50)/100);
}
```

{% endcode %}

Calculates the 50% of the deposited amount value with current ETH price, then 80% of the calculated amount is going to ABOND USDa pool to back ABOND.

Functions which are using the above logic

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

### Withdraw ETH calculation

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

```solidity
uint128 ethToReturn;
//Calculate current depositedAmount value
uint128 depositedAmountvalue = (
    depositDetail.depositedAmount * depositDetail.ethPriceAtDeposit)/_ethPrice;

if(borrowingHealth > 10000){
    // If the ethPrice is higher than deposit ethPrice,call withdrawOption in options contract
    ethToReturn = (depositedAmountvalue + (options.calculateStrikePriceGains(depositDetail.depositedAmount,depositDetail.strikePrice,_ethPrice)));
    if(ethToReturn > depositDetail.depositedAmount){
        ethRemainingInWithdraw += (ethToReturn - depositDetail.depositedAmount);
        omniChainBorrowing.ethRemainingInWithdraw += (ethToReturn - depositDetail.depositedAmount);
    }else{
        ethRemainingInWithdraw += (depositDetail.depositedAmount - ethToReturn);
        omniChainBorrowing.ethRemainingInWithdraw += (depositDetail.depositedAmount - ethToReturn);
    }
    ethValueRemainingInWithdraw += (ethRemainingInWithdraw * _ethPrice);
    omniChainBorrowing.ethValueRemainingInWithdraw += (ethRemainingInWithdraw * _ethPrice);
}else if(borrowingHealth == 10000){
    ethToReturn = depositedAmountvalue;
}else if(8000 < borrowingHealth && borrowingHealth < 10000) {
    ethToReturn = depositDetail.depositedAmount;
}else{
    revert("BorrowingHealth is Low");
}
```

{% endcode %}

Calculate the withdraw amount by taking the ratio of deposit ETH price to current ETH price.

If the current ETH price is higher than deposited ETH price,&#x20;

### Cumulative rate calculation

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

```solidity
function calculateCumulativeRate(
        uint128 noOfBorrowers,
        uint256 ratePerSec,
        uint128 lastEventTime,
        uint256 lastCumulativeRate
    )
```

{% endcode %}

| Param Name         | Type    | Description                            |
| ------------------ | ------- | -------------------------------------- |
| noOfBorrowers      | uint128 | Total number of borrowers in treasury. |
| ratePerSec         | uint256 | Interest rate per second.              |
| lastEventTime      | uint128 | Timestamp of last event.               |
| lastCumulativeRate | uint256 | Previous cumulative rate.              |

{% hint style="info" %}
Here, event represents Deposit, Withdraw and Liquidation.
{% endhint %}

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

```solidity
currentCumulativeRate = lastCumulativeRate * _rpow(ratePerSec, timeInterval, RATE_PRECISION);
currentCumulativeRate = currentCumulativeRate / RATE_PRECISION;
```

{% endcode %}

```
Current cumulative rate = last cumulative rate * (rate per second) ^ time interval.
```

To calculate the current cumulative rate, multiply the previous cumulative rate with interest rate per second to the power of time interval between two events.

Functions which are using the above logic

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

### Calculation of ratio of CDS Pool value to ETH vault value

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

```solidity
function calculateRatio(
        uint256 _amount,
        uint currentEthPrice,
        uint128 lastEthprice,
        uint128 noOfBorrowers,
        uint256 latestTotalCDSPool,
        IBorrowing.OmniChainBorrowingData memory previousData
)    
```

{% endcode %}

| Param Name         | Type                          | Description                                          |
| ------------------ | ----------------------------- | ---------------------------------------------------- |
| \_amount           | uint256                       | ETH amount, the user is depositing.                  |
| currentEthPrice    | uint256                       | Current ETH to USD price.                            |
| lastEthPrice       | uint128                       | ETH price when the last event happened.              |
| noOfBorrowers      | uint128                       | Total number of borrowers in the protocol.           |
| latestTotalCDSPool | uint256                       | Total CDS amount accumulated in the protocol.        |
| previousData       | Struct OmniChainBorrowingData | Previously stored global data of borrowing contract. |

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

```solidity
// Calculate net P/L of CDS Pool
netPLCdsPool = price difference between current and last ETH price * noOfBorrowers;
previousData.cdsPoolValue = previousData.totalCDSPool + netPLCdsPool;
```

{% endcode %}

Initially calculate the net P\&L of CDS, if the current ETH price is higher than last ETH price , then its profit else its loss.

If the ratio calculation is for first deposit in the protocol, then calculate net CDS Pool value by adding netPLCdsPool with total CDS amount accumulated. else,&#x20;

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

```solidity
previousData.cdsPoolValue = previousData.cdsPoolValue + ( 
    latestTotalCDSPool - previousData.totalCDSPool) + netPLCdsPool;
```

{% endcode %}

Add the netPLCdsPool with previous net CDS Pool value and CDS amount deposited after last event.

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

```solidity
ratio = currentCDSPoolValue / currentEthVaultValue;
```

{% endcode %}

Finally, calculate the ratio by dividing currentCDSPoolValue by currentEthVaultValue.

Functions which are using the above logic

* [Borrowing Deposit.](/autonomint/blockchain-docs/core-contracts/borrowing.md#deposittokens)
* [Borrowing Withdraw.](/autonomint/blockchain-docs/core-contracts/borrowing.md#withdraw)
* [CDS Withdraw.](/autonomint/blockchain-docs/core-contracts/cds.md#withdraw)

### Downside Calculation

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

```solidity
function calculateDownsideProtected(
    uint128 amount,
    uint128 currentEthPrice,
    uint128 depositEthPrice
) public pure returns (uint128) {
    if (currentEthPrice < depositEthPrice) {
        return
            (amount * (depositEthPrice - currentEthPrice)) /
            (100 * USDA_PRECISION);
    } else {
        return 0;
    }
}
```

{% endcode %}

Calculates the downside needs to protect by CDS user.

Functions which are using the above logic

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

### Options fees per second Calculation

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

```solidity
function calculateOptionsFeesPerSec(
    uint128 optionsFees
) public pure returns (uint128) {
    return (optionsFees * OPTIONS_FEES_PRECISION) / 30 days;
}
```

{% endcode %}

Calculates the options fees per second by dividing the options fees paid by user during deposit by number of seconds in 30 days.


---

# 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/borrowing-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.
