{
    "Function": "rebalance",
    "File": "contracts/routers/FullRouter.sol",
    "Parent Contracts": [
        "contracts/routers/StrategyRouter.sol",
        "contracts/helpers/StrategyTypes.sol",
        "contracts/interfaces/IStrategyRouter.sol"
    ],
    "High-Level Calls": [
        "IStrategy",
        "StrategyLibrary",
        "IStrategy",
        "IStrategy"
    ],
    "Internal Calls": [
        "onlyController",
        "abi.decode()",
        "_sellToken",
        "_startTempEstimateSession",
        "_batchBuySynths",
        "_getTempEstimate",
        "_buyToken",
        "_borrowToken",
        "_getTempEstimate",
        "_removeTempEstimate",
        "_repayToken"
    ],
    "Library Calls": [
        "getExpectedTokenValue"
    ],
    "Low-Level Calls": [],
    "Code": "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    }"
}