ERC20Wrapper.sol 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/extensions/ERC20Wrapper.sol)
  3. pragma solidity ^0.8.19;
  4. import {IERC20, IERC20Metadata, ERC20} from "../ERC20.sol";
  5. import {SafeERC20} from "../utils/SafeERC20.sol";
  6. /**
  7. * @dev Extension of the ERC20 token contract to support token wrapping.
  8. *
  9. * Users can deposit and withdraw "underlying tokens" and receive a matching number of "wrapped tokens". This is useful
  10. * in conjunction with other modules. For example, combining this wrapping mechanism with {ERC20Votes} will allow the
  11. * wrapping of an existing "basic" ERC20 into a governance token.
  12. *
  13. * _Available since v4.2._
  14. */
  15. abstract contract ERC20Wrapper is ERC20 {
  16. IERC20 private immutable _underlying;
  17. /**
  18. * @dev The underlying token couldn't be wrapped.
  19. */
  20. error ERC20InvalidUnderlying(address token);
  21. constructor(IERC20 underlyingToken) {
  22. if (underlyingToken == this) {
  23. revert ERC20InvalidUnderlying(address(this));
  24. }
  25. _underlying = underlyingToken;
  26. }
  27. /**
  28. * @dev See {ERC20-decimals}.
  29. */
  30. function decimals() public view virtual override returns (uint8) {
  31. try IERC20Metadata(address(_underlying)).decimals() returns (uint8 value) {
  32. return value;
  33. } catch {
  34. return super.decimals();
  35. }
  36. }
  37. /**
  38. * @dev Returns the address of the underlying ERC-20 token that is being wrapped.
  39. */
  40. function underlying() public view returns (IERC20) {
  41. return _underlying;
  42. }
  43. /**
  44. * @dev Allow a user to deposit underlying tokens and mint the corresponding number of wrapped tokens.
  45. */
  46. function depositFor(address account, uint256 value) public virtual returns (bool) {
  47. address sender = _msgSender();
  48. if (sender == address(this)) {
  49. revert ERC20InvalidSender(address(this));
  50. }
  51. if (account == address(this)) {
  52. revert ERC20InvalidReceiver(account);
  53. }
  54. SafeERC20.safeTransferFrom(_underlying, sender, address(this), value);
  55. _mint(account, value);
  56. return true;
  57. }
  58. /**
  59. * @dev Allow a user to burn a number of wrapped tokens and withdraw the corresponding number of underlying tokens.
  60. */
  61. function withdrawTo(address account, uint256 value) public virtual returns (bool) {
  62. if (account == address(this)) {
  63. revert ERC20InvalidReceiver(account);
  64. }
  65. _burn(_msgSender(), value);
  66. SafeERC20.safeTransfer(_underlying, account, value);
  67. return true;
  68. }
  69. /**
  70. * @dev Mint wrapped token to cover any underlyingTokens that would have been transferred by mistake. Internal
  71. * function that can be exposed with access control if desired.
  72. */
  73. function _recover(address account) internal virtual returns (uint256) {
  74. uint256 value = _underlying.balanceOf(address(this)) - totalSupply();
  75. _mint(account, value);
  76. return value;
  77. }
  78. }