Decreasing a position without a swap path is susceptible to slippage
Summary
The DecreaseOrderUtils.processOrder function does not check the output amount against the minimum output amount if the order does not have a swap path defined. This could lead to a situation where the user receives fewer tokens than expected, and the provided slippage protection parameter is not respected.
Vulnerability Detail
Decreasing a position and specifying the order swap type SwapCollateralTokenToPnlToken swaps the withdrawn collateral to the PnL token. This swap is performed in DecreasePositionCollateralUtils.swapWithdrawnCollateralToPnlToken while minOutputAmount is set to 0, which means that the swap is not protected against slippage.
Even though users are able to provide a slippage protection parameter in the order (order.minOutputAmount()), this parameter is not used in the DecreaseOrderUtils.processOrder function if the order does not have a swap path (order.swapPath()) defined. This means that users safely assume that their order is protected against slippage, while in this case, it is not.
Impact
Users receive fewer tokens than expected due to slippage even though they provided a slippage protection parameter.
If the order does not have a swap path, the output token is immediately transferred to the receiver. However, the output amount (result.outputAmount) is not checked against the minimum output amount (order.minOutputAmount()) specified in the order. This could lead to a situation where the receiver receives fewer tokens than expected.
As the last step of the DecreasePositionUtils.decreasePosition function, the DecreasePositionCollateralUtils.swapWithdrawnCollateralToPnlToken function is called in line 281 to swap the withdrawn collateral tokens to the PnL token. The swap result is returned.
The DecreasePositionCollateralUtils.swapWithdrawnCollateralToPnlToken function swaps the withdrawn collateral tokens to the PnL token without specifying a minOutputAmount in line 392.
berndartmueller
medium
Decreasing a position without a swap path is susceptible to slippage
Summary
The
DecreaseOrderUtils.processOrder
function does not check the output amount against the minimum output amount if the order does not have a swap path defined. This could lead to a situation where the user receives fewer tokens than expected, and the provided slippage protection parameter is not respected.Vulnerability Detail
Decreasing a position and specifying the order swap type
SwapCollateralTokenToPnlToken
swaps the withdrawn collateral to the PnL token. This swap is performed inDecreasePositionCollateralUtils.swapWithdrawnCollateralToPnlToken
whileminOutputAmount
is set to0
, which means that the swap is not protected against slippage.Even though users are able to provide a slippage protection parameter in the order (
order.minOutputAmount()
), this parameter is not used in theDecreaseOrderUtils.processOrder
function if the order does not have a swap path (order.swapPath()
) defined. This means that users safely assume that their order is protected against slippage, while in this case, it is not.Impact
Users receive fewer tokens than expected due to slippage even though they provided a slippage protection parameter.
Code Snippet
contracts/order/DecreaseOrderUtils.processOrder(..) - L76-L81
If the order does not have a swap path, the output token is immediately transferred to the receiver. However, the output amount (
result.outputAmount
) is not checked against the minimum output amount (order.minOutputAmount()
) specified in the order. This could lead to a situation where the receiver receives fewer tokens than expected.contracts/position/DecreasePositionUtils.decreasePosition(..) - L281
As the last step of the
DecreasePositionUtils.decreasePosition
function, theDecreasePositionCollateralUtils.swapWithdrawnCollateralToPnlToken
function is called in line 281 to swap the withdrawn collateral tokens to the PnL token. The swap result is returned.contracts/position/DecreasePositionCollateralUtils.swapWithdrawnCollateralToPnlToken(..) - L392
The
DecreasePositionCollateralUtils.swapWithdrawnCollateralToPnlToken
function swaps the withdrawn collateral tokens to the PnL token without specifying aminOutputAmount
in line 392.Tool used
Manual Review
Recommendation
Consider adding a check in the
DecreaseOrderUtils.processOrder
function in line 76 to ensureresult.outputAmount > order.minOutputAmount()
.Duplicate of #138