---
title: "Rounding, precision and share inflation"
url: https://dontgetrekt.today/dev/rounding-precision
section: "Is the math and the design right?"
owasp: SC07:2026
---

# Rounding, precision and share inflation

Share, interest or fee math rounds in the wrong direction or loses precision. The attacker repeats a small gain many times, or inflates the share price of an empty vault.

## How the attack works

1. A vault mints shares as `assets * totalSupply / totalAssets`. Solidity division rounds down.
2. In an empty vault, the attacker deposits 1 wei and gets 1 share. Then the attacker sends a large donation directly to the vault.
3. The next depositor's shares round down to zero. The attacker redeems the 1 share and takes both deposits.
4. Variants: rounding in favour of the user on withdraw, division before multiplication, wrong decimals (6 vs 18), many tiny actions that each round in the attacker's favour.

## Vulnerable code

```solidity
function deposit(uint256 assets) external returns (uint256 shares) {
    shares = totalSupply == 0
        ? assets
        : assets * totalSupply / token.balanceOf(address(this)); // donation moves this
    _mint(msg.sender, shares);                                  // can be 0
    token.transferFrom(msg.sender, address(this), assets);
}
```

## Fixed code

```solidity
// OpenZeppelin ERC4626 (v4.9+) adds virtual shares and assets (decimals offset),
// so a donation cannot make the next deposit round to zero.
contract Vault is ERC4626 {
    constructor(IERC20 a) ERC4626(a) ERC20("vShare", "vS") {}
    function _decimalsOffset() internal pure override returns (uint8) { return 6; }
}
// In your own math: multiply before you divide, round against the user
// (Math.mulDiv with Rounding.Ceil on withdraw), and revert when shares == 0.
```

## Review checklist

- Round in favour of the protocol: down when you give shares or assets, up when you take them.
- Multiply before you divide. Use `Math.mulDiv` for large numbers.
- Use internal accounting for total assets, not `balanceOf(address(this))`, so donations do not change the rate.
- Protect empty vaults: virtual shares, a dead-shares deposit at deploy, or a minimum first deposit.
- Check decimals of every token. Do not assume 18.

## How to test for it

- Fuzz deposit and withdraw with amounts from 1 wei to large values. Check that `redeem(deposit(x)) <= x`.
- Test the first deposit after a donation to an empty vault.

## Real exploits (39)

- 2025-11-03 Balancer V2 (Composable Stable Pools) ($128.0M): Balancer V2 Composable Stable Pools lost about $128M across several chains because _upscale() always rounded down, which let an attacker shrink the pool invariant through repeated micro-swaps. [rekt.news](https://rekt.news/balancer-rekt2) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2025-11/BalancerV2_exp.sol)
- 2023-11-22 KyberSwap Elastic ($48.0M): KyberSwap Elastic lost over $48M across six chains when an attacker used flash loans and precisely sized swaps to exploit a precision error that double-counted liquidity in its concentrated liquidity pools. [rekt.news](https://rekt.news/kyberswap-rekt) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2023-11/KyberSwap_exp.eth.1.sol)
- 2021-02-13 Alpha Homora V2 (Alpha Finance) ($37.5M): Alpha Homora V2 lost about $37.5M when an attacker abused a rounding error in borrow share math plus a publicly callable resolveReserve to borrow sUSD with zero debt shares. [rekt.news](https://rekt.news/alpha-finance-rekt)
- 2024-05-14 Sonne Finance ($20.0M): Sonne Finance, a Compound V2 fork on Optimism, lost about $20M to the known donation (empty-market share inflation) attack on a newly added VELO market, made possible by a non-atomic governance rollout. [rekt.news](https://rekt.news/sonne-finance-rekt) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2024-05/Sonne_exp.sol)
- 2025-06-25 ResupplyFi ($9.8M): ResupplyFi lost about $9.8M when an attacker used an ERC4626 donation attack on a freshly deployed, empty vault to make the exchange rate round to zero and borrow reUSD against 1 wei of collateral. [rekt.news](https://rekt.news/resupplyfi-rekt) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2025-06/ResupplyFi_exp.sol)
- 2025-02-11 zkLend ($9.6M): zkLend on Starknet lost about $9.57M when an attacker used flash loans to inflate the lending_accumulator in an empty market and exploited withdrawal rounding. [rekt.news](https://rekt.news/zklend-rekt)
- 2025-09-01 Bunni ($8.4M): Bunni lost $8.4M across Ethereum (USDC/USDT) and Unichain (ETH/weETH) pools because rounding errors in its custom Liquidity Distribution Function rebalancing and withdrawal math could be exploited. [rekt.news](https://rekt.news/bunni-rekt)
- 2023-04-15 Hundred Finance ($7.4M): Hundred Finance lost $7.4M on Optimism when an attacker donated flash-loaned WBTC to an empty hWBTC market, inflating its exchange rate and exploiting a rounding error in redeemUnderlying. [rekt.news](https://rekt.news/hundred-rekt2) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2023-04/HundredFinance_2_exp.sol)
- 2024-01-30 Abracadabra ($6.5M): Abracadabra lost $6.5M on Ethereum when an attacker exploited a rounding error in CauldronV4's borrow debt-share calculation, draining MIM from two cauldrons and depegging MIM. [rekt.news](https://rekt.news/abra-rekt)
- 2024-01-30 MIM Spell (Abracadabra) CauldronV4 ($6.5M): MIM Spell's CauldronV4 lost about $6.5M because repayForAll() and the borrow accounting broke the elastic/base ratio through precision loss, which let the attacker borrow MIM against almost no debt. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2024-01/MIMSpell2_exp.sol)

## References

- [OWASP SC07:2026](https://scs.owasp.org/sctop10/SC07-ArithmeticErrors/)
- [OpenZeppelin: ERC-4626 inflation attack](https://docs.openzeppelin.com/contracts/5.x/erc4626)
- [Solady / OpenZeppelin Math.mulDiv](https://docs.openzeppelin.com/contracts/5.x/api/utils#Math)
