{
    "Function": "findBestPathFromAmountOut",
    "File": "src/LBQuoter.sol",
    "Parent Contracts": [],
    "High-Level Calls": [
        "IJoeFactory",
        "ILBPair",
        "ILBFactory",
        "JoeLibrary",
        "JoeLibrary",
        "ILBPair",
        "ILBRouter"
    ],
    "Internal Calls": [
        "_getV2Quote",
        "revert LBQuoter_InvalidLength()",
        "_getReserves"
    ],
    "Library Calls": [
        "getAmountIn",
        "quote"
    ],
    "Low-Level Calls": [],
    "Code": "function findBestPathFromAmountOut(address[] memory _route, uint256 _amountOut)\n        public\n        view\n        returns (Quote memory quote)\n    {\n        if (_route.length < 2) {\n            revert LBQuoter_InvalidLength();\n        }\n        quote.route = _route;\n\n        uint256 swapLength = _route.length - 1;\n        quote.pairs = new address[](swapLength);\n        quote.binSteps = new uint256[](swapLength);\n        quote.fees = new uint256[](swapLength);\n        quote.amounts = new uint256[](_route.length);\n        quote.virtualAmountsWithoutSlippage = new uint256[](_route.length);\n\n        quote.amounts[swapLength] = _amountOut;\n        quote.virtualAmountsWithoutSlippage[swapLength] = _amountOut;\n\n        for (uint256 i = swapLength; i > 0; i--) {\n            // Fetch swap for V1\n            quote.pairs[i - 1] = IJoeFactory(factoryV1).getPair(_route[i - 1], _route[i]);\n            if (quote.pairs[i - 1] != address(0) && quote.amounts[i] > 0) {\n                (uint256 reserveIn, uint256 reserveOut) = _getReserves(quote.pairs[i - 1], _route[i - 1], _route[i]);\n\n                if (reserveIn > 0 && reserveOut > quote.amounts[i]) {\n                    quote.amounts[i - 1] = JoeLibrary.getAmountIn(quote.amounts[i], reserveIn, reserveOut);\n                    quote.virtualAmountsWithoutSlippage[i - 1] =\n                        (JoeLibrary.quote(quote.virtualAmountsWithoutSlippage[i], reserveOut, reserveIn) * 1000) /\n                        997;\n\n                    quote.fees[i - 1] = 0.003e18; // 0.3%\n                }\n            }\n\n            // Fetch swaps for V2\n            ILBFactory.LBPairInformation[] memory LBPairsAvailable = ILBFactory(factoryV2).getAllLBPairs(\n                IERC20(_route[i - 1]),\n                IERC20(_route[i])\n            );\n\n            if (LBPairsAvailable.length > 0 && quote.amounts[i] > 0) {\n                for (uint256 j; j < LBPairsAvailable.length; j++) {\n                    if (!LBPairsAvailable[j].ignoredForRouting) {\n                        bool swapForY = address(LBPairsAvailable[j].LBPair.tokenY()) == _route[i];\n                        try\n                            ILBRouter(routerV2).getSwapIn(LBPairsAvailable[j].LBPair, quote.amounts[i], swapForY)\n                        returns (uint256 swapAmountIn, uint256 fees) {\n                            if (\n                                swapAmountIn != 0 && (swapAmountIn < quote.amounts[i - 1] || quote.amounts[i - 1] == 0)\n                            ) {\n                                quote.amounts[i - 1] = swapAmountIn;\n                                quote.pairs[i - 1] = address(LBPairsAvailable[j].LBPair);\n                                quote.binSteps[i - 1] = LBPairsAvailable[j].binStep;\n\n                                // Getting current price\n                                (, , uint256 activeId) = LBPairsAvailable[j].LBPair.getReservesAndId();\n                                quote.virtualAmountsWithoutSlippage[i - 1] =\n                                    _getV2Quote(\n                                        quote.virtualAmountsWithoutSlippage[i],\n                                        activeId,\n                                        quote.binSteps[i - 1],\n                                        !swapForY\n                                    ) +\n                                    fees;\n\n                                quote.fees[i - 1] = (fees * 1e18) / quote.amounts[i - 1]; // fee percentage in amountIn\n                            }\n                        } catch {}\n                    }\n                }\n            }\n        }\n    }"
}