-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
LICENSE// SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.11; /// @author thirdweb // amount new tokens for to. * * See {ERC20-_mint}. * * Requirements: * * - the caller must have the MINTER_ROLE. */ function mintTo(address to, uint256 amount) public virtual { require(hasRole(MINTER_ROLE, _msgSender()), "not minter."); _mintTo(to, amount); } /// @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 Mints tokens 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; collectPrice(_req); _mintTo(receiver, _req.quantity); emit TokensMintedWithSignature(signer, receiver, _req); } /// @dev Lets a module admin set the default recipient of all primary sales. function setPrimarySaleRecipient(address _saleRecipient) external onlyRole(DEFAULT_ADMIN_ROLE) { primarySaleRecipient = _saleRecipient; emit PrimarySaleRecipientUpdated(_saleRecipient); } /// @dev Lets a module admin update the fees on primary sales. function setPlatformFeeInfo( address _platformFeeRecipient, uint256 _platformFeeBps ) external onlyRole(DEFAULT_ADMIN_ROLE) { require(_platformFeeBps <= MAX_BPS, "exceeds MAX_BPS"); platformFeeBps = uint64(_platformFeeBps); platformFeeRecipient = _platformFeeRecipient; emit PlatformFeeInfoUpdated(_platformFeeRecipient, _platformFeeBps); } /// @dev Returns the platform fee bps and recipient. function getPlatformFeeInfo() external view returns (address, uint16) { return (platformFeeRecipient, uint16(platformFeeBps)); } /// @dev Collects and distributes the primary sale value of tokens being claimed. function collectPrice(MintRequest calldata _req) internal { if (_req.price == 0) { require(msg.value == 0, "!Value"); return; } uint256 platformFees = (_req.price * platformFeeBps) / MAX_BPS; if (_req.currency == CurrencyTransferLib.NATIVE_TOKEN) { require(msg.value == _req.price, "must send total price."); } else { require(msg.value == 0, "msg value not zero"); } address saleRecipient = _req.primarySaleRecipient == address(0) ? primarySaleRecipient : _req.primarySaleRecipient; CurrencyTransferLib.transferCurrency(_req.currency, _msgSender(), platformFeeRecipient, platformFees); CurrencyTransferLib.transferCurrency(_req.currency, _msgSender(), saleRecipient, _req.price - platformFees); } /// @dev Mints amount of tokens to to function _mintTo(address _to, uint256 _amount) internal { _mint(_to, _amount); emit TokensMinted(_to, _amount); } /// @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" ); require(_req.to != address(0), "recipient undefined"); require(_req.quantity > 0, "zero quantity"); minted[_req.uid] = true; return signer; } /// @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.primarySaleRecipient, _req.quantity, _req.price, _req.currency, _req.validityStartTimestamp, _req.validityEndTimestamp, _req.uid ); } /// @dev Sets contract URI for the storefront-level metadata of the contract. function setContractURI(string calldata _uri) external onlyRole(DEFAULT_ADMIN_ROLE) { contractURI = _uri; } function _msgSender() internal view virtual override(ContextUpgradeable, ERC2771ContextUpgradeable, Multicall) returns (address sender) { return ERC2771ContextUpgradeable._msgSender(); } function _msgData() internal view virtual override(ContextUpgradeable, ERC2771ContextUpgradeable) returns (bytes calldata) { return ; } }