# CDS

The CDS is another main user facing contract in Autonomint protocol, where users can deposit stablecoins like USDa, USDT to get yields.&#x20;

During withdraw the user can get the deposited amount based on collateral price change between deposit and withdraw, Options fees and liquidation gains if opted.&#x20;

There is a redeemUSDT option also available to get the USDT, for that equal amount of USDa needs to be burn from the user.&#x20;

## Write Functions

### deposit

```solidity
function deposit(
        uint128 usdtAmount,
        uint128 usdaAmount,
        bool liquidate,
        uint128 liquidationAmount,
        uint128 lockingPeriod ) payable 
```

| Param Name        | Type    | Description                                                 |
| ----------------- | ------- | ----------------------------------------------------------- |
| usdtAmount        | uint128 | Amount USDT token user is depositing.                       |
| usdaAmount        | uint128 | Amount USDa token user is depositing.                       |
| liquidate         | bool    | Whether the user is opted for liquidation gains.            |
| liquidationAmount | uint128 | Opted liquidation amount.                                   |
| lockingPeriod     | uint128 | How long the user is willing to stay deposited in protocol. |
| msg.value         | uint256 | Lz transaction fee.                                         |

The user can able to deposit up to USDT $20K, for that the same amount of USDa is minted. After it reaches the 20K limit, the user must deposit minimum USDa amount of 80% of total depositing amount. There is an locking period option is also available to lock the deposited amount by user itself. User can also opt for liquidation gains i.e. the user can use this deposited amount to be used for liquidating collaterals.

### withdraw

```solidity
function withdraw(
    uint64 index,
    uint256 excessProfitCumulativeValue,
    uint256 nonce,
    bytes memory signature
) public payable
```

<table><thead><tr><th width="275">Param Name</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>index</td><td>uint64</td><td>Index of the user's position, tries to withdraw. </td></tr><tr><td>excessProfitCumulativeValue</td><td>uint256</td><td>The cumulative value of the excess profit, needs to remove from the upside.</td></tr><tr><td>nonce</td><td>uint256</td><td>Nonce of the signer who signed the signsture.</td></tr><tr><td>signature</td><td>bytes</td><td>Excess profit cumulative value signed by admin 2.</td></tr><tr><td>msg.value</td><td>uint256</td><td>Lz transaction fee.</td></tr></tbody></table>

The withdraw amount is calculated by&#x20;

* price change of collateral between deposit and withdraw.
* option fees earned by the user by protecting the borrower's collateral.
* If opted for liquidation, then the user will get protected liquidated position value and collateral.

{% hint style="info" %}
Option fees earned by user is calculated by using cumulative rate.
{% endhint %}

### redeemUSDT

```solidity
function redeemUSDT(uint128 usdaAmount, uint64 usdaPrice, uint64 usdtPrice) payable
```

| Param Name | Type    | Description                                  |
| ---------- | ------- | -------------------------------------------- |
| usdaAmount | uint128 | USDa amount the user is willing to exchange. |
| usdaPrice  | uint64  | USDa price.                                  |
| usdtPrice  | uint64  | USDT price.                                  |
| msg.value  | uint256 | Lz transaction fee.                          |

After user transferred USDa to the protocol through redeemUSDT() method, the same value of USDT is transferred to the user. Then the acquired USDa is burned to maintain the USDa always pegged to $1.

### calculateCumulativeRate

```solidity
function calculateCumulativeRate(uint128 fees) payable
```

| Param Name | Type    | Description                       |
| ---------- | ------- | --------------------------------- |
| fees       | uint128 | Option fees to give to CDS users. |
| msg.value  | uint256 | Lz transaction fee.               |

The option fees is distributed to the CDS users by using cumulative rate. During deposit the normalized amount of the deposited amount of the user is stored. When the user withdraws, the normalized amount is multiplied with current cumulative rate to get the deposited amount with option fees.

{% hint style="info" %}
This  function is only called by Borrowing contract.
{% endhint %}

### calculateCumulativeValue

```solidity
function calculateCumulativeValue(
    uint256 vaultBal,
    uint256 globalTotalCdsDepositedAmount,
    uint128 _price
)external
```

