ERC4626Fees.sol 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../../token/ERC20/extensions/ERC4626.sol";
  4. /// @dev ERC4626 vault with entry/exit fees expressed in https://en.wikipedia.org/wiki/Basis_point[basis point (bp)].
  5. abstract contract ERC4626Fees is ERC4626 {
  6. using Math for uint256;
  7. uint256 private constant _BASIS_POINT_SCALE = 1e4;
  8. // === Overrides ===
  9. /// @dev Preview taking an entry fee on deposit. See {IERC4626-previewDeposit}.
  10. function previewDeposit(uint256 assets) public view virtual override returns (uint256) {
  11. uint256 fee = _feeOnTotal(assets, _entryFeeBasisPoints());
  12. return super.previewDeposit(assets - fee);
  13. }
  14. /// @dev Preview adding an entry fee on mint. See {IERC4626-previewMint}.
  15. function previewMint(uint256 shares) public view virtual override returns (uint256) {
  16. uint256 assets = super.previewMint(shares);
  17. return assets + _feeOnRaw(assets, _entryFeeBasisPoints());
  18. }
  19. /// @dev Preview adding an exit fee on withdraw. See {IERC4626-previewWithdraw}.
  20. function previewWithdraw(uint256 assets) public view virtual override returns (uint256) {
  21. uint256 fee = _feeOnRaw(assets, _exitFeeBasisPoints());
  22. return super.previewWithdraw(assets + fee);
  23. }
  24. /// @dev Preview taking an exit fee on redeem. See {IERC4626-previewRedeem}.
  25. function previewRedeem(uint256 shares) public view virtual override returns (uint256) {
  26. uint256 assets = super.previewRedeem(shares);
  27. return assets - _feeOnTotal(assets, _exitFeeBasisPoints());
  28. }
  29. /// @dev Send entry fee to {_entryFeeRecipient}. See {IERC4626-_deposit}.
  30. function _deposit(address caller, address receiver, uint256 assets, uint256 shares) internal virtual override {
  31. uint256 fee = _feeOnTotal(assets, _entryFeeBasisPoints());
  32. address recipient = _entryFeeRecipient();
  33. super._deposit(caller, receiver, assets, shares);
  34. if (fee > 0 && recipient != address(this)) {
  35. SafeERC20.safeTransfer(IERC20(asset()), recipient, fee);
  36. }
  37. }
  38. /// @dev Send exit fee to {_exitFeeRecipient}. See {IERC4626-_deposit}.
  39. function _withdraw(
  40. address caller,
  41. address receiver,
  42. address owner,
  43. uint256 assets,
  44. uint256 shares
  45. ) internal virtual override {
  46. uint256 fee = _feeOnRaw(assets, _exitFeeBasisPoints());
  47. address recipient = _exitFeeRecipient();
  48. super._withdraw(caller, receiver, owner, assets, shares);
  49. if (fee > 0 && recipient != address(this)) {
  50. SafeERC20.safeTransfer(IERC20(asset()), recipient, fee);
  51. }
  52. }
  53. // === Fee configuration ===
  54. function _entryFeeBasisPoints() internal view virtual returns (uint256) {
  55. return 0; // replace with e.g. 100 for 1%
  56. }
  57. function _exitFeeBasisPoints() internal view virtual returns (uint256) {
  58. return 0; // replace with e.g. 100 for 1%
  59. }
  60. function _entryFeeRecipient() internal view virtual returns (address) {
  61. return address(0); // replace with e.g. a treasury address
  62. }
  63. function _exitFeeRecipient() internal view virtual returns (address) {
  64. return address(0); // replace with e.g. a treasury address
  65. }
  66. // === Fee operations ===
  67. /// @dev Calculates the fees that should be added to an amount `assets` that does not already include fees.
  68. /// Used in {IERC4626-mint} and {IERC4626-withdraw} operations.
  69. function _feeOnRaw(uint256 assets, uint256 feeBasisPoints) private pure returns (uint256) {
  70. return assets.mulDiv(feeBasisPoints, _BASIS_POINT_SCALE, Math.Rounding.Up);
  71. }
  72. /// @dev Calculates the fee part of an amount `assets` that already includes fees.
  73. /// Used in {IERC4626-deposit} and {IERC4626-redeem} operations.
  74. function _feeOnTotal(uint256 assets, uint256 feeBasisPoints) private pure returns (uint256) {
  75. return assets.mulDiv(feeBasisPoints, feeBasisPoints + _BASIS_POINT_SCALE, Math.Rounding.Up);
  76. }
  77. }