ERC20WrapperUpgradeable.sol 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. __ERC20Wrapper_init_unchained(underlyingToken);
  20. }
  21. function __ERC20Wrapper_init_unchained(IERC20Upgradeable underlyingToken) internal onlyInitializing {
  22. underlying = underlyingToken;
  23. }
  24. /**
  25. * @dev Allow a user to deposit underlying tokens and mint the corresponding number of wrapped tokens.
  26. */
  27. function depositFor(address account, uint256 amount) public virtual returns (bool) {
  28. SafeERC20Upgradeable.safeTransferFrom(underlying, _msgSender(), address(this), amount);
  29. _mint(account, amount);
  30. return true;
  31. }
  32. /**
  33. * @dev Allow a user to burn a number of wrapped tokens and withdraw the corresponding number of underlying tokens.
  34. */
  35. function withdrawTo(address account, uint256 amount) public virtual returns (bool) {
  36. _burn(_msgSender(), amount);
  37. SafeERC20Upgradeable.safeTransfer(underlying, account, amount);
  38. return true;
  39. }
  40. /**
  41. * @dev Mint wrapped token to cover any underlyingTokens that would have been transfered by mistake. Internal
  42. * function that can be exposed with access control if desired.
  43. */
  44. function _recover(address account) internal virtual returns (uint256) {
  45. uint256 value = underlying.balanceOf(address(this)) - totalSupply();
  46. _mint(account, value);
  47. return value;
  48. }
  49. /**
  50. * This empty reserved space is put in place to allow future versions to add new
  51. * variables without shifting down storage in the inheritance chain.
  52. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  53. */
  54. uint256[50] private __gap;
  55. }