123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/extensions/ERC20Wrapper.sol)
- pragma solidity ^0.8.0;
- import "../ERC20.sol";
- import "../utils/SafeERC20.sol";
- /**
- * @dev Extension of the ERC20 token contract to support token wrapping.
- *
- * Users can deposit and withdraw "underlying tokens" and receive a matching number of "wrapped tokens". This is useful
- * in conjunction with other modules. For example, combining this wrapping mechanism with {ERC20Votes} will allow the
- * wrapping of an existing "basic" ERC20 into a governance token.
- *
- * _Available since v4.2._
- */
- abstract contract ERC20Wrapper is ERC20 {
- IERC20 public immutable underlying;
- constructor(IERC20 underlyingToken) {
- underlying = underlyingToken;
- }
- /**
- * @dev See {ERC20-decimals}.
- */
- function decimals() public view virtual override returns (uint8) {
- try IERC20Metadata(address(underlying)).decimals() returns (uint8 value) {
- return value;
- } catch {
- return super.decimals();
- }
- }
- /**
- * @dev Allow a user to deposit underlying tokens and mint the corresponding number of wrapped tokens.
- */
- function depositFor(address account, uint256 amount) public virtual returns (bool) {
- SafeERC20.safeTransferFrom(underlying, _msgSender(), address(this), amount);
- _mint(account, amount);
- return true;
- }
- /**
- * @dev Allow a user to burn a number of wrapped tokens and withdraw the corresponding number of underlying tokens.
- */
- function withdrawTo(address account, uint256 amount) public virtual returns (bool) {
- _burn(_msgSender(), amount);
- SafeERC20.safeTransfer(underlying, account, amount);
- return true;
- }
- /**
- * @dev Mint wrapped token to cover any underlyingTokens that would have been transferred by mistake. Internal
- * function that can be exposed with access control if desired.
- */
- function _recover(address account) internal virtual returns (uint256) {
- uint256 value = underlying.balanceOf(address(this)) - totalSupply();
- _mint(account, value);
- return value;
- }
- }
|