ERC20Wrapper.sol 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.4.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 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. /// @inheritdoc IERC20Metadata
  31. function decimals() public view virtual override returns (uint8) {
  32. try IERC20Metadata(address(_underlying)).decimals() returns (uint8 value) {
  33. return value;
  34. } catch {
  35. return super.decimals();
  36. }
  37. }
  38. /**
  39. * @dev Returns the address of the underlying ERC-20 token that is being wrapped.
  40. */
  41. function underlying() public view returns (IERC20) {
  42. return _underlying;
  43. }
  44. /**
  45. * @dev Allow a user to deposit underlying tokens and mint the corresponding number of wrapped tokens.
  46. */
  47. function depositFor(address account, uint256 value) public virtual returns (bool) {
  48. address sender = _msgSender();
  49. if (sender == address(this)) {
  50. revert ERC20InvalidSender(address(this));
  51. }
  52. if (account == address(this)) {
  53. revert ERC20InvalidReceiver(account);
  54. }
  55. SafeERC20.safeTransferFrom(_underlying, sender, address(this), value);
  56. _mint(account, value);
  57. return true;
  58. }
  59. /**
  60. * @dev Allow a user to burn a number of wrapped tokens and withdraw the corresponding number of underlying tokens.
  61. */
  62. function withdrawTo(address account, uint256 value) public virtual returns (bool) {
  63. if (account == address(this)) {
  64. revert ERC20InvalidReceiver(account);
  65. }
  66. _burn(_msgSender(), value);
  67. SafeERC20.safeTransfer(_underlying, account, value);
  68. return true;
  69. }
  70. /**
  71. * @dev Mint wrapped token to cover any underlyingTokens that would have been transferred by mistake or acquired from
  72. * rebasing mechanisms. Internal function that can be exposed with access control if desired.
  73. */
  74. function _recover(address account) internal virtual returns (uint256) {
  75. uint256 value = _underlying.balanceOf(address(this)) - totalSupply();
  76. _mint(account, value);
  77. return value;
  78. }
  79. }