BorrowLiquidation Logics

Here the logics used in Borrow Liquidation contract and BorrowLib are explained.

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

ETH price ratio

borrowLiquidation.sol
uint128 ratio = BorrowLib.calculateEthPriceRatio(
        depositDetail.ethPriceAtDeposit,
        _currentEthPrice
    );
BorrowLib.sol
function calculateEthPriceRatio(
        uint128 depositEthPrice, 
        uint128 currentEthPrice
    ) public pure returns(uint128){
        return (currentEthPrice * 10000)/depositEthPrice;
    }

It will returns the ratio of current ETH price to the deposit ETH price.

Functions which are using the above logic

Borrower Debt

borrowLiquidation.sol
uint256 borrowerDebt = (
    (depositDetail.normalizedAmount * _lastCumulativeRate)/BorrowLib.RATE_PRECISION
    );

to calculate the borrower's debt we are using this formula. Last cumulative rate we will get from calculateCumulativeRate method.

BorrowLib.RATE_PRECISION is base precision used, since last cumulative rate already have this precision we are dividing it here.

Functions which are using the above logic

Return to ABOND

borrowLiquidation.sol
uint128 returnToAbond = BorrowLib.calculateReturnToAbond(
            depositDetail.depositedAmount,
            depositDetail.ethPriceAtDeposit, 
            returnToTreasury
);
BorrowLib.sol
function calculateReturnToAbond(
        uint128 depositedAmount,
        uint128 depositEthPrice,
        uint128 returnToTreasury
    ) public pure returns(uint128){
        return (
        ((((depositedAmount * depositEthPrice)/USDA_PRECISION)/100) - returnToTreasury) * 10)/100;
    }

here return to treasury is the borrower's debt. USDA_PRECISION is used for decimals, since USDa has 6 decimals only.

Functions which are using the above logic

Liquidation Amount to get from other chain

borrowLiquidation.sol
 uint256 liqAmountToGetFromOtherChain = BorrowLib.getLiquidationAmountProportions(
            liquidationAmountNeeded,
            cds.totalCdsDepositedAmount(),
            cds.omniChainCDSTotalCdsDepositedAmount(),
            cds.totalAvailableLiquidationAmount(),
            cds.omniChainCDSTotalAvailableLiquidationAmount()
        );
BorrowLib.sol
function getLiquidationAmountProportions(
        uint256 _liqAmount,
        uint256 _totalCdsDepositedAmount,
        uint256 _totalGlobalCdsDepositedAmount,
        uint256 _totalAvailableLiqAmount,
        uint256 _totalGlobalAvailableLiqAmountAmount
    ) public pure returns (uint256){

        uint256 otherChainCDSAmount = _totalGlobalCdsDepositedAmount - _totalCdsDepositedAmount;

        uint256 totalAvailableLiqAmountInOtherChain = _totalGlobalAvailableLiqAmountAmount - _totalAvailableLiqAmount;

        uint256 share = (otherChainCDSAmount * LIQ_AMOUNT_PRECISION)/_totalGlobalCdsDepositedAmount;
        uint256 liqAmountToGet = (_liqAmount * share)/LIQ_AMOUNT_PRECISION;
        uint256 liqAmountRemaining = _liqAmount - liqAmountToGet;

        if(totalAvailableLiqAmountInOtherChain == 0){
            liqAmountToGet = 0;
        }else{
            if(totalAvailableLiqAmountInOtherChain < liqAmountToGet) {
                liqAmountToGet = totalAvailableLiqAmountInOtherChain;
            }else{
                if(totalAvailableLiqAmountInOtherChain > liqAmountToGet && _totalAvailableLiqAmount < liqAmountRemaining){
                    liqAmountToGet += liqAmountRemaining - _totalAvailableLiqAmount;
                }else{
                    liqAmountToGet = liqAmountToGet;
                }
            }
        }
        return liqAmountToGet;
    }

First calculate the each chain CDS amount proportions by dividing them with global CDS amount. Based on the proportions, calculate the liquidation amount to get from each chain.

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

Functions which are using the above logic

CDS profits for Destination Chain

borrowLiquidation.sol
uint128 cdsProfitsForOtherChain = BorrowLib.getCdsProfitsProportions(
            liquidationAmountNeeded,
            uint128(liqAmountToGetFromOtherChain),
            cdsProfits
            );
BorrowLib.sol
function getCdsProfitsProportions(
        uint128 _liqAmount,
        uint128 _liqAmountToGetFromOtherChain,
        uint128 _cdsProfits
    ) public pure returns (uint128){

        uint128 share = (_liqAmountToGetFromOtherChain * LIQ_AMOUNT_PRECISION)/_liqAmount;
        uint128 cdsProfitsForOtherChain = (_cdsProfits * share)/LIQ_AMOUNT_PRECISION;

        return cdsProfitsForOtherChain;
    }
Param Name
Type
Description

_liqAmount

uint128

Total Liquidation amount needed.

_liqAmountToGetFromOtherChain

uint128

Proportion of Liquidation amount to get from destination chain in total liquidation amount.

_cdsProfits

uint128

Total CDS profits from liquidation.

Calculate the proportion of CDS amount contributed by each chain. Based on the proportions divide the CDS profits for each chain.

Functions which are using the above logic

Last updated