Skip to content
Business logic Audit finding Intermediate

Remainingforsale not updated after withdrawunsoldarctokens will cause following buy revert

The withdrawUnsoldArcTokens function does not update the stored remaining-for-sale counter after transferring unsold tokens out, so getMaxNumberOfTokens reports more tokens than the contract actually holds. A buyer relying on that figure can attempt a purchase that reverts with ContractBalanceInsufficient.

maggie Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

52794 sc low remainingforsale not updated after withdrawunsoldarctokens will cause following buy revert

Submitted on Aug 13th 2025 at 08:26:11 UTC by @maggie for Attackathon | Plume Network

Description

Brief/Intro

ArcTokenPurchase.sol#withdrawUnsoldArcTokens() allows the token admin to withdraw unsold ArcTokens after a sale (or if disabled). After a sale, if the token sale is not disabled and the admin withdraws part of the unsold ArcTokens, the remaining ArcTokens could still be sold. However, during the withdraw, the remainingForSale (as reported by getMaxNumberOfTokens) is not updated. The real tokens left on the contract become smaller than getMaxNumberOfTokens(), which can cause subsequent buy transactions to revert due to ContractBalanceInsufficient.

Vulnerability Details

Current function:

Solidity
function withdrawUnsoldArcTokens(
    address _tokenContract,
    address to,
    uint256 amount
) external onlyTokenAdmin(_tokenContract) {
    if (to == address(0)) {
        revert CannotWithdrawToZeroAddress();
    }
    if (amount == 0) {
        revert AmountMustBePositive();
    }

    ArcToken token = ArcToken(_tokenContract);
    uint256 contractBalance = token.balanceOf(address(this));
    if (contractBalance < amount) {
        revert InsufficientUnsoldTokens();
    }

    bool success = token.transfer(to, amount);
    if (!success) {
        revert ArcTokenWithdrawalFailed();
    }
}

The function compares amount to token.balanceOf(address(this)), but the unsold amount (the logical remaining amount for sale) is:

Solidity
getMaxNumberOfTokens(_tokenContract)

function getMaxNumberOfTokens(
    address _tokenContract
) external view returns (uint256) {
    TokenInfo storage info = _getPurchaseStorage().tokenInfo[_tokenContract];
    return info.totalAmountForSale - info.amountSold;
}

After withdrawing tokens, info.totalAmountForSale (or equivalent stored remaining-for-sale counter) should be updated to reflect the withdrawal. If it is not updated, getMaxNumberOfTokens(_tokenContract) will report a higher remaining-for-sale amount than actually available. A buyer relying on getMaxNumberOfTokens could attempt to buy that amount and the buy would revert due to ContractBalanceInsufficient.

Recommended change (as provided by reporter):

Solidity
function withdrawUnsoldArcTokens(
    address _tokenContract,
    address to,
    uint256 amount
) external onlyTokenAdmin(_tokenContract) {
    if (to == address(0)) {
        revert CannotWithdrawToZeroAddress();
    }
    if (amount == 0) {
        revert AmountMustBePositive();
    }

    ArcToken token = ArcToken(_tokenContract);
    TokenInfo storage info = _getPurchaseStorage().tokenInfo[_tokenContract];
    uint256 contractBalance = info.totalAmountForSale - info.amountSold;
    if (contractBalance < amount) {
        revert InsufficientUnsoldTokens();
    }

    bool success = token.transfer(to, amount);
    if (!success) {
        revert ArcTokenWithdrawalFailed();
    }
    info.totalAmountForSale = info.totalAmountForSale - amount;
}

Impact Details

After withdrawing unsold ArcTokens, the remaining-for-sale amount returned by getMaxNumberOfTokens(_tokenContract) will be greater than the real tokens available in the contract. This mismatch can cause buy operations to revert with ContractBalanceInsufficient.

References

https://github.com/immunefi-team/attackathon-plume-network/blob/main/arc/src/ArcTokenPurchase.sol?utm\\_source=immunefi#L439-L461

Proof of Concept

{% stepper %} {% step %}

  1. Enable tokenA for sale with amount = 500000000000000000000. {% endstep %}

{% step %} 2. Withdraw half of unsold ArcTokens, withdraw amount = 250000000000000000000. {% endstep %}

{% step %} 3. Query remaining for sale amount. {% endstep %} {% endstepper %}

Add the following test to /test/ArcTokenPurchase.t.sol:

Solidity
function test_WithdrawUnsoldArcTokens_poc() public {
    console.log("before withdraw: remainingForSale         = %s",purchase.getMaxNumberOfTokens(address(token)));
    vm.prank(owner);
    uint256 withdran_amount = purchase.getMaxNumberOfTokens(address(token))/2;
    purchase.withdrawUnsoldArcTokens(address(token), bob, withdran_amount);

    assertEq(token.balanceOf(bob), withdran_amount);
    console.log("after withdraw: remainingForSale          = %s",purchase.getMaxNumberOfTokens(address(token)));
    console.log("after withdraw: real tokens forsale       = %s",token.balanceOf(address(purchase)));
}

Run:

forge test --via-ir --match-path test/ArcTokenPurchase.t.sol --match-contract ArcTokenPurchaseTest --match-test test_WithdrawUnsoldArcTokens_poc -vv

Test output provided by reporter:

[PASS] test_WithdrawUnsoldArcTokens_poc() (gas: 149154)
Logs:
  before withdraw: remainingForSale         = 500000000000000000000
  after withdraw: remainingForSale          = 500000000000000000000
  after withdraw: real tokens forsale       = 250000000000000000000

Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 4.58ms (349.10µs CPU time)

This demonstrates getMaxNumberOfTokens still reporting 500...000 while actual token balance on the purchase contract is 250...000 after withdrawal.

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

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 withdrawal function checks the token balance but never decreases the stored totalAmountForSale, leaving the logical remaining-for-sale amount out of sync with the real balance.

Impact

Buyers could see a remaining-for-sale figure larger than the real balance and their buy transactions could revert with ContractBalanceInsufficient.

What the attacker needed

Token admin role; sale not disabled; unsold tokens remain

The fix

The withdrawal function should reduce the stored totalAmountForSale by the withdrawn amount and check the logical remaining amount rather than the raw token balance.

How it was found

  1. Enable a token for sale with a large amount.
  2. Withdraw half of the unsold tokens as the admin.
  3. Query the remaining-for-sale amount and observe it is unchanged.

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.

A smart contract keeps a record of how many tokens are still for sale. The admin can take back unsold tokens, but the record is not reduced when that happens. So the contract advertises more tokens than it actually has, and a normal purchase can fail.

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 maggie

View all

2 more disclosed reports, best first.

More from Plume Network

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.