Retrieve Token Type
Backgroundโ
LSP7 and LSP8 can be both used as NFTs. LSP7 is useful for NFTs where individual items are not unique and to mint large quantity of NFTs at once, whereas LSP8 is mainly used for phygitals, or NFTs with unique properties per item.
To detect if an asset is a Token, an NFT or a Collection, the ๐ LSP4 Digital Asset Metadata standard defines a data key LSP4TokenType
where this information is stored. The token type can be retrieved by querying this data key from the ๐๏ธ ERC725Y storage of the digital asset contract.
{
"name": "LSP4TokenType",
"key": "0xe0261fa95db2eb3b5439bd033cda66d56b96f92f243a8228fd87550ed7bdfdb3", // keccak256 hash of the word ยซ LSP4TokenType ยป
"keyType": "Singleton",
"valueType": "uint256",
"valueContent": "Number"
}
The LSP4TokenType
is not changeable and it is set during the token's initialization.
Value | Type | Description |
---|---|---|
0 | Token | Only valid for LSP7, meaning its a generic token, where the LSP4Metadata represents the token information. |
1 | NFT | If the contract is LSP7 or LSP8, then the LSP4Metadata represents the information of a single NFT, that has multiple ownable amounts or IDs. If it's an LSP8, each individual token ID can have its own custom metadata specific for that token, but is not a different NFT. It is still the the same NFT but just has different metadata. Metadata can be set using LSP8TokenIdFormat and LSP4Metadata for each single tokenId. See LSP8 for details. If its an LSP7 contract, the decimals function MUST return 0 . |
2 | Collection | Only valid for LSP8. The LSP4Metadata represents the information of a the collection, and each individual token ID represents its own Token or NFT and MUST have its own metadata set using LSP4Metadata and LSP8TokenIdFormat in case the individual token IDs are LSP8. See LSP8 for details. |
Implementationโ
When creating digital assets using LSP7 or LSP8, the token type is defined under the LSP4TokenType
data key. We can retrieve the value and type using getData
.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
bytes32 constant _LSP4_TOKEN_TYPE_DATA_KEY = 0xe0261fa95db2eb3b5439bd033cda66d56b96f92f243a8228fd87550ed7bdfdb3; // keccak256 hash of the word `LSP4TokenType`
enum TokenType {
TOKEN, // `0` = Token
NFT, // `1` = NFT
COLLECTION // `2` = Collection
}
After defining the token type of the asset, you can create a custom LSP7 Digital Asset Collection or LSP8 Identifiable Digital Asset Collection. During deployment, the token type is then written to the ๐๏ธ ERC725Y storage of the smart contract.