Introduction to ERC-721

Table of contents

No heading

No headings in the article.

The ERC-721 standard is a type of non-fungible token (NFT) on the Ethereum blockchain. It enables developers to create unique, one-of-a-kind tokens that represent digital assets such as art, collectibles, and more. In this blog post, we'll go through the process of writing your first ERC-721 smart contract.

To start, you'll need a development environment with the following tools installed:

  • A code editor, such as Visual Studio Code or Sublime Text

  • The Solidity compiler, which you can install through the command-line interface (CLI)

  • A local Ethereum blockchain environment, such as Ganache or Remix

Once you have these tools set up, let's dive into the code!

First, let's define the basic structure of our ERC-721 contract. We'll start by importing the OpenZeppelin library, which provides a pre-written, secure implementation of the ERC-721 standard.

pragma solidity ^0.7.0;

import "<https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC721/ERC721.sol>";

contract MyERC721 is ERC721 {
    ...
}

Next, we'll define the properties of our ERC-721 token. This includes the name, symbol, and total supply of our token.

contract MyERC721 is ERC721 {
    string public name = "My ERC-721 Token";
    string public symbol = "MYNFT";
    uint256 public totalSupply;

    ...
}

Now that we've defined the basic structure of our contract, it's time to add some functionality. We'll start by implementing the mint function, which will allow us to mint new tokens and add them to the total supply.

contract MyERC721 is ERC721 {
    ...

    function mint(uint256 id) public {
        _mint(msg.sender, id);
        totalSupply++;
    }
}

It's important to note that the mint function is only callable by the contract owner, as specified in the require statement.

Next, we'll implement the transferFrom function, which will allow us to transfer ownership of a token from one address to another.

contract MyERC721 is ERC721 {
    ...

    function transferFrom(address from, address to, uint256 id) public {
        require(from == msg.sender, "You are not the owner of this token");
        _transferFrom(from, to, id);
    }
}

And that's it! We now have a basic implementation of an ERC-721 smart contract. Of course, there's much more you can do with an ERC-721 contract, but this should give you a good starting point.

To deploy your contract to the Ethereum blockchain, you'll need to use a tool like Remix, which allows you to write, test, and deploy smart contracts in the browser. Simply paste your contract code into Remix, select the environment you want to deploy to, and hit the "Deploy" button.