Zealynx AcademyZealynx Academy
ZLX-Check

Spot the Bug: first-depositor share inflation

Question 1 of 3Score 0/3

A vault computes shares as amt * total / bal(), where bal() returns the vault's current token balance. The code compiles and matches the ERC4626 shape. What is still wrong?

Review and apply

Worked answers

Try the quiz above first, then open an answer to compare your reasoning. These notes are also available to read without completing an attempt.

  1. 1. A vault computes shares as amt * total / bal(), where bal() returns the vault's current token balance. The code compiles and matches the ERC4626 shape. What is still wrong?

    • A.Nothing, this is the standard and correct share formula
    • B.bal() reads the raw token balance, which anyone can inflate with a direct transfer
    • C._mint should run after the transferFrom, not before
    • D.The function is missing an onlyOwner modifier
    Answer and explanation

    B. bal() reads the raw token balance, which anyone can inflate with a direct transfer

    The denominator is the vault's raw token balance, and any address can send tokens straight to the vault without calling deposit. That makes the share price a number an outsider controls, not an internal accounting value the vault owns.

  2. 2. An attacker mints 1 wei of shares, then transfers a large amount of tokens directly to the vault. What happens to the next honest depositor?

    • A.Their deposit reverts because the balance changed
    • B.They receive extra shares as a bonus from the donation
    • C.Their shares round down toward zero while their tokens stay in the vault
    • D.Nothing changes, the donation is ignored by the formula
    Answer and explanation

    C. Their shares round down toward zero while their tokens stay in the vault

    The donation inflates the denominator, so the share price is now huge. amt * total / bal() for a normal deposit divides down to zero shares. The depositor's tokens remain in the vault and are captured by the attacker's single wei of shares.

  3. 3. Which change actually closes the first-depositor inflation hole?

    • A.Price shares off an internal reserve you increment on deposit, not the raw balance
    • B.Add a reentrancy guard to deposit()
    • C.Emit an event whenever the balance changes
    • D.Cap the maximum deposit amount per transaction
    Answer and explanation

    A. Price shares off an internal reserve you increment on deposit, not the raw balance

    The root cause is trusting a donatable number for the denominator. Track a reserve variable you only update inside deposit and withdraw, so a direct transfer cannot move the share price. Seeding dead shares, a virtual offset, or a minimum first deposit are established variants of the same idea. A reentrancy guard, an event, or a deposit cap do not touch the donatable denominator.

Read alongside: See it in real audits