Your interest in ERC 721 tokens implies either you want to get into NFT trading, or maybe you are a blockchain enthusiast or a developer who wants to understand the technology behind the process of creating an ERC 721 token.
ERC 721 tokens are nonfungible tokens (NFT) built on the Ethereum blockchain using the ERC 721 standard. This blog won’t cover the introduction of NFTs, considering the assumption that without knowing what NFTs are, their relevance in the real world and why they are so popular, you wouldn’t have been interested in creating them. So, the blog will dive straight into the creation process. Still, it is important to jot down a few technical information that you must brush up quickly before you start the process of creating and deploying an ERC 721 token.
- All ERC 721 tokens are nonfungible, which essentially means they are all unique compared to each other.
- ERC 721 tokens are built using ERC 721 standard; they run on the Ethereum blockchain and are governed by smart contracts
- Defining ERC 721 standard functions on a smart contract allows it to operate as tradeable NFT tokens.
How to create and deploy your ERC721 tokens to the Ethereum blockchain?
- Download a Solidity text editor, like Sublime and code your smart contract using Solidity
- Next, download the Truffle suite. The suite consists of a compiler (Truffle) and a local blockchain (Ganache). Ganache is needed for testing and development purposes only. You can get 100 fake Ethers for testing. Ganache is not connected to the Ethereum main net.
- Next, you need to unbox the truffle box into an empty folder saved on your desktop. Give the empty folder a name, say ERC721, and then call “truffle unbox” in your command line to start with the unboxing.
- Truffle unboxing will produce a few folders inside your ERC721 folder. The folders of importance are ‘Contracts,’ ‘Migrations,’ and ‘Tests.’
- In the ‘Contracts’ folder, make a new file, give it a name and save it with the extension .sol for Solidity.
- Now, as mentioned earlier, ERC 721 tokens are built using the ERC 721 standard. So, next, you need to define certain functions in your contract to meet the ERC standard. Only after defining these standard functions, your contact will get identified as an ERC 721 token. The implications of the standard functions are as follows:
Approval — it approves an address for holding an NFT
balance of — it returns all NFTs assigned to an address.
Transfer — this transfers tokens from one address to another
owner of — this returns the address of a specific NFT
transfer from — This transfers NFT ownership from one address to another address.
Approve — it sets an approved address for an NFT.
- To define the ERC 721 stand functions in your contact, run the following code:
pragma solidity ^0.5.0;
//import “@openzeppelin/contracts/token/ERC721/ERC721.sol”;
contract my721 is ERC721{
mapping(address => uint) tokens;
function approval(address _owner, address _approved,uint _tokenId){
require(tokens[_owner]==_tokenId);
tokens[_approved]=_tokenId;
}
function transfer(address _to, uint _amount) public payable{
require(_amount <= tokens[msg.sender]);
tokens[msg.sender]-=_amount;
tokens[_to]+=_amount;
}
function balanceOf(address _owner) public view returns (uint){
return tokens[_owner];
}
function ownerOf(uint _tokenId) public view returns(address){
return tokens[_id].address;
}
function TransferFrom(address _from, address _to, uint _tokenId) payable{
require(tokens[_from]==_tokenId);
tokens[_from]=0;
tokens[_to]=_tokenId;
}
function approve(address _approved, uint _tokenId) payable{
require(tokens[msg.sender]==_tokenId);
tokens[_approved]=_tokenId;
}
function mint(address _to, uint _tokenId,) public{
tokens[_to] = ‘mytoken ‘+str(uint(blockhash(block.number – 1)));
}
- Apart from the functions defined in the ERC 721 standards, you can also add other specific unique functions to describe your token’s uniqueness from other tokens. Before deploying the token on the real Ethereum blockchain, you must test these extra-added specific functions thoroughly in the local blockchain Ganache.
- After defining all functions in your contract, you need to test them. For which you need to migrate your contract to the Ganache, the local blockchain network. For the migration, write the following migrate script and then write “truffle migrate” in the command line after launching Ganache.
var my721 = artifacts.require(“my721”);
module.exports = function(deployer){
deployer.deploy(my721);
- After successfully migrating to Ganache, write your testing scripts to test specific functions other than those defined in the ERC 721 standards.
- Now, it’s time to deploy your token or smart contract on the real Ethereum blockchain. For this, simply close Ganache, the local Blockchain and then in the command line, run “truffle deploy.”
The final words
When your smart contract gets deployed on Ethereum mainnet, it will use Ether for gas (transaction cost). So, make sure you have done tests properly and ensure that the contract for the coin works, as you don’t want to waste your Ether. Once you have completed the deployment, you will be able to transfer your ERC721 tokens and do more, depending on the functionality you have implemented. Working with an experienced blockchain development company can help you create and deploy ERC 721 tokens quickly.