Skip to content
Business logic Audit finding Intermediate

getSharesForAmount returns wrong value when totalAssets == 0

The getSharesForAmount function returns zero when totalAssets is zero, but the deposit function mints shares equal to the deposit amount when totalSupply is zero. Contracts or frontends using this function to estimate deposits will get an incorrect value.

cmichel Follow

Originally disclosed on Code4rena

Submitted by cmichel

The getSharesForAmount function returns 0 if totalAssets == 0.

However, if totalSupply == 0, the actual shares that are minted in a deposit are _amount even if totalAssets == 0.

Contracts / frontends that use this function to estimate their deposit when totalSupply == 0 will return a wrong value.

Recommended Mitigation Steps

function getSharesForAmount(uint256 _amount)
    external
    view
    override
    returns (uint256)
{
+   // to match the code in `deposit`
+   if (totalSupply() == 0) return _amount;

    uint256 _totalAssets = totalAssets();
    return
        (_totalAssets > 0)
            ? ((_amount * totalSupply()) / _totalAssets)
            : 0; // @audit this should be _amount according to `deposit`
}

ramenforbreakfast (prePO) confirmed and commented:

Valid claim, I believe this is a bug in our code, still need to verify.

gzeon (judge) commented:

Agree with sponsor.


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

What the attacker needed

TotalSupply == 0; totalAssets == 0

The fix

The function should be modified to return the deposit amount when totalSupply is zero, matching the deposit logic.

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.

This is a smart contract bug where a helper function that estimates how many shares you get for a deposit gives the wrong answer in a special case. If the total supply of shares is zero, the actual deposit gives you shares equal to your deposit amount, but the estimate says zero. Anyone building a tool that relies on this estimate will show incorrect numbers.

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 cmichel

View all

196 more disclosed reports, best first.

More from prePO 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.