iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🦆

Quick and Dirty NFT Creation for Personal Use

に公開

Having a simple way to create your own NFT can be quite handy, especially for X (Twitter) Blue subscribers who want to easily set an NFT as their profile icon.

For those wondering what an NFT even is, please refer to the following article.
https://zenn.dev/razokulover/articles/7db2340f14c2cd

While the article above also explains how to issue NFTs, the following guide outlines a more casual way to implement, deploy, and mint an NFT specifically for personal use.

*Note: I take no responsibility for any trouble caused by using the methods or code described below. Please proceed at your own risk.

Preparation

Make sure to have MetaMask installed.

Setting up a local environment for implementing NFTs (ERC721) can be a hassle, so we will use Remix - Ethereum IDE, a web-based IDE. It makes implementation and deployment easy, so it's recommended for small-scale development.

Preparing Metadata Files

Place the NFT metadata (JSON) and the icon image in a suitable location.

Place the image somewhere accessible. While the standard approach is IPFS, for personal use, S3 or anything else is fine. Personally, I feel GitHub is easier to manage and might last longer than some obscure IPFS provider, so I created a directory on my GitHub Pages like this and placed the images there.

For file naming, I think using numbers like 0.jpg is better for future management costs.

The metadata file should look like this:

 {
   "description": "icon for twitter",
   "image": "https://yuheinakasaka.github.io/twitter-icons/assets/0.jpg",
   "name": "@razokulover"
 }

Feel free to set the description and name as you like. For the image field, specify the URL of the image you just uploaded. The most important part is to name this file using only the NFT ID (no extension).

  • Correct: https://example.com/hoge/metadata/0
  • Incorrect: https://example.com/hoge/metadata/0.json

NFT IDs start from 0 and increment by 1. The NFT URL will be automatically generated using this ID and the URL information set in the _baseURI() function, which will be discussed later.

NFT Implementation


Once you have the image and metadata ready, it's time for the implementation. First, open Remix - Ethereum IDE.

Click the file icon in the left navigation. You should see a "+" button under FILE EXPLORER > WORKSPACES; click it. A "Create Workspace" dialog will appear. Select "ERC721" as the template and click OK.

You should then see directories such as contracts and scripts, along with a file like MyToken.sol (the name might vary).

Rename contracts/MyToken.sol to MyNFT.sol.

Then, paste the following code into that file.

 // SPDX-License-Identifier: MIT
 pragma solidity ^0.8.9;
 
 import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
 import "@openzeppelin/contracts/access/Ownable.sol";
 
 contract MyNFT is ERC721, Ownable {
     constructor() ERC721("MyNFT", "MNT") {}
 
     function _baseURI() internal pure override returns (string memory) {
         return "https://yuheinakasaka.github.io/twitter-icons/metadata/";
     }
 
     function safeMint(address to, uint256 tokenId) public onlyOwner {
         _safeMint(to, tokenId);
     }
 }

The key part is _baseURI(). In my case, I am specifying the URL up to the metadata directory I uploaded earlier. If you use this code, you should replace this with the URL of the directory where your metadata files are located.

By setting it up this way, when you mint an NFT later, it will be accessed via a tokenURI like https://yuheinakasaka.github.io/twitter-icons/metadata/0 or https://yuheinakasaka.github.io/twitter-icons/metadata/1.

After that, simply pressing cmd + s should automatically save and compile the code.

If it fails, go to the "Solidity Compiler" in the left navigation, set the COMPILER version to match the version in the source code (0.8.9 in the case above), and click the "Compile MyNFT.sol" button. This should work.

NFT Deployment

Open the "DEPLOY & RUN TRANSACTIONS" tab in the left navigation.

Click on "ENVIRONMENT" and select "Injected Provider - MetaMask". At this point, make sure your MetaMask is set to a testnet (such as Goerli). If you can't find Goerli, refer to something like Görli Testnet Connection Method - Qiita to get it set up.

You will need test ETH for the subsequent deployment and minting, which you can obtain from a Goerli faucet like Goerli Faucet.

For now, the ENVIRONMENT section should look like this:

Next, ensure that "CONTRACT (Compiled by Remix)" is set to "MyNFT - contracts/MyNFT.sol" and click "Deploy". MetaMask will open; confirm the transaction.

After a few dozen seconds, the deployment to the testnet will be complete.

NFT Minting

Now, all that's left is to mint the NFT to your own wallet.

In the "DEPLOY & RUN TRANSACTIONS" tab in Remix, you should see a "Deployed Contracts" section below the Deploy button.

It looks like this ↓

From here, you can cast the NFT to your wallet by sending a mint request to the contract you just deployed.

You should see a safeMint button in orange; click the down arrow next to it. This will reveal input fields for to and tokenId.

Enter your wallet address in to and enter 0 for tokenId if it's your first time.

Then, click the "transact" button. MetaMask will open; confirm the transaction after verifying the details.

You can view your transaction history by entering your wallet address into the search bar of the TESTNET Goerli (GTH) Blockchain Explorer.

If you see a "Safe Mint" entry, it's a success. It should look like this:

It should also appear on the OpenSea Testnet.

To the Mainnet

After completing the steps above, all you need to do is switch from the Goerli testnet to the mainnet and repeat the process. This will allow you to successfully obtain your favorite image as your personal NFT.

Required Expenses

Writing anything to the blockchain requires gas fees. In this case, gas fees are incurred for both deploying the contract and minting the NFT.

The gas fee required for deploying to the mainnet is around $60. Minting also costs several dozen dollars. In total, it costs nearly 10,000 yen. It's quite expensive.

Summary

I've described a casual way to implement, deploy, and mint your own personal NFT. Using a web IDE makes it quite accessible as you don't need to set up a local environment.

Please keep in mind that there are services available where you can mint without creating your own contract, so this isn't necessarily the easiest method. This is just for your reference.

For any questions or correction requests regarding this article, please reach out to @razokulover.

Discussion