How to Mint a PERC721 Token: A Step-by-Step Guide

How to Mint PERC721 Tokens on Swisstronik Network

How to Mint a PERC721 Token: A Step-by-Step Guide

Imagine owning a digital collectible that’s as unique as a one-of-a-kind painting or a rare trading card. That’s what a PERC721 token represents! It’s a digital asset that proves you own something special—be it art, music, virtual pets, or even virtual real estate. Unlike cryptocurrencies like Bitcoin or Ethereum, which are interchangeable (fungible), each PERC721 token is unique and cannot be exchanged for another on a one-to-one basis.

Why Choose PERC721?

The PERC721 standard has become a favorite among creators and collectors alike for several reasons:

  • Uniqueness: Each token is distinct—no two are the same!

  • Transferability: You can easily buy, sell, or gift your tokens.

  • Interoperability: Most wallets and marketplaces support PERC721 tokens, making it easy to showcase your collection.

Ready to create your own digital masterpiece? Let’s get started!

Getting Started

Don’t worry if you’re new to coding; we’ll break everything down into simple steps. Just follow along, and soon enough, you’ll be minting your very first NFT!

Step 1: Set Up Your Development Environment

Before we jump into coding, we need to set up our tools. Here’s what you’ll need:

  1. Node.js and npm: These are essential for running JavaScript applications. You can download them from Node.js official website.

  2. Hardhat: A powerful development framework for Ethereum-based blockchains. Install it by running npm install --save-dev hardhat in your terminal.

  3. MetaMask: A popular cryptocurrency wallet that allows you to interact with Ethereum-based applications. Download MetaMask and set up your wallet.

Step 2: Create Your Smart Contract

Now comes the fun part—writing the smart contract! Open your favorite text editor and create a new file named SwistarNFT.sol. This is where we’ll define our NFT.

Here’s a simple code snippet to get you started:

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

import "./IPERC721.sol";
import "./IPERC721Metadata.sol";

contract SwistarNFT is IPERC721, IPERC721Metadata {
    string private _name;
    string private _symbol;
    mapping(uint256 => address) private _owners;
    mapping(address => uint256) private _balances;
    mapping(uint256 => address) private _tokenApprovals;
    mapping(address => mapping(address => bool)) private _operatorApprovals;
    mapping(uint256 => string) private _tokenURIs;

    constructor() {
        _name = "Swistar NFT";
        _symbol = "SNFT";
    }

    function name() external view override returns (string memory) {
        return _name;
    }

    function symbol() external view override returns (string memory) {
        return _symbol;
    }

    function tokenURI(uint256 tokenId) external view override returns (string memory) {
        require(_exists(tokenId), "PERC721Sample: URI query for nonexistent token");
        return _tokenURIs[tokenId];
    }

    function mint(uint256 tokenId, string memory uri) public {
        require(!_exists(tokenId), "PERC721Sample: Token already minted");
        _balances[msg.sender] += 1;
        _owners[tokenId] = msg.sender;
        _tokenURIs[tokenId] = uri;
    }

    function _exists(uint256 tokenId) internal view returns (bool) {
        return _owners[tokenId] != address(0);
    }
}

Don’t worry if some of this code looks complex—we’ll explain it as we go!

Step 3: Configure Hardhat

Next, let’s configure Hardhat so it knows how to deploy our smart contract. Open your hardhat.config.ts file and add the following configuration:

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-ethers";
import "@nomicfoundation/hardhat-toolbox";

const config: HardhatUserConfig = {
  solidity: "0.8.24",
  defaultNetwork: "swisstronik",
  networks: {
    swisstronik: {
      url: "https://json-rpc.testnet.swisstronik.com/",
      accounts: [`0x${process.env.PRIVATE_KEY}`],
    },
  },
  etherscan: {
    apiKey: process.env.ETHERSCAN_API_KEY,
  },
};

export default config;

Make sure to create a .env file in your project directory with your private key and EtherScan API key.

Step 4: Deploy Your Contract

Now it’s time to deploy your contract! Open your terminal and run the following command:

npx hardhat ignition deploy ignition/modules/SwistarNFTModule.ts --network swisstronik

This command compiles your smart contract and deploys it to the Swisstronik testnet—your first step into the blockchain universe!

Step 5: Mint Your First PERC721 Token

Congratulations! Your contract is now live on the blockchain. Let’s mint your first token using the mint function we defined earlier. Here’s how:

const swistarNFT = await swistarNFT.deployed();
await swistarNFT.mint(1, "ipfs://QmdXkZzWqYpHgJLZuBtKZPZrFJUaRZT1eJL1tX1t1L1t1");

In this example, we’re minting a token with ID 1 and linking it to an IPFS URL that contains its metadata. Feel free to replace this with your own unique metadata URL!

What’s Next?

You’ve taken your first steps into the world of NFTs! Here are some exciting paths you can explore next:

  • Test Your Contract: Make sure everything works as expected before going live.

  • Learn About Gas Fees: Understand transaction costs associated with minting and transferring tokens.

  • Explore Token Transfers: Discover how to send tokens between wallets.

  • Create a User Interface: Build an engaging front-end for managing your NFT collection.