Skip to content
Business logic Audit finding Intermediate

Wrong design of swap() results in unexpected and unfavorable outputs

The swap formula in VaderMath.sol causes the output amount to decrease when the input amount exceeds the reserve, meaning a user who pays more receives less. An attacker could exploit this by swapping a larger amount to receive a smaller output, potentially causing financial loss or manipulating the pool.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

The current formula to calculate the amountOut for a swap is:

https://github.com/code-423n4/2021-11-vader/blob/429970427b4dc65e37808d7116b9de27e395ce0c/contracts/dex/math/VaderMath.sol#L99-L111

function calculateSwap(
    uint256 amountIn,
    uint256 reserveIn,
    uint256 reserveOut
) public pure returns (uint256 amountOut) {
    // x * Y * X
    uint256 numerator = amountIn * reserveIn * reserveOut;

    // (x + X) ^ 2
    uint256 denominator = pow(amountIn + reserveIn);

    amountOut = numerator / denominator;
}

We believe the design (the formula) is wrong and it will result in unexpected and unfavorable outputs.

Specifically, if the amountIn is larger than the reserveIn, the amountOut starts to decrease.

Proof of Concept

Given:

  • A USDV-BTC Vader pool with the reserves of 200,000 USDV and 2 BTC.
  • If Alice swap 2 BTC for USDV, will get 50000 USDV as output;
  • If Bob swap 2.1 BTC for USDV, will only get 49970.25 USDV as output;
  • If Carol swap 2.2 BTC for USDV, will only get 49886.62 USDV as output.

For the same pool reserves, paying more for less output token is unexpected and unfavorable.

SamSteinGG (Vader) disputed:

This is the intended design of the Thorchain CLP model. Can the warden provide a tangible attack vector in the form of a test?

alcueca (judge) commented:

It is true that the effect will be surprising to the user, and the issue is acknowledged by the sponsor.

SamSteinGG (Vader) commented:

@alcueca We do not acknowledge the issue. This is the intended design of the CLP model and the amount supplied for a trade is meant to be safeguarded off-chain. It is an inherent trait of the model.

The original report as published on Code4rena, by WatchPug. 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

Likely the swap formula uses a denominator that grows faster than the numerator when amountIn exceeds reserveIn, causing the output to decrease.

Impact

Users could receive less output when paying more, leading to unexpected losses, though the sponsor claims this is intended design and the amount is safeguarded off-chain.

What the attacker needed

Access to a Vader pool; ability to swap tokens

How it was found

  1. Set up a USDV-BTC Vader pool with reserves of 200,000 USDV and 2 BTC.
  2. Swap 2 BTC for USDV and observe an output of 50,000 USDV.
  3. Swap 2.1 BTC for USDV and observe an output of 49,970.25 USDV.
  4. Swap 2.2 BTC for USDV and observe an output of 49,886.62 USDV.

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.

In a swap, you expect that paying more gives you more output. Here, the formula is designed so that if you pay more than the pool's reserve, you actually get less output. This is surprising and could be used to trick users or cause losses.

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 WatchPug

View all

197 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.