{
    "Function": "slitherConstructorConstantVariables",
    "File": "contracts/routers/FullRouter.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 FullRouter is StrategyTypes, StrategyRouter {\n    using SafeMath for uint256;\n    using SignedSafeMath for int256;\n\n    ILendingPoolAddressesProvider public immutable addressesProvider;\n    address public immutable susd;\n    mapping(int256 => mapping(address => mapping(address => int256))) private _tempEstimate;\n\n    constructor(address addressesProvider_, address controller_) public StrategyRouter(RouterCategory.LOOP, controller_) {\n        addressesProvider = ILendingPoolAddressesProvider(addressesProvider_);\n        susd = IStrategyController(controller_).oracle().susd();\n    }\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        address[] memory strategyDebt = IStrategy(strategy).debt();\n        int256[] memory estimates = new int256[](strategyItems.length + strategyDebt.length + 1);\n        _batchBuy(\n          strategy,\n          depositor,\n          amount,\n          estimates,\n          strategyItems,\n          strategyDebt\n        );\n    }\n\n    function withdraw(address strategy, bytes calldata data)\n        external\n        override\n        onlyController\n    {\n        _startTempEstimateSession(strategy);\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        address[] memory strategyItems = IStrategy(strategy).items();\n        // Deleverage debt\n        _deleverageForWithdraw(strategy, strategyItems, estimates, total);\n        // Sell loop\n        for (uint256 i = 0; i < strategyItems.length; i++) {\n            int256 estimatedValue = estimates[i];\n            if (_getTempEstimate(strategy, strategyItems[i]) > 0) {\n                estimatedValue = _getTempEstimate(strategy, strategyItems[i]);\n                _removeTempEstimate(strategy, strategyItems[i]);\n            }\n            int256 expectedValue = StrategyLibrary.getExpectedTokenValue(total, strategy, strategyItems[i]);\n            if (estimatedValue > expectedValue) {\n                _sellPath(\n                    IStrategy(strategy).getTradeData(strategyItems[i]),\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        _startTempEstimateSession(strategy);\n        //_startTempEstimateSession(strategy);\n        (uint256 total, int256[] memory estimates) = abi.decode(data, (uint256, int256[]));\n        address[] memory strategyItems = IStrategy(strategy).items();\n        address[] memory strategyDebt = IStrategy(strategy).debt();\n        // Deleverage debt\n        for (uint256 i = 0; i < strategyDebt.length; i++) {\n            _repayToken(\n                strategy,\n                strategyDebt[i],\n                total,\n                estimates[strategyItems.length + i]\n            );\n        }\n        // Sell loop\n        int256[] memory buy = new int256[](strategyItems.length);\n        for (uint256 i = 0; i < strategyItems.length; i++) {\n            address strategyItem = strategyItems[i];\n            int256 estimate = estimates[i];\n            if (_getTempEstimate(strategy, strategyItem) > 0) {\n                estimate = _getTempEstimate(strategy, strategyItem);\n                _removeTempEstimate(strategy, strategyItem);\n            }\n            int256 expected = StrategyLibrary.getExpectedTokenValue(total, strategy, strategyItem);\n            if (!_sellToken(\n                    strategy,\n                    strategyItem,\n                    estimate,\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                address strategyItem = strategyItems[i];\n                int256 expected = buy[i];\n                _buyToken(\n                    strategy,\n                    strategy,\n                    strategyItem,\n                    estimates[i],\n                    expected\n                );\n            }\n        }\n        if (IStrategy(strategy).supportsSynths()) _batchBuySynths(strategy, total);\n        // Leverage debt\n        for (uint256 i = 0; i < strategyDebt.length; i++) {\n            _borrowToken(\n                strategy,\n                strategyDebt[i],\n                total,\n                estimates[strategyItems.length + i]\n            );\n        }\n    }\n\n    function restructure(address strategy, bytes calldata data)\n        external\n        override\n        onlyController\n    {\n        _startTempEstimateSession(strategy);\n        (\n          uint256 currentTotal,\n          int256[] memory currentEstimates,\n          address[] memory currentItems,\n          address[] memory currentDebt\n        ) = abi.decode(data, (uint256, int256[], address[], address[]));\n\n        _batchSell(strategy, currentTotal, currentEstimates, currentItems, currentDebt);\n        (uint256 newTotal, int256[] memory newEstimates) = IOracle(IStrategy(strategy).oracle()).estimateStrategy(IStrategy(strategy));\n        address[] memory newItems = IStrategy(strategy).items();\n        address[] memory newDebt = IStrategy(strategy).debt();\n        _batchBuy(strategy, strategy, newTotal, newEstimates, newItems, newDebt);\n    }\n\n    function _batchSell(\n        address strategy,\n        uint256 total,\n        int256[] memory estimates,\n        address[] memory strategyItems,\n        address[] memory strategyDebt\n    ) internal {\n        for (uint256 i = 0; i < strategyDebt.length; i++) {\n            int256 estimate = estimates[strategyItems.length + i];\n            //Repay all debt that has 0 percentage\n            if (IStrategy(strategy).getPercentage(strategyDebt[i]) == 0) {\n                _repayPath(\n                    IStrategy(strategy).getTradeData(strategyDebt[i]),\n                    uint256(-estimate),\n                    total,\n                    strategy\n                );\n            } else {\n                //Only repay if above rebalance threshold\n                _repayToken(\n                    strategy,\n                    strategyDebt[i],\n                    total,\n                    estimate\n                );\n            }\n        }\n        for (uint256 i = 0; i < strategyItems.length; i++) {\n            // Convert funds into Ether\n            address strategyItem = strategyItems[i];\n            int256 estimate = estimates[i];\n            if (_getTempEstimate(strategy, strategyItem) > 0) {\n                estimate = _getTempEstimate(strategy, strategyItem);\n                _removeTempEstimate(strategy, strategyItem);\n            }\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                    estimate,\n                    StrategyLibrary.getExpectedTokenValue(total, strategy, strategyItem)\n                );\n            }\n        }\n        if (IStrategy(strategy).supportsSynths()) {\n            // Sell SUSD\n            _sellToken(\n                strategy,\n                susd,\n                estimates[estimates.length - 1], // Virtual item always at end of estimates\n                StrategyLibrary.getExpectedTokenValue(total, strategy, address(-1))\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        address[] memory strategyDebt\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        if (IStrategy(strategy).supportsSynths()) {\n            // Purchase SUSD\n            uint256 susdBalanceBefore = from == strategy ? 0 : IERC20(susd).balanceOf(strategy); // If from strategy it is rebalance or restructure, we want to use all SUSD\n            _buyToken(\n                strategy,\n                from,\n                susd,\n                estimates[estimates.length - 1],\n                StrategyLibrary.getExpectedTokenValue(total, strategy, address(-1))\n            );\n            uint256 susdBalanceAfter = IERC20(susd).balanceOf(strategy);\n            _batchBuySynths(strategy, susdBalanceAfter.sub(susdBalanceBefore));\n        }\n        for (uint256 i = 0; i < strategyDebt.length; i++) {\n            _borrowToken(\n                strategy,\n                strategyDebt[i],\n                total,\n                estimates[strategyItems.length + i]\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 _batchBuySynths(address strategy, uint256 susdBalance) internal {\n        // Use SUSD to purchase other synths\n        uint256 virtualPercentage = uint256(IStrategy(strategy).getPercentage(address(-1)));\n        address[] memory synths = IStrategy(strategy).synths();\n        for (uint256 i = 0; i < synths.length; i++) {\n            uint256 percentage = uint256(IStrategy(strategy).getPercentage(synths[i]));\n            if (percentage != 0) {\n                uint256 amount = susdBalance.mul(percentage).div(virtualPercentage);\n                _delegateSwap(\n                    IStrategy(strategy).getTradeData(synths[i]).adapters[0], // Assuming that synth only stores single SythetixAdapter\n                    amount,\n                    1,\n                    susd,\n                    synths[i],\n                    strategy,\n                    strategy\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            _sellPath(\n                IStrategy(strategy).getTradeData(token),\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        // Note: it is possible for a restructure to have an estimated value of zero,\n        // but only if it's expected value is also zero, in which case this function\n        // will end without making a purchase. So it is safe to set `isDeposit` this way\n        bool isDeposit = estimatedValue == 0;\n        if (isDeposit) {\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            TradeData memory tradeData = IStrategy(strategy).getTradeData(token);\n            if (tradeData.cache.length > 0) {\n                //Apply multiplier\n                uint16 multiplier = abi.decode(tradeData.cache, (uint16));\n                amount = amount.mul(int256(multiplier)).div(int256(DIVISOR));\n            }\n            uint256 balance = IERC20(weth).balanceOf(from);\n            _buyPath(\n                tradeData,\n                uint256(amount) > balance ? balance : uint256(amount),\n                token,\n                strategy,\n                from\n            );\n        }\n    }\n\n    function _repayToken(\n        address strategy,\n        address token,\n        uint256 total,\n        int256 estimatedValue\n    ) internal {\n        int256 expectedValue = StrategyLibrary.getExpectedTokenValue(total, strategy, token);\n        int256 rebalanceRange =\n            StrategyLibrary.getRange(\n                expectedValue,\n                IStrategy(strategy).rebalanceThreshold()\n            );\n        TradeData memory tradeData = IStrategy(strategy).getTradeData(token);\n        // We still call _repayPath even if amountInWeth == 0 because we need to check if leveraged tokens need to be deleveraged\n        uint256 amountInWeth = estimatedValue < expectedValue.add(rebalanceRange) ? uint256(-estimatedValue.sub(expectedValue)) : 0;\n        _repayPath(\n            tradeData,\n            amountInWeth,\n            total,\n            strategy\n        );\n    }\n\n    function _borrowToken(\n        address strategy,\n        address token,\n        uint256 total,\n        int256 estimatedValue\n    ) internal {\n        int256 expectedValue = StrategyLibrary.getExpectedTokenValue(total, strategy, token);\n        int256 amountInWeth;\n        bool isDeposit = estimatedValue == 0;\n        if (isDeposit) {\n            amountInWeth = expectedValue;\n        } else {\n            int256 rebalanceRange =\n                StrategyLibrary.getRange(\n                    expectedValue,\n                    IStrategy(strategy).rebalanceThreshold()\n                );\n            if (estimatedValue > expectedValue.sub(rebalanceRange)) {\n                amountInWeth = expectedValue.sub(estimatedValue);\n            }\n        }\n        if (amountInWeth < 0) {\n            TradeData memory tradeData = IStrategy(strategy).getTradeData(token);\n            _borrowPath(\n                tradeData,\n                uint256(-amountInWeth),\n                total,\n                strategy,\n                isDeposit\n            );\n        }\n    }\n\n    function _repayPath(\n        TradeData memory data,\n        uint256 amount, // weth\n        uint256 total,\n        address strategy\n    ) internal {\n        if (amount == 0 && (data.path[data.path.length-1] != weth || data.cache.length == 0)) return; // Debt doesn't need to change and no leverage tokens to deleverage so return\n        // Debt trade paths should have path.length == adapters.length,\n        // since final token can differ from the debt token defined in the strategy\n        require(data.adapters.length == data.path.length, \"Incorrect trade data\");\n        ILendingPool lendingPool = ILendingPool(addressesProvider.getLendingPool());\n        IOracle oracle = controller.oracle();\n        LeverageItem[] memory leverageItems;\n        uint256[] memory leverageLiquidity;\n\n        if (data.path[data.path.length-1] != weth) {\n            // Convert amount into the first token's currency\n            amount = amount.mul(10**18).div(uint256(oracle.estimateItem(10**18, data.path[data.path.length-1])));\n        } else if (data.cache.length > 0) {\n            // Deleverage tokens\n            leverageItems = abi.decode(data.cache, (LeverageItem[]));\n            leverageLiquidity = new uint256[](leverageItems.length);\n            if (amount == 0) {\n                // Special case where debt doesn't need to change but the relative amounts of leverage tokens do. We must first deleverage our debt\n                for (uint256 i = 0; i < leverageItems.length; i++) {\n                    leverageLiquidity[i] = _getLeverageRemaining(oracle, strategy, leverageItems[i].token, total, false);\n                    amount = amount.add(leverageLiquidity[i]);\n                }\n            } else {\n                uint256 leverageAmount = amount; // amount is denominated in weth here\n                for (uint256 i = 0; i < leverageItems.length; i++) {\n                    address token = leverageItems[i].token;\n                    if (leverageItems.length > 1) { //If multiple leveraged items, some may have less liquidity than the total amount we need to sell\n                        uint256 liquidity = _getLeverageRemaining(oracle, strategy, token, total, false);\n                        leverageLiquidity[i] = leverageAmount > liquidity ? liquidity : leverageAmount;\n                    } else {\n                        leverageLiquidity[i] = leverageAmount;\n                        _setTempEstimate(strategy, token, oracle.estimateItem(\n                          IERC20(token).balanceOf(strategy),\n                          token\n                        ));\n                    }\n                    leverageAmount = leverageAmount.sub(leverageLiquidity[i]);\n                }\n                assert(leverageAmount == 0);\n            }\n        }\n\n        while (amount > 0) {\n            if (leverageItems.length > 0) {\n                // Leverage tokens: cache can contain an array of tokens that can be purchased with the WETH received from selling debt\n                ( , , uint256 availableBorrowsETH, , , ) = lendingPool.getUserAccountData(strategy);\n                bool isLiquidityRemaining = false;\n                for (uint256 i = 0; i < leverageItems.length; i++) {\n                    if (leverageLiquidity[i] > 0 && availableBorrowsETH > 0) {\n                        // Only deleverage token when there is a disparity between the expected value and the estimated value\n                        uint256 leverageAmount = _deleverage(oracle, strategy, leverageItems[i].token, leverageLiquidity[i], availableBorrowsETH);\n                        leverageLiquidity[i] = leverageLiquidity[i].sub(leverageAmount);\n                        availableBorrowsETH = availableBorrowsETH.sub(leverageAmount);\n                        if (leverageLiquidity[i] > 0) isLiquidityRemaining = true; // Liquidity still remaining\n                    }\n                }\n                if (!isLiquidityRemaining) {\n                    // In case of deleveraging slippage, once we've fully deleveraged we just want use the weth the we've received even if its less than original amount\n                    uint256 balance = IERC20(weth).balanceOf(strategy);\n                    if (amount > balance) amount = balance;\n                }\n            }\n            for (int256 i = int256(data.adapters.length-1); i >= 0; i--) { //this doesn't work with uint256?? wtf solidity\n                uint256 _amount;\n                address _tokenIn = data.path[uint256(i)];\n                address _tokenOut;\n                address _from;\n                address _to;\n\n                if (uint256(i) == data.adapters.length-1) {\n                    uint256 balance = IERC20(_tokenIn).balanceOf(strategy);\n                    _amount = balance > amount ? amount : balance;\n                    _from = strategy;\n                    //Update amounts\n                    amount = amount.sub(_amount);\n                } else {\n                    _from = address(this);\n                    _amount = IERC20(_tokenIn).balanceOf(_from);\n                }\n                if (_amount > 0) {\n                    if (uint256(i) == 0) {\n                        _tokenOut = address(0); //Since we're repaying to the lending pool we'll set tokenOut to zero, however amount is valued in weth\n                        _to = strategy;\n                    } else {\n                        _tokenOut = data.path[uint256(i-1)];\n                        _to = address(this);\n                    }\n                    _delegateSwap(\n                        data.adapters[uint256(i)],\n                        _amount,\n                        1,\n                        _tokenIn,\n                        _tokenOut,\n                        _from,\n                        _to\n                    );\n                }\n            }\n        }\n    }\n\n    function _borrowPath(\n        TradeData memory data,\n        uint256 amount, // weth\n        uint256 total,\n        address strategy,\n        bool isDeposit\n    ) internal {\n        // Debt trade paths should have path.length == adapters.length,\n        // since final token can differ from the debt token defined in the strategy\n        require(data.adapters.length == data.path.length, \"Incorrect trade data\");\n        ILendingPool lendingPool = ILendingPool(addressesProvider.getLendingPool());\n        LeverageItem[] memory leverageItems;\n        uint256[] memory leverageLiquidity;\n\n        if (data.path[data.path.length-1] == weth && data.cache.length > 0) {\n            leverageItems = abi.decode(data.cache, (LeverageItem[]));\n            leverageLiquidity = new uint256[](leverageItems.length);\n            if (isDeposit) {\n              for (uint256 i = 0; i < leverageItems.length; i++) {\n                  leverageLiquidity[i] = _getLeveragePercentage(strategy, leverageItems[i].token, leverageItems[i].percentage, total);\n              }\n            } else {\n              IOracle oracle = controller.oracle();\n              for (uint256 i = 0; i < leverageItems.length; i++) {\n                  leverageLiquidity[i] = _getLeverageRemaining(oracle, strategy, leverageItems[i].token, total, true);\n              }\n            }\n        }\n\n        while (amount > 0) { //First loop must either borrow the entire amount or add more tokens as collateral in order to borrow more on following loops\n            ( , , uint256 availableBorrowsETH, , , ) = lendingPool.getUserAccountData(strategy);\n            for (uint256 i = 0; i < data.adapters.length; i++) {\n                uint256 _amount;\n                address _tokenIn;\n                address _tokenOut = data.path[i];\n                address _from;\n                address _to;\n                if (i == 0) {\n                    _tokenIn = address(0); //Since we are withdrawing from lendingPool's collateral reserves, we can set tokenIn to zero. However, amount will be valued in weth\n                    _amount = availableBorrowsETH > amount ? amount : availableBorrowsETH;\n                    _from = strategy;\n                    //Update amount\n                    amount = amount.sub(_amount);\n                } else {\n                    _tokenIn = data.path[i-1];\n                    _from = address(this);\n                    _amount = IERC20(_tokenIn).balanceOf(_from);\n                }\n                if (_amount > 0) {\n                    if (i == data.adapters.length-1 && leverageItems.length == 0) {\n                        _to = strategy;\n                    } else {\n                        _to = address(this);\n                    }\n                    _delegateSwap(\n                        data.adapters[i],\n                        _amount,\n                        1,\n                        _tokenIn,\n                        _tokenOut,\n                        _from,\n                        _to\n                    );\n                }\n            }\n\n            if (leverageItems.length > 0) {\n                // Leverage tokens: cache can contain an array of tokens that can be purchased with the WETH received from selling debt\n                // Only purchase token when there is a disparity between the expected value and the estimated value\n                for (uint256 i = 0; i < leverageItems.length; i++) {\n                    // Since we're inside a while loop, the last item will be when `amount` == 0\n                    bool lastItem = amount == 0 && i == leverageItems.length - 1;\n                    if (leverageLiquidity[i] > 0 || lastItem) {\n                        uint256 leverageAmount = _leverage(strategy, leverageItems[i].token, leverageLiquidity[i], lastItem);\n                        if (leverageAmount > leverageLiquidity[i]) {\n                            // Sometimes we may pay more than needed such as when we reach the lastItem\n                            // and we use the remaining weth (rather than leave it in this contract) so\n                            // just set to zero\n                            leverageLiquidity[i] = 0;\n                        } else {\n                            // If leverageLiqudity remains, it means there wasn't enough weth to reach\n                            // the expected amount, the remained will be handled on subsequent loops of\n                            // the parent while loop\n                            leverageLiquidity[i] = leverageLiquidity[i].sub(leverageAmount);\n                        }\n                    }\n                }\n            }\n        }\n    }\n\n    function _getLeveragePercentage(\n      address strategy,\n      address leverageItem,\n      uint256 leveragePercentage,\n      uint256 total\n    ) internal view returns (uint256) {\n      int256 expected = StrategyLibrary.getExpectedTokenValue(total, strategy, leverageItem);\n      return uint256(expected).mul(leveragePercentage).div(DIVISOR);\n    }\n\n    function _getLeverageRemaining(\n        IOracle oracle,\n        address strategy,\n        address leverageItem,\n        uint256 total,\n        bool isLeveraging\n    ) internal returns (uint256) {\n        int256 expected = StrategyLibrary.getExpectedTokenValue(total, strategy, leverageItem);\n        int256 estimate = oracle.estimateItem(\n            IERC20(leverageItem).balanceOf(strategy),\n            leverageItem\n        );\n        if (isLeveraging) {\n            if (expected > estimate) return uint256(expected.sub(estimate));\n        } else {\n            _setTempEstimate(strategy, leverageItem, estimate); // Store this value for _deleverage()\n            if (estimate > expected) return uint256(estimate.sub(expected));\n        }\n        return 0;\n    }\n\n    function _leverage(\n        address strategy,\n        address leverageItem,\n        uint256 leverageLiquidity,\n        bool lastItem\n    ) internal returns (uint256) {\n        uint256 wethBalance = IERC20(weth).balanceOf(address(this));\n        if (wethBalance > 0) {\n            uint256 leverageAmount;\n            if (lastItem) {\n                // If it is the last item being leveraged, use all remaining weth\n                leverageAmount = wethBalance;\n            } else {\n                leverageAmount = leverageLiquidity > wethBalance ? wethBalance : leverageLiquidity;\n            }\n            _buyPath(\n                IStrategy(strategy).getTradeData(leverageItem),\n                leverageAmount,\n                leverageItem,\n                strategy,\n                address(this)\n            );\n            return leverageAmount;\n        }\n    }\n\n    function _deleverage(\n        IOracle oracle,\n        address strategy,\n        address leverageItem,\n        uint256 leverageLiquidity,\n        uint256 available\n    ) internal returns (uint256) {\n        uint256 leverageAmount = leverageLiquidity > available ? available : leverageLiquidity;\n        uint256 leverageEstimate = uint256(_getTempEstimate(strategy, leverageItem)); //Set in _getLeverageRemaining\n        require(leverageEstimate > 0, \"Insufficient collateral\");\n        _sellPath(\n            IStrategy(strategy).getTradeData(leverageItem),\n            _estimateSellAmount(strategy, leverageItem, leverageAmount, leverageEstimate),\n            leverageItem,\n            strategy\n        );\n        // Update temp estimates with new value since tokens have been sold (it will be needed on later sell loops)\n        _setTempEstimate(strategy, leverageItem, oracle.estimateItem(\n            IERC20(leverageItem).balanceOf(strategy),\n            leverageItem\n        ));\n        return leverageAmount;\n    }\n\n    function _deleverageForWithdraw(address strategy, address[] memory strategyItems, int256[] memory estimates, uint256 total) private {\n        address[] memory strategyDebt = IStrategy(strategy).debt();\n        // Deleverage debt\n        for (uint256 i = 0; i < strategyDebt.length; i++) {\n            int256 estimatedValue = estimates[strategyItems.length + i];\n            int256 expectedValue = StrategyLibrary.getExpectedTokenValue(total, strategy, strategyDebt[i]);\n            if (estimatedValue < expectedValue) {\n                _repayPath(\n                    IStrategy(strategy).getTradeData(strategyDebt[i]),\n                    uint256(-estimatedValue.sub(expectedValue)),\n                    total,\n                    strategy\n                );\n            }\n        }\n    }\n\n    function _startTempEstimateSession(address strategy) private {\n        /*\n          To ensure that a stale \"temp\" estimate isn't leaked into other function calls\n          by not being \"delete\"d in the same external call in which it is set, we\n          associate to each external call a \"session counter\" so that it only deals with\n          temp values corresponding to its own session.\n        **/\n\n        ++_tempEstimate[0][strategy][address(0)]; // ++counter\n    }\n\n    function _getCurrentTempEstimateSession(address strategy) private view returns(int256) {\n        return _tempEstimate[0][strategy][address(0)]; // counter\n    }\n\n    function _setTempEstimate(address strategy, address item, int256 value) private {\n        int256 session = _getCurrentTempEstimateSession(strategy);\n        _tempEstimate[session][strategy][item] = value;\n    }\n\n    function _getTempEstimate(address strategy, address item) private view returns(int256) {\n        int256 session = _getCurrentTempEstimateSession(strategy);\n        return _tempEstimate[session][strategy][item];\n    }\n\n    function _removeTempEstimate(address strategy, address item) private {\n        int256 session = _getCurrentTempEstimateSession(strategy);\n        delete _tempEstimate[session][strategy][item];\n    }\n\n}"
}