CDS Logics

Here the logics used in CDS contract and CDSLib are explained.

In code, many base PRECISION are used to avoid, arithmetic, division or modulo by zero errors.

USDT depsoited till now - Logic

CDS.sol
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");
}

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

Options fees to get from each chain

CDSLib.sol
function getOptionsFeesProportions(
        uint256 optionsFees,
        uint256 _totalCdsDepositedAmount,
        uint256 _totalGlobalCdsDepositedAmount,
        uint256 _totalCdsDepositedAmountWithOptionFees,
        uint256 _totalGlobalCdsDepositedAmountWithOptionFees
)

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.

CDS.sol
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);
}
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

USDT to give in redeem - Logic

CDS.sol
function redeemUSDT(
        uint128 _usdaAmount,
        uint64 usdaPrice,
        uint64 usdtPrice
)
_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

Cumulative rate calculation

CDSLib.sol
function calculateCumulativeRate(
        uint128 _fees,
        uint256 _totalCdsDepositedAmount,
        uint256 _totalCdsDepositedAmountWithOptionFees,
        uint256 _totalGlobalCdsDepositedAmountWithOptionFees,
        uint128 _lastCumulativeRate,
        uint128 _noOfBorrowers
)
Param Name
Type
Description

_fees

uint128

Options fees to give to CDS users.

_totalCdsDepositedAmount

uint256

Total CDS amount in this chain.

_totalCdsDepositedAmountWithOptionFees

uint256

Total CDS amount with options fees in this chain.

_totalGlobalCdsDeposited AmountWithOptionFees

uint256

Total CDS amount with options fees in all chain.

_lastCumulativeRate

uint128

Previously stored cumulative rate.

_noOfBorrowers

uint128

Total number of borrowers in protocol.

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,

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

Last updated