Skip to content

Latest commit

 

History

History
52 lines (33 loc) · 2.39 KB

004.md

File metadata and controls

52 lines (33 loc) · 2.39 KB

Fresh Topaz Flamingo

Medium

Strict inequality in swap validation will cause unnecessary reverts for users

Summary

The execute function implements a swap mechanism where the received amount of tokenOut is validated against a minimum threshold calculated by MASTER.getMinAmountReceived. The comparison currently uses a strict greater-than (>) operator instead of a greater-than-or-equal-to (>=). This causes the function to revert even when the minimum required amount is received, especially when bips is set to 0.

https://github.com/sherlock-audit/2024-11-oku/blob/main/oku-custom-order-types/contracts/automatedTrigger/Bracket.sol#L549-L560

Root Cause

The conditional statement in execute:

require(
    finalTokenOut - initialTokenOut >
        MASTER.getMinAmountReceived(amountIn, tokenIn, tokenOut, bips),
    "Too Little Received"
);

uses a strict greater-than (>) comparison to enforce the minimum amount constraint. Looking at the meaning of the slippageBips variable in the getMinAmountReceived function, it means that when the user wants to convert exactly at exchangeRate, user sets slippageBips to 0. When bips is 0, the getMinAmountReceived function returns the exact calculated fair value. If the received amount is exactly equal to this value, the condition fails, leading to an unnecessary revert.

This issue stems from an incorrect logical assumption that the received amount must exceed the calculated minimum when, in fact, equality should suffice.

Impact

  • Unnecessary Reverts: If bips is 0, valid transactions where the received amount matches the expected minimum will revert, resulting in failed swaps.
  • Disruption of Operations: This issue may block swaps in low-slippage or exact-match scenarios, causing inefficiencies in trading systems relying on this function.
  • User Frustration: Users might encounter unexpected failures without a clear understanding of the underlying reason.

Mitigation

Replace the > operator with >= in the require statement within the execute function:

require(
    finalTokenOut - initialTokenOut >=
        MASTER.getMinAmountReceived(amountIn, tokenIn, tokenOut, bips),
    "Too Little Received"
);

This change ensures that swaps proceed when the received amount meets or exceeds the calculated minimum, accommodating scenarios with zero slippage or exact matching.