Skip to content

fix: enforce amountOutMin and final profitability on DEX swaps#6

Open
arawrdn wants to merge 1 commit into
Devilla:mainfrom
arawrdn:main
Open

fix: enforce amountOutMin and final profitability on DEX swaps#6
arawrdn wants to merge 1 commit into
Devilla:mainfrom
arawrdn:main

Conversation

@arawrdn
Copy link
Copy Markdown

@arawrdn arawrdn commented May 14, 2026

GM,

Implemented slippage enforcement for all DEX swaps.

Changes:

  • Added _minOut(quotedOut, slippageBps) helper.
  • Replaced amountOutMin = 0 with quoted minimum output.
  • Uses router.getAmountsOut() before each swap.
  • Applies configurable slippage in basis points.
  • Validates slippageBps <= 10_000.
  • Enforces final profitability after execution.

Impact:

  • Prevents swaps from accepting unlimited negative slippage.
  • Protects against stale quotes, MEV, and price movement.
  • Reverts if actual output is below the minimum expected amount.
  • Reverts if final balance does not exceed initialBalance + minProfit.

Example:

uint256[] memory amounts = router.getAmountsOut(amountIn, path);
uint256 minOut = _minOut(amounts[amounts.length - 1], slippageBps);

router.swapExactTokensForTokens(
    amountIn,
    minOut,
    path,
    address(this),
    block.timestamp
);

Profit check:

require(finalBalance > initialBalance + minProfit, "not profitable");

Cheers,

@Devilla
Copy link
Copy Markdown
Owner

Devilla commented May 18, 2026

@arawrdn Please create a separate .sol contract file; don't paste the entire code to the README.md file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants