Rounding errors that quietly drain vaults
On this page
In short
Rounding is a transfer of value. For each conversion, define which party may receive the remainder, use an implementation that enforces that direction, and test the full deposit-withdraw and mint-redeem round trips across extreme share prices and decimal scales. A tiny favorable error becomes a security issue when an attacker can repeat, batch, or amplify it.
The direction of rounding is a security property
Consider a vault that mints shares on deposit and burns them on withdrawal:
shares = assets * totalShares / totalAssets; // depositassets = shares * totalAssets / totalShares; // withdrawBoth lines divide. Both discard a remainder. The question that decides whether this vault is safe is not "is the formula correct?" — it is which way does each division round, and who benefits?
- A
deposit(assets)preview should round down when calculating shares. - A
mint(shares)preview should round up when calculating assets owed. - A
withdraw(assets)preview should round up when calculating shares burned. - A
redeem(shares)preview should round down when calculating assets returned.
Those directions follow the normative conversion rules in ERC-4626. The simple two-line example above hides the distinction between exact-input and exact-output operations, so a real implementation should expose separate conversion paths with explicit rounding rather than reuse one floor-dividing helper everywhere.
A numerical example
Assume a vault has 7 assets and 3 shares. A user asks to withdraw exactly 3
assets. The exact shares required are 3 × 3 / 7 = 1.2857…. Burning only 1 share
would let the user receive 3 assets while surrendering a claim worth roughly
2.33 assets. The share calculation must round up to 2.
The opposite direction applies when redeeming 1 share: 1 × 7 / 3 = 2.333…
assets. Returning 3 assets would overpay the redeemer, so the asset calculation
rounds down to 2. The same ratio needs different rounding because the user fixes
a different side of the exchange.
Where truncation hides
Precision bugs rarely announce themselves. They hide in:
- Scale mismatches — mixing 6-decimal and 18-decimal tokens without normalizing, so a truncation that looks tiny is actually twelve orders of magnitude off.
- Intermediate rounding — dividing before multiplying, when the reverse would have preserved precision.
- Unchecked casts — narrowing a
uint256touint128and silently dropping the high bits. - Fee ordering — rounding a gross amount and fee separately can create or destroy more value than calculating a single net conversion.
- Empty or near-empty supply — donations or direct transfers can change the asset/share ratio so the next depositor receives zero shares or an attacker captures the first meaningful deposit.
- Repeated reward updates — truncating an index increment on every update can strand value or systematically favor accounts that enter at a chosen time.
The dangerous truncations are the ones that are individually negligible and collectively unbounded.
Safer implementation patterns
Use multiplication-and-division helpers that accept an explicit rounding mode and preserve the full intermediate product. Keep units visible in names and types: assets, shares, basis points, token decimals, and fixed-point scales should not be interchangeable integers. Apply the checks-effects-interactions pattern around transfers, but do not assume it fixes arithmetic direction.
For tokenized vaults, compare the implementation with every preview* and
max* requirement in ERC-4626. OpenZeppelin’s ERC-4626
documentation also explains
the inflation-attack shape and the role of virtual assets and shares. A library
reduces implementation risk, but configuration, overrides, fees, and integrations
still require review.
How to test rounding
We don't eyeball formulas. We trace value through a full round-trip and assert a conservation law: a user who deposits and immediately withdraws must not come out ahead. Then we look for any path — any ordering, any token pair, any share price — that violates it.
Useful property tests include:
- Previewed values obey the standard’s required inequality relative to execution.
- A user cannot increase total assets they control through a zero-time round trip, after accounting for explicitly documented yield or fees.
- Splitting one operation into many does not improve the user’s result beyond a defined rounding tolerance.
- Total claims do not exceed controlled assets under deposits, withdrawals, donations, fee collection, and loss scenarios.
- Results remain valid for 6-, 8-, and 18-decimal assets, high share prices, one unit, zero supply, and values near implementation bounds.
When an invariant breaks, quantify the gain per operation, execution cost, repeatability, capital requirement, and available liquidity. A rounding bug’s severity depends on that exploit path—not on the fact that integer division appears in the code.
Limitations
The directions above describe the ERC-4626 interface and common vault economics; custom accounting may use different units or guarantees. Rebasing, fee-on-transfer, callback-enabled, or otherwise non-standard assets can invalidate a conversion model even when its division rounds correctly. Economic losses, administrative changes, and cross-protocol composition also need separate invariants.
Primary references
- ERC-4626: Tokenized Vaults
- OpenZeppelin Contracts: ERC-4626
- Solidity documentation: Types and conversions