How to Create and Deploy an XRC721 NFT Using Hardhat
Use Hardhat to deploy an XRC721 Token.
🧭 Table of contents
📰 Overview
Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. Get Solidity stack traces & console.log.
What you will learn
In this tutorial, you will learn how to set up Hardhat and use it to build, test, and deploy a XRC721 token on both the XDC Network mainnet and XDC Apothem testnet.
What you will do
Install and setup Hardhat
Create an XRC721 token
Compile the XRC721 token
Deploy the XRC721 token
Interact with the XRC721 token
Check the deployment status on xinfin.network
📰 About XRC721 Tokens
XRC721 is an open standard that defines an interface for non-fungible tokens on XDC blockchain:
balanceOf(address _owner) external view returns (uint256)
ownerOf(uint256 _tokenId) external view returns (address)
safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable
safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable
transferFrom(address _from, address _to, uint256 _tokenId) external payable
approve(address _approved, uint256 _tokenId) external payable
setApprovalForAll(address _operator, bool _approved) external
getApproved(uint256 _tokenId) external view returns (address)
isApprovedForAll(address _owner, address _operator) external view returns (bool)
These are the minimum required methods that allow an asset on the XDC network to be called an XRC721 token. Also, a XRC721 token must be able to emit the following Events
on the blockchain:
Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId)
Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId)
ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved)
Events are helpers that come in handy in the exhaustive process of indexing state changes, and they are essential for off-chain applications to find relevant data on the blockchain. By mapping all Transfer
events, for example, we can fetch all the historic data on token transfers more easily.
XRC721 also includes optional metadata parameters:
name
symbol
This allows your smart contract to be interrogated for its name and for details about the assets that your NFTs represent.
⚒ Starting a new Hardhat Project
There are a few technical requirements before we start. Please install the following:
Node.js v8+ LTS and npm (comes with Node)
Start by setting up tour folder. As we are creating a project called XRC721
, create a new XRC721
folder by running the following on terminal:
You can get started with Hardhat by running:
The following message will show on your console. Hit y
to continue or just press ENTER
:
The following message should log on your console:
Press ENTER
to get started with a new JavaScript Hardhat Project. Then you will be presented with the following options:
The standard Hardhat project comes with a pre-created Lock.sol
contract and deploy.js
script. It's best to clean up your working environment before moving forward:
Your folder files will look like this:
⚒ Configuring XDC Mainnet and Apothem Testnet on Hardhat
In order to get started deploying new contracts on XDC Mainnet and/or Apothem, you'll need to install a new dependency called dotenv
that will be used in the hardhat.config.js
file:
You will need to configure a .env
file with XDC Mainnet and Apothem Testnet RPC endpoints, plus the Private Key of the wallet you are using for deployment. Start by running:
Next, write the following info in our .env file:
🚨 Do not use the Private Key in the example above or you can risk losing your assets! 🚨
Finally, you can configure the hardhat.config.js
file for both Apothem and XinFin Networks by writing:
⚒ Adding Testnet XDC to Development Wallet
You should check your Signer's Address on Hardhat by accessing the Hardhat console:
If you get an error that Hardhat is not installed locally, and you are using a Windows OS, you will need to execute:
Once the hardhat console CLI opens, you can run:
This account is on the Ethereum standard format starting with 0x
, but we can simply switch 0x
for xdc
. In our example, the signer wallet address is: xdcA4e66f4Cc17752f331eaC6A20C00756156719519
.
With this account in hand, you can head to the Apothem Faucet and claim some TXDC for development purposes:
💵 Writing our first XRC721 Token
The source code for the XRC721 Token used in this tutorial is available here: XRC721 Contract Folder. But we will address all Events
, Methods
and Constants
mentioned in the section 📰 About XRC721 Tokens.
Start by creating the XRC721.sol
file:
You will have to use OpenZeppelin contracts, so please make sure it is installed using the following command:
Next, paste this code in your XRC721 file:
Thanks to OpenZeppelin, we don't have to implement all the code ourself. It's still a good excerize to go through the basic parts of XRC721 contract as explained below
💵 Events
As mentioned in 📰 About XRC721 Tokens, events are an important part of a smart contract logic. Events have indexed
variables that can be filtered by off-chain interfaces. We might be tempted to index all the variables that are tied to an on-chain event, however Solidity has a maximum of 3 indexed variable limitation for events. XRC721 has three basic events: Transfer
, Approval
and ApprovalForAll
.
💵 Methods
You must create the six methods mentioned in 📰 About XRC721 Tokens (ownerOf
, balanceOf
, safeTransferFrom
, transferFrom
, approve
, setApprovalForAll
, isApprovedForAll
and getApproved
) and a constructor
. This function is only called once, when the contract is deployed, where it contains information such as the token name, decimals and/or initial token supply:
💵 XRC165
We didn't mention it before, but XRC721 also requires implimentation of a XRC165 standard. Thanks to OpenZeppelin we don't have to implement it, but it is really simple. There is only one method, supportsInterface
, and it goes as follows:
💵 Enabling minting
Now that you have a XRC721 contract, how can you mint an NFT with it? With the mintToken
method, that's how! Each time mintToken
is called, it will create new unique token assign to tokenOwner
.
💵 Compiling and Deploying
You can now compile your XRC721.sol
by running:
If everything is correctly configured and there are no errors, you should see the following message on your console:
In order to deploy our newly compiled contract artifacts to the blockchain, we need to create a deployment script into the script folder:
Write the following script to the deploy.js
file:
If the deployment script have no errors, you can run the following command for deployment on the XDC mainnet:
Or this command, for deployment on the XDC Apothem Testnet:
In either case, you need to have enough funds to pay for gas fees on the address that is being used for development.
If the deployment is successful, the console will log the following message after migrations complete processing:
Find out how your freshly minted NFT looks on Apothem Block Explorer
🔍 Veryfing Contracts on the Block Explorer
Once you have successfully deployed your smart contract to the blockchain, it might be interesting to verify you contract on XinFin Block Explorer.
Simply grab the XRC721.sol
address from the previous step: this address is in the Ethereum standard but we can simply swap the 0x
prefix for xdc
and search for our newly deployed contract on XinFin Block Explorer:
Click in the Verify And Publish
Option.
You will be redirected to the contract verification page where you will need to fill out:
Contract Name: XRC721
Compiler: Check your
hardhat-config.js
file for Compiler VersionContract Code: Just paste everything from your
XRC721.sol
file
Once everything is filled out, press Submit!
If everything is correctly filled out, your contract page on the block explorer will display a new tab called Contract
:
For more information about Hardhat, Please Visit Hardhat Documentation. For more information about the XDC Network, Please Visit XDC Network Documentation on GitBook. Resources used during the deployment of the XRC721 Token can be found at XRC721 Contract Folder.
Last updated