Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERC4626 inflation attack mitigation #3979

Merged
merged 27 commits into from Feb 17, 2023

Conversation

Amxx
Copy link
Collaborator

@Amxx Amxx commented Jan 19, 2023

Fixes #3706
Fixes LIB-599
Fixes LIB-653

Use virtual shares and assets to mitigate inflation attacks. The vault adds 1:N virtual asset/shares to enforce an initial exchange rate. This solves the issue of unhealty/broken vaults. It also limits the effectiveness of inflation attacks

The N value is 10**offset, with offset being the decimal (precision) difference between the vault and the underlying token.

Technically, the offset correspond the difference (in orders of magnitude) between the funds required by the attackers and the funds being deposited by the user.

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

Note that this is a breaking change: the presence of virtual shares means the vault is likely to "capture" some value, and 1 unit (wei) to underlying asset might be unrecoverable. The test file shows that on a typical lifecyle

Analysis of the offset effect on the inflation attack

PR Checklist

  • Tests
  • Documentation
  • Changeset entry (run npx changeset add)

@frangio frangio self-requested a review January 23, 2023 15:59
@changeset-bot
Copy link

changeset-bot bot commented Jan 25, 2023

🦋 Changeset detected

Latest commit: e708b4d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openzeppelin-solidity Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Amxx Amxx marked this pull request as ready for review February 10, 2023 11:22
contracts/token/ERC20/extensions/ERC4626.sol Outdated Show resolved Hide resolved
docs/modules/ROOT/pages/erc4626.adoc Outdated Show resolved Hide resolved
Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Solidity code looks good to me. I have to finish going through the tests and guide.

contracts/token/ERC20/extensions/ERC4626.sol Outdated Show resolved Hide resolved
contracts/token/ERC20/extensions/ERC4626.sol Outdated Show resolved Hide resolved
contracts/token/ERC20/extensions/ERC4626.sol Show resolved Hide resolved
test/token/ERC20/extensions/ERC4626.test.js Outdated Show resolved Hide resolved
docs/modules/ROOT/pages/erc4626.adoc Outdated Show resolved Hide resolved
Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments but all looks good to me.

contracts/token/ERC20/extensions/ERC4626.sol Outdated Show resolved Hide resolved
docs/modules/ROOT/pages/erc4626.adoc Show resolved Hide resolved
docs/modules/ROOT/pages/erc4626.adoc Outdated Show resolved Hide resolved
Amxx and others added 2 commits February 16, 2023 09:49
@Amxx Amxx requested a review from frangio February 16, 2023 16:44
Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Amxx Amxx merged commit d64d7aa into OpenZeppelin:master Feb 17, 2023
@Amxx Amxx deleted the fix/erc4626/decimalsoffset branch February 17, 2023 09:08
@boringcrypto
Copy link

You're welcome 😉

@frangio
Copy link
Contributor

frangio commented Feb 17, 2023

@boringcrypto Oh, not sure if this was your point but we should probably credit YieldBox in our docs. It took us a lot of time to be convinced but that was definitely how we got to this solution (#3706 (comment)). Do you know if you were the first to propose this approach?


const { tx } = await this.vault.deposit(parseToken(1), recipient, { from: holder });

await expectEvent.inTransaction(tx, this.token, 'Transfer', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I missed them somehow, but the tests do not include any event emission tests for Deposit and Withdraw. I would say those are the most important ones to test in this context.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed here: #4072

(you should work with us 😄 )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

highly appreciate your comment and am happy it improves the overall code quality - I can't commit anything full-time currently and will keep monitoring the OZ contracts as a side project for now (but never say never 😄)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement or recommend mitigations for ERC4626 inflation attacks
5 participants