contract CompoundSafetyRatio is Exponential, DSMath {
    // solhint-disable-next-line const-name-snakecase
    ComptrollerInterface public constant comp = ComptrollerInterface(0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B);

    /// @notice Calcualted the ratio of debt / adjusted collateral
    /// @param _user Address of the user
    function getSafetyRatio(address _user) public view returns (uint) {
        // For each asset the account is in
        address[] memory assets = comp.getAssetsIn(_user);
        address oracleAddr = comp.oracle();


        uint sumCollateral = 0;
        uint sumBorrow = 0;

        for (uint i = 0; i < assets.length; i++) {
            address asset = assets[i];

            (, uint cTokenBalance, uint borrowBalance, uint exchangeRateMantissa)
                                        = CTokenInterface(asset).getAccountSnapshot(_user);

            Exp memory oraclePrice;

            if (cTokenBalance != 0 || borrowBalance != 0) {
                oraclePrice = Exp({mantissa: CompoundOracleInterface(oracleAddr).getUnderlyingPrice(asset)});
            }

            // Sum up collateral in Usd
            if (cTokenBalance != 0) {

                (, uint collFactorMantissa) = comp.markets(address(asset));

                Exp memory collateralFactor = Exp({mantissa: collFactorMantissa});
                Exp memory exchangeRate = Exp({mantissa: exchangeRateMantissa});

                (, Exp memory tokensToUsd) = mulExp3(collateralFactor, exchangeRate, oraclePrice);

                (, sumCollateral) = mulScalarTruncateAddUInt(tokensToUsd, cTokenBalance, sumCollateral);
            }

            // Sum up debt in Usd
            if (borrowBalance != 0) {
                (, sumBorrow) = mulScalarTruncateAddUInt(oraclePrice, borrowBalance, sumBorrow);
            }
        }

        if (sumBorrow == 0) return uint(-1);

        uint borrowPowerUsed = (sumBorrow * 10**18) / sumCollateral;
        return wdiv(1e18, borrowPowerUsed);
    }
}
function getSafetyRatio(address _user) public view returns (uint) {
    // For each asset the account is in
    address[] memory assets = comp.getAssetsIn(_user);
    address oracleAddr = comp.oracle();


    uint sumCollateral = 0;
    uint sumBorrow = 0;

    for (uint i = 0; i < assets.length; i++) {
        address asset = assets[i];

        (, uint cTokenBalance, uint borrowBalance, uint exchangeRateMantissa)
                                    = CTokenInterface(asset).getAccountSnapshot(_user);

        Exp memory oraclePrice;

        if (cTokenBalance != 0 || borrowBalance != 0) {
            oraclePrice = Exp({mantissa: CompoundOracleInterface(oracleAddr).getUnderlyingPrice(asset)});
        }

        // Sum up collateral in Usd
        if (cTokenBalance != 0) {

            (, uint collFactorMantissa) = comp.markets(address(asset));

            Exp memory collateralFactor = Exp({mantissa: collFactorMantissa});
            Exp memory exchangeRate = Exp({mantissa: exchangeRateMantissa});

            (, Exp memory tokensToUsd) = mulExp3(collateralFactor, exchangeRate, oraclePrice);

            (, sumCollateral) = mulScalarTruncateAddUInt(tokensToUsd, cTokenBalance, sumCollateral);
        }

        // Sum up debt in Usd
        if (borrowBalance != 0) {
            (, sumBorrow) = mulScalarTruncateAddUInt(oraclePrice, borrowBalance, sumBorrow);
        }
    }

    if (sumBorrow == 0) return uint(-1);

    uint borrowPowerUsed = (sumBorrow * 10**18) / sumCollateral;
    return wdiv(1e18, borrowPowerUsed);
}

/// @notice Calcualted the ratio of debt / adjusted collateral
/// @param _user Address of the user