DEV Community

Jamiebones
Jamiebones

Posted on

How to Create and Deploy an NFT Smart Contract

This block post will show how to create and deploy your NFT to Opensea market place. An NFT can represent anything of value, it can be a character in a game, a digital art, title deeds of an apartment and so forth.

Openzeppelin has implemented code for the ERC1155 standard. The ERC1155 standard is a contract interface that is used for both fungible tokens and NFT.

Before writing our NFT smart contract we should save our image to IPFS (InterPlanetary File System). IPFS is used to save digital media on the blockchain.

Uploading NFT Media File

The media file for the NFT smart contract will be saved on nft.storage. Navigate to the to Nft Storage. We can save our NFT media file on IPFS. Login using your Github account or use your email to sign up for the service.

Click on upload button and you will be presented with the a screen that looks like the image below.

Image description

If you want to upload just one file you can click on the upload button to do that but if you have a directory of files you will need to upload them as a CAR. A CAR is a Content Addressed Archive that allows you to pre-compute the root CID for your assets.

Since we will most likely be uploading a directory of files lets proceeds to Upload CAR.

Image description

Click to open the directory and select the NFT's Assets files. Click on the download button to download the CAR file. Go back to the site NFT Storage and upload the CAR file to upload your assets to IPFS.

Uploading NFT Metadata

The next step is to upload the metadata that will be read by Opensea. A metadata file is a JSON file that contains a JSON object with the description of the assets.

Sample JSON Object

 {
   "id" : 1,
   "name" : "the name of the image or assets",
   "description": "description of the image or assets",
   "image" : "the location of the assets"
 }

Enter fullscreen mode Exit fullscreen mode

The location of the assets is got from NFT Storage. Click on the Actions button of the uploaded asset.

Image description

A dropdown is displayed. Click on View URL. Clicking on the View URL button takes you to a page where all the images that was uploaded via CAR is listed. Click on the link to show the image on the browser. Copy the image link and use it as the image value of the JSON file.

you should name your images using numbers like: 1.png, 2.png; so it will be easy to retrieve the image location.

You will have to upload one JSON file for each image assets uploaded. Also name your JSON file using numbers like 1.json, 2.json and so forth.

We need to upload the metadata file to IPFS using the same method we used for the upload of the image file. Make a CAR file of the content of the directory of JSON metadata and upload the CAR file to IPFS via NFT Storage.

Create the Smart Contract

//SPDX-License-Modifier: MIT

pragma solidity ^0.8.7;

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


contract DASHNFT is ERC1155 {

  uint256 public constant TOKEME = 1;
  uint256 public constant VODERMOT = 2;
  uint256 public constant CYRUS = 3;
  uint256 public constant FLASHBOT = 4;

  constructor() public ERC1155("https://bafybeigjypyqmqby2hu3rqpljezi44adnb3ttlinlz77nuzqlq33c2tyta.ipfs.nftstorage.link/{id}.json") {
      _mint(msg.sender, TOKEME, 100, "");
      _mint(msg.sender, VODERMOT, 100, "");
      _mint(msg.sender, CYRUS, 100, "");
      _mint(msg.sender, FLASHBOT, 100, "");
  }

function uri(uint256 _tokenId) override public view returns (string memory){
    return string(abi.encodePacked(
      "https://bafybeigjypyqmqby2hu3rqpljezi44adnb3ttlinlz77nuzqlq33c2tyta.ipfs.nftstorage.link/",
      Strings.toString(_tokenId),
      ".json"
    )
    );
  }

}
Enter fullscreen mode Exit fullscreen mode

We are making use of OpenZeppelin implementation for ERC1155. At the top of the file we imported the ERC1155.sol file, we also imported an utility contract file that is used for concatenating strings together. Our contract inherits from the ERC1155 contract.

We defined four constant variables which we initialized with values from 1 to 4. The URL of the metadata of the assets is passed to the constructor of ERC1155.

https://bafybeigjypyqmqby2hu3rqpljezi44adnb3ttlinlz77nuzqlq33c2tyta.ipfs.nftstorage.link/{id}.json
Enter fullscreen mode Exit fullscreen mode

Notice the id in curly braces {id}. This id represents the number that we named the JSON file. For each minted NFT, the contract will call the function uri to get the complete location of the metadata. We are calling the _mint function to mint 100 copies each of the four types of NFT we have and assigned them to msg.sender.

Our custom implementation of the uri function concatenates the id of the NFT with its base URL and returns the complete URL of the NFT metadata.

Deploy your contract to a blockchain testnet and copy the contract address and proceed to the testnet of Opensea and search using the deployed NFT contract address. Your NFT contract and its image will be displayed on Opeansea.

Thanks for reading

Top comments (2)

Collapse
 
imintify profile image
iMintify

2020 this was the way now you can create Smart Contract with a single line of code. Check out our app! imintify.com/nft-smart-contract/

youtu.be/OmN-7dBnjQ8

Image description

Collapse
 
imintify profile image
iMintify

Image description