ERC20Wrapper.sol 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0-rc.0) (token/ERC20/extensions/ERC20Wrapper.sol)
  3. pragma solidity ^0.8.20;
  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. abstract contract ERC20Wrapper is ERC20 {
  14. IERC20 private immutable _underlying;
  15. /**
  16. * @dev The underlying token couldn't be wrapped.
  17. */
  18. error ERC20InvalidUnderlying(address token);
  19. constructor(IERC20 underlyingToken) {
  20. if (underlyingToken == this) {
  21. revert ERC20InvalidUnderlying(address(this));
  22. }
  23. _underlying = underlyingToken;
  24. }
  25. /**
  26. * @dev See {ERC20-decimals}.
  27. */
  28. function decimals() public view virtual override returns (uint8) {
  29. try IERC20Metadata(address(_underlying)).decimals() returns (uint8 value) {
  30. return value;
  31. } catch {
  32. return super.decimals();
  33. }
  34. }
  35. /**
  36. * @dev Returns the address of the underlying ERC-20 token that is being wrapped.
  37. */
  38. function underlying() public view returns (IERC20) {
  39. return _underlying;
  40. }
  41. /**
  42. * @dev Allow a user to deposit underlying tokens and mint the corresponding number of wrapped tokens.
  43. */
  44. function depositFor(address account, uint256 value) public virtual returns (bool) {
  45. address sender = _msgSender();
  46. if (sender == address(this)) {
  47. revert ERC20InvalidSender(address(this));
  48. }
  49. if (account == address(this)) {
  50. revert ERC20InvalidReceiver(account);
  51. }
  52. SafeERC20.safeTransferFrom(_underlying, sender, address(this), value);
  53. _mint(account, value);
  54. return true;
  55. }
  56. /**
  57. * @dev Allow a user to burn a number of wrapped tokens and withdraw the corresponding number of underlying tokens.
  58. */
  59. function withdrawTo(address account, uint256 value) public virtual returns (bool) {
  60. if (account == address(this)) {
  61. revert ERC20InvalidReceiver(account);
  62. }
  63. _burn(_msgSender(), value);
  64. SafeERC20.safeTransfer(_underlying, account, value);
  65. return true;
  66. }
  67. /**
  68. * @dev Mint wrapped token to cover any underlyingTokens that would have been transferred by mistake. Internal
  69. * function that can be exposed with access control if desired.
  70. */
  71. function _recover(address account) internal virtual returns (uint256) {
  72. uint256 value = _underlying.balanceOf(address(this)) - totalSupply();
  73. _mint(account, value);
  74. return value;
  75. }
  76. }