RareSkills Solidity Interview Question #27 Answered: How can an ERC1155 token be made into a non-fungible token?
This series will provide answers to the list of Solidity interview questions that were published by RareSkills..
Question #27 (Easy): How can an ERC1155 token be made into a non-fungible token?
Answer: An ERC1155 token can be made into a non-fungible token by ensuring that it’s token id has a maximum supply of one, which represents a unique asset. Also, a uri can be specified for the token id, which should contain the metadata for the token, such as, image url, attributes, and other properties. This will allow the token to function as a non-fungible token.
Demonstration:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
contract MyNFT is ERC1155 {
uint256 public constant UNIQUE_TOKEN_ID = 1;
string public name;
string public symbol;
constructor() ERC1155("https://myapi.com/api/token/{id}.json") {
name = "My Unique NFT";
symbol = "MUNFT";
_mint(msg.sender, UNIQUE_TOKEN_ID, 1, "");
}
function uri(uint256 tokenId) override public pure returns (string memory) {
return string(
abi.encodePacked(
"https://myapi.com/api/token/",
tokenId,
".json"
)
);
}
}
Further Discussion:
The criteria for fungibility is as follows:
- Fungible Tokens: Are interchangeable and identical tokens, meaning each token is the same as every other token. One unit of a fungible token has the same value and properties as another unit. Examples of fungible tokens include cryptocurrencies like Bitcoin and Ether.
- Non-Fungible Tokens (NFTs): Are unique and cannot be exchanged on a one-to-one basis with another token because each has distinct characteristics. An NFT represents ownership or proof of authenticity of a unique item or piece of content, such as digital art, collectibles, or even real estate in virtual worlds.
Connect with me:
- Github: https://github.com/FaybianB
- LinkedIn: https://www.linkedin.com/in/faybianbyrd/
Previous articles:
- Question #17 (Easy): What is the check-effects pattern?
- Question #18 (Easy): What is the minimum amount of Ether required to run a solo staking node?
- Question #19 (Easy): What is the difference between fallback and receive?
- Question #20 (Easy): What is reentrancy?
- Question #21 (Easy): As of the Shanghai upgrade, what is the gas limit per block?
- Question #22 (Easy): What prevents infinite loops from running forever?
- Question #23 (Easy): What is the difference between tx.origin and msg.sender?
- Question #24 (Easy): How do you send Ether to a contract that does not have payable functions, or a receive or fallback?
- Question #25 (Easy): What is the difference between view and pure?
- Question #26 (Easy): What is the difference between transferFrom and safeTransferFrom in ERC721?
Disclosure: Some content may have been generated with the help of artificial intelligence.