How To Create ERC-721 (NFT) Token on Avalanche C-Chain
I wrote about how to create an ERC-20 token on avalanche C-Chain using openZeppelin, remix, and metamask in my previous article, but what if not all tokens are alike? That’s the point to use NFT tokens. ERC-721 is a standard that represents ownership of non-fungible tokens. Every token is unique in this standard and transferable between accounts.
Avalanche C-Chain, in principle, acts like Ethereum and compatible with smart contracts which are the core concept in Ethereum. We can create any token, strategy, or anything that is written in a smart contract language and can be deployed to Ethereum. Once these requirements are met, it means that Avalanche C-Chain can be used.
When you copy codes from the article please remember to remove and add double quotes again, because problems may occur regarding them.
Firstly, we should set our metamask to connect to Avalanche C-Chain. The process is the same as my previous article. Let’s set metamask step by step again.
Click to metamask icon on the browser and select network from the drop-down menu. We should connect to C-Chain now. Click to “Custom RPC”.
Now, we need to fill in these boxes with correct values.
Network Name: Avalanche C-Chain
New RPC URL:
- http://localhost:9650/ext/bc/C/rpc for Local Testnet
- https://api.avax-test.network/ext/bc/C/rpc for Fuji Testnet
- https://api.avax.network/ext/bc/C/rpc for Mainnet
ChainID:
0xa868
for Local Testnet0xa869
for Fuji Testnet0xa86a
for Mainnet
Symbol: C-AVAX
Explorer:
- n/a for Localnet
- https://cchain.explorer.avax-test.network for Fuji Testnet
- https://cchain.explorer.avax.network for Mainnet
After setting up all the parameters correctly, we should see this page. Now, we have 0 C-AVAX. “C” refers to C-chain and we have to get some C-AVAX to interact with the network.
Let’s go to avax faucet and paste our address with prefix “C-”.
For example my address is “0xfe8886bec537252040Dff36448C0F104Be635650” ,I need to paste my account address as “C-0xfe8886bec537252040Dff36448C0F104Be635650”
After copy and paste the address here, click request 2.0000 C-AVAX. This test faucet token has no value, it is just for development purposes.
Then, check your wallet. You should have some test tokens in your metamask.
Now, we can create our NFT token on Remix. Open Remix on your browser or follow this link.
You should see this page. On this page, first, click “SOLIDITY” from “Featured Plugins” and click the “New File” button. When you click the New File button, you will see a pop-up that requires a file name. You can choose a name or leave it default.
Since we will use an ERC-721 contract from OpenZeppelin, just paste this line to the file and save.
You will see lots of pop-ups appearing below your browser and a new file imported from openZeppelin is being added to remix under the GitHub section. At the end of the importing process, remix should look like the picture below.
We have ERC721PresetMinterPauserAutoId.sol.sol file in the presets. This file is written by OpenZeppelin according to ERC-721 standards with respect to minter and pauser functionality.
Open the “SOLIDITY COMPILER” on the second tab and select the solidity version that meets with the solidity version written in the file as “pragma solidity …..”. The version should be equal to or bigger than the file’s version. For example, in my file, “pragma solidity ^0.6.0” is written and the version is 0.6.0. So, in the compiler, I set the solidity version as 0.6.6. After setting up the solidity version click to compile button. If you did not change anything in the file or the solidity version is not wrong, you should not get an error. Also, you can ignore warnings for now.
This contract includes the following abilities:
- the ability for holders to burn (destroy) their tokens
- a minter role that allows for token minting (creation)
- a minter role that allows stopping all token transfers
- token ID and URI auto-generation
We do not have to change or modify anything on the contract source code because it is already prepared and ready for deployment. Let’s come to “DEPLOY % RUN TRANSACTİONS” section which is under the “SOLIDITY COMPILER” tab.
Now, we have a tool to deploy a contract. The first thing we should do is to change the environment from javascript VM to injected Web3. If it is the first time you are using remix, after selecting injected Web3, a pop-up will show up to ask to connect the account. Just connect your account and you will see your address on the “account” textbox.
The last thing we should set for preparing to deploy is selecting the correct contract in the “CONTRACT” section. Select the contract that meet the name “ ERC721PresetMinterPauserAutoId.sol.sol”. This is the main contract we will deploy, others are just helper contracts that fulfill the main contract requirements.
Now, we are ready to deploy our contract by giving token specifications on deploy sections. Name and Symbol fields are clear. BASEURI is a little bit complex, since we want to create a NFT with an image, it should have an image URL. But it is not simple to pass away an image URL and create NFT, you should satisfy an ERC-721 metadata format for that.
{
"id": 0,
"description": "My NFT",
"external_url": "https://forum.openzeppelin.com/t/create-an-nft- and-deploy-to-a-public-testnet-using-truffle/2961",
"image": "https://twemoji.maxcdn.com/svg/1f40e.svg",
"name": "My NFT 0"
}
In order to create a NFT with your own image, you need to have a JSON server like this one and provide information on that server. Then, copy and paste the link to BASEURI field. For this example, I will use the image that already exists on the JSON web server. If you do not want to deal with an image just leave it blank.
Fill in the name and symbol parts as you wish. For the BASEURI field, you may want to create your own JSON server or you can use this one like me. You may copy and paste the link below.
http://my-json-server.typicode.com/abcoathup/samplenft/tokens/0
Then, click to transact button and confirm all pop-ups. If everything goes well, you should see a deployed contract under the “Deployed Contracts” tab and a log on the terminal.
Before minting a NFT token, let’s check our transaction. Click the arrow beside the “DEBUG” button and copy the transaction hash. Then, go to avalanche C-Chain explorer and paste it to the search field. You will see your transaction information and the NFT address.
The first address is your own account address and the second address is the NFT token address. you can click on that and see more information.
Let’s mint one token to our own address or to any address we want.
Here, under the deployed contracts tab, there is a bunch of functions tied to the contract. Expand mint function and paste an address you want to send one NFT, then click to transact again.
After all, go to your metamask wallet and click to Add Token button. You need to copy your NFT address in order to add metamask. Then just paste NFT address, it will automatically pull SYMBOL and decimal. As you can see, decimal precision is 0 because it is a non-fungible token.
Finally, you should see 1 NFT in your account.
The problem is there is no app or service to see the picture of the NFT yet, but soon it would be available. On Ethereum, there is an available platform that allows you to create a NFT with your own picture and sell it from there. These platforms handle all processes including setting images, JSON webservers etc.
There will be such platforms for C-Chain, too. We would be able to create tokens without struggling with remix, openZeppelin, or anything we used in this article.
See you in the next article…
Murat CELİKTEPE
LinkedIn → celiktepemurat
Twitter → muratctp