SSSwap

First Flight #41
Beginner FriendlyRust
100 EXP
View results
Submission Details
Impact: low
Likelihood: medium
Invalid

Missing or Incorrect Liquidity Check on Swap Output Amount

Description:

In the swap_exact_in instruction, the code includes a check to ensure that the amount_out does not exceed the available reserve of the output token. However, the current check uses a strict less-than comparison (amount_out < reserve_b), which prevents users from swapping the full reserve amount. Additionally, the check only covers one swap direction (zero_for_one == true) and does not symmetrically validate the other direction (zero_for_one == false). This inconsistency may lead to unexpected swap failures or potential liquidity issues.

Infected Code Snippet:

if zero_for_one {
require!(amount_out < reserve_b, AmmError::InsufficientLiquidity);
} else {
require!(amount_out < reserve_a, AmmError::InsufficientLiquidity);
}

Impact:

The current strict less-than check can reject valid swap transactions where the user intends to swap the entire available reserve, causing a poor user experience or failed transactions. Moreover, the absence of a similar check in the reverse swap direction leaves the contract vulnerable to inconsistent behavior and potential underflow or liquidity errors.

Recommendation:

  • Change the check to use <= (less than or equal) to allow swapping the full available reserve amount.

  • Add symmetric reserve checks for both swap directions as shown below:

if zero_for_one {
require!(amount_out <= reserve_b, AmmError::InsufficientLiquidity);
} else {
require!(amount_out <= reserve_a, AmmError::InsufficientLiquidity);
}
Updates

Lead Judging Commences

0xtimefliez Lead Judge 5 days ago
Submission Judgement Published
Invalidated
Reason: Design choice

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.