Incorrect amount given as input to `_handleRebalanceIn` when `flashRebalance` is called
The issue occurs in the `flashRebalance` function below :\\n```\\nfunction flashRebalance(\\n    DestinationInfo storage destInfoOut,\\n    DestinationInfo storage destInfoIn,\\n    IERC3156FlashBorrower receiver,\\n    IStrategy.RebalanceParams memory params,\\n    FlashRebalanceParams memory flashParams,\\n    bytes calldata data\\n) external returns (uint256 idle, uint256 debt) {\\n    // rest of code\\n\\n    // Handle increase (shares coming "In", getting underlying from the swapper and trading for new shares)\\n    if (params.amountIn > 0) {\\n        IDestinationVault dvIn = IDestinationVault(params.destinationIn);\\n\\n        // get "before" counts\\n        uint256 tokenInBalanceBefore = IERC20(params.tokenIn).balanceOf(address(this));\\n\\n        // Give control back to the solver so they can make use of the "out" assets\\n        // and get our "in" asset\\n        bytes32 flashResult = receiver.onFlashLoan(msg.sender, params.tokenIn, params.amountIn, 0, data);\\n\\n        // We assume the solver will send us the assets\\n        uint256 tokenInBalanceAfter = IERC20(params.tokenIn).balanceOf(address(this));\\n\\n        // Make sure the call was successful and verify we have at least the assets we think\\n        // we were getting\\n        if (\\n            flashResult != keccak256("ERC3156FlashBorrower.onFlashLoan")\\n                || tokenInBalanceAfter < tokenInBalanceBefore + params.amountIn\\n        ) {\\n            revert Errors.FlashLoanFailed(params.tokenIn, params.amountIn);\\n        }\\n\\n        if (params.tokenIn != address(flashParams.baseAsset)) {\\n            // @audit should be `tokenInBalanceAfter - tokenInBalanceBefore` given to `_handleRebalanceIn`\\n            (uint256 debtDecreaseIn, uint256 debtIncreaseIn) =\\n                _handleRebalanceIn(destInfoIn, dvIn, params.tokenIn, tokenInBalanceAfter);\\n            idleDebtChange.debtDecrease += debtDecreaseIn;\\n            idleDebtChange.debtIncrease += debtIncreaseIn;\\n        } else {\\n            idleDebtChange.idleIncrease += tokenInBalanceAfter - tokenInBalanceBefore;\\n        }\\n    }\\n    // rest of code\\n}\\n```\\n\\nAs we can see from the code above, the function executes a flashloan in order to receive th tokenIn amount which should be the difference between `tokenInBalanceAfter` (balance of the contract after the flashloan) and `tokenInBalanceBefore` (balance of the contract before the flashloan) : `tokenInBalanceAfter` - `tokenInBalanceBefore`.\\nBut when calling the `_handleRebalanceIn` function the wrong deposit amount is given as input, as the total balance `tokenInBalanceAfter` is used instead of the received amount `tokenInBalanceAfter` - tokenInBalanceBefore.\\nBecause the `_handleRebalanceIn` function is supposed to deposit the input amount to the destination vault, this error can result in sending a larger amount of funds to DV then what was intended or this error can cause a DOS of the `flashRebalance` function (due to the insufficient amount error when performing the transfer to DV), all of this will make the rebalance operation fail (or not done correctely) which can have a negative impact on the LMPVault.
Use the correct received tokenIn amount `tokenInBalanceAfter - tokenInBalanceBefore` as input to the `_handleRebalanceIn` function :\\n```\\nfunction flashRebalance(\\n    DestinationInfo storage destInfoOut,\\n    DestinationInfo storage destInfoIn,\\n    IERC3156FlashBorrower receiver,\\n    IStrategy.RebalanceParams memory params,\\n    FlashRebalanceParams memory flashParams,\\n    bytes calldata data\\n) external returns (uint256 idle, uint256 debt) {\\n    // rest of code\\n\\n    // Handle increase (shares coming "In", getting underlying from the swapper and trading for new shares)\\n    if (params.amountIn > 0) {\\n        IDestinationVault dvIn = IDestinationVault(params.destinationIn);\\n\\n        // get "before" counts\\n        uint256 tokenInBalanceBefore = IERC20(params.tokenIn).balanceOf(address(this));\\n\\n        // Give control back to the solver so they can make use of the "out" assets\\n        // and get our "in" asset\\n        bytes32 flashResult = receiver.onFlashLoan(msg.sender, params.tokenIn, params.amountIn, 0, data);\\n\\n        // We assume the solver will send us the assets\\n        uint256 tokenInBalanceAfter = IERC20(params.tokenIn).balanceOf(address(this));\\n\\n        // Make sure the call was successful and verify we have at least the assets we think\\n        // we were getting\\n        if (\\n            flashResult != keccak256("ERC3156FlashBorrower.onFlashLoan")\\n                || tokenInBalanceAfter < tokenInBalanceBefore + params.amountIn\\n        ) {\\n            revert Errors.FlashLoanFailed(params.tokenIn, params.amountIn);\\n        }\\n\\n        if (params.tokenIn != address(flashParams.baseAsset)) {\\n            // @audit Use `tokenInBalanceAfter - tokenInBalanceBefore` as input\\n            (uint256 debtDecreaseIn, uint256 debtIncreaseIn) =\\n                _handleRebalanceIn(destInfoIn, dvIn, params.tokenIn, tokenInBalanceAfter - tokenInBalanceBefore);\\n            idleDebtChange.debtDecrease += debtDecreaseIn;\\n            idleDebtChange.debtIncrease += debtIncreaseIn;\\n        } else {\\n            idleDebtChange.idleIncrease += tokenInBalanceAfter - tokenInBalanceBefore;\\n        }\\n    }\\n    // rest of code\\n}\\n```\\n
See summary
```\\nfunction flashRebalance(\\n    DestinationInfo storage destInfoOut,\\n    DestinationInfo storage destInfoIn,\\n    IERC3156FlashBorrower receiver,\\n    IStrategy.RebalanceParams memory params,\\n    FlashRebalanceParams memory flashParams,\\n    bytes calldata data\\n) external returns (uint256 idle, uint256 debt) {\\n    // rest of code\\n\\n    // Handle increase (shares coming "In", getting underlying from the swapper and trading for new shares)\\n    if (params.amountIn > 0) {\\n        IDestinationVault dvIn = IDestinationVault(params.destinationIn);\\n\\n        // get "before" counts\\n        uint256 tokenInBalanceBefore = IERC20(params.tokenIn).balanceOf(address(this));\\n\\n        // Give control back to the solver so they can make use of the "out" assets\\n        // and get our "in" asset\\n        bytes32 flashResult = receiver.onFlashLoan(msg.sender, params.tokenIn, params.amountIn, 0, data);\\n\\n        // We assume the solver will send us the assets\\n        uint256 tokenInBalanceAfter = IERC20(params.tokenIn).balanceOf(address(this));\\n\\n        // Make sure the call was successful and verify we have at least the assets we think\\n        // we were getting\\n        if (\\n            flashResult != keccak256("ERC3156FlashBorrower.onFlashLoan")\\n                || tokenInBalanceAfter < tokenInBalanceBefore + params.amountIn\\n        ) {\\n            revert Errors.FlashLoanFailed(params.tokenIn, params.amountIn);\\n        }\\n\\n        if (params.tokenIn != address(flashParams.baseAsset)) {\\n            // @audit should be `tokenInBalanceAfter - tokenInBalanceBefore` given to `_handleRebalanceIn`\\n            (uint256 debtDecreaseIn, uint256 debtIncreaseIn) =\\n                _handleRebalanceIn(destInfoIn, dvIn, params.tokenIn, tokenInBalanceAfter);\\n            idleDebtChange.debtDecrease += debtDecreaseIn;\\n            idleDebtChange.debtIncrease += debtIncreaseIn;\\n        } else {\\n            idleDebtChange.idleIncrease += tokenInBalanceAfter - tokenInBalanceBefore;\\n        }\\n    }\\n    // rest of code\\n}\\n```\\n