{
    "Function": "slitherConstructorConstantVariables",
    "File": "contracts/routers/LoopRouter.sol",
    "Parent Contracts": [
        "contracts/routers/StrategyRouter.sol",
        "contracts/helpers/StrategyTypes.sol",
        "contracts/interfaces/IStrategyRouter.sol"
    ],
    "High-Level Calls": [],
    "Internal Calls": [],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "contract LoopRouter is StrategyTypes, StrategyRouter {\n\n    constructor(address controller_) public StrategyRouter(RouterCategory.LOOP, controller_) {}\n\n    function deposit(address strategy, bytes calldata data)\n        external\n        override\n        onlyController\n    {\n        (address depositor, uint256 amount) =\n            abi.decode(data, (address, uint256));\n        address[] memory strategyItems = IStrategy(strategy).items();\n        int256[] memory estimates = new int256[](strategyItems.length);\n        _batchBuy(\n          strategy,\n          depositor,\n          amount,\n          estimates,\n          strategyItems\n        );\n    }\n\n    function withdraw(address strategy, bytes calldata data)\n        external\n        override\n        onlyController\n    {\n        (uint256 percentage, uint256 total, int256[] memory estimates) =\n            abi.decode(data, (uint256, uint256, int256[]));\n\n        uint256 expectedWeth = total.mul(percentage).div(10**18);\n        total = total.sub(expectedWeth);\n\n        // Sell loop\n        address[] memory strategyItems = IStrategy(strategy).items();\n        for (uint256 i = 0; i < strategyItems.length; i++) {\n            int256 estimatedValue = estimates[i];\n            int256 expectedValue = StrategyLibrary.getExpectedTokenValue(total, strategy, strategyItems[i]);\n            if (estimatedValue > expectedValue) {\n                TradeData memory tradeData = IStrategy(strategy).getTradeData(strategyItems[i]);\n                _sellPath(\n                    tradeData,\n                    _estimateSellAmount(strategy, strategyItems[i], uint256(estimatedValue.sub(expectedValue)), uint256(estimatedValue)),\n                    strategyItems[i],\n                    strategy\n                );\n            }\n        }\n    }\n\n    function rebalance(address strategy, bytes calldata data) external override onlyController {\n        (uint256 total, int256[] memory estimates) = abi.decode(data, (uint256, int256[]));\n        address[] memory strategyItems = IStrategy(strategy).items();\n        int256[] memory buy = new int256[](strategyItems.length);\n        // Sell loop\n        for (uint256 i = 0; i < strategyItems.length; i++) {\n            int expected = StrategyLibrary.getExpectedTokenValue(total, strategy, strategyItems[i]);\n            if (!_sellToken(\n                    strategy,\n                    strategyItems[i],\n                    estimates[i],\n                    expected \n                )\n            ) buy[i] = expected;\n            // semantic overloading to cache `expected` since it will be used in next loop. \n        }\n        // Buy loop\n        for (uint256 i = 0; i < strategyItems.length; i++) {\n            if (buy[i] != 0) {\n                _buyToken(\n                    strategy,\n                    strategy,\n                    strategyItems[i],\n                    estimates[i],\n                    buy[i]\n                );\n            }\n        }\n    }\n\n    function restructure(address strategy, bytes calldata data)\n        external\n        override\n        onlyController\n    {\n        (\n          uint256 currentTotal,\n          int256[] memory currentEstimates,\n          address[] memory currentItems\n        ) = abi.decode(data, (uint256, int256[], address[]));\n\n        _batchSell(strategy, currentTotal, currentEstimates, currentItems);\n        (uint256 newTotal, int256[] memory newEstimates) = IStrategy(strategy).oracle().estimateStrategy(IStrategy(strategy));\n        address[] memory newItems = IStrategy(strategy).items();\n        _batchBuy(strategy, strategy, newTotal, newEstimates, newItems);\n    }\n\n    function _batchSell(\n        address strategy,\n        uint256 total,\n        int256[] memory estimates,\n        address[] memory strategyItems\n    ) internal {\n        for (uint256 i = 0; i < strategyItems.length; i++) {\n            // Convert funds into Ether\n            address strategyItem = strategyItems[i];\n            if (IStrategy(strategy).getPercentage(strategyItem) == 0) {\n                //Sell all tokens that have 0 percentage\n                _sellPath(\n                    IStrategy(strategy).getTradeData(strategyItem),\n                    IERC20(strategyItem).balanceOf(strategy),\n                    strategyItem,\n                    strategy\n                );\n            } else {\n                //Only sell if above rebalance threshold\n                _sellToken(\n                    strategy,\n                    strategyItem,\n                    estimates[i],\n                    StrategyLibrary.getExpectedTokenValue(total, strategy, strategyItem)\n                );\n            }\n        }\n    }\n\n    function _batchBuy(\n        address strategy,\n        address from,\n        uint256 total,\n        int256[] memory estimates,\n        address[] memory strategyItems\n    ) internal {\n        for (uint256 i = 0; i < strategyItems.length; i++) {\n            address strategyItem = strategyItems[i];\n            _buyToken(\n                strategy,\n                from,\n                strategyItem,\n                estimates[i],\n                StrategyLibrary.getExpectedTokenValue(total, strategy, strategyItem)\n            );\n        }\n        int256 percentage = IStrategy(strategy).getPercentage(weth);\n        if (percentage > 0 && from != strategy) {\n            if (from == address(this)) {\n              // Send all WETH\n              IERC20(weth).safeTransfer(strategy, IERC20(weth).balanceOf(from));\n            } else {\n              // Calculate remaining WETH\n              // Since from is not address(this), we know this is a deposit, so estimated value not relevant\n              uint256 amount =\n                  total.mul(uint256(percentage))\n                       .div(DIVISOR);\n              IERC20(weth).safeTransferFrom(\n                  from,\n                  strategy,\n                  amount\n              );\n            }\n        }\n    }\n\n    function _sellToken(\n        address strategy,\n        address token,\n        int256 estimatedValue,\n        int256 expectedValue\n    ) internal returns (bool) {\n        int256 rebalanceRange =\n            StrategyLibrary.getRange(\n                expectedValue,\n                IStrategy(strategy).rebalanceThreshold()\n            );\n        if (estimatedValue > expectedValue.add(rebalanceRange)) {\n            TradeData memory tradeData = IStrategy(strategy).getTradeData(token);\n            _sellPath(\n                tradeData,\n                _estimateSellAmount(strategy, token, uint256(estimatedValue.sub(expectedValue)), uint256(estimatedValue)),\n                token,\n                strategy\n            );\n            return true;\n        }\n        return false;\n    }\n\n    function _buyToken(\n        address strategy,\n        address from,\n        address token,\n        int256 estimatedValue,\n        int256 expectedValue\n    ) internal {\n        int256 amount;\n        if (estimatedValue == 0) {\n            amount = expectedValue;\n        } else {\n            int256 rebalanceRange =\n                StrategyLibrary.getRange(\n                    expectedValue,\n                    IStrategy(strategy).rebalanceThreshold()\n                );\n            if (estimatedValue < expectedValue.sub(rebalanceRange)) {\n                amount = expectedValue.sub(estimatedValue);\n            }\n        }\n        if (amount > 0) {\n            uint256 balance = IERC20(weth).balanceOf(from);\n            _buyPath(\n                IStrategy(strategy).getTradeData(token),\n                uint256(amount) > balance ? balance : uint256(amount),\n                token,\n                strategy,\n                from\n            );\n        }\n    }\n}"
}