Skip to content
Business logic Audit finding Intermediate

Since buyToken function has no slippage checking, users can get less tokens than expected when they buy tokens directly

The buyToken function lacks a slippage check, so a user's purchase can be front-run by another buy or a settleAuction, causing them to receive fewer tokens than expected. An attacker can exploit this by buying tokens first to raise the price.

deepplus Follow with Aymen0909, adeolu and 13 others

Originally disclosed on Code4rena

Submitted by deepplus, also found by Aymen0909, adeolu, passteque, jnforja, KupiaSec, Tricko, wangxx2026, zhaojie, 0xDING99YA, SadeeqXmosh, DanielArmstrong, SpicyMeatball, 0xmystery, Inference, and rvierdiiev

Users can buy NontransferableERC20Token by calling buyToken function directly. At that time, the expected amount of tokens they will receive is determined based on current supply and their paying ether amount. But, due to some transactions(such as settleAuction or another user’s buyToken) which is running in front of caller’s transaction, they can get less token than they expected.

Proof of Concept

The VRGDAC always exponentially increase the price of tokens if the supply is ahead of schedule. Therefore, if another transaction of buying token is frontrun against a user’s buying token transaction, the token price can arise than expected.

For instance, let’s assume that ERC20TokenEmitter is initialized with following params:

  • target price: 1 ether
  • decay percent: 10 %
  • per time unit: 10 ether

To avoid complexity, we will assume that the supply of token so far is consistent with the schedule. When alice tries to buy token with 5 ether, expected amount is calculated by getTokenQuoteForEther(5 ether) and the value is about 4.87 ether. However, if Bob’s transaction to buy tokens with 10 ether is executed before Alice, the real amount which Alice will receive is about 4.43 ether.

You can check result through following test:

    function testBuyTokenWithoutSlippageCheck() public {
        address alice = makeAddr("Alice");
        address bob = makeAddr("Bob");

        vm.deal(address(alice), 100000 ether);
        vm.deal(address(bob), 100000 ether);

        address[] memory recipients = new address[](1);
        recipients[0] = address(1);
        uint256[] memory bps = new uint256[](1);
        bps[0] = 10_000;

        // expected amount of minting token when alice calls buyToken
        int256 expectedAmount = erc20TokenEmitter.getTokenQuoteForEther(5 ether);

        vm.startPrank(bob);
        // assume that bob calls buy token with 10 ether
        erc20TokenEmitter.buyToken{ value: 10 ether }(
            recipients,
            bps,
            IERC20TokenEmitter.ProtocolRewardAddresses({
                builder: address(0),
                purchaseReferral: address(0),
                deployer: address(0)
            })
        );

        vm.stopPrank();

        vm.startPrank(alice);
        // calculate the amount of tokens which alice will actually receive
        int256 realAmount = erc20TokenEmitter.getTokenQuoteForEther(5 ether);

        vm.stopPrank();

        emit log_string("Expected Amount: ");
        emit log_int(expectedAmount);
        emit log_string("Real Amount: ");
        emit log_int(realAmount);

        assertLt(realAmount, expectedAmount, "Alice should receive less than expected if Bob frontrun buyToken");
    }

Therefore, Alice will get about 0.44 ether less tokens than expected since there is no any checking of slippage in buyToken function.

Tools Used

VS Code

Recommended Mitigation Steps

Add slippage checking to buyToken function. This slippage checking should be executed only when the user calls buyToken function directly. In other words, it should not be executed when settleAuction calls buyToken function.

rocketman-21 (Revolution) acknowledged and commented:

This is intended and a consequence of how the VRGDA functions, when people buy tokens the price goes up if it is ahead of schedule.

Not ideal UX, but not going to fix for now.

0xTheC0der (Judge) commented:

Even though the increasing price is intended, it’s state of the art to introduce a slippage parameter to protect users from receiving less than expected. Therefore, maintaining Medium severity seems appropriate.


The original report as published on Code4rena, by deepplus. 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 buyToken function does not accept or enforce a minimum-output (slippage) parameter, so the number of tokens minted is determined solely by the current supply at execution time.

Impact

A user can receive fewer tokens than expected when their buyToken transaction is front-run; the project acknowledged this as intended behaviour but the judge noted a slippage parameter is state of the art.

What the attacker needed

User calls buyToken directly; attacker can front-run the transaction

The fix

Add a slippage check to buyToken that is applied only when users call it directly, not when settleAuction calls it.

How it was found

  1. Alice calculates the expected token amount for 5 ether using getTokenQuoteForEther.
  2. Bob calls buyToken with 10 ether before Alice's transaction.
  3. Alice's buyToken then executes and she receives fewer tokens than her earlier quote.

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 buy tokens, the price depends on how many have been sold so far. If someone else buys just before you, the price goes up and you get fewer tokens for your money. This report says the buying function should let you set a minimum number of tokens you will accept, so you are not surprised.

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 from Revolution 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.