Creating ERC721 Tokens
How to create and deploy these tokens on the Ethereum blockchain.
The rise of new cryptocurrencies is becoming more prevalent everyday. Because of the vast opportunities in market for a variety of challenges, it is impossible to predict how many coins we’ll see in the future. Because of this, it’s important to understand how to create a coin, or at least the technology behind it. Today I’m going to be showing you how to create your own ERC721 token, like I did.
ERC721 tokens are non fungible tokens (NFT), which essentially means they are all unique compared to each other, unlike the standard ERC20 tokens. Imagine trading one American dollar for another American dollar. This is allowed because they are the same currency that holds the value, which means you still end up with one dollar. Now, imagine trading one house with another house. This isn’t allowed because they hold different values and have different properties, such as are in different locations, and have other values that make it unique, but it is still a house. This is the difference between standard tokens and NFTs.
Also, because of the ERC part of the name, they run on the Ethereum blockchain. I assume that you already know what the Ethereum blockchain is, but if you don’t you can read my other article on researching the top 10 cryptocurrencies with the highest market cap, as I explain more about it there. I chose to do this because it is so easy for beginners to understand how to work with Truffle development tools, which you will need to download (specifically Truffle and Ganache).
The first thing you need to do is download a good Solidity text editor, my favorite is Sublime. Solidity is the programming language needed to code for smart contracts and if you already have a programming background, it’s really simple to follow.
As I said before, for beginners purposes we are going to download the truffle suite for development which consists of a compiler (Truffle), and local blockchain (Ganache). Ganache will be used for testing and development purposes only, it’s not connected to the Ethereum main net and it allows you to get 100 fake Ether for testing purposes.
Once you have everything downloaded, you can start by unboxing a truffle box into an empty folder in your desktop named with your project name by calling “truffle unbox” in your command line. This will produce a few folders, the most important ones being contracts, migrations and tests. In the contracts folder make a new file and name it whatever you want but with the extension .sol for solidity.
ERC721 tokens have certain standards that have to be met in order to be defined as that kind of token. For this you have to define the following functions in the contract:
1. Approval — approve an address for holding an NFT.
2. BalanceOf — returns all NFTs assigned to an address.
3. Transfer — transfer tokens from one address to another address.
4. OwnerOf — returns the address of a specific NFT.
5. TransferFrom — transfers ownership of an NFT to another address.
6. Approve — set an approved address for an NFT.
On top of these functions you can define what makes your token non fungible, or unique like a mint function. I won’t walk you through this function’s code but it is easy to understand in my Github repository below its all there if you
want to see an example. Mine simply mints each coin with a random number that is different than other coins.
After you define each function, you will need to migrate it move it to the local blockchain on Ganache. To do this you have to write this migrate script and write “truffle migrate” in the command line after launching Ganache.
After you migrate it, you can test it by writing testing scripts.
This is to test specific unique functions different than those defined in the standards to make sure there are no flaws in your code before deploying it on the real Ethereum blockchain.
The test script for this project just checks to made sure each token has it’s unique random number and that some basic functions we defined above work.
Now its time to deploy it to the real Ethereum blockchain! You can do this in a couple of ways, the main one is to close Ganache and the local blockchain and run “truffle deploy” in the command line. This will deploy the smart contract on Ethereum mainet but it will use Ether for gas or transaction costs
which means you have to be sure the contract for the coin works!
After that you are done. You can now interact with your contract and transfer your ERC721 tokens, create new ones, and more with the functionality you implement. What you do with them is up to you.
All the code for this project can be found in my Github.