How to create and deploy an ERC-1155 NFT

overview

ERC1155 has become the gold standard for creating NFTs; every major marketplace lists new tokens as ERC1155. In this article, we will learn about the ERC1155 token standard and how to create an ERC1155 token.

What we will do:

  1. Create 3 NFT collections
  2. Create and deploy ERC-1155 contracts
  3. Update the contract to be compatible with OpenSea
  4. Deploy our collection of NFTs

what do you need:

v2-fc9de678e6150b187b37988ffcbab75f_1440w

What is ERC1155?

ERC1155 is a multi-token standard that allows the creation of fungible, non-fungible and semi-fungible tokens in one contract. Before ERC1155, if a use case required ERC20 (fungible) and ERC721 (non-fungible) tokens, separate contracts were required to achieve this. ERC1155 also allows multiple NFT collections to be launched in one smart contract, rather than creating different contracts for each collection; this improves the efficiency of smart contract construction and minimizes the number of transactions, which is very important because it consumes more Less blockchain space. With ERC1155, it is also possible to transfer tokens in batches instead of transferring tokens to a single address as in the previous standard.

A common example of ERC1155 application is blockchain-based decentralized games, since games require coins and collectibles, so ERC1155 has become the standard there. ERC1155 has also become a standard in the NFT field.

The previous ERC721 had a one-to-one mapping of token IDs to addresses. ERC1155 has a fairly complex mapping where addresses in token id combinations map to token balances.

Create metadata URI

We'll create 3 NFT collections (rock, paper, and scissors), with one NFT in each collection. To upload our files to the decentralized storage IPFS, we can upload files via CLI or use this very easy to use tool NFT Storage .

We will use the second option, NFT storage. Log in to NFT to store and upload image files of rock, paper and scissors. After a successful upload, you should see something like this:

The NFT storage UI displays a form for uploading files.

Click on "Actions" and copy the IPFS URL for each image; we'll need it as metadata for each collection.

Example of option to copy IPFS URL from NFT storage UI.

We'll create three JSON metadata files to store information about our collection of NFTs.

  • 1.json: Rock collection
  • 2.json: Paper collection
  • 3.json: Scissors collection

Our 1.json file looks like this:

{
    
     //1.

    "name": "Rocks",

    "description": "This is a collection of Rock NFTs.",

    "image": "https://ipfs.io/ipfs/bafkreifvhjdf6ve4jfv6qytqtux5nd4nwnelioeiqx5x2ez5yrgrzk7ypi",

}
  • name : The name of the NFT.

  • description : A description of the NFT.

  • image : The link (IPFS URL) to the image we obtained earlier.

  • If a collection has multiple images (which is usually the case), an extra parameter id is added to distinguish the markers in the collection.

Create the remaining JSON file 2. json and 3. json, for cloth and scissors collections respectively.

To efficiently upload all JSON files to IPFS, we will archive them in a content-addressable format. https://car.ipfs.io/ Helps archive files in the IPFS-compatible content-addressable archive (.car) format.

Go to IPFS CAR and upload the three JSON files above. Once uploaded, download the .car file and upload it to NFT Storage . All of our JSON files are now archived on IPFS. Copy the IPFS URL of the uploaded .car file and you should be able to access the JSON file by entering the filename at the end of the URL, for example:

https://ipfs.io/ipfs/bafybeihjjkwdrxxjnuwevlqtqmh3iegcadc32sio4wmo7bv2gbf34qs34a/1.json

The JSON of the Rock collection displayed on the web page.

Create and deploy ERC1155 contracts

We will use the OpenZeppelin contract library to create our ERC1155 contract and deploy it on the Ropsten testnet using the Ethereum REMIX IDE . Make sure you have some Ropsten test ETH, you can also get test coins from Ropsten Faucet .

Create a new file token.sol in REMIX and paste the following code into it.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

contract rockPaperScissors is ERC1155 {
    uint256 public constant Rock = 1;
    uint256 public constant Paper = 2;
    uint256 public constant Scissors = 3;

    constructor() ERC1155("https://ipfs.io/ipfs/bafybeihjjkwdrxxjnuwevlqtqmh3iegcadc32sio4wmo7bv2gbf34qs34a/{id}.json") {
        _mint(msg.sender, Rock, 1, "");
        _mint(msg.sender, Paper, 1, "");
        _mint(msg.sender, Scissors, 1, "");
    }
}

Explanation of the code above:

Line 1: Specifies the SPDX license type, added after Solidity ^0.6.8. These licenses can help resolve/avoid copyright issues whenever the source code of a smart contract is made available to the public. If you don't want to specify any license type, you can use the special value UNLICENSED or simply skip the entire comment (doesn't cause an error, just a warning).

Line 2: Declare the Solidity version.

Line 4: Import the OpenZeppelin ERC1155 contract.

Lines 6-9: Create a contract called rockPaperScissors and create three variables Rock , Paper , and Scissors ; then assign an appropriate ID to each.

Lines 11-15: Initialize the constructor with the link to the car file as an argument, and cast the different NFT collections with the argument:

The address where tokens will be minted, where msg.sender refers to the deployer of the contract. token id, we have assigned a name to the token id, so use the name here. Amount of each token. The last one is the data field left blank here.

Compile the contract, go to the third tab of the left menu, select Injected Web3 as environment and deploy by selecting the correct contract name:

REMIX window with deployment options.

Approve transactions from MetaMask. After the transaction is completed, your contract will be deployed.

Now you can perform functions like get token balance by entering address and token ID. We can also retrieve the token's URI by entering the token id.

REMIX window for deployed contracts.

OpenSea does not support the returned URI format. So we need to override the URI function to return the filename as a string:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

contract rockPaperScissors is ERC1155 {
    uint256 public constant Rock = 1;
    uint256 public constant Paper = 2;
    uint256 public constant Scissors = 3;

    constructor() ERC1155("https://ipfs.io/ipfs/bafybeihjjkwdrxxjnuwevlqtqmh3iegcadc32sio4wmo7bv2gbf34qs34a/{id}.json") {
        _mint(msg.sender, Rock, 1, "");
        _mint(msg.sender, Paper, 1, "");
        _mint(msg.sender, Scissors, 1, "");
    }

    function uri(uint256 _tokenid) override public pure returns (string memory) {
        return string(
            abi.encodePacked(
                "https://ipfs.io/ipfs/bafybeihjjkwdrxxjnuwevlqtqmh3iegcadc32sio4wmo7bv2gbf34qs34a/",
                Strings.toString(_tokenid),".json"
            )
        );
    }
}

Replenish:

Line 5: Import the OpenZeppelin contract and convert Integer to String.

Lines 18-25: Override the URI function by creating a custom URI function and converting the token from an integer to a string, then return the full URI.

Recompile the contract and deploy. When you now query the contract for a URI, it will return the formats supported by OpenSea.

The output is a REMIX window with an OpenSea compatible URI.

in conclusion

Congratulations on deploying your ERC1155 token. If you made it here by now, you know about the ERC1155 multi-token standard and how to create and deploy an ERC1155 NFT.

Guess you like

Origin blog.csdn.net/hanru723/article/details/125778477