# CDS Logics

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

### USDT depsoited till now - Logic

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

```solidity
if(usdtAmountDepositedTillNow < usdtLimit){
    if((usdtAmountDepositedTillNow + usdtAmount) <= usdtLimit){
        require(usdtAmount == totalDepositingAmount,'100% of amount must be USDT');
    }else{
        revert("Surplus USDT amount");
    }
}else{
    require(usdaAmount >= (usdaLimit * totalDepositingAmount)/100,"Required USDa amount not met");
}
```

{% endcode %}

Check whether the USDT limit in CDS is reached or not by including the current depositing amount also. If its reached, the USDa amount must be 80% of the depositing amount.

Functions which are using the above logic

* [CDS Deposit.](/autonomint/blockchain-docs/core-contracts/cds.md#deposit)

### Options fees to get from each chain

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

```solidity
function getOptionsFeesProportions(
        uint256 optionsFees,
        uint256 _totalCdsDepositedAmount,
        uint256 _totalGlobalCdsDepositedAmount,
        uint256 _totalCdsDepositedAmountWithOptionFees,
        uint256 _totalGlobalCdsDepositedAmountWithOptionFees
)
```

{% endcode %}

Calculate the each chain CDS amount proportions by dividing them with global CDS amount. Based on the proportions, calculate the options fees to get from each chain.

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

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

```solidity
if(optionsFeesToGetFromOtherChain > 0 && ethAmount == 0){
    functionToDo = ITreasury.FunctionToDo(2);
}else if(optionsFeesToGetFromOtherChain == 0 && ethAmount > 0){
    functionToDo = ITreasury.FunctionToDo(3);
}else if(optionsFeesToGetFromOtherChain > 0 && ethAmount > 0){
    functionToDo = ITreasury.FunctionToDo(4);
}
```

{% endcode %}

```solidity
enum FunctionToDo { DUMMY, UPDATE, TOKEN_TRANSFER, NATIVE_TRANSFER, BOTH_TRANSFER}
```

Suppose, if there is no liquidation happened between deposit and withdraw of the CDS user, then the ethAmount will be zero.So functionToDo in treasury will be Token transfer only. Likewise, all condtions are applied.

Functions which are using the above logic

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

### USDT to give in redeem - Logic

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

```solidity
function redeemUSDT(
        uint128 _usdaAmount,
        uint64 usdaPrice,
        uint64 usdtPrice
)
```

{% endcode %}

```solidity
_usdtAmount = (usdaPrice * _usdaAmount/usdtPrice);
```

The amount of USDT to give to the user will be calculated by dividing the USDa price of one token by USDT price of one token. Multiply the ratio with USDa amount, the user is redeeming.

Functions which are using the above logic

* [Redeem USDT.](/autonomint/blockchain-docs/core-contracts/cds.md#redeemusdt)

### Cumulative rate calculation

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

```solidity
function calculateCumulativeRate(
        uint128 _fees,
        uint256 _totalCdsDepositedAmount,
        uint256 _totalCdsDepositedAmountWithOptionFees,
        uint256 _totalGlobalCdsDepositedAmountWithOptionFees,
        uint128 _lastCumulativeRate,
        uint128 _noOfBorrowers
)
```

{% endcode %}

<table><thead><tr><th width="263">Param Name</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>_fees</td><td>uint128</td><td>Options fees to give to CDS users.</td></tr><tr><td>_totalCdsDepositedAmount</td><td>uint256</td><td>Total CDS amount in this chain.</td></tr><tr><td>_totalCdsDepositedAmountWithOptionFees</td><td>uint256</td><td>Total CDS amount with options fees in this chain.</td></tr><tr><td>_totalGlobalCdsDeposited AmountWithOptionFees</td><td>uint256</td><td>Total CDS amount with options fees in all chain.</td></tr><tr><td>_lastCumulativeRate</td><td>uint128</td><td>Previously stored cumulative rate.</td></tr><tr><td>_noOfBorrowers</td><td>uint128</td><td>Total number of borrowers in protocol.</td></tr></tbody></table>

```solidity
uint128 netCDSPoolValue = uint128(_totalGlobalCdsDepositedAmountWithOptionFees);
uint128 percentageChange = (_fees * PRECISION)/netCDSPoolValue;
```

Calculate the percentage change by dividing fees by netCDSPoolvalue. If the number of borrowers nil, then the current cumulative rate will be 1 + percentage change. Else,&#x20;

```
currentCumulativeRate = (1 * PRECISION) + percentageChange;
```

current cumulative rate will be previous cumulative rate multiple percentage change plus one.

```
currentCumulativeRate = _lastCumulativeRate * ((1 * PRECISION) + percentageChange);
```

Functions which are using the above logic

* [calculateCumulativeRate.](/autonomint/blockchain-docs/core-contracts/cds.md#calculatecumulativerate)


---

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