Skip to content

Latest commit

 

History

History
346 lines (274 loc) · 8.2 KB

LiquityMath.md

File metadata and controls

346 lines (274 loc) · 8.2 KB

LiquityMath.sol

View Source: contracts/Dependencies/LiquityMath.sol

LiquityMath

Contract Members

Constants & Variables

uint256 internal constant DECIMAL_PRECISION;
uint256 internal constant NICR_PRECISION;

Functions


_min

function _min(uint256 _a, uint256 _b) internal pure
returns(uint256)

Arguments

Name Type Description
_a uint256
_b uint256
Source Code
 _min(uint _a, uint _b) internal pure returns (uint) {
        return (_a < _b) ? _a : _b;
    }

    fu

_max

function _max(uint256 _a, uint256 _b) internal pure
returns(uint256)

Arguments

Name Type Description
_a uint256
_b uint256
Source Code
 _max(uint _a, uint _b) internal pure returns (uint) {
        return (_a >= _b) ? _a : _b;
    }

    /*

decMul

function decMul(uint256 x, uint256 y) internal pure
returns(decProd uint256)

Arguments

Name Type Description
x uint256
y uint256
Source Code
 decMul(uint x, uint y) internal pure returns (uint decProd) {
        uint prod_xy = x.mul(y);

        decProd = prod_xy.add(DECIMAL_PRECISION / 2).div(DECIMAL_PRECISION);
    }

    /*

_decPow

function _decPow(uint256 _base, uint256 _minutes) internal pure
returns(uint256)

Arguments

Name Type Description
_base uint256
_minutes uint256
Source Code
 _decPow(uint _base, uint _minutes) internal pure returns (uint) {

        if (_minutes > 525600000) {_minutes = 525600000;}  // cap to avoid overflow

        if (_minutes == 0) {return DECIMAL_PRECISION;}

        uint y = DECIMAL_PRECISION;
        uint x = _base;
        uint n = _minutes;

        // Exponentiation-by-squaring
        while (n > 1) {
            if (n % 2 == 0) {
                x = decMul(x, x);
                n = n.div(2);
            } else { // if (n % 2 != 0)
                y = decMul(x, y);
                x = decMul(x, x);
                n = (n.sub(1)).div(2);
            }
        }

        return decMul(x, y);
  }

    fu

_getAbsoluteDifference

function _getAbsoluteDifference(uint256 _a, uint256 _b) internal pure
returns(uint256)

Arguments

Name Type Description
_a uint256
_b uint256
Source Code
 _getAbsoluteDifference(uint _a, uint _b) internal pure returns (uint) {
        return (_a >= _b) ? _a.sub(_b) : _b.sub(_a);
    }

    fu

_computeNominalCR

function _computeNominalCR(uint256 _coll, uint256 _debt) internal pure
returns(uint256)

Arguments

Name Type Description
_coll uint256
_debt uint256
Source Code
 _computeNominalCR(uint _coll, uint _debt) internal pure returns (uint) {
        if (_debt > 0) {
            return _coll.mul(NICR_PRECISION).div(_debt);
        }
        // Return the maximal value for uint256 if the Trove has a debt of 0. Represents "infinite" CR.
        else { // if (_debt == 0)
            return 2**256 - 1;
        }
    }

    fu

_computeCR

function _computeCR(uint256 _coll, uint256 _debt, uint256 _price) internal pure
returns(uint256)

Arguments

Name Type Description
_coll uint256
_debt uint256
_price uint256
Source Code
 _computeCR(uint _coll, uint _debt, uint _price) internal pure returns (uint) {
        if (_debt > 0) {
            uint newCollRatio = _coll.mul(_price).div(_debt);

            return newCollRatio;
        }
        // Return the maximal value for uint256 if the Trove has a debt of 0. Represents "infinite" CR.
        else { // if (_debt == 0)
            return 2**256 - 1; 
        }
    }
}

Contracts