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

Yield fees (Step 2) #210

Merged
merged 110 commits into from
Feb 5, 2024
Merged

Yield fees (Step 2) #210

merged 110 commits into from
Feb 5, 2024

Conversation

EndymionJkb
Copy link
Collaborator

@EndymionJkb EndymionJkb commented Dec 29, 2023

Description

Introduce yield fee computation in computePoolData (formerly getPoolData).

There are some tricky reentrancy issues. I believe computePoolData has to update the balances (otherwise, the "before" call on a swap would be inaccurate), which means it needs to be reentrant.

This is fine except for initialize (probably should be initializePool, come to think of it, so that people don't think the Vault is being initialized...), which is already reentrant. So I made a non-reentrant initialize (with no external calls), and a reentrant _initialize with the callback, similar to swap/_swap, addLiquidity/_addLiquidity, etc.

Generalized the protocol fee handling (since we now have both swap and yield fees, which are collected together), and added events to enable fine-grained tracking by pool (and also validation through a simple invariant).

Deferring "boosted" pool handling, buffers, etc. And deferring optimizing pool balance storage (see separate PR for the infrastructure for that), since those are huge things by themselves.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Dependency changes
  • Code refactor / cleanup
  • Documentation or wording changes
  • Other

Checklist:

  • The diff is legible and has no extraneous changes
  • Complex code has been commented, including external interfaces
  • Tests have 100% code coverage
  • The base branch is either main, or there's a description of how to merge

Issue Resolution

Closes #171

# Conflicts:
#	pkg/vault/contracts/Vault.sol
# Conflicts:
#	pkg/interfaces/contracts/vault/IVault.sol
#	pkg/pool-weighted/contracts/WeightedPool8020Factory.sol
#	pkg/pool-weighted/contracts/WeightedPoolFactory.sol
#	pkg/pool-weighted/test/foundry/WeightedPool.t.sol
#	pkg/vault/contracts/test/PoolFactoryMock.sol
#	pkg/vault/contracts/test/VaultMock.sol
# Conflicts:
#	pkg/vault/contracts/Vault.sol
Copy link

openzeppelin-code bot commented Jan 2, 2024

Yield fees (Step 2)

Generated at commit: 5df7732eea9e5402d36b0859a9f8988377d26d71

🚨 Report Summary

Severity Level Results
Contracts Critical
High
Medium
Low
Note
Total
0
0
0
13
36
49
Dependencies Critical
High
Medium
Low
Note
Total
0
0
0
0
0
0

For more details view the full report in OpenZeppelin Code Inspector

Base automatically changed from generalize-protocol-fees to main January 26, 2024 18:19
Copy link
Contributor

@jubeira jubeira left a comment

Choose a reason for hiding this comment

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

Nice job with the tests.
I'll add a few more; just one more comment for now.

pkg/vault/contracts/VaultCommon.sol Outdated Show resolved Hide resolved
# Conflicts:
#	pkg/vault/.forge-snapshots/routerRemoveLiquidityNative.snap
#	pkg/vault/.forge-snapshots/routerRemoveLiquidityWETH.snap
#	pkg/vault/.forge-snapshots/vaultRemoveLiquidityProportional.snap
#	pkg/vault/.forge-snapshots/vaultRemoveLiquiditySingleTokenExactIn.snap
#	pkg/vault/.forge-snapshots/vaultRemoveLiquiditySingleTokenExactOut.snap
# Conflicts:
#	pkg/vault/.forge-snapshots/routerAddLiquidityNative.snap
#	pkg/vault/.forge-snapshots/routerAddLiquidityWETH.snap
#	pkg/vault/.forge-snapshots/routerRemoveLiquidityNative.snap
#	pkg/vault/.forge-snapshots/routerRemoveLiquidityWETH.snap
#	pkg/vault/.forge-snapshots/routerSwapExactInNative.snap
#	pkg/vault/.forge-snapshots/routerSwapExactInWETH.snap
#	pkg/vault/.forge-snapshots/vaultAddLiquiditySingleTokenExactOut.snap
#	pkg/vault/.forge-snapshots/vaultAddLiquidityUnbalanced.snap
#	pkg/vault/.forge-snapshots/vaultProtocolSwapFeeGivenIn.snap
#	pkg/vault/.forge-snapshots/vaultRemoveLiquidityProportional.snap
#	pkg/vault/.forge-snapshots/vaultRemoveLiquiditySingleTokenExactIn.snap
#	pkg/vault/.forge-snapshots/vaultRemoveLiquiditySingleTokenExactOut.snap
#	pkg/vault/.forge-snapshots/vaultSwapExactIn.snap
#	pkg/vault/.forge-snapshots/vaultSwapFeeGivenIn.snap
TokenType tokenType = poolData.tokenConfig[i].tokenType;

// Do not charge yield fees until the pool is initialized.
// ERC4626 tokens always pay yield fees; WITH_RATE tokens pay unless exempt.
Copy link
Contributor

Choose a reason for hiding this comment

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

The flag yieldFeeExempt is essentially ignored for ERC4626 in this case. This is not really clear in how we define the TokenConfig currently and will cause some confusion.

Copy link
Collaborator Author

@EndymionJkb EndymionJkb Feb 5, 2024

Choose a reason for hiding this comment

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

I've updated the TokenConfig documentation. I believe it's true that ERC4646 tokens charge yield fees by definition, and cannot be marked exempt. In the buffer PR #250, it explicitly checks for this (vs silently failing). This was something in the old "internal buffer" PR that didn't get copied over to the external pool one, so good catch.

Copy link
Contributor

@ylv-io ylv-io left a comment

Choose a reason for hiding this comment

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

Great job @EndymionJkb . Thank you for adding tests @jubeira 🙏

@EndymionJkb EndymionJkb merged commit 3161e98 into main Feb 5, 2024
8 checks passed
@EndymionJkb EndymionJkb deleted the yield-fees branch February 5, 2024 22:09
This was referenced Feb 6, 2024
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.

Yield exemption flag per token
5 participants