1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v5.4.0-rc.0) (token/ERC20/extensions/ERC20Wrapper.sol)
- pragma solidity ^0.8.20;
- import {IERC20, IERC20Metadata, ERC20} from "../ERC20.sol";
- import {SafeERC20} from "../utils/SafeERC20.sol";
- /**
- * @dev Extension of the ERC-20 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" ERC-20 into a governance token.
- *
- * WARNING: Any mechanism in which the underlying token changes the {balanceOf} of an account without an explicit transfer
- * may desynchronize this contract's supply and its underlying balance. Please exercise caution when wrapping tokens that
- * may undercollateralize the wrapper (i.e. wrapper's total supply is higher than its underlying balance). See {_recover}
- * for recovering value accrued to the wrapper.
- */
- abstract contract ERC20Wrapper is ERC20 {
- IERC20 private immutable _underlying;
- /**
- * @dev The underlying token couldn't be wrapped.
- */
- error ERC20InvalidUnderlying(address token);
- constructor(IERC20 underlyingToken) {
- if (underlyingToken == this) {
- revert ERC20InvalidUnderlying(address(this));
- }
- _underlying = underlyingToken;
- }
- /// @inheritdoc IERC20Metadata
- function decimals() public view virtual override returns (uint8) {
- try IERC20Metadata(address(_underlying)).decimals() returns (uint8 value) {
- return value;
- } catch {
- return super.decimals();
- }
- }
- /**
- * @dev Returns the address of the underlying ERC-20 token that is being wrapped.
- */
- function underlying() public view returns (IERC20) {
- return _underlying;
- }
- /**
- * @dev Allow a user to deposit underlying tokens and mint the corresponding number of wrapped tokens.
- */
- function depositFor(address account, uint256 value) public virtual returns (bool) {
- address sender = _msgSender();
- if (sender == address(this)) {
- revert ERC20InvalidSender(address(this));
- }
- if (account == address(this)) {
- revert ERC20InvalidReceiver(account);
- }
- SafeERC20.safeTransferFrom(_underlying, sender, address(this), value);
- _mint(account, value);
- 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 value) public virtual returns (bool) {
- if (account == address(this)) {
- revert ERC20InvalidReceiver(account);
- }
- _burn(_msgSender(), value);
- SafeERC20.safeTransfer(_underlying, account, value);
- return true;
- }
- /**
- * @dev Mint wrapped token to cover any underlyingTokens that would have been transferred by mistake or acquired from
- * rebasing mechanisms. 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;
- }
- }
|