Submitted by Jeiwan, also found by KIntern_NA and cccz
https://github.com/code-423n4/2022-10-traderjoe/blob/main/src/LBRouter.sol#L891
https://github.com/code-423n4/2022-10-traderjoe/blob/main/src/LBRouter.sol#L896
Output amount is calculated incorrectly for a Trader Joe V1 pool when swapping tokens across multiple pools and some of the pools in the chain are V1 ones. Calculated amounts will always be smaller than expected ones, which will always affect chained swaps that include V1 pools.
LBRouter is a high-level contract that serves as the main contract users will interact with. The contract implements a lot of security checks and helper functions that make usage of LBPair contracts easier and more user-friendly. Some examples of such functions:
Under the hood, these three functions call _swapSupportingFeeOnTransferTokens, which is the function that actually performs swaps. The function supports both Trader Joe V1 and V2 pools: when _binStep is 0 (which is never true in V2 pools), its assumed that the current pool is a V1 one. For V1 pools, the function calculates output amounts based on pools reserves and balances:
However, these calculations are incorrect. Heres the difference:
These calculations are implemented correctly in JoeLibrary.getAmountOut, which is used in LBQuoter.  Also its used in Trader Joe V1 to calculate output amounts in similar functions:
Consider using the JoeLibrary.getAmountOut function in the _swapSupportingFeeOnTransferTokens function of LBRouter when computing output amounts for V1 pools.
0x0Louis (Trader Joe) confirmed
Alex the Entreprenerd (judge) commented:
