- Gaming assets: With ERC-1155, developers can create game assets that can be fungible or non-fungible. For example, fungible ERC-1155 tokens can represent in-game currencies, while non-fungible ERC-1155 tokens can represent unique weapons, characters, or virtual land.
- Digital collectibles: Similar to ERC-721, ERC-1155 can be used to create and trade digital collectibles. However, ERC-1155 offers additional flexibility, allowing for the creation of fungible and non-fungible tokens under the same contract. This enables the creation of collections with varying levels of scarcity and uniqueness.
- Tokenized real-world assets: ERC-1155 tokens can also represent ownership of real-world assets such as real estate or shares in a company. By combining fungible and non-fungible tokens, ERC-1155 offers a more efficient solution for fractional ownership of assets.
- Batch operations: One of the significant advantages of ERC-1155 is the ability to perform batch operations. Developers can transfer multiple tokens in a single transaction, making it more cost-efficient and reducing gas fees.
Deployment parameters
The Multi-Token template creates a smart contract representing and controlling any number of token types. These tokens can of the ERC-20, ERC-721 or any other standard. To create a contract using this template, provide the following parameter values when deploying a smart contract template using thePOST: /templates/{id}/deploy
API.
Template ID: aea21da6-0aa2-4971-9a1a-5098842b1248
Template deployment parameters
Here is an example of the
templateParametersJSON object within the request
body to
deploy a contract from a template
for the ERC-1155 Multi-Token template.
In this example, the
defaultAdmin, primarySaleRecipient, and
royaltyRecipient parameters are the same address but can be set distinctly
based on your use case.JSON
Common functions
This section lists the most commonly used functions on the Multi-Token template, along with their respective parameters and potential failure scenarios. These functions include:- mintTo [write]
- safeTransferFrom [write]
- setApprovalForAll [write]
- setTokenURI [write]
- burn [write]
- safeBatchTransferFrom [write]
- balanceOfBatch [read]
- balanceOf [read]
- nextTokenIdToMint [read]
- uri [read]
At this time, not all failure scenarios or error messages received from the
blockchain are passed through Circle’s APIs. Instead, you will receive a
generic
ESTIMATION_ERROR
error. If available, the errorDetails field will have more information on
the cause of failure.mintTo [write]
ThemintTo function allows you to create new NFTs or increase the supply of
existing NFTs. It is a flexible function that can cater to both scenarios.
Parameters
Failure scenarios
- Insufficient Role: The
mintTofunction is defined with theonlyRole(MINTER\_ROLE)modifier, meaning only addresses with theMINTER\_ROLEcan call this function. The function will revert and fail if the caller does not have the necessary role. - Token ID Overflow: If the
_tokenIdparameter is set totype(uint256).max(the maximum value for a uint256), the function will attempt to create a new token and assign the next available token ID. However, an overflow can occur if thenextTokenIdToMintvariable has reached its maximum value. This overflow condition will cause the function to fail. - Invalid Token ID: If the
_tokenIdparameter is not set totype(uint256).max, the function will attempt to mint an NFT with the specified token ID. However, if the provided_tokenIdvalue is greater than or equal to the value ofnextTokenIdToMint, the function will revert and fail with the following error message.
”invalid id” - Existing Token ID with Non-Empty URI: When
_tokenIdis provided and already exists, the function checks whether the associated metadata URI for that token ID is empty. If the URI is not empty, the token has already been minted and has an associated URI. In this case, the function will fail and revert, preventing the same token ID from being minted multiple times. - Minting to Zero Address: The function checks whether the
_toaddress is the zero addressaddress(0). Minting tokens to the zero address is prohibited, as it represents an invalid or non-existent address. If_tois the zero address, the function will fail and revert with the following error message.
”ERC1155: mint to the zero address” - Rejection by ERC1155Receiver Contract: If the recipient address
_tois a contract, the function will attempt to call theonERC1155Receivedfunction of that contract to check if the contract supports receiving the NFT. If the contract’sonERC1155Receivedfunction rejects the transfer by returning a value other thanIERC1155ReceiverUpgradeable.onERC1155Received.selector, the function will revert and fail with the following error message.
_ “ERC1155: ERC1155Receiver rejected tokens”_
Notes
- Creating New NFTs: When you pass
type(uint256).maxvia the_tokenIdparameter, the function will create a new NFT with_tokenIdequal tonextTokenIdToMintand assign it to the specified_toaddress. The_uriparameter allows you to provide the metadata URI for the newly created NFT. Theamountparameter allows you to specify how many instances of this NFT with the given ID should be minted. - Increasing Supply of Existing NFTs: If you pass an existing token ID via
the
tokenIdparameter, the function will increase the supply of that specific NFT. Instead of creating a new token ID, the function will mint additional instances of the existing NFT, adding to the current supply. Again, the_amountparameter determines how many additional instances of the NFT should be minted.
Solidity
safeTransferFrom [write]
ThesafeTransferFrom function allows for transferring a specified amount of a
particular token ID from one address from to another address to.
Parameters
Failure scenarios
- Transfer to Zero Address: The function verifies if the to address is the
zero address
address(0). Transfers to the zero address are not permitted, as it represents an invalid or non-existent address. If thetoaddress is the zero address, the function fails and reverts with the following error message.
”ERC1155: transfer to the zero address” - Insufficient Balance: The function checks if the from address has a
sufficient balance of the specified token ID (id) to perform the transfer. If
the balance exceeds the specified amount, the function fails and reverts to
the following error message.
”ERC1155: insufficient balance for transfer” - Caller Not Authorized: The function verifies if the caller of the
_msgSender()function is either the owner of the tokens (from) or has been approved as an operator forfrom. If the caller is neither the token owner nor an approved operator, the function fails and reverts with the following error message.
”ERC1155: caller is not token owner or approved” - Before/After Token Transfer Hooks: The function calls the
_beforeTokenTransferand_afterTokenTransferhooks to update any necessary state or perform additional checks. These hooks may contain custom business logic that can cause the transfer to fail if certain conditions are not met. - ERC1155Receiver Contract Rejection: If the
toaddress is a contract, the function attempts to call theonERC1155Receivedfunction of that contract to check if the contract supports receiving the tokens. If the contract’sonERC1155Receivedfunction rejects the transfer by returning a value other thanIERC1155ReceiverUpgradeable.onERC1155Received.selector, the function fails and reverts with the following error message.
”ERC1155: ERC1155Receiver rejected tokens”
Solidity
setApprovalForAll [write]
ThesetApprovalForAll function is used to set the approval status for an
operator to manage all tokens of the caller (owner) on their behalf.
Parameters
Failure scenarios
- The function requires that the caller (owner) cannot set the approval status
for themselves. If the operator address provided is the same as the owner
address, the function will fail with the given following error message.
”ERC1155: setting approval status for self”
Notes
- Once an operator is approved using the
setApprovalForAllfunction, they can act on behalf of the token owner. This includes performing actions such as transferring tokens.
Solidity
setTokenURI [write]
ThesetTokenURI function is used to set the metadata URI for a given NFT.
Parameters
Failure scenarios
- The function requires the metadata URI to have a length greater than zero.
This error occurs when the input parameter
_uriis an empty string.
_“NFTMetadata: empty metadata” _ - This error occurs if the
_canSetMetadata()function returns false. It indicates that the caller has no authority or permission to set the metadata for the given NFT.
”NFTMetadata: not authorized to set metadata” - This error occurs when
uriFrozenis true, indicating that the metadata is frozen and cannot be updated.
”NFTMetadata: metadata is frozen”
Solidity
burn [write]
This function allows a token owner to burn a specified amount (value) of tokens they own.Parameters
Failure scenarios
- This error occurs when the caller of the burn function is neither the owner of
the tokens nor approved to burn them. The caller must either be the account
that owns the tokens or have approval from the owner to burn the tokens.
_“ERC1155: caller is not owner nor approved” _ - The function checks that the burning amount does not exceed the available
balance. This error occurs if the amount of tokens specified to be burned
(
amount) is greater than the balance of tokens (fromBalance) owned by the specified account.
”ERC1155: burn amount exceeds balance” - This error occurs if the from address (the address from which the tokens are
being burned) is the zero address (0x000…). This address is generally
reserved as an invalid or non-existent address and cannot be used for token
burning.
”ERC1155: burn from the zero address”
Solidity
safeBatchTransferFrom [write]
This function enables the safe transfer of multiple ERC1155 tokens from one address (from) to another address (to) in a batch.
Parameters
Failure scenarios
- This error occurs if the caller of the function is neither the token owner nor
approved to perform the transfer. The caller must either be the from address
or have approval from the from address to transfer the tokens.
”ERC1155: caller is not token owner or approved” - This error occurs if the lengths of the
idsand amounts arrays do not match. Each ID should have a corresponding amount to be transferred. The arrays should have the same length.
”ERC1155: ids and amounts length mismatch” - This error occurs if the
toaddress is the zero address (0x000…). Transferring tokens to the zero address is not allowed as it is generally used to represent an invalid or non-existent address.
”ERC1155: transfer to the zero address” - This error occurs if the from address does not have a sufficient balance of
tokens to transfer. The function checks that the from address has enough
tokens of each ID to fulfill the transfer.
”ERC1155: insufficient balance for transfer” - This error occurs if the to address is a contract and the contract does not
implement the
onERC1155BatchReceivedfunction from theIERC1155ReceiverUpgradeableinterface or if the function returns a value other thanonERC1155BatchReceived.selector. This check ensures that the receiving contract can handle the transferred tokens properly.
”ERC1155: ERC1155Receiver rejected tokens”
Solidity
balanceOfBatch [read]
ThebalanceOfBatch function retrieves the balances of multiple accounts for
multiple token IDs in a single function call.
Parameters
Failure scenarios
- Mismatched Array Lengths: The function requires that the length of the
accounts array is equal to the length of the
idsarray. If this condition is not met, it will throw a required exception with the following error message.
”ERC1155: accounts and ids length mismatch”
Solidity
balanceOf [read]
ThebalanceOf function retrieves the balance of a specific account for a
particular token ID.
Parameters
Failure scenarios
- Zero Address: The function requires that the account parameter is not set
to the zero address
address(0). If this condition is not met, it will throw a required exception with the following error message.
”ERC1155: address zero is not a valid owner”.
Solidity
uri [read]
The URI function retrieves the associated URI with a specific token ID. This URI provides a way to access metadata and additional information about the token.Parameters
Solidity
Public Variables
Public variables are accessible from within the contract and can be accessed from external contracts. Solidity automatically generates a getter function for public state variables.nextTokenIdToMint [read]
ThenextTokenIdToMint variable is a public constant on the smart contract. An
unsigned integer (uint256) represents the next token ID minted or created when
type(uint256).max is passed to the mintTo function.
Solidity