ERC-721 (NFT)

Overview​

Launching an NFT collection on Velocity follows very similar steps to how you would on Ethereum. As is the case with Ethereum, you will need to implement the ERC721 standard to create a Non-Fungible Token. For those familiar with Object-Oriented Programming, it's much like implementing an interface. You need to implement each of the following events/functions:

interface ERC721 /* is ERC165 */ {
    event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);=
    event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);
    event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);

    function balanceOf(address _owner) external view returns (uint256);
    function ownerOf(uint256 _tokenId) external view returns (address);
    function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable;
    function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;
    function transferFrom(address _from, address _to, uint256 _tokenId) external payable;
    function approve(address _approved, uint256 _tokenId) external payable;
    function setApprovalForAll(address _operator, bool _approved) external;
    function getApproved(uint256 _tokenId) external view returns (address);
    function isApprovedForAll(address _owner, address _operator) external view returns (bool);
}

interface ERC165 {
    function supportsInterface(bytes4 interfaceID) external view returns (bool);
}

NOTE: NFTs are not necessarily ERC-721 tokens, they can also be ERC-1155, for example.

For this walk through, we're going to be using Hardhat (configure it with Velocity).

Prerequisites​

To follow along, it's recommended to review and be familiar with the documentation on deploying a contract. You will also need to have a working Node.js >=16.0 installation and a small amount of VLCT for gas.

Step 1: Set up your environment​

Select Create an empty hardhat.config.js and hit enter. Now, install the hardhat-toolbox plugin:

Configure hardhat with Velocity.

Step 2: Host NFT Art on IPFS​

IPFS (InterPlanetary File System) is the decentralized way to store files. NFT artwork tends to be pretty large, so storing them on-chain isn't an option.

  1. Download the IPFS CLI by following the instructions here.

  2. Create IPFS repository

  1. Open another terminal window and run the following:

Now open the first terminal window and run the following to add your image file (image.png) to ipfs:

This will output a hash prefixed by a "Qm". copy that and add the β€œhttps://ipfs.io/ipfs/” prefix to it.

  1. Create a JSON file and add it to IPFS:

and then run:

Another "Qm" prefixed hash string will be generated. Copy that down and add the same β€œhttps://ipfs.io/ipfs/” prefix to it.

Step 3: Implement the ERC-721 Token Contract​

  1. Create a new directory called contracts and create a file called VelocityNft.sol

  1. Open VelocityNft.sol in your favorite text editor or IDE (VS Code has a Hardhat extension). To keep it simple for the sake of this tutorial, we're going to import OpenZeppelin's ERC-721 Implementation. To use this, quickly run in the terminal:

If you go and take a look at the repository, you'll notice how they implement the ERC-721 standard as mentioned above. Take some time to look through the code, and then edit VelocityNft.sol to look like this:

  1. Now that you've got that all coded up, it's time to compile and deploy. You can also see here for more deployment info. Run from project root:

This should compile without errors. Create a directory called scripts, and within it add a file called deploy.js. Add the following:

Now, edit your hardhat.config.js to look something like this:

Proper private key management is critical. To safeguard the mnemonic, it has been added to a file called mnemonic.txt in this case. DO NOT PUSH THIS TO GITHUB OR COMMIT TO SOURCE CONTROL. Even if you delete it after, assume it will live on forever after being committed and is compromised. Add mnemonic.txt to your .gitignore if you plan on committing, or store securely it in an environment variable.

To deploy, run:

Congrats, you have deployed a basic ERC-721 contract to Velocity! If you like, you can build out a front end to view your NFT. For now, you can view your token in Velocity explorer.

Last updated