Skip to content
Business logic Audit finding Intermediate

amountOutMin passed in as 0 in AgentToken::_swapTax leads to loss of funds due to slippage

The AgentToken contract's tax swap function calls the Uniswap router with a minimum output amount of zero, so the swap will accept any amount of tokens in return. An attacker could manipulate the pool price before the swap to make the contract receive far fewer tokens than expected, causing a loss for the project tax recipient.

YouCrossTheLineAlfie Follow with 0x60scs, adam-idarrha and 14 others

Originally disclosed on Code4rena

Submitted by YouCrossTheLineAlfie, also found by 0x60scs, adam-idarrha, anchabadze, bytesguard, classic-k, DanielTan, debo, gmh5225, gregom, harsh123, odessos42, Olugbenga, PolarizedLight, slowbugmayor, Topmark, and unique

https://github.com/code-423n4/2025-04-virtuals-protocol/blob/28e93273daec5a9c73c438e216dde04c084be452/contracts/virtualPersona/AgentToken.sol#L793

Finding description

The AgentToken::_swapTax is called via AgentToken::_autoSwap whenever a AgentToken::_transfer call takes place.

On further inspection, it can be observed that _swapTax internally calls uniswap router’s swapExactTokensForTokensSupportingFeeOnTransferTokens function where amountOutMin is passed as 0.

    function _swapTax(uint256 swapBalance_, uint256 contractBalance_) internal {
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = pairToken;

        // Wrap external calls in try / catch to handle errors
        try
            _uniswapRouter.swapExactTokensForTokensSupportingFeeOnTransferTokens(
                swapBalance_,
                0,                                                                  <<@ -- // amountOutMin is passed as 0
                path,
                projectTaxRecipient,
                block.timestamp + 600
            )
        {

           // . . . Rest of the code . . .

        } catch {
            // Don't allow a failed external call (in this case to uniswap) to stop a transfer.
            // Emit that this has occurred and continue.
            emit ExternalCallError(5);
        }
    }

This can lead to unexpectedly high slippage, leading to loss of funds for projectTaxRecipient.

Impact

Loss of funds for the projectTaxRecipient due to high slippage.

Recommended mitigation steps

It is recommended to calculate amountOutMin instead of passing 0:

function swapTax(uint256 swapBalance_, uint256 contractBalance_) internal {
    address[] memory path = new address[](2);
    path[0] = address(this);
    path[1] = pairToken;

+    // Calculate the minimum amount out with slippage tolerance
+    uint256 amountOutMin = calculateMinimumAmountOut(swapBalance_, path);

    // Wrap external calls in try / catch to handle errors
    try
        _uniswapRouter.swapExactTokensForTokensSupportingFeeOnTransferTokens(
            swapBalance_,
-            0,
+            amountOutMin,
            path,
            projectTaxRecipient,
            block.timestamp + 600
        )
    {
        // We will not have swapped all tax tokens IF the amount was greater than the max auto swap.
        // We therefore cannot just set the pending swap counters to 0. Instead, in this scenario,
        // we must reduce them in proportion to the swap amount vs the remaining balance + swap
        // amount.
        //
        // For example:
        //  * swap Balance is 250
        //  * contract balance is 385.
        //  * projectTaxPendingSwap is 300
        //
        // The new total for the projectTaxPendingSwap is:
        //   = 300 - ((300 * 250) / 385)
        //   = 300 - 194
        //   = 106
        if (swapBalance_ < contractBalance_) {
            projectTaxPendingSwap -= uint128((projectTaxPendingSwap * swapBalance_) / contractBalance_);
        } else {
            projectTaxPendingSwap = 0;
        }
    } catch {
        // Don't allow a failed external call (in this case to uniswap) to stop a transfer.
        // Emit that this has occurred and continue.
        emit ExternalCallError(5);
    }
}

Below is the calculateMinimumAmountOut implementation:

/**
 * @dev Calculates the minimum amount out for a swap to protect against front-running
 * @param amountIn The amount of tokens to swap
 * @param path The swap path
 * @return minAmountOut The minimum amount of tokens to receive
 */
function calculateMinimumAmountOut(uint256 amountIn, address[] memory path) internal view returns (uint256 minAmountOut) {
    // Use getAmountsOut to calculate the expected output amount
    uint256[] memory amountsOut;

    try _uniswapRouter.getAmountsOut(amountIn, path) returns (uint256[] memory amounts) {
        amountsOut = amounts;
    } catch {
        // If getAmountsOut fails, use a fallback method or return 0
        return 0; // In case of failure, fallback to 0 to ensure the transaction doesn't revert
    }

    // If successful, calculate minimum amount with slippage tolerance (e.g., 3%)
    if (amountsOut.length > 1) {
        uint256 expectedAmountOut = amountsOut[amountsOut.length - 1];
        // Apply 3% slippage tolerance: amountOutMin = expectedAmountOut * 0.97
        minAmountOut = expectedAmountOut * 97 / 100;
    }

    return minAmountOut;
}

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

Impact

The project tax recipient could lose funds due to high slippage, as the swap accepts any output amount.

What the attacker needed

Attacker can influence pool price

The fix

The swap should calculate a minimum output amount, for example using the router's expected output with a slippage tolerance, instead of passing zero.

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 a contract swaps tokens on a decentralised exchange, it can set a minimum amount it is willing to receive to protect against sudden price changes. Here that minimum was set to zero, so the swap would go through no matter how few tokens came back. Someone who can influence the price just before the swap could make the contract get a bad deal, and the project loses 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 YouCrossTheLineAlfie

View all

10 more disclosed reports, best first.

More from Virtuals Protocol

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.