Calculate the normalized amount by dividing the total borrowed amount with cumulative rate. Cumulative rate should get from calculateCumulativeRate method.
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,
Cumulative rate calculation
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.
Here, event represents Deposit, Withdraw and Liquidation.
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.
Calculation of ratio of CDS Pool value to ETH vault value
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.
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,
Add the netPLCdsPool with previous net CDS Pool value and CDS amount deposited after last event.
Finally, calculate the ratio by dividing currentCDSPoolValue by currentEthVaultValue.
function calculateNormAmount(
uint256 amount,
uint256 cumulativeRate
) public pure returns(uint256){
return (amount * RATE_PRECISION)/cumulativeRate;
}
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);
}
// Calculate net P/L of CDS Pool
netPLCdsPool = price difference between current and last ETH price * noOfBorrowers;
previousData.cdsPoolValue = previousData.totalCDSPool + netPLCdsPool;