Understanding and Using ERC1155 tokens
Simplifying using ERC1155 standard
For a programmer, laziness is a virtue.
ERC1155 Tokens
So last month I built a platform where you can mint and sell any file as Nfts. This could be a digital platform for content creators to monetize their works like ebooks, blogs, videos, music, arts or anything. But the problem was what if the creator wants to sell his content to multiple people? Nfts are Non-Fungible Tokens where each token is different from a different token. Using the ERC1155 Nft standard was the perfect solution to this.
What are Nft standards? Nfts are smart contracts holding digital content. These smart contracts import other contracts. These are sometimes called token standards. The most commonly used are 3 token standards: ERC20, ERC721 and ERC1155.
ERC1155 is a semi-fungible token standard where tokens can have multiple copies of them. Now, the Nfts can be unique as well as they can have multiple copies at the same time. Another implementation of this can be when you want to send the same Nft to multiple recipients you can just increase the number of copies while minting.
Below is an example of using ERC1155
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/extensions/ERC1155URIStorage.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol";
contract Test is ERC1155URIStorage {
constructor() ERC1155("") {}
function createToken(string memory tokenURI, uint256 supply) public payable {
uint256 currentToken = 1;
_mint(msg.sender, currentToken, supply, "");
_setURI(currentToken, tokenURI);
}
}
Above contract consist of importing the ERC1155 and ERC1155URIStorage contract for using the _mint and _setURI function from them for minting the token and associating metadata with it.
Function createToken() takes two arguments: URI containing metadata for Nfts (like the image, name etc.) and the supply of tokens the user wants.
For this example, we assumed there is only a single token with tokenId of 1 with multiple copies.