// SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.0; // Interface import { ISignatureMint1155 } from "./ISignatureMint1155.sol"; // Token import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; // Protocol control center. import { ProtocolControl } from "../../ProtocolControl.sol"; // Signature utils import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; import "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol"; // Access Control + security import "@openzeppelin/contracts/access/AccessControlEnumerable.sol"; import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; // Royalties import "@openzeppelin/contracts/interfaces/IERC2981.sol"; // Meta transactions import "@openzeppelin/contracts/metatx/ERC2771Context.sol"; // Utils import "@openzeppelin/contracts/utils/Strings.sol"; import "@openzeppelin/contracts/utils/Multicall.sol"; // Helper interfaces import { IWETH } from "../../interfaces/IWETH.sol"; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; contract SignatureMint1155 is ISignatureMint1155, ERC1155, EIP712, AccessControlEnumerable, ERC2771Context, IERC2981, ReentrancyGuard, Multicall { using ECDSA for bytes32; using Strings for uint256; bytes32 private constant TYPEHASH = keccak256( "MintRequest(address to,address royaltyRecipient,uint256 royaltyBps,address primarySaleRecipient,uint256 tokenId,string uri,uint256 quantity,uint256 pricePerToken,address currency,uint128 validityStartTimestamp,uint128 validityEndTimestamp,bytes32 uid)" ); /// @dev Only TRANSFER_ROLE holders can have tokens transferred from or to them, during restricted transfers. bytes32 public constant TRANSFER_ROLE = keccak256("TRANSFER_ROLE"); /// @dev Only MINTER_ROLE holders can sign off on `MintRequest`s. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); /// @dev The address interpreted as native token of the chain. address private constant NATIVE_TOKEN = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; /// @dev The address of the native token wrapper contract. address public immutable nativeTokenWrapper; /// @dev The next token ID of the NFT to mint. uint256 public nextTokenIdToMint; /// @dev Owner of the contract (purpose: OpenSea compatibility, etc.) address private _owner; /// @dev The adress that receives all primary sales value. address public defaultSaleRecipient; /// @dev The adress that receives all primary sales value. address public defaultRoyaltyRecipient; /// @dev Contract interprets 10_000 as 100%. uint64 private constant MAX_BPS = 10_000; /// @dev The % of secondary sales collected as royalties. See EIP 2981. uint64 public royaltyBps; /// @dev The % of primary sales collected by the contract as fees. uint120 public feeBps; /// @dev Whether transfers on tokens are restricted. bool public transfersRestricted; /// @dev Contract level metadata. string public contractURI; /// @dev The protocol control center. ProtocolControl internal controlCenter; /// @dev Mapping from mint request UID => whether the mint request is processed. mapping(bytes32 => bool) private minted; mapping(uint256 => string) private _tokenURI; /// @dev Token ID => total circulating supply of tokens with that ID. mapping(uint256 => uint256) public totalSupply; /// @dev Token ID => royalty recipient and bps for token mapping(uint256 => RoyaltyInfo) private royaltyInfoForToken; /// @dev Checks whether the caller is a module admin. modifier onlyModuleAdmin() { require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "not module admin."); _; } /// @dev Checks whether the caller has MINTER_ROLE. modifier onlyMinter() { require(hasRole(MINTER_ROLE, _msgSender()), "not minter."); _; } constructor( string memory _contractURI, address payable _controlCenter, address _trustedForwarder, address _nativeTokenWrapper, address _saleRecipient, address _royaltyRecipient, uint128 _royaltyBps, uint128 _feeBps ) ERC1155("") EIP712("SignatureMint1155", "1") ERC2771Context(_trustedForwarder) { // Set the protocol control center controlCenter = ProtocolControl(_controlCenter); nativeTokenWrapper = _nativeTokenWrapper; defaultSaleRecipient = _saleRecipient; defaultRoyaltyRecipient = _royaltyRecipient; contractURI = _contractURI; royaltyBps = uint64(_royaltyBps); feeBps = uint120(_feeBps); address deployer = _msgSender(); _owner = deployer; _setupRole(DEFAULT_ADMIN_ROLE, deployer); _setupRole(MINTER_ROLE, deployer); _setupRole(TRANSFER_ROLE, deployer); } /// ===== Public functions ===== /** * @dev Returns the address of the current owner. */ function owner() public view returns (address) { return hasRole(DEFAULT_ADMIN_ROLE, _owner) ? _owner : address(0); } /// @dev Verifies that a mint request is signed by an account holding MINTER_ROLE (at the time of the function call). function verify(MintRequest calldata _req, bytes calldata _signature) public view returns (bool, address) { address signer = recoverAddress(_req, _signature); return (!minted[_req.uid] && hasRole(MINTER_ROLE, signer), signer); } /// @dev Returns the URI for a tokenId function tokenURI(uint256 _tokenId) public view returns (string memory) { return _tokenURI[_tokenId]; } /// @dev Returns the URI for a tokenId function uri(uint256 _tokenId) public view override returns (string memory) { return _tokenURI[_tokenId]; } /// @dev Lets an account with MINTER_ROLE mint an NFT. function mintTo( address _to, uint256 _tokenId, string calldata _uri, uint256 _amount ) external onlyMinter { uint256 tokenIdToMint; if (_tokenId == type(uint256).max) { tokenIdToMint = nextTokenIdToMint; nextTokenIdToMint += 1; } else { require(_tokenId < nextTokenIdToMint, "invalid id"); tokenIdToMint = _tokenId; } // `_mintTo` is re-used. `mintTo` just adds a minter role check. _mintTo(_to, _uri, tokenIdToMint, _amount); } /// ===== External functions ===== /// @dev Mints an NFT according to the provided mint request. function mintWithSignature(MintRequest calldata _req, bytes calldata _signature) external payable nonReentrant { address signer = verifyRequest(_req, _signature); address receiver = _req.to == address(0) ? _msgSender() : _req.to; uint256 tokenIdToMint; if (_req.tokenId == type(uint256).max) { tokenIdToMint = nextTokenIdToMint; nextTokenIdToMint += 1; } else { require(_req.tokenId < nextTokenIdToMint, "invalid id"); tokenIdToMint = _req.tokenId; } if (_req.royaltyRecipient != address(0)) { royaltyInfoForToken[tokenIdToMint] = RoyaltyInfo({ recipient: _req.royaltyRecipient, bps: _req.royaltyBps }); } _mintTo(receiver, _req.uri, tokenIdToMint, _req.quantity); collectPrice(_req); emit MintWithSignature(signer, receiver, tokenIdToMint, _req); } /// @dev See EIP 2981 function royaltyInfo(uint256 tokenId, uint256 salePrice) external view virtual returns (address receiver, uint256 royaltyAmount) { (address recipient, uint256 bps) = getRoyaltyInfoForToken(tokenId); receiver = recipient; royaltyAmount = (salePrice * bps) / MAX_BPS; } /// @dev Returns the platform fee bps and recipient. function getDefaultRoyaltyInfo() external view returns (address, uint16) { return (defaultRoyaltyRecipient, uint16(royaltyBps)); } /// @dev Returns the royalty recipient for a particular token Id. function getRoyaltyInfoForToken(uint256 _tokenId) public view returns (address, uint16) { RoyaltyInfo memory royaltyForToken = royaltyInfoForToken[_tokenId]; return royaltyForToken.recipient == address (0) ? (defaultRoyaltyRecipient, uint16(royaltyBps)) : (royaltyForToken.recipient, uint16(royaltyForToken.bps)); } // ===== Setter functions ===== /// @dev Lets a module admin set the default recipient of all primary sales. function setDefaultSaleRecipient(address _saleRecipient) external onlyModuleAdmin { defaultSaleRecipient = _saleRecipient; emit NewDefaultSaleRecipient(_saleRecipient); } /// @dev Lets a module admin update the royalty bps and recipient. function setDefaultRoyaltyInfo(address _royaltyRecipient, uint256 _royaltyBps) external onlyModuleAdmin { require(_royaltyBps <= MAX_BPS, "exceed royalty bps"); defaultRoyaltyRecipient = _royaltyRecipient; royaltyBps = uint64(_royaltyBps); emit DefaultRoyalty(_royaltyRecipient, _royaltyBps); } /// @dev Lets a module admin set the royalty recipient for a particular token Id. function setRoyaltyInfoForToken(uint256 _tokenId, address _recipient, uint256 _bps) external onlyModuleAdmin { require(_bps <= MAX_BPS, "exceed royalty bps"); royaltyInfoForToken[_tokenId] = RoyaltyInfo({ recipient: _recipient, bps: _bps }); emit RoyaltyForToken(_tokenId, _recipient, _bps); } /// @dev Lets a module admin update the fees on primary sales. function setFeeBps(uint256 _feeBps) public onlyModuleAdmin { require(_feeBps <= MAX_BPS, "bps <= 10000."); feeBps = uint120(_feeBps); emit PrimarySalesFeeUpdates(_feeBps); } /// @dev Lets a module admin restrict token transfers. function setRestrictedTransfer(bool _restrictedTransfer) external onlyModuleAdmin { transfersRestricted = _restrictedTransfer; emit TransfersRestricted(_restrictedTransfer); } /// @dev Lets a module admin set a new owner for the contract. The new owner must be a module admin. function setOwner(address _newOwner) external onlyModuleAdmin { require(hasRole(DEFAULT_ADMIN_ROLE, _newOwner), "new owner not module admin."); address _prevOwner = _owner; _owner = _newOwner; emit NewOwner(_prevOwner, _newOwner); } /// @dev Lets a module admin set the URI for contract-level metadata. function setContractURI(string calldata _uri) external onlyModuleAdmin { contractURI = _uri; } /// ===== Internal functions ===== /// @dev Mints an NFT to `to` function _mintTo( address _to, string calldata _uri, uint256 _tokenId, uint256 _amount ) internal { if (bytes(_tokenURI[_tokenId]).length == 0) { require(bytes(_uri).length > 0, "empty uri."); _tokenURI[_tokenId] = _uri; } _mint(_to, _tokenId, _amount, ""); emit TokenMinted(_to, _tokenId, _tokenURI[_tokenId], _amount); } /// @dev Returns the address of the signer of the mint request. function recoverAddress(MintRequest calldata _req, bytes calldata _signature) internal view returns (address) { return _hashTypedDataV4(keccak256(_encodeRequest(_req))).recover(_signature); } /// @dev Resolves 'stack too deep' error in `recoverAddress`. function _encodeRequest(MintRequest calldata _req) internal pure returns (bytes memory) { return abi.encode( TYPEHASH, _req.to, _req.royaltyRecipient, _req.royaltyBps, _req.primarySaleRecipient, _req.tokenId, keccak256(bytes(_req.uri)), _req.quantity, _req.pricePerToken, _req.currency, _req.validityStartTimestamp, _req.validityEndTimestamp, _req.uid ); } /// @dev Verifies that a mint request is valid. function verifyRequest(MintRequest calldata _req, bytes calldata _signature) internal returns (address) { (bool success, address signer) = verify(_req, _signature); require(success, "invalid signature"); require( _req.validityStartTimestamp <= block.timestamp && _req.validityEndTimestamp >= block.timestamp, "request expired" ); minted[_req.uid] = true; return signer; } /// @dev Collects and distributes the primary sale value of tokens being claimed. function collectPrice(MintRequest memory _req) internal { if (_req.pricePerToken == 0) { return; } uint256 totalPrice = _req.pricePerToken * _req.quantity; uint256 fees = (totalPrice * feeBps) / MAX_BPS; if (_req.currency == NATIVE_TOKEN) { require(msg.value == totalPrice, "must send total price."); } address saleRecipient = _req.primarySaleRecipient == address(0) ? defaultSaleRecipient : _req.primarySaleRecipient; transferCurrency(_req.currency, _msgSender(), controlCenter.getRoyaltyTreasury(address(this)), fees); transferCurrency(_req.currency, _msgSender(), saleRecipient, totalPrice - fees); } /// @dev Transfers a given amount of currency. function transferCurrency( address _currency, address _from, address _to, uint256 _amount ) internal { if (_amount == 0) { return; } if (_currency == NATIVE_TOKEN) { if (_from == address(this)) { IWETH(nativeTokenWrapper).withdraw(_amount); safeTransferNativeToken(_to, _amount); } else if (_to == address(this)) { require(_amount == msg.value, "native token value does not match bid amount."); IWETH(nativeTokenWrapper).deposit{ value: _amount }(); } else { safeTransferNativeToken(_to, _amount); } } else { safeTransferERC20(_currency, _from, _to, _amount); } } /// @dev Transfers `amount` of native token to `to`. function safeTransferNativeToken(address to, uint256 value) internal { (bool success, ) = to.call{ value: value }(""); if (!success) { IWETH(nativeTokenWrapper).deposit{ value: value }(); safeTransferERC20(nativeTokenWrapper, address(this), to, value); } } /// @dev Transfer `amount` of ERC20 token from `from` to `to`. function safeTransferERC20( address _currency, address _from, address _to, uint256 _amount ) internal { if (_from == _to) { return; } uint256 balBefore = IERC20(_currency).balanceOf(_to); bool success = _from == address(this) ? IERC20(_currency).transfer(_to, _amount) : IERC20(_currency).transferFrom(_from, _to, _amount); uint256 balAfter = IERC20(_currency).balanceOf(_to); require(success && balAfter == balBefore + _amount, "failed to transfer currency."); } /// ===== Low-level overrides ===== /// @dev Lets a token owner burn the tokens they own (i.e. destroy for good) function burn( address account, uint256 id, uint256 value ) public virtual { require( account == _msgSender() || isApprovedForAll(account, _msgSender()), "ERC1155: caller is not owner nor approved." ); _burn(account, id, value); } /// @dev Lets a token owner burn multiple tokens they own at once (i.e. destroy for good) function burnBatch( address account, uint256[] memory ids, uint256[] memory values ) public virtual { require( account == _msgSender() || isApprovedForAll(account, _msgSender()), "ERC1155: caller is not owner nor approved." ); _burnBatch(account, ids, values); } /** * @dev See {ERC1155-_beforeTokenTransfer}. */ function _beforeTokenTransfer( address operator, address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data ) internal virtual override { super._beforeTokenTransfer(operator, from, to, ids, amounts, data); // if transfer is restricted on the contract, we still want to allow burning and minting if (transfersRestricted && from != address(0) && to != address(0)) { require(hasRole(TRANSFER_ROLE, from) || hasRole(TRANSFER_ROLE, to), "restricted to TRANSFER_ROLE holders."); } if (from == address(0)) { for (uint256 i = 0; i < ids.length; ++i) { totalSupply[ids[i]] += amounts[i]; } } if (to == address(0)) { for (uint256 i = 0; i < ids.length; ++i) { totalSupply[ids[i]] -= amounts[i]; } } } function supportsInterface(bytes4 interfaceId) public view virtual override(AccessControlEnumerable, ERC1155, IERC165) returns (bool) { return super.supportsInterface(interfaceId) || interfaceId == type(IERC1155).interfaceId || interfaceId == type(IERC2981).interfaceId; } function _msgSender() internal view virtual override(Context, ERC2771Context) returns (address sender) { return ERC2771Context._msgSender(); } function _msgData() internal view virtual override(Context, ERC2771Context) returns (bytes calldata) { return ERC2771Context._msgData(); } }