ERC1155 Base
import "@thirdweb-dev/contracts/base/ERC1155Base.sol";
The ERC1155Base
smart contract implements the ERC1155 NFT standard.
It allows for minting NFTs to yourself (or to someone else) and selling those NFTs on a marketplace.
Detected Extensions
Once deployed, you can use the features made available by these extensions on the SDK and dashboard:
Click on each feature to learn more about what functions are available.
- ERC1155
- ERC1155Mintable
- ERC1155BatchMintable
- ERC1155Burnable
- ERC1155Enumerable
- Royalty
- ContractMetadata
- Ownable
Usage
Import the contract and inherit from it.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@thirdweb-dev/contracts/base/ERC1155Base.sol";
contract MyNFT is ERC1155Base {
constructor(
address _defaultAdmin,
string memory _name,
string memory _symbol,
address _royaltyRecipient,
uint128 _royaltyBps
)
ERC1155Base(
_defaultAdmin,
_name,
_symbol,
_royaltyRecipient,
_royaltyBps
)
{}
}
Functions to Override
The following functions have been implemented on this contract & are available to be overridden to add custom logic:
uri
/// @notice Returns the metadata URI for the given tokenId.
function uri(uint256 _tokenId) public view virtual override returns (string memory) {
string memory uriForToken = _uri[_tokenId];
if (bytes(uriForToken).length > 0) {
return uriForToken;
}
string memory batchUri = _getBaseURI(_tokenId);
return string(abi.encodePacked(batchUri, _tokenId.toString()));
}
mintTo
/**
* @notice Lets an authorized address mint NFTs to a recipient.
* @dev - The logic in the `_canMint` function determines whether the caller is authorized to mint NFTs.
* - If `_tokenId == type(uint256).max` a new NFT at tokenId `nextTokenIdToMint` is minted. If the given
* `tokenId < nextTokenIdToMint`, then additional supply of an existing NFT is being minted.
*
* @param _to The recipient of the NFTs to mint.
* @param _tokenId The tokenId of the NFT to mint.
* @param _tokenURI The full metadata URI for the NFTs minted (if a new NFT is being minted).
* @param _amount The amount of the same NFT to mint.
*/
function mintTo(
address _to,
uint256 _tokenId,
string memory _tokenURI,
uint256 _amount
) public virtual {
require(_canMint(), "Not authorized to mint.");
uint256 tokenIdToMint;
uint256 nextIdToMint = nextTokenIdToMint();
if (_tokenId == type(uint256).max) {
tokenIdToMint = nextIdToMint;
nextTokenIdToMint_ += 1;
_setTokenURI(nextIdToMint, _tokenURI);
} else {
require(_tokenId < nextIdToMint, "invalid id");
tokenIdToMint = _tokenId;
}
_mint(_to, tokenIdToMint, _amount, "");
}
batchMintTo
/**
* @notice Lets an authorized address mint multiple NEW NFTs at once to a recipient.
* @dev The logic in the `_canMint` function determines whether the caller is authorized to mint NFTs.
* If `_tokenIds[i] == type(uint256).max` a new NFT at tokenId `nextTokenIdToMint` is minted. If the given
* `tokenIds[i] < nextTokenIdToMint`, then additional supply of an existing NFT is minted.
* The metadata for each new NFT is stored at `baseURI/{tokenID of NFT}`
*
* @param _to The recipient of the NFT to mint.
* @param _tokenIds The tokenIds of the NFTs to mint.
* @param _amounts The amounts of each NFT to mint.
* @param _baseURI The baseURI for the `n` number of NFTs minted. The metadata for each NFT is `baseURI/tokenId`
*/
function batchMintTo(
address _to,
uint256[] memory _tokenIds,
uint256[] memory _amounts,
string memory _baseURI
) public virtual {
require(_canMint(), "Not authorized to mint.");
require(_amounts.length > 0, "Minting zero tokens.");
require(_tokenIds.length == _amounts.length, "Length mismatch.");
uint256 nextIdToMint = nextTokenIdToMint();
uint256 startNextIdToMint = nextIdToMint;
uint256 numOfNewNFTs;
for (uint256 i = 0; i < _tokenIds.length; i += 1) {
if (_tokenIds[i] == type(uint256).max) {
_tokenIds[i] = nextIdToMint;
nextIdToMint += 1;
numOfNewNFTs += 1;
} else {
require(_tokenIds[i] < nextIdToMint, "invalid id");
}
}
if (numOfNewNFTs > 0) {
_batchMintMetadata(startNextIdToMint, numOfNewNFTs, _baseURI);
}
nextTokenIdToMint_ = nextIdToMint;
_mintBatch(_to, _tokenIds, _amounts, "");
}
burn
/**
* @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);
}
burnBatch
/**
* @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);
}
_beforeTokenTransfer
/// @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];
}
}
}