An ERC-4626 vault is a standard interface for a contract that holds one ERC-20 asset and issues shares, themselves an ERC-20, that represent a proportional claim on everything in the vault. Depositors get shares = assets × totalSupply / totalAssets, rounded down. The share inflation attack (also called the first-depositor or donation attack) exploits that rounding: an attacker deposits 1 wei into an empty vault, then donates a large amount of the asset directly to it, so the next depositor's shares round down to zero or one and the attacker's single share captures most of the victim's deposit. The standard defences are virtual shares with a decimals offset (as in OpenZeppelin's implementation), seeding the vault with dead shares at deployment, and slippage checks on the shares a deposit returns.
What does ERC-4626 standardise?
Before ERC-4626, every yield vault on Ethereum had its own function names and maths, and every integration needed an adapter. EIP-4626 fixed one interface on top of ERC-20:
| Function | Purpose | Rounds |
|---|---|---|
asset() | The underlying token | |
totalAssets() | Assets the vault manages, including accrued yield | |
convertToShares(assets) / convertToAssets(shares) | Exchange rate, ignoring fees and limits | Down |
deposit(assets, receiver) | Pay exact assets, receive shares | Shares down |
mint(shares, receiver) | Receive exact shares, pay assets | Assets up |
withdraw(assets, receiver, owner) | Receive exact assets, burn shares | Shares up |
redeem(shares, receiver, owner) | Burn exact shares, receive assets | Assets down |
previewX / maxX | Simulate each action, report limits | As the action |
The rounding column is part of the spec: every conversion rounds in favour of the vault, so no sequence of deposits and withdrawals can extract more than was put in. Each preview function must match what the action would do in the same block, which is what lets routers and lending markets compose vaults.
The share price is totalAssets / totalSupply. Yield raises totalAssets without minting shares, so each share becomes worth more. That is the intended behaviour, and it is also the lever the attack pulls.
How does the share inflation attack work?
Take a naive vault whose totalAssets() is just asset.balanceOf(address(this)), with an 18-decimal token:
- The vault is empty. The attacker deposits 1 wei and receives 1 share. Now
totalSupply = 1,totalAssets = 1. - The victim submits a deposit of 10,000 tokens (
10_000e18). - The attacker front-runs it by transferring 10,000 tokens straight to the vault with
transfer, notdeposit. No shares are minted, so one share is now backed by10_000e18 + 1wei. - The victim's deposit executes:
shares = 10_000e18 × 1 / (10_000e18 + 1), which rounds down to 0. The victim's tokens enter the vault and they receive nothing. - The attacker redeems their 1 share for the whole balance, about 20,000 tokens, a profit of 10,000.
A common half-fix is to revert when a deposit would mint zero shares. It doesn't help much. The attacker donates 5,000 tokens instead, so the victim gets exactly 1 share for 10,000 tokens: two shares now back 15,000 tokens, the attacker redeems half and walks away 2,500 tokens ahead, and the victim has lost 2,500.
The attack needs an empty or nearly empty vault, a share price that direct transfers can move, and a victim who deposits without checking the result. Front-running helps but isn't required: an attacker can inflate a fresh vault and wait. A flash loan supplies the donation capital when the attacker doesn't have it.
The same rounding-plus-donation pattern drained empty markets in Compound v2 forks, including Hundred Finance in 2023 and Sonne Finance in 2024, where the share is a cToken and the exchange rate is cash divided by supply.
Here it is as a Foundry test against the naive vault:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {Test} from "forge-std/Test.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract TestToken is ERC20 {
constructor() ERC20("Test", "TST") {}
}
contract NaiveVault is ERC20 {
IERC20 public immutable asset;
constructor(IERC20 asset_) ERC20("Naive Vault", "nvTST") {
asset = asset_;
}
function totalAssets() public view returns (uint256) {
return asset.balanceOf(address(this)); // donations move this
}
function deposit(uint256 assets) external returns (uint256 shares) {
uint256 supply = totalSupply();
shares = supply == 0 ? assets : (assets * supply) / totalAssets();
asset.transferFrom(msg.sender, address(this), assets);
_mint(msg.sender, shares);
}
function redeem(uint256 shares) external returns (uint256 assets) {
assets = (shares * totalAssets()) / totalSupply();
_burn(msg.sender, shares);
asset.transfer(msg.sender, assets);
}
}
contract InflationAttackTest is Test {
TestToken token = new TestToken();
NaiveVault vault = new NaiveVault(IERC20(address(token)));
address attacker = makeAddr("attacker");
address victim = makeAddr("victim");
function test_FirstDepositorStealsDeposit() public {
deal(address(token), attacker, 10_000e18 + 1);
deal(address(token), victim, 10_000e18);
vm.startPrank(attacker);
token.approve(address(vault), type(uint256).max);
vault.deposit(1); // 1 wei -> 1 share
token.transfer(address(vault), 10_000e18); // donation, no shares minted
vm.stopPrank();
vm.startPrank(victim);
token.approve(address(vault), type(uint256).max);
assertEq(vault.deposit(10_000e18), 0); // rounds down to nothing
vm.stopPrank();
vm.prank(attacker);
vault.redeem(1);
assertEq(token.balanceOf(attacker), 20_000e18 + 1);
assertEq(token.balanceOf(victim), 0);
}
}
How do virtual shares and the decimals offset stop it?
OpenZeppelin's ERC4626 (v4.9 and later) computes the exchange rate as if the vault always held some extra virtual shares and assets:
// inside OpenZeppelin ERC4626
function _convertToShares(uint256 assets, Math.Rounding rounding) internal view virtual returns (uint256) {
return assets.mulDiv(totalSupply() + 10 ** _decimalsOffset(), totalAssets() + 1, rounding);
}
This does two things.
The virtual share captures part of any donation. With the default offset of 0, there is always one phantom share and one phantom wei. Replay the attack: the attacker's 1 wei buys 1 share, the 10,000-token donation is spread across 2 shares (one real, one virtual), and the victim's deposit mints 1 share. When the attacker redeems, they get about 6,667 tokens for the 10,000 they donated. The attack loses money. The victim still loses about a third of their deposit to rounding, so it remains possible as pure griefing, but the attacker pays at least as much as they destroy.
The offset makes rounding errors tiny. Overriding _decimalsOffset() gives shares more decimals than the asset and multiplies the virtual shares by 10 ** offset. With an offset of 6, the 1-wei first deposit mints a million shares, and to round a victim's deposit down to zero the attacker would have to donate millions of times the deposit size. Each step of offset makes the attack roughly ten times more expensive.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {ERC4626} from "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract SimpleVault is ERC4626 {
constructor(IERC20 asset_) ERC20("Simple Vault Share", "svTST") ERC4626(asset_) {}
// shares get 6 more decimals than the asset; inflation now costs ~1e6x more
function _decimalsOffset() internal pure override returns (uint8) {
return 6;
}
}
Run the earlier attack against SimpleVault and the victim receives about 2 million shares that are worth almost exactly their 10,000 tokens, while the attacker gets back only about half of the donation. The rest is captured by the virtual shares. Front-ends must then read decimals() from the share token, not assume it matches the asset.
What other defences should a vault use?
Seed the vault with dead shares. Deposit a small amount at deployment and mint the shares to an address nobody controls, as Uniswap V2 does by burning its first 1,000 liquidity units. Do it in the same transaction as deployment, from a factory or deploy script, so there is never a block where the vault is empty and exposed.
Track assets internally. If totalAssets() comes from a stored variable updated on deposits, withdrawals and harvests, a direct transfer doesn't change the share price at all. This needs care for vaults whose yield arrives as balance increases, but it removes donation as an attack lever.
Check the shares you receive. Users and routers should pass a minimum. ERC-4626 itself has no slippage parameter, so wrap it:
function depositWithMinShares(IERC4626 vault, uint256 assets, uint256 minShares, address receiver)
external
returns (uint256 shares)
{
IERC20(vault.asset()).safeTransferFrom(msg.sender, address(this), assets);
IERC20(vault.asset()).forceApprove(address(vault), assets);
shares = vault.deposit(assets, receiver);
require(shares >= minShares, "slippage: too few shares");
}
(ERC-5143 proposes the same idea as overloads on the vault itself.) This router assumes using SafeERC20 for IERC20;. Front-ends should compute minShares from previewDeposit and a tolerance, the same way swaps use a minimum output to resist MEV and sandwich attacks.
Don't treat the share price as an oracle. If a lending market values vault shares with convertToAssets, a donation that inflates the share price inflates the collateral too. That is a form of oracle manipulation. Cap the rate of change, use a time-weighted rate, or price the underlying separately.
What else goes wrong in ERC-4626 vaults?
- Wrong rounding direction.
previewWithdrawandpreviewMintmust round up. Rounding down there lets users withdraw slightly more than their shares are worth, and loops of tiny withdrawals compound it. - Fee-on-transfer and rebasing tokens. If the vault receives less than
assets, minting shares for the full amount inflates supply. Measure balance before and after, or refuse such assets. - Inconsistent previews. Integrators rely on
previewDepositmatchingdeposit. Fees or limits applied in one but not the other break routers. - Reentrancy through hooks. Tokens with transfer hooks (ERC-777) can re-enter between the share calculation and the transfer. Follow checks-effects-interactions and see how to prevent reentrancy.
max*functions that lie. IfmaxDepositreturnstype(uint256).maxwhile the strategy is capped or paused, integrations revert unexpectedly.
The best way to catch these is an invariant suite: random sequences of deposits, mints, withdrawals, redemptions and donations from several actors, asserting that no actor can withdraw more than they put in plus their share of yield, and that totalAssets always covers what convertToAssets(totalSupply()) promises. Fuzz and invariant tests in Foundry shows how to set up handlers for exactly this.
Checklist
| Check | Why |
|---|---|
Use OpenZeppelin ERC4626 (v4.9+) or an implementation with virtual shares | Makes donation attacks unprofitable |
Override _decimalsOffset() (for example to 6) for high-value vaults | Makes rounding loss negligible and the attack prohibitively expensive |
| Seed dead shares in the deployment transaction | The vault is never empty |
Consider internal accounting for totalAssets() | Donations can't move the share price |
Enforce minShares / maxAssets in routers and front-ends | Victims can't be front-run into bad rates |
| Round in favour of the vault in all four paths | No value leaks through rounding |
Never use raw convertToAssets as a collateral oracle | Inflation becomes a borrowing exploit |
| Invariant-test with donations included | Catches the cases unit tests miss |
Get the weekly commit
New blockchain deep dives every week.