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