Skip to content
Smart contracts Audit finding Intermediate

Donation attack posible on Staking.sol because its totalAsset() uses asset.balanceOf()

The Staking contract's totalAssets() reads the token balance of the contract, so anyone can inflate it by sending tokens directly. An attacker can front-run a victim's deposit so that the victim's shares round down to zero, causing the victim to lose the deposited tokens.

Kaysoft Follow

Originally disclosed on Immunefi

Critical View original
  • Disclosed after under an hour

56850 sc critical donation attack posible on staking sol because its totalasset uses asset balanceof

Submitted on Oct 21st 2025 at 07:41:15 UTC by @kaysoft for Audit Comp | Belong

Description

Brief/Intro

The Staking.sol inherits ERC4626 from solady. This ERC4626 contract uses the ERC20 balanceOf(address(this)) function to keep record of the total assets in the contract. The totalAssets() function of Staking.sol can be increased by anyone by direct donation.

This totalAssets() function is used in shares minting during deposit and the attacker's aim will be to cause the victim to mint zero shares losing their deposits.

Vulnerability Details

The main attack is to cause victims to mint zero shares during deposits. Zero shares means the victim cannot withdraw their deposit.

The totalAssets() is the denominator in the convertToShares function of ERC4626. An attacker can frontrun a victim's deposit with a direct transfer of Long token to the Staking.sol contract increasing totalAssets() without changing totalSupply().

Due to solidity rounding, a victim deposit can round down to zero if the totalAssets() is made large enough with the donation. shares = userAsset × totalSupply / totalAssets.

This issue compounds if the next depositor deposits asset amount less than the current total amount. The Staking Contract takes in the asset and mints zero shares again. Now totalAssets has increased again while totalSupply remains the same; subsequent users who deposit less than the new totalAssets also mint zero shares.

Example snippet from solady ERC4626:

Solidity
function totalAssets() public view virtual returns (uint256 assets) {
    assets = SafeTransferLib.balanceOf(asset(), address(this));//@audit donation attack
}

function convertToShares(uint256 assets) public view virtual returns (uint256 shares) {
    if (!_useVirtualShares()) {
        uint256 supply = totalSupply();
        return _eitherIsZero(assets, supply)
            ? _initialConvertToShares(assets)
            : FixedPointMathLib.fullMulDiv(assets, supply, totalAssets());
    }
    uint256 o = _decimalsOffset();
    if (o == uint256(0)) {
        return FixedPointMathLib.fullMulDiv(assets, totalSupply() + 1, _inc(totalAssets()));
    }
    return FixedPointMathLib.fullMulDiv(assets, totalSupply() + 10 ** o, _inc(totalAssets()));
}

Issue Scenario

{% stepper %} {% step %}

Step

Staking.sol is deployed {% endstep %}

{% step %}

Step

Bob (victim) tries to deposit 100 Long tokens {% endstep %}

{% step %}

Step

Alice (attacker) frontruns Bob to directly transfer 101 Long tokens to Staking.sol {% endstep %}

{% step %}

Step

Bob's deposit is executed with 100 × 1 / 101 = 0 {% endstep %}

{% step %}

Step

Zero shares are minted to Bob making Bob lose his 100 Long tokens {% endstep %}

{% step %}

Step

John (another victim) deposits 150 Long tokens and gets 150 × 1 / 201 = 0 {% endstep %} {% endstepper %}

As total assets keep growing while totalSupply remains the same, repeated small donations or repeated deposits smaller than the current totalAssets can cause many users to mint zero shares and lose funds.

Impact Details

  • Causing loss of funds to the first depositors by making them mint zero shares.

{% hint style="info" %} Recommendation: Consider overriding the totalAssets(...) function of the ERC4626 then keep record of the total assets with a storage variable which will be updated during deposit(), withdraw() and distributeRewards(). {% endhint %}

Proof of Concept

{% stepper %} {% step %}

Test setup

  1. Copy and paste the test below into the staking.test.sol file in the 'Staking features' test suite
  2. Run yarn test {% endstep %}

{% step %}

Observed effect

This demonstrates a donation attack that causes the first depositor to lose their Long token deposit amount. {% endstep %} {% endstepper %}

Solidity
// File: staking.test.ts
it('Donation to cause Zero shares minting', async () => {
      const { staking, long, admin, minter, user1, user2 } = await loadFixture(fixture);

      const amount = ethers.utils.parseEther('100');
      const amountUser2 = ethers.utils.parseEther('101');

      //1. Fund user1 and user2
      await long.connect(admin).transfer(user1.address, amount);
      await long.connect(admin).transfer(user2.address, amountUser2);

      //2. user1 getting ready to deposit.
      await long.connect(user1).approve(staking.address, amount);

      //3. user2 frontruns user1 to increase totalAssets while totalSupply remain same.
      await long.connect(user2).transfer(staking.address, amountUser2);

      //4. user1 deposit is executed after user2's donation attack
      const tx = await staking.connect(user1).deposit(amount, user1.address);

      // Zero shares minted to user1 causing loss of user1's 100 Long token.
      expect(await staking.balanceOf(user1.address)).to.eq(0);
      
    });

The original report as published on Immunefi, by kaysoft. 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 contract calculates total assets from its own token balance instead of tracking deposits in a storage variable, so direct transfers change the share price.

Impact

The first depositors could lose their deposited tokens because they receive zero shares and cannot withdraw.

What the attacker needed

Attacker can front-run the victim's deposit; attacker holds enough tokens to donate

The fix

The totalAssets function should be overridden to track total assets in a storage variable updated during deposit, withdraw and distributeRewards.

How it was found

  1. Deploy Staking.sol.
  2. Victim prepares to deposit 100 Long tokens.
  3. Attacker front-runs by transferring 101 Long tokens directly to the contract.
  4. Victim's deposit executes and mints zero shares.
  5. Victim loses the deposited tokens.

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 staking contract gives you shares in exchange for tokens. The number of shares is your deposit multiplied by the existing shares and divided by the total tokens the contract holds. If someone sends tokens straight to the contract before your deposit, the total goes up and your share calculation can round down to zero, so you get nothing back.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by Kaysoft

View all

22 more disclosed reports, best first.

More from Belong

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.