| Param Name                    | Type    | Description                                                         |
| ----------------------------- | ------- | ------------------------------------------------------------------- |
| vaultBal                      | uint256 | Total collateral deposited by borrowers in ETH value in all chains. |
| globalTotalCdsDepositedAmount | uint256 | Total CDS deposited amount in all chains                            |
| \_price                       | uint128 | Current ETH price.                                                  |

Calculates the cumulative value based on comparing last stored ETH price and current ETH price multiplied with Vault to CDS ratio.

{% hint style="info" %}
This  function is only called by Borrowing contract.
{% endhint %}

### cdsAmountToReturn

```solidity
function cdsAmountToReturn(
    address _user,
    uint64 index,
    uint128 cumulativeValue,
    bool cumulativeValueSign,
    uint256 excessProfitCumulativeValue
) private view returns (uint256)
```

Calculates the USDa amount to return to the user during withdraw, based on price fluctuations and collateral amount in borrow since deposit.

## Setter Functions

<table data-view="cards"><thead><tr><th>Function Name</th><th>Param Name</th><th>Param Type</th><th>Description</th></tr></thead><tbody><tr><td>setAdmin</td><td>_admin</td><td>address</td><td>Admin address.</td></tr><tr><td>setBorrowingContract</td><td>_address</td><td>address</td><td>Borrowing contract address.</td></tr><tr><td>setTreasury</td><td>_treasury</td><td>address</td><td>Treasury contract address.</td></tr><tr><td>setBorrowLiquidation</td><td>_address</td><td>address</td><td>BorrowLiquidation contract address.</td></tr><tr><td>setWithdrawTimeLimit</td><td>_timeLimit</td><td>uint64</td><td>CDS withdraw locking period.</td></tr><tr><td>setUSDaLimit</td><td>percent</td><td>uint8</td><td>USDa percent, the user must deposit after $20k USDT.</td></tr><tr><td>setUsdtLimit</td><td>amount</td><td>uint64</td><td>USDT limit.</td></tr><tr><td>setDstEid</td><td>_eid</td><td>uint32</td><td>Destination chain EID.</td></tr><tr><td>setAdminTwo</td><td>hashedAddress32</td><td>bytes</td><td>Hashed admin two address.</td></tr></tbody></table>

## Update Functions

<table data-view="cards"><thead><tr><th>Function Name</th><th>Param Name</th><th>Param Type</th><th>Description</th></tr></thead><tbody><tr><td>updateLastEthPrice</td><td>priceAtEvent</td><td>uint128</td><td>ETH price to update.</td></tr><tr><td>updateLiquidationInfo</td><td>index, liquidationData</td><td>uint128,struct LiquidationInfo</td><td>Liquidation Info to update.</td></tr><tr><td>updateTotalAvailableLiquidationAmount</td><td>amount</td><td>uint128</td><td>Available liquidation amount to update.</td></tr><tr><td>updateTotalCdsDepositedAmount</td><td>_amount</td><td>uint256</td><td>CDS amount to update.</td></tr><tr><td>updateTotalCdsDepositedAmountWithOptionFees</td><td>_amount</td><td>uint256</td><td>CDS amount with option fees to update.</td></tr><tr><td>updateDownsideProtected</td><td>downsideProtectedAmount</td><td>uint128</td><td>Downside protected amount during borrow withdraw.</td></tr></tbody></table>

## ReadFunctions

<table data-view="cards"><thead><tr><th>Function Name</th><th>Description</th><th>Param Name</th><th>Param Type</th></tr></thead><tbody><tr><td>getLatestData</td><td>Gets the latest ETH price.</td><td></td><td></td></tr><tr><td>getCumulativeValue</td><td>Gets the current cumulative value.</td><td>omniChainData, value, gains</td><td>uint128,struct LiquidationInfo</td></tr><tr><td>getOptionsFeesProportions</td><td>Gives the options fees to get from other chains based on given options fees.</td><td>optionsFees</td><td>uint256</td></tr><tr><td>getCDSDepositDetails</td><td>Gets the cds details for the given address and index, and total index of the address.</td><td>depositor, index</td><td>address, uint64</td></tr><tr><td>getTotalCdsDepositedAmount</td><td>Get the total CDS deposited amount.</td><td></td><td></td></tr><tr><td>getTotalCdsDepositedAmountWithOptionsFees</td><td>Get the total CDS deposited amount with options fees.</td><td></td><td></td></tr></tbody></table>


---

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