Skip to content
Business logic Audit finding Intermediate

VaderRouter.calculateOutGivenIn calculates wrong swap

The VaderRouter.calculateOutGivenIn function for three-path swaps incorrectly performs the first swap in pool1 instead of pool0, mixing up the order of reserves. This causes all such swap calculations to return wrong results, potentially leading to reverted transactions or loss of funds for contracts or scripts relying on the value.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel

The 3-path hop in VaderRouter.calculateOutGivenIn is supposed to first swap foreign assets to native assets in pool0, and then the received native assets to different foreign assets again in pool1.

The first argument of VaderMath.calculateSwap(amountIn, reserveIn, reserveOut) must refer to the same token as the second argument reserveIn. The code however mixes these positions up and first performs a swap in pool1 instead of pool0:

function calculateOutGivenIn(uint256 amountIn, address[] calldata path)
    external
    view
    returns (uint256 amountOut)
{
  if(...) {
  } else {
    return
        VaderMath.calculateSwap(
            VaderMath.calculateSwap(
                // @audit the inner trade should not be in pool1 for a forward swap. amountIn foreign => next param should be foreignReserve0
                amountIn,
                nativeReserve1,
                foreignReserve1
            ),
            foreignReserve0,
            nativeReserve0
        );
  }

 /** @audit instead should first be trading in pool0!
    VaderMath.calculateSwap(
        VaderMath.calculateSwap(
            amountIn,
            foreignReserve0,
            nativeReserve0
        ),
        nativeReserve1,
        foreignReserve1
    );
  */

Impact

All 3-path swaps computations through VaderRouter.calculateOutGivenIn will return the wrong result. Smart contracts or off-chain scripts/frontends that rely on this value to trade will have their transaction reverted, or in the worst case lose funds.

Recommended Mitigation Steps

Return the following code instead which first trades in pool0 and then in pool1:

return
  VaderMath.calculateSwap(
      VaderMath.calculateSwap(
          amountIn,
          foreignReserve0,
          nativeReserve0
      ),
      nativeReserve1,
      foreignReserve1
  );

SamSteinGG (Vader) confirmed

The original report as published on Code4rena, by cmichel. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The arguments to VaderMath.calculateSwap were mixed up, causing the first swap to use pool1's reserves instead of pool0's.

Impact

All three-path swap calculations return wrong results, which could cause transactions to revert or, in the worst case, lose funds for those relying on the value.

What the attacker needed

None

The fix

The function should be fixed to first swap in pool0 and then in pool1, as shown in the recommended code.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

This is a bug in a smart contract function that calculates how many tokens you get when swapping through two pools. The code swaps the pools in the wrong order, so the calculated amount is incorrect. If a program uses this wrong number to make a trade, the trade might fail or lose money.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by cmichel

View all

196 more disclosed reports, best first.

More from Vader Protocol contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.