LCP

Non-Fungible means the things that hold some uniqueness, and are not interchangeable with similar items. Similarly, NFT (Non-Fungible Tokens) are the tokens used to define ownership of unique items. These items can be art, videos, audio, 3D models, or even real estate.

We have already shown you how to create tokens using ERC20 smart contract. We also have a particular blog based on Flow Blockchain, where you can learn how to Mint NFT on Flow Blockchain. In this blog, we will see how to develop an ERC1155 NFT smart contract and mint our very first NFT. We are going to use Remix IDE for the development and testing of the contract.

Creating Smart Contract

Firstly, open Remix and create a new file named SeafluxNFT.sol as shown in the image below.

A screenshot showing the Remix IDE with a new file named SeafluxNFT.sol being created.

Now, we will add the SPDX License Identifier and the Solidity version (We are using 0.8.11 for this contract). Next, we import a few modules provided by OpenZeppelin and add the declaration for the contract. Modules are required to import the methods of a contract standard, say ERC1155 or ERC20, or else, we need to write the abstract methods on our own.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.11;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Burnable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract SeafluxNFT is ERC1155, Ownable, ERC1155Burnable {

}


Pre-requisite:

A few pre-requisite are required before we proceed with the smart contract.

We have imported the following libraries in the above code:

  • ERC1155.sol: This module provides the standard abstract methods for ERC1155 smart contracts.
  • Ownable.sol: This module provides a modifier named onlyOwner which, when added with a function, restricts it from calling by anyone else except the owner.
  • ERC1155Burnable.sol: This module is an extension of the ERC1155 contract, which add the ability to destroy an NFT by the owner.
  • IERC20.sol: This module provides the standard abstract methods for ERC20 smart contract.

We have declared a contract named SeafluxNFT which inherits the ERC1155, ERC1155Burnable, and Ownable modules.

Next, we are going to declare the following variables:

  • mintPrice: The price of the NFT
  • name: The name which will be displayed on Opensea
  • maxSupply: The total number of NFTs that can be minted.
  • totalMinted: The total number of NFTs that are minted.

contract SeafluxNFT is ERC1155, Ownable, ERC1155Burnable {

    uint256 public mintPrice = 0.001 ether;
    string public name = "Seaflux NFT";
    uint256 public maxSupply=10000;
    uint256 internal totalMinted;

}

Now let's add a constructor in our code to initialize the ERC1155 class by passing in the IPFS path of our NFT, which will be used to display our NFT once it is minted. A constructor is a method commonly used in Object Oriented Programming and gets called whenever a class is initialized. In Solidity contracts, however, the constructor is called only once: when the contract is deployed.

 constructor()
        ERC1155("https://ipfs.io/ipfs/QmRyFNM4yS2Le6FHMb8KoFq7276NBNPZd5Hc5VmnukUoWT")
    {}

Next, we will add the following functions to the contract:

  • mint: The mint function is called to mint NFTs. This function will take the number of NFTs as the argument. Here we have added some necessary checks to make sure that the number of NFTs to be minted is not greater than the supply remaining. The ethers sent with the transaction should not be less than the number of NFTs * Minting Price, and not more than 10 NFTs can be minted in one transaction. Once these conditions are met, the mint method of ERC1155 is called to send the NFT to the sender's address. Note that we have added 1 in the mint function, which represents the ID of the NFT. Once the minting is done, totalMinted is increased by 1.


function mint(uint256 numOfNFTs) external payable {
        require(totalMinted + numOfNFTs undefined maxSupply,"Minting would exceed max supply");
        require(mintPrice * numOfNFTs undefined= msg.value,"Not enough MATIC sent");
        require(numOfNFTs undefined= 10,"Only up to 10 NFTs can be minted");
        _mint(msg.sender, 1, numOfNFTs, "");
        totalMinted += numOfNFTs;
    }

  • getTotalSupply: This function returns the total supply of the NFT
    function getTotalSupply() external view returns (uint256) {
        return maxSupply;
   }

  • getMinted: This function returns the number of NFTs that have been minted so far.
    function getMinted() external view returns (uint256) {
        return totalMinted;
    }

  • withdraw: This function helps to withdraw the Ethers that are available in the contract. Once a user has minted the NFT, the ethers sent during the transaction are kept in the smart contract. Only the owner of the smart contract can use this function. It checks the balance of ethers in the contract, and if it is greater than 0, it transfers the ethers it holds to its owner.
    function withdraw() public onlyOwner {
        require(address(this).balance undefined 0);
        uint256 contractBalance = address(this).balance;
        payable(_msgSender()).transfer(contractBalance);
    }

After adding all the functions, the contract will look like this:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.11;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Burnable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract SeafluxNFT is ERC1155, Ownable, ERC1155Burnable {
    uint256 public mintPrice = 0.001 ether;
    string public name = "Seaflux NFT";
    uint256 public maxSupply=10000;
    uint256 internal totalMinted;

    constructor()
        ERC1155("https://ipfs.io/ipfs/QmRyFNM4yS2Le6FHMb8KoFq7276NBNPZd5Hc5VmnukUoWT")
    {}

    function mint(uint256 numOfNFTs) external payable {
        require(totalMinted + numOfNFTs undefined maxSupply,"Minting would exceed max supply");
        require(mintPrice * numOfNFTs undefined= msg.value,"Not enough MATIC sent");
        require(numOfNFTs undefined= 10,"Only up to 10 NFTs can be minted");
        _mint(msg.sender, 1, numOfNFTs, "");
        totalMinted += numOfNFTs;
    }

    function getTotalSupply() external view returns (uint256) {
        return maxSupply;
    }

    function getMinted() external view returns (uint256) {
        return totalMinted;
    }

    function withdraw() public onlyOwner {
        require(address(this).balance undefined 0);
        uint256 contractBalance = address(this).balance;
        payable(_msgSender()).transfer(contractBalance);
    }
}

Now, head over to the Compile section in the Remix and check for any issues in the contract.

A screenshot of the Remix IDE's Compile section, displaying the contract compilation results without any issues.

Once the contract is successfully compiled, move it to the Deploy section. Select the Inject Provider - Metamask

A screenshot illustrating the process of moving the compiled contract to the Deploy section in Remix. The Inject Provider - Metamask option is selected.

Next, click on the Deploy button, which will deploy the contract on the selected testnet/mainnet.

A screenshot showing the Deploy button being clicked, initiating the deployment process of the contract to the chosen network.

Minting the NFT in OpenSea

Once the contract is deployed, we will mint 1 NFT. Add 1 as the argument for the mint function. This NFT is priced at 0.001 ETH, which is equal to 1 Finney. Make sure to send in 1 Finney with the transaction. Now call the mint function and wait for the transaction to go through.

Once the transaction goes through, head on to the Testnet Opensea and login with your Metamask. Select the address with which you minted the NFT and open the profile page. You will see your first-ever minted NFT in the Collected tab.

An image demonstrating the NFT minting process on OpenSea using Metamask, including relevant transaction details.

End Note

We have now minted our first NFT in your Metamask wallet. We used Remix to write all the Solidity code. We saw the variables that were used while deploying the smart contract and what are its functions, along with writing the codes for the contract. Now kindly head over to Remix and mint your first NFT.

We have also uploaded all the codes on our GitHub Repository head on to have access.

We, at Seaflux, are Blockchain enthusiasts who are helping enterprises worldwide. Have a query or want to discuss Blockchain projects? Schedule a meeting with us here, we'll be happy to talk to you!

Jay Mehta - Director of Engineering
Jay Mehta

Director of Engineering

Contact Us