ERC4626.sol 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.0) (token/ERC20/extensions/ERC4626.sol)
  3. pragma solidity ^0.8.0;
  4. import "../ERC20.sol";
  5. import "../utils/SafeERC20.sol";
  6. import "../../../interfaces/IERC4626.sol";
  7. import "../../../utils/math/Math.sol";
  8. /**
  9. * @dev Implementation of the ERC4626 "Tokenized Vault Standard" as defined in
  10. * https://eips.ethereum.org/EIPS/eip-4626[EIP-4626].
  11. *
  12. * This extension allows the minting and burning of "shares" (represented using the ERC20 inheritance) in exchange for
  13. * underlying "assets" through standardized {deposit}, {mint}, {redeem} and {burn} workflows. This contract extends
  14. * the ERC20 standard. Any additional extensions included along it would affect the "shares" token represented by this
  15. * contract and not the "assets" token which is an independent contract.
  16. *
  17. * CAUTION: Deposits and withdrawals may incur unexpected slippage. Users should verify that the amount received of
  18. * shares or assets is as expected. EOAs should operate through a wrapper that performs these checks such as
  19. * https://github.com/fei-protocol/ERC4626#erc4626router-and-base[ERC4626Router].
  20. *
  21. * _Available since v4.7._
  22. */
  23. abstract contract ERC4626 is ERC20, IERC4626 {
  24. using Math for uint256;
  25. IERC20Metadata private immutable _asset;
  26. /**
  27. * @dev Set the underlying asset contract. This must be an ERC20-compatible contract (ERC20 or ERC777).
  28. */
  29. constructor(IERC20Metadata asset_) {
  30. _asset = asset_;
  31. }
  32. /** @dev See {IERC4626-asset}. */
  33. function asset() public view virtual override returns (address) {
  34. return address(_asset);
  35. }
  36. /** @dev See {IERC4626-totalAssets}. */
  37. function totalAssets() public view virtual override returns (uint256) {
  38. return _asset.balanceOf(address(this));
  39. }
  40. /** @dev See {IERC4626-convertToShares}. */
  41. function convertToShares(uint256 assets) public view virtual override returns (uint256 shares) {
  42. return _convertToShares(assets, Math.Rounding.Down);
  43. }
  44. /** @dev See {IERC4626-convertToAssets}. */
  45. function convertToAssets(uint256 shares) public view virtual override returns (uint256 assets) {
  46. return _convertToAssets(shares, Math.Rounding.Down);
  47. }
  48. /** @dev See {IERC4626-maxDeposit}. */
  49. function maxDeposit(address) public view virtual override returns (uint256) {
  50. return _isVaultCollateralized() ? type(uint256).max : 0;
  51. }
  52. /** @dev See {IERC4626-maxMint}. */
  53. function maxMint(address) public view virtual override returns (uint256) {
  54. return type(uint256).max;
  55. }
  56. /** @dev See {IERC4626-maxWithdraw}. */
  57. function maxWithdraw(address owner) public view virtual override returns (uint256) {
  58. return _convertToAssets(balanceOf(owner), Math.Rounding.Down);
  59. }
  60. /** @dev See {IERC4626-maxRedeem}. */
  61. function maxRedeem(address owner) public view virtual override returns (uint256) {
  62. return balanceOf(owner);
  63. }
  64. /** @dev See {IERC4626-previewDeposit}. */
  65. function previewDeposit(uint256 assets) public view virtual override returns (uint256) {
  66. return _convertToShares(assets, Math.Rounding.Down);
  67. }
  68. /** @dev See {IERC4626-previewMint}. */
  69. function previewMint(uint256 shares) public view virtual override returns (uint256) {
  70. return _convertToAssets(shares, Math.Rounding.Up);
  71. }
  72. /** @dev See {IERC4626-previewWithdraw}. */
  73. function previewWithdraw(uint256 assets) public view virtual override returns (uint256) {
  74. return _convertToShares(assets, Math.Rounding.Up);
  75. }
  76. /** @dev See {IERC4626-previewRedeem}. */
  77. function previewRedeem(uint256 shares) public view virtual override returns (uint256) {
  78. return _convertToAssets(shares, Math.Rounding.Down);
  79. }
  80. /** @dev See {IERC4626-deposit}. */
  81. function deposit(uint256 assets, address receiver) public virtual override returns (uint256) {
  82. require(assets <= maxDeposit(receiver), "ERC4626: deposit more than max");
  83. uint256 shares = previewDeposit(assets);
  84. _deposit(_msgSender(), receiver, assets, shares);
  85. return shares;
  86. }
  87. /** @dev See {IERC4626-mint}. */
  88. function mint(uint256 shares, address receiver) public virtual override returns (uint256) {
  89. require(shares <= maxMint(receiver), "ERC4626: mint more than max");
  90. uint256 assets = previewMint(shares);
  91. _deposit(_msgSender(), receiver, assets, shares);
  92. return assets;
  93. }
  94. /** @dev See {IERC4626-withdraw}. */
  95. function withdraw(
  96. uint256 assets,
  97. address receiver,
  98. address owner
  99. ) public virtual override returns (uint256) {
  100. require(assets <= maxWithdraw(owner), "ERC4626: withdraw more than max");
  101. uint256 shares = previewWithdraw(assets);
  102. _withdraw(_msgSender(), receiver, owner, assets, shares);
  103. return shares;
  104. }
  105. /** @dev See {IERC4626-redeem}. */
  106. function redeem(
  107. uint256 shares,
  108. address receiver,
  109. address owner
  110. ) public virtual override returns (uint256) {
  111. require(shares <= maxRedeem(owner), "ERC4626: redeem more than max");
  112. uint256 assets = previewRedeem(shares);
  113. _withdraw(_msgSender(), receiver, owner, assets, shares);
  114. return assets;
  115. }
  116. /**
  117. * @dev Internal conversion function (from assets to shares) with support for rounding direction.
  118. *
  119. * Will revert if assets > 0, totalSupply > 0 and totalAssets = 0. That corresponds to a case where any asset
  120. * would represent an infinite amount of shares.
  121. */
  122. function _convertToShares(uint256 assets, Math.Rounding rounding) internal view virtual returns (uint256 shares) {
  123. uint256 supply = totalSupply();
  124. return
  125. (assets == 0 || supply == 0)
  126. ? assets.mulDiv(10**decimals(), 10**_asset.decimals(), rounding)
  127. : assets.mulDiv(supply, totalAssets(), rounding);
  128. }
  129. /**
  130. * @dev Internal conversion function (from shares to assets) with support for rounding direction.
  131. */
  132. function _convertToAssets(uint256 shares, Math.Rounding rounding) internal view virtual returns (uint256 assets) {
  133. uint256 supply = totalSupply();
  134. return
  135. (supply == 0)
  136. ? shares.mulDiv(10**_asset.decimals(), 10**decimals(), rounding)
  137. : shares.mulDiv(totalAssets(), supply, rounding);
  138. }
  139. /**
  140. * @dev Deposit/mint common workflow.
  141. */
  142. function _deposit(
  143. address caller,
  144. address receiver,
  145. uint256 assets,
  146. uint256 shares
  147. ) internal virtual {
  148. // If _asset is ERC777, `transferFrom` can trigger a reenterancy BEFORE the transfer happens through the
  149. // `tokensToSend` hook. On the other hand, the `tokenReceived` hook, that is triggered after the transfer,
  150. // calls the vault, which is assumed not malicious.
  151. //
  152. // Conclusion: we need to do the transfer before we mint so that any reentrancy would happen before the
  153. // assets are transferred and before the shares are minted, which is a valid state.
  154. // slither-disable-next-line reentrancy-no-eth
  155. SafeERC20.safeTransferFrom(_asset, caller, address(this), assets);
  156. _mint(receiver, shares);
  157. emit Deposit(caller, receiver, assets, shares);
  158. }
  159. /**
  160. * @dev Withdraw/redeem common workflow.
  161. */
  162. function _withdraw(
  163. address caller,
  164. address receiver,
  165. address owner,
  166. uint256 assets,
  167. uint256 shares
  168. ) internal virtual {
  169. if (caller != owner) {
  170. _spendAllowance(owner, caller, shares);
  171. }
  172. // If _asset is ERC777, `transfer` can trigger a reentrancy AFTER the transfer happens through the
  173. // `tokensReceived` hook. On the other hand, the `tokensToSend` hook, that is triggered before the transfer,
  174. // calls the vault, which is assumed not malicious.
  175. //
  176. // Conclusion: we need to do the transfer after the burn so that any reentrancy would happen after the
  177. // shares are burned and after the assets are transferred, which is a valid state.
  178. _burn(owner, shares);
  179. SafeERC20.safeTransfer(_asset, receiver, assets);
  180. emit Withdraw(caller, receiver, owner, assets, shares);
  181. }
  182. function _isVaultCollateralized() private view returns (bool) {
  183. return totalAssets() > 0 || totalSupply() == 0;
  184. }
  185. }