In today's blog post, we will explore a random but critical topic in the Blockchain field - Non-Fungible Tokens (NFTs). More specifically, we are going to learn how to create our own NFT using the Ethereum Blockchain and the Solidity programming language.
Non-Fungible Tokens (NFTs) represent a new class of digital assets. Unlike Bitcoin or Ethereum, NFTs are unique and not interchangeable. This uniqueness and scarcity are what gives NFTs value.
Before we dive into the code, ensure you have Node.js installed on your machine. After that, we are going to install Truffle, which is a development framework for Ethereum.
npm install -g truffle
In creating our NFT, we will need to adhere to the ERC721 standard. This standard provides a mapping from a unique identifier, the token ID, to an owner's address. Here's how we can declare it using Solidity:
pragma solidity ^0.5.0; import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.1/contracts/token/ERC721/ERC721.sol"; contract MyNonFungibleToken is ERC721 { uint256 public tokenCounter; constructor () public ERC721 ("NFT", "MNFT") { tokenCounter = 0; } function createCollectible(string memory tokenURI) public returns (uint256) { uint256 newItemId = tokenCounter; _mint(msg.sender, newItemId); _setTokenURI(newItemId, tokenURI); tokenCounter = tokenCounter + 1; return newItemId; } }
In this code, we are creating a new contract MyNonFungibleToken
which extends from the ERC721 token contract standard from the OpenZeppelin library. The createCollectible
function allows for the creation of new unique tokens.
This was a simple demonstration on how to create a Non-Fungible Token using Solidity. NFTs have a broad application with digital art, games, real estate, and other digital goods. This is just scratching the surface of what can be achieved with NFTs. Happy learning!