This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
TheNaubit - Users trying to reduce their positions with market orders will always revert #120
Labels
Fix Submitted
Fix to the issue has been submitted
Has Duplicates
A valid issue with 1+ other issues describing the same vulnerability
Medium
A valid Medium severity issue
Reward
A payout will be made for this issue
Sponsor Confirmed
The sponsor acknowledged this issue is valid
Will Fix
The sponsor confirmed this issue will be fixed
TheNaubit
high
Users trying to reduce their positions with market orders will always revert
Summary
Reducing the users' positions with market orders will always revert due to a wrong value assigned to the
min_amount_out
variable when swapping part of the funds out.Vulnerability Detail
In the
Vault
contract, there is thereduce_position
function, which is called by the users to reduce their positions. When the function is called, themin_amount_out
variable used by UniswapV3 as a slippage protection is calculated with the following code:When the function receives a
min_amount_out
equal to 0, it means the user wants to reduce the position with a market order (for example, to reduce it right now). But in that case, some slippage is calculated to bring some protection to the swap. That protection is calculated with the function_market_order_min_amount_out
which basically does the following:The important part (for us) in that code is the call to
_quote_token_to_token
, which basically queries the price relation of both tokens and multiply it by the_amount_in
var. But... if we check the original function (the one I wrote at the beginning of this section), the value passed as_amount_in
to_market_order_min_amount_out
is theposition_amount
value instead of the amount of the position to be reduced (contained in the var_reduce_by_amount
).And since in the
_swap
function we are swapping only the_reduce_by_amount
amount andposition_amount
is always greater than_reduce_by_amount
, the_min_amount_out
value that we send in the_swap
function will be always greater than the greatest amount out we could receive, making the_swap
function to always revert thus making thereduce_position
to revert also.Impact
Users won't be able to reduce their positions using market orders, potentially making them to lose funds due to not being able to reduce their positions in the right moment (since they should use non-market orders, which may not be right thing for their situation).
Code Snippet
https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L309-L311
Tool used
Manual Review
Recommendation
Change the code like this:
The text was updated successfully, but these errors were encountered: