Skip to content
Business logic Audit finding Intermediate

Wrong calculation of erc20Delta and ethDelta

The contract incorrectly calculates the change in ETH and ERC20 token balances after a swap because the original balance already includes the user's input amount. This causes users to lose ETH refunds and suffer unexpected losses on arbitrage swaps between the same token.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug, also found by harleythedog

Swap.sol L200-L225

function fillZrxQuote(
    IERC20 zrxBuyTokenAddress,
    address payable zrxTo,
    bytes calldata zrxData,
    uint256 ethAmount
) internal returns (uint256, uint256) {
    uint256 originalERC20Balance = 0;
    if(!signifiesETHOrZero(address(zrxBuyTokenAddress))) {
        originalERC20Balance = zrxBuyTokenAddress.balanceOf(address(this));
    }
    uint256 originalETHBalance = address(this).balance;

    (bool success,) = zrxTo.call{value: ethAmount}(zrxData);
    require(success, "Swap::fillZrxQuote: Failed to fill quote");

    uint256 ethDelta = address(this).balance.subOrZero(originalETHBalance);
    uint256 erc20Delta;
    if(!signifiesETHOrZero(address(zrxBuyTokenAddress))) {
        erc20Delta = zrxBuyTokenAddress.balanceOf(address(this)).subOrZero(originalERC20Balance);
        require(erc20Delta > 0, "Swap::fillZrxQuote: Didn't receive bought token");
    } else {
        require(ethDelta > 0, "Swap::fillZrxQuote: Didn't receive bought ETH");
    }

    return (erc20Delta, ethDelta);
}

When a user tries to swap unwrapped ETH to ERC20, even if there is a certain amount of ETH refunded, at L215, ethDelta will always be 0.

That’s because originalETHBalance already includes the msg.value sent by the caller.

Let’s say the ETH balance of the contract is 1 ETH before the swap.

  • A user swaps 10 ETH to USDC;
  • originalETHBalance will be 11 ETH;
  • If there is 1 ETH of refund;
  • ethDelta will be 0 as the new balance is 2 ETH and subOrZero(2, 11) is 0.

Similarly, erc20Delta is also computed wrong.

Consider a special case of a user trying to arbitrage from WBTC to WBTC, the originalERC20Balance already includes the input amount, erc20Delta will always be much lower than the actual delta amount.

For example, for an arb swap from 1 WBTC to 1.1 WBTC, the ethDelta will be 0.1 WBTC while it should be 1.1 WBTC.

Impact

  • User can not get ETH refund for swaps from ETH to ERC20 tokens;
  • Arb swap with the same input and output token will suffer the loss of almost all of their input amount unexpectedly.

Recommendation

Consider subtracting the input amount from the originalBalance.

Shadowfiend (Tally) confirmed:

This doesn’t allow explicit stealing by an attacker, but does leak value. We would suggest a (2) severity on this.

0xean (judge) commented:

This results in a user losing assets that they will never be able to recover. Per documentation

3 — High: Assets can be stolen/lost/compromised directly (or indirectly if there is a valid attack path that does not have hand-wavy hypotheticals).

Lost assets are a high sev.

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

The original balance is recorded after the user's input has already been added to the contract, so the delta calculation subtracts the input from the output, underreporting the actual change.

Impact

Users cannot get ETH refunds for ETH-to-ERC20 swaps, and arbitrage swaps with the same input and output token lose almost all of their input amount.

What the attacker needed

User performs a swap with ETH or same-token arbitrage

The fix

The original balance should be adjusted by subtracting the input amount before calculating the delta.

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.

When you swap tokens, the contract checks how much you received by comparing the balance before and after. But it forgets that your own deposit is already counted in the 'before' balance, so the difference looks smaller than it really is. This means you might not get your change back, or you might lose money on certain trades.

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