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