# ERC4626 Inflation attack discussion

**URL:** <https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643>\
**Category:** Smart Contracts\
**Tags:** openzeppelin-contracts\
**Created:** [September 20, 2024, 5:46am UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643 "2024-09-20T05:46:12Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![cryptomoon42](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@cryptomoon42](https://forum.openzeppelin.com/u/cryptomoon42)\
**Post date:** [September 20, 2024, 5:46am UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/1 "2024-09-20T05:46:12Z")

</div>

I was reading about vault inflation attacks from this article [https://blog.openzeppelin.com/a-novel-defense-against-erc4626-inflation-attacks](https://blog.openzeppelin.com/a-novel-defense-against-erc4626-inflation-attacks)

Have doubt regarding third case mentioned(image attached)

It talks about dead shares(like burning of Minimum Liquidity in uni v2 pools during initial mint).

The cons mentions that "The strategy doesn't completely resolve the issue. Inflation attacks are still possible, at a reduced profit."

I want to know whether the attack is still profitable for the attacker since minimum liquidity is burned? I did some notebook math and can't find scenarios where donation attack is profitable but the article says otherwise. Does anyone have any more insights on this?

 ![Screenshot 2024-09-20 at 11.00.28 AM](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/3X/1/6/164080f35e05441e5a7c0fa2638c6fc6db321da8.png)

---

<div class="post-metadata">

**Author:** ![Skyge](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/skyge/32/343_2.png) [@Skyge](https://forum.openzeppelin.com/u/Skyge)\
**Post date:** [September 20, 2024, 1:46pm UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/2 "2024-09-20T13:46:40Z")

</div>

Hi, welcome to the community! 👋

While this strategy doesn't completely resolve the issue, inflation attacks remain possible.  
For instance, an attacker initially deposits 2 wei, receiving 1 wei as a share and leaving 1 wei as a dead share. Subsequently, the attacker can donate 200e18 tokens to the vault  
before another user deposits 100e18 tokens. In this scenario, the user receives 0 shares  
because `shareAmountOut = assetsIn * totalSupply / totalAssets = 100e18 * 2 / (200e18 + 2) = 0`. Consequently, 2 share tokens can acquire 300e18 assets (200e18 + 100e18), enabling attacker to obtain 150e18 assets by 1 share. Although this appears to result in a loss of 50e18 assets for the attacker, in reality, anyone depositing less than 100e18 assets will receive 0 shares, allowing the attacker to gain more assets per share.

---

<div class="post-metadata">

**Author:** ![cryptomoon42](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@cryptomoon42](https://forum.openzeppelin.com/u/cryptomoon42)\
**Post date:** [September 21, 2024, 3:14pm UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/3 "2024-09-21T15:14:32Z")

</div>

Hey @Skyge , thanks for your reply. Won't such issues exist in the ERC4626 implementation by OpenZeppelin([https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC4626.sol)) when function `_decimalsOffset` returns `0`.

---

<div class="post-metadata">

**Author:** ![Skyge](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/skyge/32/343_2.png) [@Skyge](https://forum.openzeppelin.com/u/Skyge)\
**Post date:** [September 21, 2024, 3:20pm UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/4 "2024-09-21T15:20:51Z")

</div>

Do you mean the `_decimalsOffset` is equal to 0 at this function:

```solidity
/**
     * @dev Internal conversion function (from assets to shares) with support for rounding direction.
     */
    function _convertToShares(uint256 assets, Math.Rounding rounding) internal view virtual returns (uint256) {
        return assets.mulDiv(totalSupply() + 10 ** _decimalsOffset(), totalAssets() + 1, rounding);
    }

    /**
     * @dev Internal conversion function (from shares to assets) with support for rounding direction.
     */
    function _convertToAssets(uint256 shares, Math.Rounding rounding) internal view virtual returns (uint256) {
        return shares.mulDiv(totalAssets() + 1, totalSupply() + 10 ** _decimalsOffset(), rounding);
    }

```

---

<div class="post-metadata">

**Author:** ![cryptomoon42](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@cryptomoon42](https://forum.openzeppelin.com/u/cryptomoon42)\
**Post date:** [September 21, 2024, 3:26pm UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/5 "2024-09-21T15:26:49Z")

</div>

Thanks @Skyge for pointing it out. Updated the comment.

---

<div class="post-metadata">

**Author:** ![Skyge](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/skyge/32/343_2.png) [@Skyge](https://forum.openzeppelin.com/u/Skyge)\
**Post date:** [September 22, 2024, 9:02am UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/6 "2024-09-22T09:02:28Z")

</div>

You can have a look at the [erc4626:defending\_with\_a\_virtual\_offset](https://docs.openzeppelin.com/contracts/5.x/erc4626#defending_with_a_virtual_offset), which has a detailed explanation about this.

---

<div class="post-metadata">

**Author:** ![cryptomoon42](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@cryptomoon42](https://forum.openzeppelin.com/u/cryptomoon42)\
**Post date:** [September 22, 2024, 11:50am UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/7 "2024-09-22T11:50:24Z")

</div>

Thanks @Skyge for pointing it out. I had a look at that article.

 ![Screenshot 2024-09-22 at 5.18.05 PM](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/3X/d/c/dc2e13a868e54b7a09647109ddf4a85cfadc6f94.png)

The article mentions above points. It doesn't highlight the fact that attack can be profitable in same way as burning minimum liquidity of `1` shares when `_decimalsOffset()` function returns `0` in vault. The attacker will be at loss initially but when subsequent depositors gets `0` shares, it may turn into profits for attacker.

I think that would be helpful to add in that blog.

---

<div class="post-metadata">

**Author:** ![Skyge](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/skyge/32/343_2.png) [@Skyge](https://forum.openzeppelin.com/u/Skyge)\
**Post date:** [September 23, 2024, 12:24am UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/8 "2024-09-23T00:24:42Z")

</div>

Emmm, not sure what do you mean, is it like this:

- Attacker deposits 1 asset
- Attacker donates some assets
- Attacker burn 1 share
- User deposits some assets

Or could you please explain the steps?

---

<div class="post-metadata">

**Author:** ![cryptomoon42](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@cryptomoon42](https://forum.openzeppelin.com/u/cryptomoon42)\
**Post date:** [September 23, 2024, 3:56am UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/9 "2024-09-23T03:56:52Z")

</div>

I meant something like following in ERC4626 vault where `_decimalsOffset()` returns 0. The attacker will be at loss initially but can turn it into profit eventually.

- Attacker deposits 1 asset
- Attacker shares =1
- total shares = 2(due to virtual shares)
- total asset = 2(due to virtual assets)

Victim wants to deposit 5000 but was frontrun:

Attacker donates = 10000

- Attacker shares =1
- total shares = 2(due to virtual shares)
- total asset = 10002

Attacker loss due to virtual share of `1` = 10002/2 = 5001

- Victim deposit = 5000
- shares alloted = 2 \* 5000/10002 = 0

Now,

- Attacker shares =1
- total shares = 2(due to virtual shares)
- Victim shares = 0
- total asset = 15002

Finally,  
Attacker total deposit as of now = 15002  
Attacker loss = 10000-15002/2 = 10000-7501 = 2499

With each deposits attacker will overcome loss and eventually get in profit. In this case if he had frontrun 3 victims with 5000 amount each then he would end up in profit

---

<div class="post-metadata">

**Author:** ![cryptomoon42](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@cryptomoon42](https://forum.openzeppelin.com/u/cryptomoon42)\
**Post date:** [September 24, 2024, 8:06am UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/10 "2024-09-24T08:06:51Z")

</div>

@Skyge have written the steps here: [ERC4626 Inflation attack discussion - #9 by cryptomoon42](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/9)

Not sure whether you are notified about the replies or not.

---

<div class="post-metadata">

**Author:** ![Skyge](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/skyge/32/343_2.png) [@Skyge](https://forum.openzeppelin.com/u/Skyge)\
**Post date:** [September 24, 2024, 10:33am UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/11 "2024-09-24T10:33:12Z")

</div>

Notice your explanation, but need more time to confirm.

---

<div class="post-metadata">

**Author:** ![Skyge](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/skyge/32/343_2.png) [@Skyge](https://forum.openzeppelin.com/u/Skyge)\
**Post date:** [January 24, 2025, 2:20am UTC](https://forum.openzeppelin.com/t/erc4626-inflation-attack-discussion/41643/12 "2025-01-24T02:20:22Z")

</div>

> [@cryptomoon42](#):
>
> I meant something like following in ERC4626 vault where `_decimalsOffset()` returns 0. The attacker will be at loss initially but can turn it into profit eventually.

Yes, you are right, have found something at the [original PR](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3979) about this, as it explained:

- An offset of 0 doesn't offer protection
- An offset of 9 makes attack very difficult, but changes the decimal value of the vault.
