{
    "Function": "slitherConstructorConstantVariables",
    "File": "contracts/libraries/StrategyLibrary.sol",
    "Parent Contracts": [],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "library StrategyLibrary {\n    using SafeMath for uint256;\n    using SignedSafeMath for int256;\n\n    int256 private constant DIVISOR = 1000;\n\n    function getExpectedTokenValue(\n        uint256 total,\n        address strategy,\n        address token\n    ) public view returns (int256) {\n        int256 percentage = IStrategy(strategy).getPercentage(token);\n        if (percentage == 0) return 0;\n        return int256(total).mul(percentage).div(DIVISOR);\n    }\n\n    function getRange(int256 expectedValue, uint256 threshold) public pure returns (int256) {\n        if (threshold == 0) return 0;\n        return expectedValue.mul(int256(threshold)).div(DIVISOR);\n    }\n\n    /**\n     * @notice This function gets the strategy value from the oracle and checks\n     *         whether the strategy is balanced. Necessary to confirm the balance\n     *         before and after a rebalance to ensure nothing fishy happened\n     */\n    function verifyBalance(address strategy, address oracle) public view returns (bool, uint256, int256[] memory) {\n        (uint256 total, int256[] memory estimates) =\n            IOracle(oracle).estimateStrategy(IStrategy(strategy));\n        uint256 threshold = IStrategy(strategy).rebalanceThreshold();\n\n        bool balanced = true;\n        address[] memory strategyItems = IStrategy(strategy).items();\n        for (uint256 i = 0; i < strategyItems.length; i++) {\n            int256 expectedValue = getExpectedTokenValue(total, strategy, strategyItems[i]);\n            if (expectedValue > 0) {\n                int256 rebalanceRange = getRange(expectedValue, threshold);\n                if (estimates[i] > expectedValue.add(rebalanceRange)) {\n                    balanced = false;\n                    break;\n                }\n                if (estimates[i] < expectedValue.sub(rebalanceRange)) {\n                    balanced = false;\n                    break;\n                }\n            } else {\n                // Token has an expected value of 0, so any value can cause the contract\n                // to be 'unbalanced' so we need an alternative way to determine balance.\n                // Min percent = 0.1%. If token value is above, consider it unbalanced\n                if (estimates[i] > getRange(int256(total), 1)) {\n                    balanced = false;\n                    break;\n                }\n            }\n        }\n        if (balanced) {\n            address[] memory strategyDebt = IStrategy(strategy).debt();\n            for (uint256 i = 0; i < strategyDebt.length; i++) {\n              int256 expectedValue = getExpectedTokenValue(total, strategy, strategyDebt[i]);\n              int256 rebalanceRange = getRange(expectedValue, threshold);\n              uint256 index = strategyItems.length + i;\n               // Debt\n               if (estimates[index] < expectedValue.add(rebalanceRange)) {\n                   balanced = false;\n                   break;\n               }\n               if (estimates[index] > expectedValue.sub(rebalanceRange)) {\n                   balanced = false;\n                   break;\n               }\n            }\n        }\n        return (balanced, total, estimates);\n    }\n\n    /**\n     * @notice This function gets the strategy value from the oracle and determines\n     *         how out of balance the strategy using an absolute value.\n     */\n    function amountOutOfBalance(address strategy, uint256 total, int256[] memory estimates) public view returns (uint256) {\n        if (total == 0) return 0;\n        uint256 amount = 0;\n        address[] memory strategyItems = IStrategy(strategy).items();\n        for (uint256 i = 0; i < strategyItems.length; i++) {\n            int256 expectedValue = getExpectedTokenValue(total, strategy, strategyItems[i]);\n            if (estimates[i] > expectedValue) {\n                amount = amount.add(uint256(estimates[i].sub(expectedValue)));\n            } else if (estimates[i] < expectedValue) {\n                amount = amount.add(uint256(expectedValue.sub(estimates[i])));\n            }\n        }\n        address[] memory strategyDebt = IStrategy(strategy).debt();\n        for (uint256 i = 0; i < strategyDebt.length; i++) {\n            int256 expectedValue = getExpectedTokenValue(total, strategy, strategyDebt[i]);\n            uint256 index = strategyItems.length + i;\n            if (estimates[index] > expectedValue) {\n                amount = amount.add(uint256(estimates[index].sub(expectedValue)));\n            } else if (estimates[index] < expectedValue) {\n                amount = amount.add(uint256(expectedValue.sub(estimates[index])));\n            }\n        }\n        return (amount.mul(10**18).div(total));\n    }\n\n    function checkBalance(address strategy, uint256 balanceBefore, uint256 total, int256[] memory estimates) public view {\n        uint256 balanceAfter = amountOutOfBalance(strategy, total, estimates);\n        if (balanceAfter > uint256(10**18).mul(IStrategy(strategy).rebalanceThreshold()).div(uint256(DIVISOR)))\n            require(balanceAfter <= balanceBefore, \"Lost balance\");\n    }\n}"
}