|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "./ERC721Base.sol"; |
| 5 | + |
| 6 | +import { TokenStore, ERC1155Receiver, IERC1155Receiver } from "../extension/TokenStore.sol"; |
| 7 | +import { Multicall } from "../extension/Multicall.sol"; |
| 8 | +import "../extension/SoulboundERC721A.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * BASE: ERC721Base |
| 12 | + * EXTENSION: TokenStore, SoulboundERC721A |
| 13 | + * |
| 14 | + * The `ERC721Multiwrap` contract uses the `ERC721Base` contract, along with the `TokenStore` and |
| 15 | + * `SoulboundERC721A` extension. |
| 16 | + * |
| 17 | + * The `ERC721Multiwrap` contract lets you wrap arbitrary ERC20, ERC721 and ERC1155 tokens you own |
| 18 | + * into a single wrapped token / NFT. |
| 19 | + * |
| 20 | + * The `SoulboundERC721A` extension lets you make your NFTs 'soulbound' i.e. non-transferrable. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +contract ERC721Multiwrap is Multicall, TokenStore, SoulboundERC721A, ERC721Base { |
| 25 | + /*////////////////////////////////////////////////////////////// |
| 26 | + Permission control roles |
| 27 | + //////////////////////////////////////////////////////////////*/ |
| 28 | + |
| 29 | + /// @dev Only MINTER_ROLE holders can wrap tokens, when wrapping is restricted. |
| 30 | + bytes32 private constant MINTER_ROLE = keccak256("MINTER_ROLE"); |
| 31 | + /// @dev Only UNWRAP_ROLE holders can unwrap tokens, when unwrapping is restricted. |
| 32 | + bytes32 private constant UNWRAP_ROLE = keccak256("UNWRAP_ROLE"); |
| 33 | + /// @dev Only assets with ASSET_ROLE can be wrapped, when wrapping is restricted to particular assets. |
| 34 | + bytes32 private constant ASSET_ROLE = keccak256("ASSET_ROLE"); |
| 35 | + |
| 36 | + /*////////////////////////////////////////////////////////////// |
| 37 | + Events |
| 38 | + //////////////////////////////////////////////////////////////*/ |
| 39 | + |
| 40 | + /// @dev Emitted when tokens are wrapped. |
| 41 | + event TokensWrapped( |
| 42 | + address indexed wrapper, |
| 43 | + address indexed recipientOfWrappedToken, |
| 44 | + uint256 indexed tokenIdOfWrappedToken, |
| 45 | + Token[] wrappedContents |
| 46 | + ); |
| 47 | + |
| 48 | + /// @dev Emitted when tokens are unwrapped. |
| 49 | + event TokensUnwrapped( |
| 50 | + address indexed unwrapper, |
| 51 | + address indexed recipientOfWrappedContents, |
| 52 | + uint256 indexed tokenIdOfWrappedToken |
| 53 | + ); |
| 54 | + |
| 55 | + /*////////////////////////////////////////////////////////////// |
| 56 | + Events |
| 57 | + //////////////////////////////////////////////////////////////*/ |
| 58 | + |
| 59 | + /// @notice Checks whether the caller holds `role`, when restrictions for `role` are switched on. |
| 60 | + modifier onlyRoleWithSwitch(bytes32 role) { |
| 61 | + _checkRoleWithSwitch(role, msg.sender); |
| 62 | + _; |
| 63 | + } |
| 64 | + |
| 65 | + /*////////////////////////////////////////////////////////////// |
| 66 | + Constructor |
| 67 | + //////////////////////////////////////////////////////////////*/ |
| 68 | + |
| 69 | + constructor( |
| 70 | + string memory _name, |
| 71 | + string memory _symbol, |
| 72 | + address _royaltyRecipient, |
| 73 | + uint128 _royaltyBps, |
| 74 | + address _nativeTokenWrapper |
| 75 | + ) ERC721Base(_name, _symbol, _royaltyRecipient, _royaltyBps) TokenStore(_nativeTokenWrapper) { |
| 76 | + restrictTransfers(false); |
| 77 | + } |
| 78 | + |
| 79 | + /*/////////////////////////////////////////////////////////////// |
| 80 | + Public gette functions |
| 81 | + //////////////////////////////////////////////////////////////*/ |
| 82 | + |
| 83 | + /// @dev See ERC-165 |
| 84 | + function supportsInterface(bytes4 interfaceId) |
| 85 | + public |
| 86 | + view |
| 87 | + virtual |
| 88 | + override(ERC1155Receiver, ERC721Base) |
| 89 | + returns (bool) |
| 90 | + { |
| 91 | + return |
| 92 | + super.supportsInterface(interfaceId) || |
| 93 | + ERC721Base.supportsInterface(interfaceId) || |
| 94 | + interfaceId == type(IERC1155Receiver).interfaceId; |
| 95 | + } |
| 96 | + |
| 97 | + /*/////////////////////////////////////////////////////////////// |
| 98 | + Wrapping / Unwrapping logic |
| 99 | + //////////////////////////////////////////////////////////////*/ |
| 100 | + |
| 101 | + /** |
| 102 | + * @notice Wrap multiple ERC1155, ERC721, ERC20 tokens into a single wrapped NFT. |
| 103 | + * |
| 104 | + * @param _tokensToWrap The tokens to wrap. |
| 105 | + * @param _uriForWrappedToken The metadata URI for the wrapped NFT. |
| 106 | + * @param _recipient The recipient of the wrapped NFT. |
| 107 | + * |
| 108 | + * @return tokenId The tokenId of the wrapped NFT minted. |
| 109 | + */ |
| 110 | + function wrap( |
| 111 | + Token[] calldata _tokensToWrap, |
| 112 | + string calldata _uriForWrappedToken, |
| 113 | + address _recipient |
| 114 | + ) public payable virtual onlyRoleWithSwitch(MINTER_ROLE) returns (uint256 tokenId) { |
| 115 | + if (!hasRole(ASSET_ROLE, address(0))) { |
| 116 | + for (uint256 i = 0; i < _tokensToWrap.length; i += 1) { |
| 117 | + _checkRole(ASSET_ROLE, _tokensToWrap[i].assetContract); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + tokenId = nextTokenIdToMint(); |
| 122 | + |
| 123 | + _storeTokens(msg.sender, _tokensToWrap, _uriForWrappedToken, tokenId); |
| 124 | + |
| 125 | + _safeMint(_recipient, 1); |
| 126 | + |
| 127 | + emit TokensWrapped(msg.sender, _recipient, tokenId, _tokensToWrap); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @notice Unwrap a wrapped NFT to retrieve underlying ERC1155, ERC721, ERC20 tokens. |
| 132 | + * |
| 133 | + * @param _tokenId The token Id of the wrapped NFT to unwrap. |
| 134 | + * @param _recipient The recipient of the underlying ERC1155, ERC721, ERC20 tokens of the wrapped NFT. |
| 135 | + */ |
| 136 | + function unwrap(uint256 _tokenId, address _recipient) public virtual onlyRoleWithSwitch(UNWRAP_ROLE) { |
| 137 | + require(_tokenId < nextTokenIdToMint(), "wrapped NFT DNE."); |
| 138 | + require(isApprovedOrOwner(msg.sender, _tokenId), "caller not approved for unwrapping."); |
| 139 | + |
| 140 | + _burn(_tokenId); |
| 141 | + _releaseTokens(_recipient, _tokenId); |
| 142 | + |
| 143 | + emit TokensUnwrapped(msg.sender, _recipient, _tokenId); |
| 144 | + } |
| 145 | + |
| 146 | + /*/////////////////////////////////////////////////////////////// |
| 147 | + Internal functions |
| 148 | + //////////////////////////////////////////////////////////////*/ |
| 149 | + |
| 150 | + /// @dev See {ERC721-_beforeTokenTransfer}. |
| 151 | + function _beforeTokenTransfers( |
| 152 | + address from, |
| 153 | + address to, |
| 154 | + uint256 startTokenId, |
| 155 | + uint256 quantity |
| 156 | + ) internal virtual override(ERC721A, SoulboundERC721A) { |
| 157 | + super._beforeTokenTransfers(from, to, startTokenId, quantity); |
| 158 | + SoulboundERC721A._beforeTokenTransfers(from, to, startTokenId, quantity); |
| 159 | + } |
| 160 | + |
| 161 | + /// @dev Returns whether transfers can be restricted in a given execution context. |
| 162 | + function _canRestrictTransfers() internal virtual override returns (bool) { |
| 163 | + return msg.sender == owner(); |
| 164 | + } |
| 165 | +} |
0 commit comments