ERC20WrapperUpgradeable.sol 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/ERC20Wrapper.sol)
  3. pragma solidity ^0.8.0;
  4. import "../ERC20Upgradeable.sol";
  5. import "../utils/SafeERC20Upgradeable.sol";
  6. import "../../../proxy/utils/Initializable.sol";
  7. /**
  8. * @dev Extension of the ERC20 token contract to support token wrapping.
  9. *
  10. * Users can deposit and withdraw "underlying tokens" and receive a matching number of "wrapped tokens". This is useful
  11. * in conjunction with other modules. For example, combining this wrapping mechanism with {ERC20Votes} will allow the
  12. * wrapping of an existing "basic" ERC20 into a governance token.
  13. *
  14. * _Available since v4.2._
  15. */
  16. abstract contract ERC20WrapperUpgradeable is Initializable, ERC20Upgradeable {
  17. IERC20Upgradeable public underlying;
  18. function __ERC20Wrapper_init(IERC20Upgradeable underlyingToken) internal onlyInitializing {
  19. __Context_init_unchained();
  20. __ERC20Wrapper_init_unchained(underlyingToken);
  21. }
  22. function __ERC20Wrapper_init_unchained(IERC20Upgradeable underlyingToken) internal onlyInitializing {
  23. underlying = underlyingToken;
  24. }
  25. /**
  26. * @dev Allow a user to deposit underlying tokens and mint the corresponding number of wrapped tokens.
  27. */
  28. function depositFor(address account, uint256 amount) public virtual returns (bool) {
  29. SafeERC20Upgradeable.safeTransferFrom(underlying, _msgSender(), address(this), amount);
  30. _mint(account, amount);
  31. return true;
  32. }
  33. /**
  34. * @dev Allow a user to burn a number of wrapped tokens and withdraw the corresponding number of underlying tokens.
  35. */
  36. function withdrawTo(address account, uint256 amount) public virtual returns (bool) {
  37. _burn(_msgSender(), amount);
  38. SafeERC20Upgradeable.safeTransfer(underlying, account, amount);
  39. return true;
  40. }
  41. /**
  42. * @dev Mint wrapped token to cover any underlyingTokens that would have been transfered by mistake. Internal
  43. * function that can be exposed with access control if desired.
  44. */
  45. function _recover(address account) internal virtual returns (uint256) {
  46. uint256 value = underlying.balanceOf(address(this)) - totalSupply();
  47. _mint(account, value);
  48. return value;
  49. }
  50. uint256[50] private __gap;
  51. }