{
    "Function": "getSwapIn",
    "File": "src/LBRouter.sol",
    "Parent Contracts": [
        "src/interfaces/ILBRouter.sol"
    ],
    "High-Level Calls": [
        "FeeHelper",
        "BinHelper",
        "ILBPair",
        "FeeHelper",
        "ILBPair",
        "Math512Bits",
        "ILBPair",
        "FeeHelper",
        "ILBPair",
        "Math512Bits"
    ],
    "Internal Calls": [
        "revert LBRouter__SwapOverflows(uint256)",
        "revert LBRouter__WrongAmounts(uint256,uint256)",
        "revert LBRouter__BrokenSwapSafetyCheck()",
        "revert LBRouter__WrongAmounts(uint256,uint256)"
    ],
    "Library Calls": [
        "shiftDivRoundUp",
        "mulShiftRoundUp",
        "getFeeAmount",
        "updateVariableFeeParameters",
        "getPriceFromId",
        "updateVolatilityAccumulated"
    ],
    "Low-Level Calls": [],
    "Code": "function getSwapIn(\n        ILBPair _LBPair,\n        uint256 _amountOut,\n        bool _swapForY\n    ) public view override returns (uint256 amountIn, uint256 feesIn) {\n        (uint256 _pairReserveX, uint256 _pairReserveY, uint256 _activeId) = _LBPair.getReservesAndId();\n\n        if (_amountOut == 0 || (_swapForY ? _amountOut > _pairReserveY : _amountOut > _pairReserveX))\n            revert LBRouter__WrongAmounts(_amountOut, _swapForY ? _pairReserveY : _pairReserveX); // If this is wrong, then we're sure the amounts sent are wrong\n\n        FeeHelper.FeeParameters memory _fp = _LBPair.feeParameters();\n        _fp.updateVariableFeeParameters(_activeId);\n\n        uint256 _amountOutOfBin;\n        uint256 _amountInWithFees;\n        uint256 _reserve;\n        // Performs the actual swap, bin per bin\n        // It uses the findFirstNonEmptyBinId function to make sure the bin we're currently looking at\n        // has liquidity in it.\n        while (true) {\n            {\n                (uint256 _reserveX, uint256 _reserveY) = _LBPair.getBin(uint24(_activeId));\n                _reserve = _swapForY ? _reserveY : _reserveX;\n            }\n            uint256 _price = BinHelper.getPriceFromId(_activeId, _fp.binStep);\n            if (_reserve != 0) {\n                _amountOutOfBin = _amountOut > _reserve ? _reserve : _amountOut;\n\n                uint256 _amountInToBin = _swapForY\n                    ? _amountOutOfBin.shiftDivRoundUp(Constants.SCALE_OFFSET, _price)\n                    : _price.mulShiftRoundUp(_amountOutOfBin, Constants.SCALE_OFFSET);\n\n                // We update the fee, but we don't store the new volatility reference, volatility accumulated and indexRef to not penalize traders\n                _fp.updateVolatilityAccumulated(_activeId);\n                uint256 _fee = _fp.getFeeAmount(_amountInToBin);\n                _amountInWithFees = _amountInToBin + _fee;\n\n                if (_amountInWithFees + _reserve > type(uint112).max) revert LBRouter__SwapOverflows(_activeId);\n                amountIn += _amountInWithFees;\n                feesIn += _fee;\n                _amountOut -= _amountOutOfBin;\n            }\n\n            if (_amountOut != 0) {\n                _activeId = _LBPair.findFirstNonEmptyBinId(uint24(_activeId), _swapForY);\n            } else {\n                break;\n            }\n        }\n        if (_amountOut != 0) revert LBRouter__BrokenSwapSafetyCheck(); // Safety check, but should never be false as it would have reverted on transfer\n    }"
}