Contract Source Code:
// SPDX-License-Identifier: Apache 2.0
pragma solidity ^0.8.0;
import "./interface/IERC1155.sol";
import "./interface/IERC1155Metadata.sol";
import "./interface/IERC1155Receiver.sol";
contract ERC1155 is IERC1155, IERC1155Metadata {
/*//////////////////////////////////////////////////////////////
State variables
//////////////////////////////////////////////////////////////*/
string public name;
string public symbol;
/*//////////////////////////////////////////////////////////////
Mappings
//////////////////////////////////////////////////////////////*/
mapping(address => mapping(uint256 => uint256)) public override balanceOf;
mapping(address => mapping(address => bool)) public override isApprovedForAll;
mapping(uint256 => string) internal _uri;
/*//////////////////////////////////////////////////////////////
Constructor
//////////////////////////////////////////////////////////////*/
constructor(string memory _name, string memory _symbol) {
name = _name;
symbol = _symbol;
}
/*//////////////////////////////////////////////////////////////
View functions
//////////////////////////////////////////////////////////////*/
function supportsInterface(bytes4 interfaceId) public view virtual returns (bool) {
return
interfaceId == 0x01ffc9a7 || // ERC165 Interface ID for ERC165
interfaceId == 0xd9b67a26 || // ERC165 Interface ID for ERC1155
interfaceId == 0x0e89341c; // ERC165 Interface ID for ERC1155MetadataURI
}
function uri(uint256 tokenId) public view virtual override returns (string memory) {
return _uri[tokenId];
}
function balanceOfBatch(address[] memory accounts, uint256[] memory ids)
public
view
virtual
override
returns (uint256[] memory)
{
require(accounts.length == ids.length, "LENGTH_MISMATCH");
uint256[] memory batchBalances = new uint256[](accounts.length);
for (uint256 i = 0; i < accounts.length; ++i) {
batchBalances[i] = balanceOf[accounts[i]][ids[i]];
}
return batchBalances;
}
/*//////////////////////////////////////////////////////////////
ERC1155 logic
//////////////////////////////////////////////////////////////*/
function setApprovalForAll(address operator, bool approved) public virtual override {
address owner = msg.sender;
require(owner != operator, "APPROVING_SELF");
isApprovedForAll[owner][operator] = approved;
emit ApprovalForAll(owner, operator, approved);
}
function safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
) public virtual override {
require(from == msg.sender || isApprovedForAll[from][msg.sender], "!OWNER_OR_APPROVED");
_safeTransferFrom(from, to, id, amount, data);
}
function safeBatchTransferFrom(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) public virtual override {
require(from == msg.sender || isApprovedForAll[from][msg.sender], "!OWNER_OR_APPROVED");
_safeBatchTransferFrom(from, to, ids, amounts, data);
}
/*//////////////////////////////////////////////////////////////
Internal logic
//////////////////////////////////////////////////////////////*/
function _safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
) internal virtual {
require(to != address(0), "TO_ZERO_ADDR");
address operator = msg.sender;
_beforeTokenTransfer(operator, from, to, _asSingletonArray(id), _asSingletonArray(amount), data);
uint256 fromBalance = balanceOf[from][id];
require(fromBalance >= amount, "INSUFFICIENT_BAL");
unchecked {
balanceOf[from][id] = fromBalance - amount;
}
balanceOf[to][id] += amount;
emit TransferSingle(operator, from, to, id, amount);
_doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
}
function _safeBatchTransferFrom(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual {
require(ids.length == amounts.length, "LENGTH_MISMATCH");
require(to != address(0), "TO_ZERO_ADDR");
address operator = msg.sender;
_beforeTokenTransfer(operator, from, to, ids, amounts, data);
for (uint256 i = 0; i < ids.length; ++i) {
uint256 id = ids[i];
uint256 amount = amounts[i];
uint256 fromBalance = balanceOf[from][id];
require(fromBalance >= amount, "INSUFFICIENT_BAL");
unchecked {
balanceOf[from][id] = fromBalance - amount;
}
balanceOf[to][id] += amount;
}
emit TransferBatch(operator, from, to, ids, amounts);
_doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
}
function _setTokenURI(uint256 tokenId, string memory newuri) internal virtual {
_uri[tokenId] = newuri;
}
function _mint(
address to,
uint256 id,
uint256 amount,
bytes memory data
) internal virtual {
require(to != address(0), "TO_ZERO_ADDR");
address operator = msg.sender;
_beforeTokenTransfer(operator, address(0), to, _asSingletonArray(id), _asSingletonArray(amount), data);
balanceOf[to][id] += amount;
emit TransferSingle(operator, address(0), to, id, amount);
_doSafeTransferAcceptanceCheck(operator, address(0), to, id, amount, data);
}
function _mintBatch(
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual {
require(to != address(0), "TO_ZERO_ADDR");
require(ids.length == amounts.length, "LENGTH_MISMATCH");
address operator = msg.sender;
_beforeTokenTransfer(operator, address(0), to, ids, amounts, data);
for (uint256 i = 0; i < ids.length; i++) {
balanceOf[to][ids[i]] += amounts[i];
}
emit TransferBatch(operator, address(0), to, ids, amounts);
_doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data);
}
function _burn(
address from,
uint256 id,
uint256 amount
) internal virtual {
require(from != address(0), "FROM_ZERO_ADDR");
address operator = msg.sender;
_beforeTokenTransfer(operator, from, address(0), _asSingletonArray(id), _asSingletonArray(amount), "");
uint256 fromBalance = balanceOf[from][id];
require(fromBalance >= amount, "INSUFFICIENT_BAL");
unchecked {
balanceOf[from][id] = fromBalance - amount;
}
emit TransferSingle(operator, from, address(0), id, amount);
}
function _burnBatch(
address from,
uint256[] memory ids,
uint256[] memory amounts
) internal virtual {
require(from != address(0), "FROM_ZERO_ADDR");
require(ids.length == amounts.length, "LENGTH_MISMATCH");
address operator = msg.sender;
_beforeTokenTransfer(operator, from, address(0), ids, amounts, "");
for (uint256 i = 0; i < ids.length; i++) {
uint256 id = ids[i];
uint256 amount = amounts[i];
uint256 fromBalance = balanceOf[from][id];
require(fromBalance >= amount, "INSUFFICIENT_BAL");
unchecked {
balanceOf[from][id] = fromBalance - amount;
}
}
emit TransferBatch(operator, from, address(0), ids, amounts);
}
function _beforeTokenTransfer(
address operator,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual {}
function _doSafeTransferAcceptanceCheck(
address operator,
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
) private {
if (to.code.length > 0) {
try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
if (response != IERC1155Receiver.onERC1155Received.selector) {
revert("TOKENS_REJECTED");
}
} catch Error(string memory reason) {
revert(reason);
} catch {
revert("!ERC1155RECEIVER");
}
}
}
function _doSafeBatchTransferAcceptanceCheck(
address operator,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) private {
if (to.code.length > 0) {
try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
bytes4 response
) {
if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
revert("TOKENS_REJECTED");
}
} catch Error(string memory reason) {
revert(reason);
} catch {
revert("!ERC1155RECEIVER");
}
}
}
function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) {
uint256[] memory array = new uint256[](1);
array[0] = element;
return array;
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/**
@title ERC-1155 Multi Token Standard
@dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1155.md
Note: The ERC-165 identifier for this interface is 0xd9b67a26.
*/
interface IERC1155 {
/**
@dev Either `TransferSingle` or `TransferBatch` MUST emit when tokens are transferred, including zero value transfers as well as minting or burning (see "Safe Transfer Rules" section of the standard).
The `_operator` argument MUST be msg.sender.
The `_from` argument MUST be the address of the holder whose balance is decreased.
The `_to` argument MUST be the address of the recipient whose balance is increased.
The `_id` argument MUST be the token type being transferred.
The `_value` argument MUST be the number of tokens the holder balance is decreased by and match what the recipient balance is increased by.
When minting/creating tokens, the `_from` argument MUST be set to `0x0` (i.e. zero address).
When burning/destroying tokens, the `_to` argument MUST be set to `0x0` (i.e. zero address).
*/
event TransferSingle(
address indexed _operator,
address indexed _from,
address indexed _to,
uint256 _id,
uint256 _value
);
/**
@dev Either `TransferSingle` or `TransferBatch` MUST emit when tokens are transferred, including zero value transfers as well as minting or burning (see "Safe Transfer Rules" section of the standard).
The `_operator` argument MUST be msg.sender.
The `_from` argument MUST be the address of the holder whose balance is decreased.
The `_to` argument MUST be the address of the recipient whose balance is increased.
The `_ids` argument MUST be the list of tokens being transferred.
The `_values` argument MUST be the list of number of tokens (matching the list and order of tokens specified in _ids) the holder balance is decreased by and match what the recipient balance is increased by.
When minting/creating tokens, the `_from` argument MUST be set to `0x0` (i.e. zero address).
When burning/destroying tokens, the `_to` argument MUST be set to `0x0` (i.e. zero address).
*/
event TransferBatch(
address indexed _operator,
address indexed _from,
address indexed _to,
uint256[] _ids,
uint256[] _values
);
/**
@dev MUST emit when approval for a second party/operator address to manage all tokens for an owner address is enabled or disabled (absense of an event assumes disabled).
*/
event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);
/**
@dev MUST emit when the URI is updated for a token ID.
URIs are defined in RFC 3986.
The URI MUST point a JSON file that conforms to the "ERC-1155 Metadata URI JSON Schema".
*/
event URI(string _value, uint256 indexed _id);
/**
@notice Transfers `_value` amount of an `_id` from the `_from` address to the `_to` address specified (with safety call).
@dev Caller must be approved to manage the tokens being transferred out of the `_from` account (see "Approval" section of the standard).
MUST revert if `_to` is the zero address.
MUST revert if balance of holder for token `_id` is lower than the `_value` sent.
MUST revert on any other error.
MUST emit the `TransferSingle` event to reflect the balance change (see "Safe Transfer Rules" section of the standard).
After the above conditions are met, this function MUST check if `_to` is a smart contract (e.g. code size > 0). If so, it MUST call `onERC1155Received` on `_to` and act appropriately (see "Safe Transfer Rules" section of the standard).
@param _from Source address
@param _to Target address
@param _id ID of the token type
@param _value Transfer amount
@param _data Additional data with no specified format, MUST be sent unaltered in call to `onERC1155Received` on `_to`
*/
function safeTransferFrom(
address _from,
address _to,
uint256 _id,
uint256 _value,
bytes calldata _data
) external;
/**
@notice Transfers `_values` amount(s) of `_ids` from the `_from` address to the `_to` address specified (with safety call).
@dev Caller must be approved to manage the tokens being transferred out of the `_from` account (see "Approval" section of the standard).
MUST revert if `_to` is the zero address.
MUST revert if length of `_ids` is not the same as length of `_values`.
MUST revert if any of the balance(s) of the holder(s) for token(s) in `_ids` is lower than the respective amount(s) in `_values` sent to the recipient.
MUST revert on any other error.
MUST emit `TransferSingle` or `TransferBatch` event(s) such that all the balance changes are reflected (see "Safe Transfer Rules" section of the standard).
Balance changes and events MUST follow the ordering of the arrays (_ids[0]/_values[0] before _ids[1]/_values[1], etc).
After the above conditions for the transfer(s) in the batch are met, this function MUST check if `_to` is a smart contract (e.g. code size > 0). If so, it MUST call the relevant `ERC1155TokenReceiver` hook(s) on `_to` and act appropriately (see "Safe Transfer Rules" section of the standard).
@param _from Source address
@param _to Target address
@param _ids IDs of each token type (order and length must match _values array)
@param _values Transfer amounts per token type (order and length must match _ids array)
@param _data Additional data with no specified format, MUST be sent unaltered in call to the `ERC1155TokenReceiver` hook(s) on `_to`
*/
function safeBatchTransferFrom(
address _from,
address _to,
uint256[] calldata _ids,
uint256[] calldata _values,
bytes calldata _data
) external;
/**
@notice Get the balance of an account's Tokens.
@param _owner The address of the token holder
@param _id ID of the Token
@return The _owner's balance of the Token type requested
*/
function balanceOf(address _owner, uint256 _id) external view returns (uint256);
/**
@notice Get the balance of multiple account/token pairs
@param _owners The addresses of the token holders
@param _ids ID of the Tokens
@return The _owner's balance of the Token types requested (i.e. balance for each (owner, id) pair)
*/
function balanceOfBatch(address[] calldata _owners, uint256[] calldata _ids)
external
view
returns (uint256[] memory);
/**
@notice Enable or disable approval for a third party ("operator") to manage all of the caller's tokens.
@dev MUST emit the ApprovalForAll event on success.
@param _operator Address to add to the set of authorized operators
@param _approved True if the operator is approved, false to revoke approval
*/
function setApprovalForAll(address _operator, bool _approved) external;
/**
@notice Queries the approval status of an operator for a given owner.
@param _owner The owner of the Tokens
@param _operator Address of authorized operator
@return True if the operator is approved, false if not
*/
function isApprovedForAll(address _owner, address _operator) external view returns (bool);
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/**
Note: The ERC-165 identifier for this interface is 0x0e89341c.
*/
interface IERC1155Metadata {
/**
@notice A distinct Uniform Resource Identifier (URI) for a given token.
@dev URIs are defined in RFC 3986.
The URI may point to a JSON file that conforms to the "ERC-1155 Metadata URI JSON Schema".
@return URI string
*/
function uri(uint256 _id) external view returns (string memory);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC1155/IERC1155Receiver.sol)
pragma solidity ^0.8.0;
import "./IERC165.sol";
/**
* @dev _Available since v3.1._
*/
interface IERC1155Receiver is IERC165 {
/**
* @dev Handles the receipt of a single ERC1155 token type. This function is
* called at the end of a `safeTransferFrom` after the balance has been updated.
*
* NOTE: To accept the transfer, this must return
* `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
* (i.e. 0xf23a6e61, or its own function selector).
*
* @param operator The address which initiated the transfer (i.e. msg.sender)
* @param from The address which previously owned the token
* @param id The ID of the token being transferred
* @param value The amount of tokens being transferred
* @param data Additional data with no specified format
* @return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` if transfer is allowed
*/
function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
) external returns (bytes4);
/**
* @dev Handles the receipt of a multiple ERC1155 token types. This function
* is called at the end of a `safeBatchTransferFrom` after the balances have
* been updated.
*
* NOTE: To accept the transfer(s), this must return
* `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
* (i.e. 0xbc197c81, or its own function selector).
*
* @param operator The address which initiated the batch transfer (i.e. msg.sender)
* @param from The address which previously owned the token
* @param ids An array containing ids of each token being transferred (order and length must match values array)
* @param values An array containing amounts of each token being transferred (order and length must match ids array)
* @param data Additional data with no specified format
* @return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` if transfer is allowed
*/
function onERC1155BatchReceived(
address operator,
address from,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata data
) external returns (bytes4);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC165 standard, as defined in the
* [EIP](https://eips.ethereum.org/EIPS/eip-165).
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* [EIP section](https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified)
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/**
* @title Batch-mint Metadata
* @notice The `BatchMintMetadata` is a contract extension for any base NFT contract. It lets the smart contract
* using this extension set metadata for `n` number of NFTs all at once. This is enabled by storing a single
* base URI for a batch of `n` NFTs, where the metadata for each NFT in a relevant batch is `baseURI/tokenId`.
*/
contract BatchMintMetadata {
/// @dev Largest tokenId of each batch of tokens with the same baseURI.
uint256[] private batchIds;
/// @dev Mapping from id of a batch of tokens => to base URI for the respective batch of tokens.
mapping(uint256 => string) private baseURI;
/**
* @notice Returns the count of batches of NFTs.
* @dev Each batch of tokens has an in ID and an associated `baseURI`.
* See {batchIds}.
*/
function getBaseURICount() public view returns (uint256) {
return batchIds.length;
}
/**
* @notice Returns the ID for the batch of tokens the given tokenId belongs to.
* @dev See {getBaseURICount}.
* @param _index ID of a token.
*/
function getBatchIdAtIndex(uint256 _index) public view returns (uint256) {
if (_index >= getBaseURICount()) {
revert("Invalid index");
}
return batchIds[_index];
}
/// @dev Returns the id for the batch of tokens the given tokenId belongs to.
function _getBatchId(uint256 _tokenId) internal view returns (uint256 batchId, uint256 index) {
uint256 numOfTokenBatches = getBaseURICount();
uint256[] memory indices = batchIds;
for (uint256 i = 0; i < numOfTokenBatches; i += 1) {
if (_tokenId < indices[i]) {
index = i;
batchId = indices[i];
return (batchId, index);
}
}
revert("Invalid tokenId");
}
/// @dev Returns the baseURI for a token. The intended metadata URI for the token is baseURI + tokenId.
function _getBaseURI(uint256 _tokenId) internal view returns (string memory) {
uint256 numOfTokenBatches = getBaseURICount();
uint256[] memory indices = batchIds;
for (uint256 i = 0; i < numOfTokenBatches; i += 1) {
if (_tokenId < indices[i]) {
return baseURI[indices[i]];
}
}
revert("Invalid tokenId");
}
/// @dev Sets the base URI for the batch of tokens with the given batchId.
function _setBaseURI(uint256 _batchId, string memory _baseURI) internal {
baseURI[_batchId] = _baseURI;
}
/// @dev Mints a batch of tokenIds and associates a common baseURI to all those Ids.
function _batchMintMetadata(
uint256 _startId,
uint256 _amountToMint,
string memory _baseURIForTokens
) internal returns (uint256 nextTokenIdToMint, uint256 batchId) {
batchId = _startId + _amountToMint;
nextTokenIdToMint = batchId;
batchIds.push(batchId);
baseURI[batchId] = _baseURIForTokens;
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
import "./interface/ILazyMint.sol";
import "./BatchMintMetadata.sol";
/**
* The `LazyMint` is a contract extension for any base NFT contract. It lets you 'lazy mint' any number of NFTs
* at once. Here, 'lazy mint' means defining the metadata for particular tokenIds of your NFT contract, without actually
* minting a non-zero balance of NFTs of those tokenIds.
*/
abstract contract LazyMint is ILazyMint, BatchMintMetadata {
uint256 internal nextTokenIdToLazyMint;
/**
* @notice Lets an authorized address lazy mint a given amount of NFTs.
*
* @param _amount The number of NFTs to lazy mint.
* @param _baseURIForTokens The base URI for the 'n' number of NFTs being lazy minted, where the metadata for each
* of those NFTs is `${baseURIForTokens}/${tokenId}`.
* @param _data Additional bytes data to be used at the discretion of the consumer of the contract.
* @return batchId A unique integer identifier for the batch of NFTs lazy minted together.
*/
function lazyMint(
uint256 _amount,
string calldata _baseURIForTokens,
bytes calldata _data
) public virtual override returns (uint256 batchId) {
if (!_canLazyMint()) {
revert("Not authorized");
}
if (_amount == 0) {
revert("0 amt");
}
uint256 startId = nextTokenIdToLazyMint;
(nextTokenIdToLazyMint, batchId) = _batchMintMetadata(startId, _amount, _baseURIForTokens);
emit TokensLazyMinted(startId, startId + _amount - 1, _baseURIForTokens, _data);
return batchId;
}
/// @dev Returns whether lazy minting can be performed in the given execution context.
function _canLazyMint() internal view virtual returns (bool);
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
import "./interface/IOwnable.sol";
/**
* @title Ownable
* @notice Thirdweb's `Ownable` is a contract extension to be used with any base contract. It exposes functions for setting and reading
* who the 'owner' of the inheriting smart contract is, and lets the inheriting contract perform conditional logic that uses
* information about who the contract's owner is.
*/
abstract contract Ownable is IOwnable {
/// @dev Owner of the contract (purpose: OpenSea compatibility)
address private _owner;
/// @dev Reverts if caller is not the owner.
modifier onlyOwner() {
if (msg.sender != _owner) {
revert("Not authorized");
}
_;
}
/**
* @notice Returns the owner of the contract.
*/
function owner() public view override returns (address) {
return _owner;
}
/**
* @notice Lets an authorized wallet set a new owner for the contract.
* @param _newOwner The address to set as the new owner of the contract.
*/
function setOwner(address _newOwner) external override {
if (!_canSetOwner()) {
revert("Not authorized");
}
_setupOwner(_newOwner);
}
/// @dev Lets a contract admin set a new owner for the contract. The new owner must be a contract admin.
function _setupOwner(address _newOwner) internal {
address _prevOwner = _owner;
_owner = _newOwner;
emit OwnerUpdated(_prevOwner, _newOwner);
}
/// @dev Returns whether owner can be set in the given execution context.
function _canSetOwner() internal view virtual returns (bool);
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/**
* Thirdweb's `LazyMint` is a contract extension for any base NFT contract. It lets you 'lazy mint' any number of NFTs
* at once. Here, 'lazy mint' means defining the metadata for particular tokenIds of your NFT contract, without actually
* minting a non-zero balance of NFTs of those tokenIds.
*/
interface ILazyMint {
/// @dev Emitted when tokens are lazy minted.
event TokensLazyMinted(uint256 indexed startTokenId, uint256 endTokenId, string baseURI, bytes encryptedBaseURI);
/**
* @notice Lazy mints a given amount of NFTs.
*
* @param amount The number of NFTs to lazy mint.
*
* @param baseURIForTokens The base URI for the 'n' number of NFTs being lazy minted, where the metadata for each
* of those NFTs is `${baseURIForTokens}/${tokenId}`.
*
* @param extraData Additional bytes data to be used at the discretion of the consumer of the contract.
*
* @return batchId A unique integer identifier for the batch of NFTs lazy minted together.
*/
function lazyMint(
uint256 amount,
string calldata baseURIForTokens,
bytes calldata extraData
) external returns (uint256 batchId);
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;
/**
* Thirdweb's `Ownable` is a contract extension to be used with any base contract. It exposes functions for setting and reading
* who the 'owner' of the inheriting smart contract is, and lets the inheriting contract perform conditional logic that uses
* information about who the contract's owner is.
*/
interface IOwnable {
/// @dev Returns the owner of the contract.
function owner() external view returns (address);
/// @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;
/// @dev Emitted when a new Owner is set.
event OwnerUpdated(address indexed prevOwner, address indexed newOwner);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Strings.sol)
pragma solidity ^0.8.0;
/**
* @dev String operations.
*/
library TWStrings {
bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/
function toString(uint256 value) internal pure returns (string memory) {
// Inspired by OraclizeAPI's implementation - MIT licence
// https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
if (value == 0) {
return "0";
}
uint256 temp = value;
uint256 digits;
while (temp != 0) {
digits++;
temp /= 10;
}
bytes memory buffer = new bytes(digits);
while (value != 0) {
digits -= 1;
buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
value /= 10;
}
return string(buffer);
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/
function toHexString(uint256 value) internal pure returns (string memory) {
if (value == 0) {
return "0x00";
}
uint256 temp = value;
uint256 length = 0;
while (temp != 0) {
length++;
temp >>= 8;
}
return toHexString(value, length);
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
*/
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = _HEX_SYMBOLS[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol)
pragma solidity ^0.8.0;
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
*/
modifier nonReentrant() {
// On the first call to nonReentrant, _notEntered will be true
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
// Any calls to nonReentrant after this point will fail
_status = _ENTERED;
_;
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.17;
import {ERC1155} from "@thirdweb-dev/contracts/eip/ERC1155.sol";
import "@thirdweb-dev/contracts/extension/Ownable.sol";
import "@thirdweb-dev/contracts/extension/BatchMintMetadata.sol";
import "@thirdweb-dev/contracts/extension/LazyMint.sol";
import "@thirdweb-dev/contracts/lib/TWStrings.sol";
import "@thirdweb-dev/contracts/openzeppelin-presets/security/ReentrancyGuard.sol";
/**
* WWWWWWWWW WNNNW
* WNXK0OkkxxxxxxkkOO0XNW N0xdddx0N
* WNX0kxoooooooooooooooooodkOKNW NOoooooookN
* WXOxooooooooooooooooooooooooooxOXW NkoooooooxX
* WXkdoooooooooooooooooooooooooooooookKW WKxoooooxKW
* NOdoooooooooooooooooooooooooooooooooodK NX000KN
* WKxoooooooooooooooooooodkOOOxooooooooodKW
* WKdoooooooooooooooooooxOKW WNOdooooooxXW
* Kxoooooooooooooooooood0WW WKxooookX W
* NkoooooooooooddxdoooooON NOooON WKOX
* W0dooooooooodOXNNXOdooxX WKKW WKdoOW
* WOoooooooood0W WXkxKW N0dooxN
* NkoooooooookN WNW NKX NkooooxX
* NkooooooooxX WOoxKW WXxoooooxX
* WOoooooooo0W WKdoodON WKxooooookN
* XdooooookN WXN Xxooooox0KKKkdooooooo0W
* WOooooodKW W0dkXW NOoooooooooooooooooookN
* NkooooOW XxoodOKXX00koooooooooooooooooooxXW
* XkoooOW NOoooooodooooooooooooooooooooooxKW
* NOdodOKXXKkdooooooooooooooooooooooooooookXW
* WKkoooddooooooooooooooooooooooooooooox0N
* WKkdooooooooooooooooooooooooooooox0NW
* WXOxdoooooooooooooooooooooooxOKN
* WXKOkxdooooooooooooddxO0XN
* WNXKK00OOOO000KXNW
* WWWWWWWWW
*
* BASE: ERC1155Base
* EXTENSION: LazyMint
*
* The `MFGACHA` smart contract implements the ERC1155 NFT standard.
* It includes the following additions to standard ERC1155 logic:
*
* - Lazy minting
*
* - Ability to mint NFTs via the provided `mintTo` and `batchMintTo` functions.
*
* - Ownership of the contract, with the ability to restrict certain functions to
* only be called by the contract's owner.
*
*
* The `MFGACHA` contract uses the `LazyMint` extension.
*
* 'Lazy minting' means defining the metadata of NFTs without minting it to an address. Regular 'minting'
* of NFTs means actually assigning an owner to an NFT.
*
* As a contract admin, this lets you prepare the metadata for NFTs that will be minted by an external party,
* without paying the gas cost for actually minting the NFTs.
*
*/
contract MFGACHA is
ERC1155,
Ownable,
BatchMintMetadata,
LazyMint,
ReentrancyGuard
{
using TWStrings for uint256;
/// @notice The end time(unix timestamp) of the claim duration
uint256 public claimEndTimestamp;
/*//////////////////////////////////////////////////////////////
Mappings
//////////////////////////////////////////////////////////////*/
/**
* @notice Returns the total supply of NFTs of a given tokenId
* @dev Mapping from tokenId => total circulating supply of NFTs of that tokenId.
*/
mapping(uint256 => uint256) public totalSupply;
/*//////////////////////////////////////////////////////////////
Constructor
//////////////////////////////////////////////////////////////*/
constructor(
string memory _name,
string memory _symbol,
uint256 _claimEndTimestamp
) ERC1155(_name, _symbol) {
_setupOwner(msg.sender);
claimEndTimestamp = _claimEndTimestamp;
}
/*//////////////////////////////////////////////////////////////
Overriden metadata logic
//////////////////////////////////////////////////////////////*/
/// @notice Returns the metadata URI for the given tokenId.
function uri(
uint256 _tokenId
) public view virtual override returns (string memory) {
string memory batchUri = _getBaseURI(_tokenId);
return string(abi.encodePacked(batchUri, _tokenId.toString()));
}
/*//////////////////////////////////////////////////////////////
CLAIM LOGIC
//////////////////////////////////////////////////////////////*/
/// @dev Emitted when tokens are claimed
event TokensClaimed(
address indexed claimer,
uint256 indexed tokenId,
uint256 quantityClaimed
);
/**
* @dev The logic in `verifyClaim` determines whether the caller is authorized to mint NFTs.
* The logic in `transferTokensOnClaim` does actual minting of tokens,
* can also be used to apply other state changes.
*
* @param _tokenId The tokenId of the lazy minted NFT to mint.
*/
function _claim(uint256 _tokenId) internal {
verifyClaim(_tokenId); // Add your claim verification logic by overriding this function.
_mint(msg.sender, _tokenId, 1, "");
emit TokensClaimed(msg.sender, _tokenId, 1);
}
/**
* @notice Lets an address claim multiple lazy minted NFTs at once to a recipient.
* This function prevents any reentrant calls, and is not allowed to be overridden.
*
* Contract creators should override `verifyClaim` and `transferTokensOnClaim`
* functions to create custom logic for verification and claiming,
* for e.g. price collection, allowlist, max quantity, etc.
*
* @param _tokenId The tokenId of the lazy minted NFT to mint.
*/
function claim(uint256 _tokenId) public nonReentrant {
_claim(_tokenId);
}
/**
* @notice Let's try if you can't get the NFT you want.
*/
function claimAll() public nonReentrant {
for (uint256 tokenId = 0; tokenId < nextTokenIdToMint(); tokenId++) {
_claim(tokenId);
}
}
/**
* @notice Override this function to add logic for claim verification, based on conditions
* such as allowlist, price, max quantity etc.
*
* @dev Checks a request to claim NFTs against a custom condition.
*
* @param _tokenId The tokenId of the lazy minted NFT to mint.
*/
function verifyClaim(uint256 _tokenId) public view {
require(_tokenId < nextTokenIdToMint(), "invalid id");
require(
block.timestamp < claimEndTimestamp,
"The claimable period has ended."
);
}
/**
* @notice Lets an owner or approved operator burn NFTs of the given tokenId.
*
* @param _owner The owner of the NFT to burn.
* @param _tokenId The tokenId of the NFT to burn.
* @param _amount The amount of the NFT to burn.
*/
function burn(
address _owner,
uint256 _tokenId,
uint256 _amount
) external virtual {
address caller = msg.sender;
require(
caller == _owner || isApprovedForAll[_owner][caller],
"Unapproved caller"
);
require(
balanceOf[_owner][_tokenId] >= _amount,
"Not enough tokens owned"
);
_burn(_owner, _tokenId, _amount);
}
/**
* @notice Lets an owner or approved operator burn NFTs of the given tokenIds.
*
* @param _owner The owner of the NFTs to burn.
* @param _tokenIds The tokenIds of the NFTs to burn.
* @param _amounts The amounts of the NFTs to burn.
*/
function burnBatch(
address _owner,
uint256[] memory _tokenIds,
uint256[] memory _amounts
) external virtual {
address caller = msg.sender;
require(
caller == _owner || isApprovedForAll[_owner][caller],
"Unapproved caller"
);
require(_tokenIds.length == _amounts.length, "Length mismatch");
for (uint256 i = 0; i < _tokenIds.length; i += 1) {
require(
balanceOf[_owner][_tokenIds[i]] >= _amounts[i],
"Not enough tokens owned"
);
}
_burnBatch(_owner, _tokenIds, _amounts);
}
/**
* @notice For owner to update claimEndTimestamp.
*
* @param _timestamp The end time(unix timestamp) of the claim duration.
*/
function setClaimEndTimestamp(uint256 _timestamp) external onlyOwner {
claimEndTimestamp = _timestamp;
}
/*//////////////////////////////////////////////////////////////
View functions
//////////////////////////////////////////////////////////////*/
/// @notice The tokenId assigned to the next new NFT to be lazy minted.
function nextTokenIdToMint() public view virtual returns (uint256) {
return nextTokenIdToLazyMint;
}
/*//////////////////////////////////////////////////////////////
Internal functions
//////////////////////////////////////////////////////////////*/
/// @dev Returns whether lazy minting can be done in the given execution context.
function _canLazyMint() internal view virtual override returns (bool) {
return msg.sender == owner();
}
/// @dev Returns whether owner can be set in the given execution context.
function _canSetOwner() internal view virtual override returns (bool) {
return msg.sender == owner();
}
/// @dev Runs before every token transfer / mint / burn.
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 (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];
}
}
}
}