ERC20Wrapper.sol 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.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 ERC-20 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" ERC-20 into a governance token.
  12. *
  13. * WARNING: Any mechanism in which the underlying token changes the {balanceOf} of an account without an explicit transfer
  14. * may desynchronize this contract's supply and its underlying balance. Please exercise caution when wrapping tokens that
  15. * may undercollateralize the wrapper (i.e. wrapper's total supply is higher than its underlying balance). See {_recover}
  16. * for recovering value accrued to the wrapper.
  17. */
  18. abstract contract ERC20Wrapper is ERC20 {
  19. IERC20 private immutable _underlying;
  20. /**
  21. * @dev The underlying token couldn't be wrapped.
  22. */
  23. error ERC20InvalidUnderlying(address token);
  24. constructor(IERC20 underlyingToken) {
  25. if (underlyingToken == this) {
  26. revert ERC20InvalidUnderlying(address(this));
  27. }
  28. _underlying = underlyingToken;
  29. }
  30. /**
  31. * @dev See {ERC20-decimals}.
  32. */
  33. function decimals() public view virtual override returns (uint8) {
  34. try IERC20Metadata(address(_underlying)).decimals() returns (uint8 value) {
  35. return value;
  36. } catch {
  37. return super.decimals();
  38. }
  39. }
  40. /**
  41. * @dev Returns the address of the underlying ERC-20 token that is being wrapped.
  42. */
  43. function underlying() public view returns (IERC20) {
  44. return _underlying;
  45. }
  46. /**
  47. * @dev Allow a user to deposit underlying tokens and mint the corresponding number of wrapped tokens.
  48. */
  49. function depositFor(address account, uint256 value) public virtual returns (bool) {
  50. address sender = _msgSender();
  51. if (sender == address(this)) {
  52. revert ERC20InvalidSender(address(this));
  53. }
  54. if (account == address(this)) {
  55. revert ERC20InvalidReceiver(account);
  56. }
  57. SafeERC20.safeTransferFrom(_underlying, sender, address(this), value);
  58. _mint(account, value);
  59. return true;
  60. }
  61. /**
  62. * @dev Allow a user to burn a number of wrapped tokens and withdraw the corresponding number of underlying tokens.
  63. */
  64. function withdrawTo(address account, uint256 value) public virtual returns (bool) {
  65. if (account == address(this)) {
  66. revert ERC20InvalidReceiver(account);
  67. }
  68. _burn(_msgSender(), value);
  69. SafeERC20.safeTransfer(_underlying, account, value);
  70. return true;
  71. }
  72. /**
  73. * @dev Mint wrapped token to cover any underlyingTokens that would have been transferred by mistake or acquired from
  74. * rebasing mechanisms. Internal function that can be exposed with access control if desired.
  75. */
  76. function _recover(address account) internal virtual returns (uint256) {
  77. uint256 value = _underlying.balanceOf(address(this)) - totalSupply();
  78. _mint(account, value);
  79. return value;
  80. }
  81. }